The Dice Game
Hello,
This is my first BLUEJ project which i am publishing or sharing with anyone else
About the Project:
- This Project is a very easy 2-players dice game
- This is a royalty free project
- This project is a basic mixture of what i am learning now i.e In 10th ICSE
Requirements:
- Bluej(Of coarse)
- Basic knowledge of running a program in bluej
How To Play:
- Enter h to know rules
- Enter s to start the game
- Enter Player 1 and Player 2 then dice will be rolled by the computer
- Enter s to continue the game if one round is over
- Enter e to exit the game
Program:
import java.util.*;
class diceGame
{
Scanner sc=new Scanner(System.in);
String player1,player2;
int roll,output1=0,output2=0;
void players()
{
System.out.println("Enter the name of Player 1");
System.out.println("--------------------------");
player1=sc.next();
System.out.println("Enter the name of Player 2");
System.out.println("--------------------------");
player2=sc.next();
}
void diceRoll()
{
for(int i=0;i<5;i++)
{
roll=(int)(Math.random()*6)+1;
System.out.println(roll);
output1+=roll;
}
System.out.println(output1);
for(int i=0;i<5;i++)
{
roll=(int)(Math.random()*6)+1;
System.out.println(roll);
output2+=roll;
}
System.out.println(output2);
}
void result()
{
if(output1>output2)
System.out.println("Congrats\n**********\n"+player1+"\nis the winner\n**********");
if(output1<output2)
System.out.println("Congrats\n**********\n"+player2+"\nis the winner\n**********");
if(output1==output2)
System.out.println("___//-Its a tie-\\___");
output1=0;output2=0;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
diceGame ob=new diceGame();
System.out.println();
System.out.println("--------------------------");
System.out.println("|HellWelcome to Dice Game|");
System.out.println("--------------------------");
System.out.println("*****Enter 'h' for rules*****\n*****Enter 's' to Start the Game*****");
char a;
a=sc.next().charAt(0);
int infinite=1;
while(infinite>0)
{
if(a=='h')
{
System.out.println("This is a simple dice Game\nJust enter your names and Computer will Automaticly roll the dice");
System.out.println("----------------------------------------------");
System.out.println("Enter s to Start");
a=sc.next().charAt(0);
if(a=='s')
{
ob.players();
ob.diceRoll();
ob.result();
System.out.println("Enter s to continue the game\nEnter e to exit");
a=sc.next().charAt(0);
if(a=='e')
System.exit(0);
else if(a=='s')
continue;
else
System.out.println("Wront Input");System.exit(0);
}
}
else if(a=='s')
{
ob.players();
ob.diceRoll();
ob.result();
System.out.println("Enter s to continue the game\nEnter e to exit");
a=sc.next().charAt(0);
if(a=='e')
System.exit(0);
else if(a=='s')
continue;
else
System.out.println("Wront Input");System.exit(0);
}
else
System.out.println("Wront Input");System.exit(0);
}
}
}
class diceGame
{
Scanner sc=new Scanner(System.in);
String player1,player2;
int roll,output1=0,output2=0;
void players()
{
System.out.println("Enter the name of Player 1");
System.out.println("--------------------------");
player1=sc.next();
System.out.println("Enter the name of Player 2");
System.out.println("--------------------------");
player2=sc.next();
}
void diceRoll()
{
for(int i=0;i<5;i++)
{
roll=(int)(Math.random()*6)+1;
System.out.println(roll);
output1+=roll;
}
System.out.println(output1);
for(int i=0;i<5;i++)
{
roll=(int)(Math.random()*6)+1;
System.out.println(roll);
output2+=roll;
}
System.out.println(output2);
}
void result()
{
if(output1>output2)
System.out.println("Congrats\n**********\n"+player1+"\nis the winner\n**********");
if(output1<output2)
System.out.println("Congrats\n**********\n"+player2+"\nis the winner\n**********");
if(output1==output2)
System.out.println("___//-Its a tie-\\___");
output1=0;output2=0;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
diceGame ob=new diceGame();
System.out.println();
System.out.println("--------------------------");
System.out.println("|HellWelcome to Dice Game|");
System.out.println("--------------------------");
System.out.println("*****Enter 'h' for rules*****\n*****Enter 's' to Start the Game*****");
char a;
a=sc.next().charAt(0);
int infinite=1;
while(infinite>0)
{
if(a=='h')
{
System.out.println("This is a simple dice Game\nJust enter your names and Computer will Automaticly roll the dice");
System.out.println("----------------------------------------------");
System.out.println("Enter s to Start");
a=sc.next().charAt(0);
if(a=='s')
{
ob.players();
ob.diceRoll();
ob.result();
System.out.println("Enter s to continue the game\nEnter e to exit");
a=sc.next().charAt(0);
if(a=='e')
System.exit(0);
else if(a=='s')
continue;
else
System.out.println("Wront Input");System.exit(0);
}
}
else if(a=='s')
{
ob.players();
ob.diceRoll();
ob.result();
System.out.println("Enter s to continue the game\nEnter e to exit");
a=sc.next().charAt(0);
if(a=='e')
System.exit(0);
else if(a=='s')
continue;
else
System.out.println("Wront Input");System.exit(0);
}
else
System.out.println("Wront Input");System.exit(0);
}
}
}
You can download bluej from here: https://www.bluej.org/
*****You can report me if you find any bugs At: yaplayz55@gmail.com *****
*****Support Me for more such programs and other stuff*****
*****You can also correct me and guide me where i can do better*****
*****Thank You*****
*****Peace*****