Question
Create a classic card game WAR. Consider classes such as Card, Deck, and Player and what fields and methods they might each have. You can
Create a classic card game WAR. Consider classes such as Card, Deck, and Player and what fields and methods they might each have. You can implement the game however youd like (i.e. printing to the console, using alert, or some other way). The completed project should, when ran, do the following: :
::Deal 26 Cards to two Players from a Deck.
:::Iterate through the turns where each Player plays a Card
:::The Player who played the higher card is awarded a point
:::Ties result in zero points for either Player :
::After all cards have been played, display the score.
:::Write Unit Tests using Mocha and Chai for each of the functions you write.
NEED THE CODE TO ANSWER THE PROMPT ASAP. JAVASCRIPT ONLY.
OUTPUTS FROM CODE SHOULD INCLUDE:
-WHO WINS EACH ROUND (ONLY 26 ROUNDS)
-TOTAL PLAYER ONE POINTS AND TOTAL PLAYER 2 POINTS
-WINNER OF THE GAME
***MY CODE KEEPS RETURNING EACH ROUND AS A TIE, AND RETURNS PLAYER 1 AND PLAYER 2 POINTS AS 0***
^ NEED THIS FIXED. NEED RETURN VALUE TO BE CONSISTENT WITH CONDITIONS IMPLEMENTED
MY CURRENT CODE IS BELOW:
const SUITS = ['Spades','Hearts','Clubs','Diamonds'];
const VALUES = [{'A': 1},{'2': 2},{'3': 3},{'4': 4},{'5': 5},{'6': 6},{'7': 7},{'8': 8},{'9': 9},{'10': 10},{'J': 11},{'Q': 12},{'K': 13}]
class Deck {
constructor(cards = freshDeck()){
this.cards = cards;
}
get numberOfCards(){
return this.cards.length
}
shuffle(){
for (let i = this.numberOfCards - 1; i > 0; i--){
const newIndex = Math.floor(Math.random() * (i + 1));
const oldValue = this.cards[newIndex];
this.cards[newIndex] = this.cards[i];
this.cards[i] = oldValue;
}
}
}
class Player {
constructor(player1, player2) {
this.player1 = player1;
this.player2 = player2;
}
}
class Card {
constructor(suit, value, player1Card, player2Card){
this.suit = suit;
this.value = value;
this.player1Card = player1Card;
this.player2Card = player2Card;
}
}
function freshDeck(){
return SUITS.flatMap(suit => {
return VALUES.map(value => {
return new Card(suit, value)
})
})
}
const deck = new Deck();
deck.shuffle();
let player1Deck, player2Deck;
startGame()
function startGame(){
const deck = new Deck();
deck.shuffle();
const deckMidpoint = Math.ceil(deck.numberOfCards / 2);
player1Deck = new Deck(deck.cards.slice(0, deckMidpoint));
player2Deck = new Deck(deck.cards.slice(deckMidpoint, deck.numberOfCards));
//player 1 deck and player 2 deck split evenly and randomly
console.log(player1Deck);
console.log(player2Deck);
let player1Points = 0;
let player2Points = 0;
const gameRounds = 26;
for (let i = 0; i < gameRounds; i++) {
const player1Card = player1Deck.cards[i].value;
const player2Card = player2Deck.cards[i].value;
if (player1Card > player2Card) {
console.log(`Player 1 wins round ${i+1}`);
player1Points++;
} else if (player1Card < player2Card) {
console.log(`Player 2 wins round ${i+1}`);
player2Points++;
} else {
console.log(`Round ${i+1} is a tie`);
}
}
console.log(`Player 1 Points: ${player1Points}`);
console.log(`Player 2 Points: ${player2Points}`);
if (player1Points > player2Points) {
console.log("Player 1 wins the game!");
} else if (player1Points < player2Points) {
console.log("Player 2 wins the game!");
} else {
console.log("The game is a tie!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started