backend/src/main/java/com/coolioasjulio/influence/Game.java
/**
     * Plays a full game. Blocks until the game is complete.
     */
Plays a full game. Blocks until the game is complete.
Plays a full game . Blocks until the game is complete .
public void playGame() {
        try {
            int playerIndex = 0;
            update();
            while (playersAlive() > 1) {
                Player player = players[playerIndex];
                Action action = getAction(player);
                Player target = action.targeted ? getTarget(player) : null;
                if (target != null) {
                    if (action.card != null) {
                        log("%s uses a(n) %s to do %s on %s", player, action.card, action, target);
                    } else {
                        log("%s does %s on %s", player, action, target);
                    }
                } else {
                    if (action.card != null) {
                        log("%s uses a(n) %s to do %s", player, action.card, action);
                    } else {
                        log("%s does %s", player, action);
                    }
                }
                doAction(action, player, target);
                for (int i = 0; i < players.length; i++) {
                    if (players[i] != null && players[i].getInfluence() == 0) {
                        playerDied(players[i]);
                        players[i] = null;
                    }
                }
                update();
                do {
                    playerIndex = (playerIndex + 1) % players.length;
                } while (players[playerIndex] == null);
            }
            playerWon(Arrays.stream(players).filter(Objects::nonNull).findFirst().orElseThrow(IllegalStateException::new));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
public void playGame ( ) { try { int playerIndex = 0 ; update ( ) ; while ( playersAlive ( ) > 1 ) { Player player = players [ playerIndex ] ; Action action = getAction ( player ) ; Player target = action . targeted ? getTarget ( player ) : null ; if ( target != null ) { if ( action . card != null ) { log ( "%s uses a(n) %s to do %s on %s" , player , action . card , action , target ) ; } else { log ( "%s does %s on %s" , player , action , target ) ; } } else { if ( action . card != null ) { log ( "%s uses a(n) %s to do %s" , player , action . card , action ) ; } else { log ( "%s does %s" , player , action ) ; } } doAction ( action , player , target ) ; for ( int i = 0 ; i < players . length ; i ++ ) { if ( players [ i ] != null && players [ i ] . getInfluence ( ) == 0 ) { playerDied ( players [ i ] ) ; players [ i ] = null ; } } update ( ) ; do { playerIndex = ( playerIndex + 1 ) % players . length ; } while ( players [ playerIndex ] == null ) ; } playerWon ( Arrays . stream ( players ) . filter ( Objects :: nonNull ) . findFirst ( ) . orElseThrow ( IllegalStateException :: new ) ) ; } catch ( InterruptedException e ) { Thread . currentThread ( ) . interrupt ( ) ; } }
Plays a full game.
Plays a full game .
// If this action has a target, prompt frontend for the target // Execute this action, which may have a target // Mark all players with 0 influence as dead // Update the frontend to reflect the new state // Advance the playerIndex to point to the next player that's alive (looping over from the end back to the start) // The only remaining player is the winner // Re-interrupt the thread to signal callers that this game was interrupted