diff --git a/src/index.js b/src/index.js index a5ad216..5d901f7 100644 --- a/src/index.js +++ b/src/index.js @@ -15,10 +15,11 @@ document.addEventListener('DOMContentLoaded', () => { }); const room = new Room(); -room.addPlayer('newini'); -room.printPlayers(); -room.initDeck(); -room.shuffleDeck(); -room.startGame(); +room.addHuman('newini'); +room.addBot(); + +setTimeout( () => { room.startGame(); }, 1000); + +//room.startGame(); console.log('Uno End') diff --git a/src/js/bot.js b/src/js/bot.js new file mode 100644 index 0000000..185cedb --- /dev/null +++ b/src/js/bot.js @@ -0,0 +1,19 @@ +import Player from './player.js'; + +export default class Bot extends Player { + constructor() { + super('bot'); + } + + playCard(top_card) { + for (let i=0; i { for (let num=0; num<=12; num++) { @@ -42,24 +45,41 @@ export default class Room { for (let i=0; i<7; i++) { player.addCard( this.draw() ); } - player.printCards(); }); + console.log(this._players); } draw() { + /* Draw randomly */ const card_i = Math.floor( Math.random() * this._cards.length ); - return this._cards.splice(card_i, 1); + return this._cards.splice(card_i, 1)[0]; } - startGame() { + + async startGame() { + await( this.initDeck() ); + await( this.shuffleDeck() ); + let count = 0; + let current_turn = 0; + let top_card = this.draw(); + while (true) { - const card = this.playerTurn(); - if (this._players[0].isEmpty()) { + console.log('count: ' + count + ', current_turn: ' + current_turn); + const player = this._players[current_turn]; + + const card = await( player.playCard(top_card) ); + if (card) { + console.log('player: ' + player.name + ' played card num: ' + card.num + ', color: ' + card.color); + top_card = card; + } else { + player.addCard( this.draw() ); + } + + if (player.isEmpty()) { console.log('game end') break } + current_turn = (current_turn >= this._players.length-1) ? 0 : ++current_turn; + count++; } } - async playerTurn() { - return this._players[0].playCard(0); - } }