Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Main {
- public static String [] board = {"1-Home","2","3-Snake","4","5-Ladder","6","7","8","9-Ladder","10","11","12","13","14-Snake","15","16","17-Snake","18","19-Ladder","20-Snake","21","22","23-Ladder","24","25-Ladder","26","27","28","29-Snake","30","31-Ladder","32","33","34-Snake","35-Snake","36-Snake","37","38-Ladder","39","40","41","42-Ladder","43","44-Snake","45","46","47","48","49-Snake","50","51"};
- public static void main(String[] args) {
- int currPos1 = 0;
- int currPos2 = 0;
- int home = 0;
- int diceOne,diceTwo;
- int currPos =0;
- String playerOneName, playerTwoName;
- printBoard();
- System.out.println("Welcome to Snakes and Ladders!"); //Introductions and option for rules
- System.out.println("Do you want the instructions for Snakes and Ladders(YES/NO)");
- String instructions = In.getString();
- while (instructions.toUpperCase().equals("YES")){
- //Instructions for Snakes and Ladders choose(YES/NO)
- System.out.println("This is a text based Snake and Ladders game!");
- System.out.println("There is going to be two players.");
- System.out.println("There is going to be an option for one or two dices.");
- System.out.println("If a player lands on a snake than the player moves back a certain amount determined by the snakes amouunt and opposite for the Ladder.");
- System.out.println("The first player to land at home wins, but if you exceed the amount of spaces to land on home, than you move to home and retrace the steps remaining.");
- System.out.println(" ");
- System.out.println("Do you want the instructions again?(YES/NO)");
- instructions = In.getString();
- }
- //Get player names
- System.out.println("Enter your name First Player");
- playerOneName = In.getString();
- System.out.println("");
- System.out.println("Enter your name Second Player");
- playerTwoName = In.getString();
- //Game Starts
- do{
- System.out.println("How many die's do you want to play with First Player?(One/Two)");
- String dice = In.getString();
- //Get the amount of dices used by each player
- if (dice.toUpperCase().equals("ONE")){
- diceOne = diceRoll();
- currPos1 = (int)diceOne+currPos1;
- System.out.println("You rolled a "+diceOne+"and you're at position"+currPos1+".");
- }
- else{
- diceOne = diceRoll();
- diceTwo = diceRoll();
- currPos1 = (int)diceOne+ diceTwo+ currPos1;
- System.out.println("You rolled a "+diceOne+ "and"+diceTwo+"and you're at position"+currPos1+".");
- }
- if(board[currPos1-1].contains("Ladder"))
- {
- currPos1 = ladder(currPos1);
- }
- else if (board[currPos1-1].contains("Snake"))
- {
- currPos1 = snake(currPos1);
- }
- System.out.println("How many die's do you want to play with Second Player?(One/Two)");
- dice = In.getString();
- if (dice.toUpperCase().equals("ONE")){
- diceOne = diceRoll();
- currPos2 = (int)diceOne+currPos2;
- System.out.println("You rolled a "+diceOne+"and you're at position"+currPos2+".");
- }else{
- diceOne = diceRoll();
- diceTwo = diceRoll();
- currPos2 = (int)diceOne+ diceTwo+ currPos2;
- System.out.println(currPos2);
- System.out.println("You rolled a "+diceOne+ "and"+diceTwo+"and you're at position"+currPos2+".");
- }
- if(board[currPos2-1].contains("Ladder")){
- currPos2 = ladder(currPos2);
- }
- else if (board[currPos2-1].contains("Snake"))
- {
- currPos2 = snake(currPos2);
- }
- }while(currPos1<51 && currPos2 <51);
- if(currPos1 <=52){
- ladder(currPos);
- snake(currPos);
- }
- else{
- System.out.println("You have passed the boundaries, move back the amount of steps left in your roll.");
- }
- if(currPos2 <=52){
- ladder(currPos);
- snake(currPos);
- }
- else{
- System.out.println("You have passed the boundaries, move back the amount of steps left in your roll.");
- }
- }
- public static void printBoard(){ //Print the conetents of the board
- for(String i : board){
- System.out.print(i+"\t");
- }
- System.out.println("");
- }
- public static int diceRoll(){ //Random number generator for dices
- return (int) (Math.random()* 6) +1;
- }
- public static boolean checkWin(int currPos1, int currPos2){
- if (currPos1 ==51 && currPos2 == 51){
- return true;
- }
- return false;
- }
- public static int ladder(int currPos){ //if landed on a ladder change corresponding position value
- int stepIncrease = 0;
- if (currPos ==5){
- stepIncrease = 3;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==9){
- stepIncrease =5;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==19){
- stepIncrease =8;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==23){
- stepIncrease =9;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==25){
- stepIncrease = 5;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==31){
- stepIncrease = 2;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==38){
- stepIncrease = 4;
- currPos = currPos+stepIncrease;
- }
- else if(currPos ==42){
- stepIncrease = 3;
- currPos = currPos+stepIncrease;
- }
- System.out.println("You landed on a ladder and your positin is"+currPos+".");
- return currPos;
- }
- public static int snake(int currPos){
- int stepDecrease = 0;
- if (currPos ==3)
- stepDecrease = 2;
- else if(currPos ==14)
- stepDecrease = 5;
- else if(currPos ==17)
- stepDecrease = 4;
- else if(currPos ==20)
- stepDecrease = 3;
- else if(currPos ==29)
- stepDecrease = 1;
- else if(currPos==34)
- stepDecrease = 2;
- else if(currPos==35)
- stepDecrease=4;
- else if(currPos==36)
- stepDecrease = 5;
- else if(currPos==44)
- stepDecrease = 3;
- else if(currPos==49)
- stepDecrease = 6;
- currPos = currPos - stepDecrease;
- return currPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement