Advertisement
KillianMills

SnakeLadderGame.java

Oct 20th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.08 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class SnakeLadderGame{
  4.        
  5.         public static void main (String [] args){
  6.  
  7.                         Board board = new Board();
  8.                
  9.                
  10.                 Die gameDie = new Die();
  11.                
  12.                 int turn = 0; // Turn tracking variable.
  13.                 int winnersID = 999; // Random number, other than the actual players.
  14.                 boolean winFlag = false; // This will keep the loop of the game going.
  15.                 int numberOfPlaers;
  16.                
  17.                
  18.                 Scanner getInput = new Scanner(System.in); // Scanner used for user input.
  19.                 System.out.println("Enter the number of players: ");
  20.                 int noOfPlayers=getInput.nextInt(); // Take user input for the number of players that will take part in the game.
  21.                
  22.                 // If the value entered is greater than 4 enter the while loop below, and ask the user to enter a new value.
  23.                
  24.                 while(noOfPlayers>4){ // While loop that restricts the number of players to 4. If more than 4, ask for valid range.
  25.                         System.out.println("\nSorry, the maximum # of players is 4. Try again: ");
  26.                         noOfPlayers=getInput.nextInt(); // Get the value from the user.      
  27.                 }
  28.                
  29.                 getInput.close(); // Closing the scanner object.
  30.                
  31.                 ArrayList<Player>myListOfPlayers=playerSetup(noOfPlayers); // We're creating a list of players.
  32.                
  33.                
  34.                 while(winFlag==false){ // Game loop starts here.
  35.                        
  36.                                 System.out.println("Player no." + (turn + 1) + "'s turn.");
  37.                                
  38.                                 if(myListOfPlayers.get(turn).playerLocation == 0){
  39.                                         int tempRollScore = gameDie.rollDie();
  40.                                        
  41.                                         if(tempRollScore == 1){
  42.                                                 myListOfPlayers.get(turn).playerLocation = 1;
  43.                                                 System.out.println("Player no." + (turn+1) + " has entered the board.");
  44.                                         }
  45.                                        
  46.                                         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.
  47.                                 turn = 0;
  48.                                         }else{ // Else just increment so that the next player can take turn.
  49.                                 turn++;
  50.                                         }
  51.                                 }
  52.                                
  53.                                 else if(myListOfPlayers.get(turn).playerLocation >= 1){
  54.                                        
  55.                                         myListOfPlayers.get(turn).playerLocation = myListOfPlayers.get(turn).playerLocation + gameDie.rollDie(); // Moving the player forwards.
  56.                            
  57.                             myListOfPlayers.get(turn).playerLocation = board.checkIfSpecial(myListOfPlayers.get(turn).playerLocation);
  58.                            
  59.                             if(myListOfPlayers.get(turn).playerLocation >= 100){ // Here I noticed we don't need the "isWinner bool flag"
  60.                                     winnersID = turn; // The winnersID is the one that's taking the turn.
  61.                             winFlag = true; // This will break the loop.
  62.                             }
  63.                            
  64.                             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.
  65.                                     turn = 0;
  66.                             }else{ // Else just increment so that the next player can take turn.
  67.                                     turn++;
  68.                             }
  69.                                 }
  70.                                
  71.                                 else{ // If player has not yet thrown 1 go to the next player.
  72.                                        
  73.                                         System.out.println("Player no." + (turn+1) + " has not yet entered the board.");
  74.                                        
  75.                                         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.
  76.                                         turn = 0;
  77.                                 }else{ // Else just increment so that the next player can take turn.
  78.                                         turn++;
  79.                                 }
  80.                                 }
  81.                        
  82.                        
  83.                 }
  84.                
  85.                 System.out.println("The WINNER is player #" + (winnersID + 1) + " ! CONGRATULATIONS!!!!"); // Announcing the winner.
  86.                
  87.                
  88.         }
  89.        
  90.        
  91.         /* ----------------------------------------------------------------------------------------------------------------------------
  92.          * playerSetup() -> This method will accept as parameter the number of players to take part in the game.
  93.          *                                      Afterwards a new ArrayList will be created that will contain all the players and then return the ArrayList.
  94.          */
  95.         static ArrayList<Player> playerSetup(int numberOfPlayers){
  96.                
  97.                 ArrayList<Player> playerList = new ArrayList<>(); // ArrayList containing Players.
  98.                
  99.                 for(int i=0;i<numberOfPlayers;i++){ // This for loop will put the players into the ArrayList.
  100.                         playerList.add(new Player(0,i)); // Add new Player  with parameters 1) Position | 2) Player ID.
  101.                         System.out.println("Player #" + (i+1) + ". was created!\n"); // Notifying the user the players have been successfully created.
  102.                 }
  103.                
  104.                 return playerList; // Method returns the created ArrayList.
  105.         }
  106.         // -------------- END OF playerSetup() METHOD ---------------------------------
  107. }
  108. // ---------------------- END OF the Game CLASS ---------------------------------------
  109.  
  110.  
  111. class Player{
  112.         int playerLocation;
  113.         int playerID;
  114.        
  115.         Player(int playerLocation, int playerID){
  116.                 this.playerLocation=playerLocation;
  117.                 this.playerID=playerID;
  118.         }
  119. }
  120.  
  121.  
  122. import java.util.*;
  123.  
  124. class Board{
  125.        
  126.        private int[] ladderStart = {4,9,20,28,40,51,63,71};
  127.        private int[] ladderEnd = {14,31,38,84,59,67,81,91};
  128.        private int[] snakeStart = {17,54,62,64,87,93,95,99};
  129.        private int[] snakeEnd = {7,34,19,60,24,73,75,78};
  130.        
  131.        Board(){
  132.            
  133.        }
  134.        
  135.        int checkIfSpecial(int playerPosition){
  136.            
  137.            for(int i = 0; i < ladderStart.length; i++){
  138.                    if(playerPosition == ladderStart[i]){
  139.                            playerPosition = ladderEnd[i];
  140.                            System.out.println("Player went up the ladder at position " + ladderStart[i] + " to " + ladderEnd[i]);
  141.                    }
  142.            }
  143.            
  144.            for(int i = 0; i < snakeStart.length; i++){
  145.                    if(playerPosition == snakeStart[i]){
  146.                            playerPosition = snakeEnd[i];
  147.                            System.out.println("Player went down the snake from position " + snakeStart[i] + " to " + snakeEnd[i]);
  148.                    }
  149.            }
  150.            
  151.            
  152.            return playerPosition;
  153.        }
  154. }
  155.  
  156.  
  157. import java.util.*;
  158.  
  159. class Die{
  160.         int dieValue;
  161.        
  162.         int rollDie(){
  163.                 Random rngForDie = new Random();
  164.                 dieValue=rngForDie.nextInt(6)+1;
  165.                 System.out.println("The Die rolled: "+dieValue);
  166.                 return dieValue;
  167.         }      
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement