Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Snakes and Ladders, utilities Board Player and Die classes
- import java.util.*;
- class Game{
- public static void main (String [] args){
- Board board = new Board();
- Die gameDie = new Die();
- int turn = 0; // Turn tracking variable.
- int winnersID = 999; // Random number, other than the actual players.
- boolean winFlag = false; // This will keep the loop of the game going.
- int numberOfPlaers;
- Scanner getInput = new Scanner(System.in); // Scanner used for user input.
- System.out.println("Enter the number of players: ");
- int noOfPlayers=getInput.nextInt(); // Take user input for the number of players that will take part in the game.
- // If the value entered is greater than 4 enter the while loop below, and ask the user to enter a new value.
- while(noOfPlayers>4){ // While loop that restricts the number of players to 4. If more than 4, ask for valid range.
- System.out.println("\nSorry, the maximum # of players is 4. Try again: ");
- noOfPlayers=getInput.nextInt(); // Get the value from the user.
- }
- getInput.close(); // Closing the scanner object.
- ArrayList<Player>myListOfPlayers=playerSetup(noOfPlayers); // We're creating a list of players.
- while(winFlag==false){ // Game loop starts here.
- System.out.println("Player no." + (turn + 1) + "'s turn.");
- if(myListOfPlayers.get(turn).playerLocation == 0){
- int tempRollScore = gameDie.rollDie();
- if(tempRollScore == 1){
- myListOfPlayers.get(turn).playerLocation = 1;
- System.out.println("Player no." + (turn+1) + " has entered the board.");
- }
- 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.
- turn = 0;
- }else{ // Else just increment so that the next player can take turn.
- turn++;
- }
- }
- else if(myListOfPlayers.get(turn).playerLocation >= 1){
- myListOfPlayers.get(turn).playerLocation = myListOfPlayers.get(turn).playerLocation + gameDie.rollDie(); // Moving the player forwards.
- myListOfPlayers.get(turn).playerLocation = board.checkIfSpecial(myListOfPlayers.get(turn).playerLocation);
- if(myListOfPlayers.get(turn).playerLocation >= 100){ // Here I noticed we don't need the "isWinner bool flag"
- winnersID = turn; // The winnersID is the one that's taking the turn.
- winFlag = true; // This will break the loop.
- }
- 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.
- turn = 0;
- }else{ // Else just increment so that the next player can take turn.
- turn++;
- }
- }
- else{ // If player has not yet thrown 1 go to the next player.
- System.out.println("Player no." + (turn+1) + " has not yet entered the board.");
- 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.
- turn = 0;
- }else{ // Else just increment so that the next player can take turn.
- turn++;
- }
- }
- }
- System.out.println("The WINNER is player #" + (winnersID + 1) + " ! CONGRATULATIONS!!!!"); // Announcing the winner.
- }
- /* ----------------------------------------------------------------------------------------------------------------------------
- * playerSetup() -> This method will accept as parameter the number of players to take part in the game.
- * Afterwards a new ArrayList will be created that will contain all the players and then return the ArrayList.
- */
- static ArrayList<Player> playerSetup(int numberOfPlayers){
- ArrayList<Player> playerList = new ArrayList<>(); // ArrayList containing Players.
- for(int i=0;i<numberOfPlayers;i++){ // This for loop will put the players into the ArrayList.
- playerList.add(new Player(0,i)); // Add new Player with parameters 1) Position | 2) Player ID.
- System.out.println("Player #" + (i+1) + ". was created!\n"); // Notifying the user the players have been successfully created.
- }
- return playerList; // Method returns the created ArrayList.
- }
- // -------------- END OF playerSetup() METHOD ---------------------------------
- }
- // ---------------------- END OF the Game CLASS ---------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement