Search code snippets, questions, articles... Chrome Extension

War - Affirm - Asset Management AIM - 11/18/21

/*
Part 1:

 * this is a two player card game
 * the game starts with a deck of 52 cards represented as
   unique integers [1...52]
 * the cards are randomly shuffled and then dealt out to both players evenly.
 * on each turn:
     * both players turn over their top-most card
     * the player with the higher valued card takes
       the cards and puts them in their win pile
       (scoring 1 point per card)
 * this continues until all the players have no cards left
 * the player with the highest score (number of cards in their win pile) wins.
    * if they have the same number of cards in their win pile, tiebreaker goes to the player with the highest card in their win pile.

Be able to play the game with N players.
An input to the game will now be a list of strings (of length N) indicating the player names.
The deck contains M cards of distinct integers.
It is not guaranteed M % N == 0. If there are leftover cards they should randomly be handed out to remaining players.
i.e. with 17 cards and 5 people: 2 people get 4 cards and 3 get 3 cards
For example the input: game(["Joe", "Jill", "Bob"], 5) would be a game between 3 players and 5 cards.
you should print the name of the player that won the game.
*/

process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    input += chunk;
});
process.stdin.on("end", function () {
    // now we can read/parse input
    
    const shuffleArray = array => {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            const temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    
    let shuffledDeck = Array.from({length: 52}, (_, i) => i + 1);
    shuffleArray(shuffledDeck);
    
    shuffleArray(shuffledDeck);
    console.log('shuffledDeck', shuffledDeck);
    
    let player1Deck = shuffledDeck.slice(0, 26);
    let player2Deck = shuffledDeck.slice(27);
    
    console.log('player1Deck', player1Deck);
    console.log('player2Deck', player2Deck);

    let player1WinningDeck = [];
    let player2WinningDeck = [];

    while (player1Deck.length > 0 || player2Deck.length > 0) {
        let player1CurrentCard = player1Deck.pop();
        let player2CurrrentCard = player2Deck.pop();
        
        if (player1CurrentCard > player2CurrrentCard) {
            player1WinningDeck.push(player1CurrentCard);
            player1WinningDeck.push(player2CurrrentCard);
        } else {
            player2WinningDeck.push(player1CurrentCard);
            player2WinningDeck.push(player2CurrrentCard);
        }
    }

    if (player1WinningDeck.length > player2WinningDeck.length) {
        console.log('player 1 won with ' + player1WinningDeck.length + ' points');
    } else if (player1WinningDeck.length < player2WinningDeck.length) {
        console.log('player 2 won with ' + player2WinningDeck.length + ' points');
    } else { 
        let sortedPlayer1WinningDeck = player1WinningDeck.sort();
        let sortedPlayer2WinningDeck = player2WinningDeck.sort();
        
        let player1MaxCard = sortedPlayer1WinningDeck[sortedPlayer1WinningDeck.length - 1]
        let player2MaxCard = sortedPlayer2WinningDeck[sortedPlayer2WinningDeck.length - 1]
        if (player1MaxCard > player2MaxCard) {
            console.log('player 1 won with ' + player1WinningDeck.length + ' points with max card ' + player1MaxCard);
        } else {
            console.log('player 2 won with ' + player2WinningDeck.length + ' points with max card ' + player2MaxCard);
        }
    }
    
    function war(players, numCards) {
        let playerDecks = {};
        let winningDecks = {};
        
        let shuffledDeck = Array.from({length: numCards}, (_, i) => i + 1);
        shuffleArray(shuffledDeck);
        
        // shuffledDeck.length / player.length 
        
        players.map((player) => {
            playerDecks[player] = [];
            winningDecks[player] = [];
        });
        
        for (let i = 0; i < shuffledDeck.length; i++) {
            let playerIndex = i % player.length 
            
            playerDecks[players[playerIndex]].push(card);
        }
        
        
        let hasCard s = true
        
        while (playerDecks.length) {
            let playingCards = [];
            
            for (const [player, playerDeck] of Object.entries(playerDecks)) {
                if (playerDeck.length == 0) {
                    delete playerDecks.player
                }
                
                playingCards.push({
                    player: player,
                    card: playerDeck.pop()
                });
            }
            
            let sortedPlayingCards = playingCards.sort((firstEl, secondEl) => { firstEl.card - secondEl.card } )
            let winningCard = sortedPlayingCards[playingCards.length]
            
            winningDecks[winningCard.player] = playerDecks[winningCard.player].concat(playingCards.map(playingCard => playingCard.card));
        }
        
        
        
        let arrWinning = []
        for (const [player, playerDeck] of Object.entries(winningDecks)) {
            arrWinning.push({
                player: player, 
                deckLength: playerDeck.length
            })
        }
        
        
        arrWinning.sort((firstEl, secondEl) => { firstEl.deckLength - secondEl.deckLength } )
        let tiedWinners = arrWinning.reduce( (tiedWinners, arrWinner) => 
            if (arrWinner.deckLength == arrWinning[arrWinning.length.deckLength) {
                tiedWinners.push(arrWinner);
            }
        }, []);
        
        
        if (tiedWinners.length == 1) {
            
        } else {
            tiedWinners.map((tiedWinner) => {
                winningDecks[tiedWinner.player].sort()
            })
            winningCards = tiedWinners.reduce((winningCard, tiedWinner) => {
                winningDecks.length
                winningDecks[tiedWinner.player][winningDecks.length - 1]
            }, []).sort()
            winningCards[winningCards.length]
            
        }
        
        
    }
});
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Was this helpful?
0 Comments
Join Devsheet Ask a Question