Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- public class Day21_Dirac_Dice {
- private static long player1Wins = 0;
- private static long player2Wins = 0;
- public static void main(String[] args) {
- int player1Starting = 4;
- int player2Starting = 8;
- long part2 = part2(player1Starting, player2Starting);
- System.out.println(player1Wins + " " + player2Wins);
- }
- // Given a game state, plays the game from this point onwards.
- private static int playGame(Game game, int diceSum) {
- while (game.player1Score < 21 && game.player2Score < 21) {
- if (game.player1Turn) {
- game.setPlayer1Position((((game.player1Position+diceSum) - 1) % 10) + 1);
- game.updatePlayer1Score(game.player1Position);
- game.togglePlayerTurn();
- } else {
- game.setPlayer2Position((((game.player2Position+diceSum) - 1) % 10) + 1);
- game.updatePlayer2Score(game.player2Position);
- game.togglePlayerTurn();
- }
- Game g = new Game(game.player1Position, game.player2Position, game.player1Score, game.player2Score, game.player1Turn);
- playGame(g, 3);
- playGame(g, 4);
- playGame(g, 5);
- playGame(g, 6);
- playGame(g, 7);
- playGame(g, 8);
- playGame(g, 9);
- }
- if (game.player1Score > game.player2Score) {
- player1Wins++;
- } else {
- player2Wins++;
- }
- return 0;
- }
- // Part 2: Using quantum die.
- private static long part2(int player1Starting, int player2Starting) {
- Game g = new Game(player1Starting, player2Starting, 0, 0, true);
- playGame(g, 3);
- playGame(g, 4);
- playGame(g, 5);
- playGame(g, 6);
- playGame(g, 7);
- playGame(g, 8);
- playGame(g, 9);
- return 0L;
- }
- static class Game {
- int player1Position;
- int player2Position;
- int player1Score;
- int player2Score;
- boolean player1Turn;
- public Game(int player1Position, int player2Position, int player1Score, int player2Score, boolean player1Turn) {
- this.player1Position = player1Position;
- this.player2Position = player2Position;
- this.player1Score = player1Score;
- this.player2Score = player2Score;
- this.player1Turn = player1Turn;
- }
- public void setPlayer1Position(int position) {
- this.player1Position = position;
- }
- public void setPlayer2Position(int position) {
- this.player2Position = position;
- }
- public void updatePlayer1Score(int score) {
- this.player1Score += score;
- }
- public void updatePlayer2Score(int score) {
- this.player2Score += score;
- }
- public void togglePlayerTurn() {
- this.player1Turn = !this.player1Turn;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment