Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class SnakeLadderGame{
- 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 ---------------------------------------
- class Player{
- int playerLocation;
- int playerID;
- Player(int playerLocation, int playerID){
- this.playerLocation=playerLocation;
- this.playerID=playerID;
- }
- }
- import java.util.*;
- class Board{
- private int[] ladderStart = {4,9,20,28,40,51,63,71};
- private int[] ladderEnd = {14,31,38,84,59,67,81,91};
- private int[] snakeStart = {17,54,62,64,87,93,95,99};
- private int[] snakeEnd = {7,34,19,60,24,73,75,78};
- Board(){
- }
- int checkIfSpecial(int playerPosition){
- for(int i = 0; i < ladderStart.length; i++){
- if(playerPosition == ladderStart[i]){
- playerPosition = ladderEnd[i];
- System.out.println("Player went up the ladder at position " + ladderStart[i] + " to " + ladderEnd[i]);
- }
- }
- for(int i = 0; i < snakeStart.length; i++){
- if(playerPosition == snakeStart[i]){
- playerPosition = snakeEnd[i];
- System.out.println("Player went down the snake from position " + snakeStart[i] + " to " + snakeEnd[i]);
- }
- }
- return playerPosition;
- }
- }
- import java.util.*;
- class Die{
- int dieValue;
- int rollDie(){
- Random rngForDie = new Random();
- dieValue=rngForDie.nextInt(6)+1;
- System.out.println("The Die rolled: "+dieValue);
- return dieValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement