Added bot, seperated human from player

This commit is contained in:
Eunchong Kim 2021-07-17 17:24:35 +09:00
parent 3ccf773315
commit 5285d5e2d2
6 changed files with 97 additions and 30 deletions

View File

@ -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')

19
src/js/bot.js Normal file
View File

@ -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<this._cards.length; i++) {
const card = this._cards[i];
if ( top_card.isMatch(card) ) {
this._cards.splice(i, 1)[0];
return card;
} else {
return null;
}
}
}
}

View File

@ -3,10 +3,25 @@ export default class Card {
this._num = num;
this._color = color;
}
get num() {
return this._num;
}
set num(num) {
this._num = num;
}
get color() {
return this._color;
}
set color(color) {
this._color = color;
}
isMatch(card) {
if ( (this._num === card.num) || (this._color === card.color) ) {
return true;
} else {
return false;
}
}
}

11
src/js/human.js Normal file
View File

@ -0,0 +1,11 @@
import Player from './player.js';
export default class Human extends Player {
constructor(name) {
super(name);
}
playCard(top_card) {
return this._cards.splice(0, 1)[0];
}
}

View File

@ -1,20 +1,21 @@
import Card from './card.js';
export default class Player {
constructor(name) {
this._name = name;
this._cards = [];
}
get name() {
return this._name;
}
set name(name) {
this._name = name;
}
addCard(card) {
this._cards.push(card);
}
printCards() {
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

@ -1,22 +1,25 @@
import Player from './player.js';
import Human from './human.js';
import Bot from './bot.js';
import Card from './card.js';
export default class Room {
constructor(id) {
this._id = (typeof id === 'number') ? id : 0;
this._n_players = 0;
this._players = [];
this._cards = [];
this._turn_who = 0;
this.ALL_COLORS = ['red', 'yellow', 'green', 'blue'];
}
addPlayer(player_name) {
const player = new Player(player_name);
this._players.push(player);
}
printPlayers() {
addHuman(name) {
this._players.push( new Human(name) );
console.log(this._players);
}
addBot() {
this._players.push( new Bot() );
console.log(this._players);
}
initDeck() {
this.ALL_COLORS.forEach( (color) => {
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);
}
}