import java.util.*; // This is a revision of our previous dumb board game. Here, the board itself is stored as an array where // board[i] is an int that tells you whether you can move forward or backward when landing on that square. // For instance, if a player lands on board[1], the value is 1 so the player moves forward 1 square. public class BoardGame { public static void main(String[] args) { int die, player1, player2, turn=0; Random generator=new Random(); Scanner in=new Scanner(System.in); System.out.println("We are going to play a simple board game. As we move around, we land on squares that might move us forward or backward."); System.out.println("Press the key after each turn"); int board[] = {0, 1, 0, -1, 3, 3, 0, 0, 0, -3, 2, -9, 0, 4, 0, -3, 0, -1, 1, 0, -4, -7, 2, 3, 0, 0, 0, -1, -3, 2, 1, 0, -1, 0, 2, -2, 0, 3, 1, 0, 0, -2, -7, 3, -4, 1, 0, 0, -2, 0}; player1=0; player2=0; // start both players at square 0 while(player1<=49&&player2<=49) { turn++; System.out.print("Turn : " + turn); die=generator.nextInt(6)+1; player1+=die; if(player1<=49) { if(board[player1]==0) System.out.print(" You roll " + die + " and are now at square " + player1); else { System.out.print(" You roll " + die + " landing on " + board[player1]); if(board[player1]>0) System.out.print(" and move forward to square "); else System.out.print(" and move backward to square "); player1+=board[player1]; System.out.print(player1); } die=generator.nextInt(6)+1; player2+=die; if(player2<=49) { if(board[player2]==0) System.out.println(" I roll " + die + " and am now at square " + player2); else { System.out.print(" I roll " + die + " landing on " + board[player2]); if(board[player2]>0) System.out.print(" and move forward to square "); else System.out.print(" and move backward to square "); player2+=board[player2]; System.out.print(player2); } } } in.nextLine(); // pause for the user to press } if(player1>player2) System.out.println("You win!"); else if(player2>player1) System.out.println("I win!"); else System.out.println("Its a tie"); } }