Added room class
This commit is contained in:
parent
7b82f26def
commit
c1b8b8a86b
24
src/index.js
24
src/index.js
@ -3,10 +3,7 @@ console.log('Uno Start')
|
|||||||
import './css/uno-game.css';
|
import './css/uno-game.css';
|
||||||
import Card from './js/card.js';
|
import Card from './js/card.js';
|
||||||
import Player from './js/player.js';
|
import Player from './js/player.js';
|
||||||
|
import Room from './js/room.js';
|
||||||
let card = new Card(1, 'green');
|
|
||||||
|
|
||||||
console.log(card.num)
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const div = document.getElementById('uno-game');
|
const div = document.getElementById('uno-game');
|
||||||
@ -14,19 +11,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const _all_colors = ['red', 'yellow', 'green', 'blue'];
|
const room = new Room();
|
||||||
function generateCards() {
|
room.addPlayer('newini');
|
||||||
let cards = [];
|
room.printPlayers();
|
||||||
for (let i=0; i<7; i++) {
|
room.initCards();
|
||||||
cards.push({
|
room.shuffleDeck();
|
||||||
num: Math.floor( Math.random() * 14 ),
|
|
||||||
color: _all_colors[ Math.floor( Math.random() * 4 ) ],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const player = new Player(cards);
|
|
||||||
player.printCards();
|
|
||||||
|
|
||||||
}
|
|
||||||
generateCards()
|
|
||||||
|
|
||||||
console.log('Uno End')
|
console.log('Uno End')
|
||||||
|
|||||||
@ -1,16 +1,14 @@
|
|||||||
import Card from './card.js';
|
import Card from './card.js';
|
||||||
|
|
||||||
export default class Player {
|
export default class Player {
|
||||||
constructor(cards) {
|
constructor(name) {
|
||||||
this._n = cards.length;
|
this._name = name;
|
||||||
this._cards = [];
|
this._cards = [];
|
||||||
cards.forEach( (card) => {
|
}
|
||||||
this._cards.push( new Card( card['num'], card['color'] ) );
|
addCard(card) {
|
||||||
});
|
this._cards.push(card);
|
||||||
}
|
}
|
||||||
printCards() {
|
printCards() {
|
||||||
this._cards.forEach( (card) => {
|
console.log(this._cards);
|
||||||
console.log(card.num)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
src/js/room.js
Normal file
50
src/js/room.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import Player from './player.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.ALL_COLORS = ['red', 'yellow', 'green', 'blue'];
|
||||||
|
}
|
||||||
|
addPlayer(player_name) {
|
||||||
|
const player = new Player(player_name);
|
||||||
|
this._players.push(player);
|
||||||
|
}
|
||||||
|
printPlayers() {
|
||||||
|
console.log(this._players);
|
||||||
|
}
|
||||||
|
initCards() {
|
||||||
|
this.ALL_COLORS.forEach( (color) => {
|
||||||
|
for (let num=0; num<=12; num++) {
|
||||||
|
const card = new Card(num, color);
|
||||||
|
this._cards.push( card );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.ALL_COLORS.forEach( (color) => {
|
||||||
|
for (let num=1; num<=12; num++) {
|
||||||
|
const card = new Card(num, color);
|
||||||
|
this._cards.push( card );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (let i=0; i<4; i++) {
|
||||||
|
for (let num=13; num<=14; num++) {
|
||||||
|
const card = new Card(num, 'none');
|
||||||
|
this._cards.push( card );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log( this._cards );
|
||||||
|
}
|
||||||
|
shuffleDeck() {
|
||||||
|
this._players.forEach( (player) => {
|
||||||
|
for (let i=0; i<7; i++) {
|
||||||
|
let j = Math.floor( Math.random() * this._cards.length );
|
||||||
|
const card = this._cards.splice(j, 1);
|
||||||
|
player.addCard(card);
|
||||||
|
}
|
||||||
|
player.printCards();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user