Guest User

Untitled

a guest
Dec 21st, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. public class Day21_Dirac_Dice {
  6.     private static long player1Wins = 0;
  7.     private static long player2Wins = 0;
  8.  
  9.     public static void main(String[] args) {
  10.                
  11.         int player1Starting = 4;
  12.         int player2Starting = 8;
  13.  
  14.         long part2 = part2(player1Starting, player2Starting);
  15.         System.out.println(player1Wins + " " + player2Wins);
  16.     }
  17.  
  18.     // Given a game state, plays the game from this point onwards.
  19.     private static int playGame(Game game, int diceSum) {
  20.         while (game.player1Score < 21 && game.player2Score < 21) {
  21.             if (game.player1Turn) {
  22.                 game.setPlayer1Position((((game.player1Position+diceSum) - 1) % 10) + 1);
  23.                 game.updatePlayer1Score(game.player1Position);
  24.                 game.togglePlayerTurn();
  25.             } else {
  26.                 game.setPlayer2Position((((game.player2Position+diceSum) - 1) % 10) + 1);
  27.                 game.updatePlayer2Score(game.player2Position);
  28.                 game.togglePlayerTurn();
  29.             }
  30.  
  31.             Game g = new Game(game.player1Position, game.player2Position, game.player1Score, game.player2Score, game.player1Turn);
  32.             playGame(g, 3);
  33.             playGame(g, 4);
  34.             playGame(g, 5);
  35.             playGame(g, 6);
  36.             playGame(g, 7);
  37.             playGame(g, 8);
  38.             playGame(g, 9);
  39.  
  40.         }
  41.  
  42.         if (game.player1Score > game.player2Score) {
  43.             player1Wins++;
  44.         } else {
  45.             player2Wins++;
  46.         }
  47.  
  48.         return 0;
  49.     }
  50.  
  51.     // Part 2: Using quantum die.
  52.     private static long part2(int player1Starting, int player2Starting) {
  53.         Game g = new Game(player1Starting, player2Starting, 0, 0, true);
  54.         playGame(g, 3);
  55.         playGame(g, 4);
  56.         playGame(g, 5);
  57.         playGame(g, 6);
  58.         playGame(g, 7);
  59.         playGame(g, 8);
  60.         playGame(g, 9);
  61.  
  62.         return 0L;
  63.     }
  64.  
  65.     static class Game {
  66.         int player1Position;
  67.         int player2Position;
  68.         int player1Score;
  69.         int player2Score;
  70.         boolean player1Turn;
  71.  
  72.         public Game(int player1Position, int player2Position, int player1Score, int player2Score, boolean player1Turn) {
  73.             this.player1Position = player1Position;
  74.             this.player2Position = player2Position;
  75.             this.player1Score = player1Score;
  76.             this.player2Score = player2Score;
  77.             this.player1Turn = player1Turn;
  78.         }
  79.  
  80.         public void setPlayer1Position(int position) {
  81.             this.player1Position = position;
  82.         }
  83.  
  84.         public void setPlayer2Position(int position) {
  85.             this.player2Position = position;
  86.         }
  87.  
  88.         public void updatePlayer1Score(int score) {
  89.             this.player1Score += score;
  90.         }
  91.  
  92.         public void updatePlayer2Score(int score) {
  93.             this.player2Score += score;
  94.         }
  95.  
  96.         public void togglePlayerTurn() {
  97.             this.player1Turn = !this.player1Turn;
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment