Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JavaScript Assignment (Need Help Finishing Code) (Please use only JS code, which will print on the node. NO CSS or HTML needed) Create a classic

JavaScript Assignment (Need Help Finishing Code) (Please use only JS code, which will print on the node. NO CSS or HTML needed)

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..

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, 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, 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

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions

Question

Discuss the different types of leadership

Answered: 1 week ago

Question

Write a note on Organisation manuals

Answered: 1 week ago

Question

Define Scientific Management

Answered: 1 week ago

Question

Explain budgetary Control

Answered: 1 week ago