Skip to main content

SNAKE AND LADDER || JAVA GAME




import java.util.*;

class SnakeLadderDemo
{
public static void main(String args[])
{

System.out.println("\t\t\t\t\t\t\tW.E.L.C.O.M.E");
System.out.println();
System.out.println("\t\t\t\t\t\t\tThis is a game of  Snake and Ladder.");
System.out.println("\t\t\t\t\t\t\tInstructions.");
System.out.println();
System.out.println("\t\t\t\t\t\t\tHere you will be competing against the PC.");
System.out.println("\t\t\t\t\t\t\tThere are ladders and snake if you find a snake");
System.out.println("\t\t\t\t\t\t\tyour position will be reduced to the one pointed by Snake.");
System.out.println("\t\t\t\t\t\t\tIf however,you meet a ladder your position will be");
System.out.println("\t\t\t\t\t\t\tincreased to the pointed square.");
System.out.println("\t\t\t\t\t\t\tThe first one to reach 100 will be the Winner.");
System.out.println();
System.out.println();
System.out.println("Coded By :Rahul Ranjan");
System.out.println();
System.out.println("*************************************************************************************************************************************************");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
int i=100;
int p=i-9;
int k=0;
while(i>=0)
{
int j=i;

 /* N O T E :    HERE IF AND ELSE CONDITIONS  ARE NOT REQUIRED IT IS JUST DONE TO MAKE OUTPUT LOOK BETTER.THIS IS BECAUSE 100 IS A THREE DIGIT NUMBER WHILE ALL OTHER ROWS HAVING NUMBERS ARE EACH 2 DIGIT SO THE 90'S ROW IS SHIFTED RIGHT BY 1 DIGIT BECAUSE OF 100 BEING A THREE DIGIT NUMBER.SIMILARLY IN THE LAST COLUMN EACH NUMBER WILL BE OF ONE DIGIT SO WE HAVE TO SHIFT 10 BY 1 DIGIT TO COUNTER 100 AND THEN ALL OTHER DIGIT UP TILL 1 BY 2 TO MAKE ALL THE NUMBERS LOOK IN THE SAME COLUMN.THIS IS OPTIONAL AND YOU CAN REMOVE THESE CONDITIONS.*/


while(i>=p && k<101 && i!=0)                    
{

if(i%10==9 && i <99)
{
System.out.print(" "+i+"             ");
i--;
k++;
}

if(i<9 && i>=1)
{
System.out.print(" "+i+"             ");
i--;
k++;
}
/*YOU CAN SIMPLY USE THE STATEMENTS WRITTEN IN ELSE INSIDE THE WHILE LOOP ,IF YOU WISH.BUT THE OUTPUT WILL NOT BE PROPERLY ALIGNED. */
else
{
System.out.print(i+"             ");
i--;
k++;
}


}
System.out.println("");
System.out.println("");
i=j-10;
p=i-9;
}
int pl=0;
int x[];
x=new int[2];
x[0]=1;
x[1]=1;

int y=1;
System.out.println("Do you want to play the Game: Press 1 for Yes || 0 for No  ?");
Scanner scm=new Scanner(System.in);
int sp=scm.nextInt();

while(sp==1 && x[0]<100 && x[1]<100)
{

/*THROWING DICE */

for(pl=0;pl<2;pl++)
{
i=0;
Random r=new Random();

System.out.println("!!!DICE THROWN!!!");


System.out.println();
System.out.println("***********Number on Dice is : **********");

int n=r.nextInt(6) + 1;

System.out.println(n);

if(x[pl]+n<=100)
{
x[pl]=x[pl]+n;
}
/*CREATING LADDERS AND SNAKES*/



int number=x[pl];

int df=number;
switch(number)
{
/*CREATING LADDERS*/
case 2:
{
number=17;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 9:
{
number=12;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 21:
{
number=23;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 34:
{
number=39;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 49:
{
number=75;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 52:
{
number=98;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 65:
{
number=82;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 92:
{
number=99;
System.out.println("*********Ladder********");
System.out.println();
}
break;
case 79:
{
number=95;
System.out.println("*********Ladder********");
System.out.println();
}
break;
/*NOW CREATING SNAKES*/
case 8:
{
number=1;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 15:
{
number=3;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 25:
{
number=20;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 35:
{
number=3;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 42:
{
number=4;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 97:
{
number=27;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
case 84:
{
number=5;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
  case 67:
{
number=18;
System.out.println("*********S N A K E ********");
System.out.println();
}
break;
default:
{
number=df;
}
}

x[pl]=number;

if(pl==0 && x[0]<100 && x[1]<100)
{
System.out.println("The new position of Player is:  "+x[pl]);
System.out.println();
}
if(pl==1 && x[0]<100 && x[1]<100)
{
System.out.println("The new position of Computer is:  "+x[pl]);
System.out.println();
}
if(x[pl]>=100 && pl==0)
{
System.out.println("Y O U    W O N");
}
if(x[pl]>=100 && pl==1)
{
System.out.println("Y O U    L O S T");
}
}
System.out.println("Do you want to play the Game: Press 1 for Yes || 0 for No  ?");
sp=scm.nextInt();


}


System.out.println("Exiting Game......");
System.out.println("Click X to quit cmd");



}
}





/*This program has however one flaw,but that can be ignored because the code is abosolutely perfect and the flaw never affect the outcome of the game.The small problem is that if you win ,the dice will still be thrown for computer but it's position will not be calculated and the program will exit what happens here is it just prints the statement because of the loop.Once again: The code works perfectly fine and nothing will effect the outcome of game.*/


Comments

Popular posts from this blog

Check if two strings are equal.

In this post we will see how to check if two strings are equal in C.For this we will use strcmp function. It compares two strings  lexicographically i.e character by character, if the two character matches moves to next character and compares it. It returns 0 if both strings are equal. #include<stdio.h> #include<string.h> int main() { char str[25]; printf("Enter string 1.\n"); gets(str); char str1[25]; printf("\nEnter string 2.\n"); gets(str1); int i = strcmp(str, str1); if(i==0) { printf("Equal.\n"); } else { printf("Not Equal.\n"); } return 0; }

Java program to remove all whitespaces from entered string.

import java.util.*; class RemoveWhiteSpaceFromString { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your string"); String s=sc.nextLine(); System.out.println(); System.out.println("Removing Whitespaces"); System.out.println(); s=s.replaceAll("\\s+",""); System.out.println(s); } }