Added basic game start in room

This commit is contained in:
Eunchong Kim 2021-07-17 15:53:55 +09:00
parent acbfdc01d1
commit 3ccf773315
3 changed files with 29 additions and 6 deletions

View File

@ -17,7 +17,8 @@ document.addEventListener('DOMContentLoaded', () => {
const room = new Room(); const room = new Room();
room.addPlayer('newini'); room.addPlayer('newini');
room.printPlayers(); room.printPlayers();
room.initCards(); room.initDeck();
room.shuffleDeck(); room.shuffleDeck();
room.startGame();
console.log('Uno End') console.log('Uno End')

View File

@ -11,4 +11,11 @@ export default class Player {
printCards() { printCards() {
console.log(this._cards); console.log(this._cards);
} }
playCard(i) {
console.log(this._cards);
return this._cards.splice(i, 1);
}
isEmpty() {
return (this._cards.length === 0) ? true : false;
}
} }

View File

@ -7,6 +7,7 @@ export default class Room {
this._n_players = 0; this._n_players = 0;
this._players = []; this._players = [];
this._cards = []; this._cards = [];
this._turn_who = 0;
this.ALL_COLORS = ['red', 'yellow', 'green', 'blue']; this.ALL_COLORS = ['red', 'yellow', 'green', 'blue'];
} }
addPlayer(player_name) { addPlayer(player_name) {
@ -16,7 +17,7 @@ export default class Room {
printPlayers() { printPlayers() {
console.log(this._players); console.log(this._players);
} }
initCards() { initDeck() {
this.ALL_COLORS.forEach( (color) => { this.ALL_COLORS.forEach( (color) => {
for (let num=0; num<=12; num++) { for (let num=0; num<=12; num++) {
const card = new Card(num, color); const card = new Card(num, color);
@ -35,16 +36,30 @@ export default class Room {
this._cards.push( card ); this._cards.push( card );
} }
} }
console.log( this._cards );
} }
shuffleDeck() { shuffleDeck() {
this._players.forEach( (player) => { this._players.forEach( (player) => {
for (let i=0; i<7; i++) { for (let i=0; i<7; i++) {
let j = Math.floor( Math.random() * this._cards.length ); player.addCard( this.draw() );
const card = this._cards.splice(j, 1);
player.addCard(card);
} }
player.printCards(); player.printCards();
}); });
} }
draw() {
const card_i = Math.floor( Math.random() * this._cards.length );
return this._cards.splice(card_i, 1);
}
startGame() {
let count = 0;
while (true) {
const card = this.playerTurn();
if (this._players[0].isEmpty()) {
console.log('game end')
break
}
}
}
async playerTurn() {
return this._players[0].playCard(0);
}
} }