Advertisement
KillianMills

Game.java

Nov 11th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.43 KB | None | 0 0
  1. // Snakes and Ladders, utilities Board Player and Die classes
  2.  
  3. import java.util.*;
  4.  
  5. class Game{
  6.        
  7.         public static void main (String [] args){
  8.  
  9.                         Board board = new Board();
  10.                
  11.                
  12.                 Die gameDie = new Die();
  13.                
  14.                 int turn = 0; // Turn tracking variable.
  15.                 int winnersID = 999; // Random number, other than the actual players.
  16.                 boolean winFlag = false; // This will keep the loop of the game going.
  17.                 int numberOfPlaers;
  18.                
  19.                
  20.                 Scanner getInput = new Scanner(System.in); // Scanner used for user input.
  21.                 System.out.println("Enter the number of players: ");
  22.                 int noOfPlayers=getInput.nextInt(); // Take user input for the number of players that will take part in the game.
  23.                
  24.                 // If the value entered is greater than 4 enter the while loop below, and ask the user to enter a new value.
  25.                
  26.                 while(noOfPlayers>4){ // While loop that restricts the number of players to 4. If more than 4, ask for valid range.
  27.                         System.out.println("\nSorry, the maximum # of players is 4. Try again: ");
  28.                         noOfPlayers=getInput.nextInt(); // Get the value from the user.      
  29.                 }
  30.                
  31.                 getInput.close(); // Closing the scanner object.
  32.                
  33.                 ArrayList<Player>myListOfPlayers=playerSetup(noOfPlayers); // We're creating a list of players.
  34.                
  35.                
  36.                 while(winFlag==false){ // Game loop starts here.
  37.                        
  38.                                 System.out.println("Player no." + (turn + 1) + "'s turn.");
  39.                                
  40.                                 if(myListOfPlayers.get(turn).playerLocation == 0){
  41.                                         int tempRollScore = gameDie.rollDie();
  42.                                        
  43.                                         if(tempRollScore == 1){
  44.                                                 myListOfPlayers.get(turn).playerLocation = 1;
  45.                                                 System.out.println("Player no." + (turn+1) + " has entered the board.");
  46.                                         }
  47.                                        
  48.                                         if(turn == (noOfPlayers-1)){ //If the turn is of the last player, and we have no winner yet, set the turn to be of first player.
  49.                                 turn = 0;
  50.                                         }else{ // Else just increment so that the next player can take turn.
  51.                                 turn++;
  52.                                         }
  53.                                 }
  54.                                
  55.                                 else if(myListOfPlayers.get(turn).playerLocation >= 1){
  56.                                        
  57.                                         myListOfPlayers.get(turn).playerLocation = myListOfPlayers.get(turn).playerLocation + gameDie.rollDie(); // Moving the player forwards.
  58.                            
  59.                             myListOfPlayers.get(turn).playerLocation = board.checkIfSpecial(myListOfPlayers.get(turn).playerLocation);
  60.                            
  61.                             if(myListOfPlayers.get(turn).playerLocation >= 100){ // Here I noticed we don't need the "isWinner bool flag"
  62.                                     winnersID = turn; // The winnersID is the one that's taking the turn.
  63.                             winFlag = true; // This will break the loop.
  64.                             }
  65.                            
  66.                             if(turn == (noOfPlayers-1)){ //If the turn is of the last player, and we have no winner yet, set the turn to be of first player.
  67.                                     turn = 0;
  68.                             }else{ // Else just increment so that the next player can take turn.
  69.                                     turn++;
  70.                             }
  71.                                 }
  72.                                
  73.                                 else{ // If player has not yet thrown 1 go to the next player.
  74.                                        
  75.                                         System.out.println("Player no." + (turn+1) + " has not yet entered the board.");
  76.                                        
  77.                                         if(turn == (noOfPlayers-1)){ //If the turn is of the last player, and we have no winner yet, set the turn to be of first player.
  78.                                         turn = 0;
  79.                                 }else{ // Else just increment so that the next player can take turn.
  80.                                         turn++;
  81.                                 }
  82.                                 }
  83.                        
  84.                        
  85.                 }
  86.                
  87.                 System.out.println("The WINNER is player #" + (winnersID + 1) + " ! CONGRATULATIONS!!!!"); // Announcing the winner.
  88.                
  89.                
  90.         }
  91.        
  92.        
  93.         /* ----------------------------------------------------------------------------------------------------------------------------
  94.          * playerSetup() -> This method will accept as parameter the number of players to take part in the game.
  95.          *                                      Afterwards a new ArrayList will be created that will contain all the players and then return the ArrayList.
  96.          */
  97.         static ArrayList<Player> playerSetup(int numberOfPlayers){
  98.                
  99.                 ArrayList<Player> playerList = new ArrayList<>(); // ArrayList containing Players.
  100.                
  101.                 for(int i=0;i<numberOfPlayers;i++){ // This for loop will put the players into the ArrayList.
  102.                         playerList.add(new Player(0,i)); // Add new Player  with parameters 1) Position | 2) Player ID.
  103.                         System.out.println("Player #" + (i+1) + ". was created!\n"); // Notifying the user the players have been successfully created.
  104.                 }
  105.                
  106.                 return playerList; // Method returns the created ArrayList.
  107.         }
  108.         // -------------- END OF playerSetup() METHOD ---------------------------------
  109. }
  110. // ---------------------- END OF the Game CLASS ---------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement