Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

(I've asked this question multiple times and the responses have been incomplete.)

Alter my current code if necessary.

This is what I have so far. What code do I add to finish the project?

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){

this.suit = suit;

this.value = value;

}

}

function freshDeck(){

return SUITS.flatMap(suit => {

return VALUES.map(value => {

return new Card(suit, value)

})

})

}

const deck = new Deck();

deck.shuffle();

let player1Deck, player2Deck, player1Points, player2Points;

startGame()

function startGame(){

const deck = new Deck();

deck.shuffle();

let player1Points = 0;

let player2Points = 0;

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);

function player1Card(player1Deck) {

for (let i = 0; i < player1Deck.cards.length; i++){

return player1Deck.cards[i].value;

}

}

console.log(player1Card(player1Deck))

function player2Card(player2Deck) {

for (let i = 0; i < player2Deck.cards.length; i++){

return player2Deck.cards[i].value;

}

}

console.log(player2Card(player2Deck));

if (player1Card > player2Card){

return 'Player 1 wins round.';

player1Points++;

} else if (player1Card < player2Card){

return 'Player 2 wins round.';

player2Points++;

} else {

return 'Both players tie. No points rewarded.'

}

if (player1Points > player2Points){

return 'Player 1 wins the game.'

} else if (player1Points < player2Points){

return 'Player 2 wins the game'

} else {

return 'Both players tie. No winner.'

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions