0% found this document useful (0 votes)
6 views4 pages

Final Java Game

The document is a Java program that implements a simple two-player game with two main games: Guess the Number and Hangman. Players take turns guessing a number or letters, with rules specified for each game, and the program determines the winner based on the number of attempts. The game can be played multiple times until players choose to stop.

Uploaded by

tamasabanerjee96
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Final Java Game

The document is a Java program that implements a simple two-player game with two main games: Guess the Number and Hangman. Players take turns guessing a number or letters, with rules specified for each game, and the program determines the winner based on the number of attempts. The game can be played multiple times until players choose to stop.

Uploaded by

tamasabanerjee96
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;
class JavaGame
{
String p1, p2, tw, tl;
Scanner ab = new Scanner([Link]);
void start()
{
[Link]("Enter name of Player 1");
p1 = [Link]();
[Link]("Enter name of Player 2");
p2 = [Link]();
}
void toss()
{
int e;
char y;
[Link]("Welcome to Toss");
[Link](p1 + " enter 0 for head, 1 for tail");
e = [Link]();
int max=1, min=0;
int range = max - min + 1;
int random = (int)([Link]()*range + min);
[Link]("Randomly generated number is "+random);
if (random == e)
{tw = p1; tl = p2;}
else
{tw = p2; tl = p1;}
}
void results(String p)
{
[Link]("The winner is "+p);
[Link]("Congratulations "+p);
}
void rules()
{
[Link]("Let's Proceed with the game. The rules of the game are:");
[Link]("1. The computer will choose a random number from 1-100.");
[Link]("2. You will be given eight chances to guess the number.");
[Link]("3. Every time you guess a number, your range will be narrowed. eg if you guess 70 in first try,
computer will show you less than 70.");
[Link]("4. The player who guesses their number with the least tries, wins.");
[Link]("5. Player 1 goes first, followed by player 2.");
[Link]("ALL THE BEST");
}
int guess(String p)
{
int max=100, min=1; int i,n, k=0;
int range = max - min + 1;
int random = (int)([Link]()*range + min);
[Link](p+" enter your guesses (one by one)");
for (i=1; i<=8; i++)
{
n = [Link]();
if (n>random)
[Link]("Below "+n+" Try again.");
if (n<random)
[Link]("Above "+n+" Try again.");
if (n==random)
{k = 1; [Link]("Congratulations. It is "+n+" Well done"); break;}
}
if (k==0)
[Link]("It was "+random);
return i;
}
void rules2()
{
[Link]("Let's proceed with the game. The rules of the game are:");
[Link]("1. The computer will choose a 4 lettered - word.");
[Link]("2. You have to guess the letters present in the word.");
[Link]("3. Everytime you pick a letter which is present in the word, computer will display the
positions in which the letter is present");
[Link]("4. Everytime you pick a letter which is not present in the word, you get a strike.");
[Link]("5. The game gets over when you get the seventh strike.");
[Link]("6. The player who guesses their word with the least tries, wins.");
[Link]("7. Player 1 goes first, followed by player 2.");
[Link]("ALL THE BEST");
}
int hangman(String p)
{ Scanner ab = new Scanner([Link]);
int i=0, j, k=0, l, count = 0;
String c, ch, g ="____";
char b,d;
int max=35, min=0;
int range = max - min + 1;
int random = (int)([Link]()*range + min);
String s[]
={"aims","arts","ants","bill","bath","bomb","body","bowl","boat","brew","cake","cars","desk","draw","ends","eats",
"flow","fame","fire","game","girl","goat","guys","grin","horn","hums","inky","jars","kite","loaf","mint","wars","waxy
","yams","yolk"};
ch = s[random];
[Link](p +" enter your guesses (letters or word)");
[Link](g);
while(count<7)
{ ++i; k =0;
c = [Link]();
l = [Link]();
if (l==1)
{ k=0;
b = [Link](0);
for (j=0; j<4; j++)
{
d = [Link](j);
if (b==d)
{
k=1;
g = [Link](0,j) + b + [Link](j+1);
[Link](b + " is present at position" + (j+1) + "\t \t \t");
}
}
if (k==0)
[Link](b + " is not present. Strike no. " + ++count + "\t \t \t" + g);
else
[Link](g);
}
else
{
if ([Link](ch))
{k = 1;
[Link]("Congratulations. It is "+ ch +" Well done");
break;
}
else
{
k=0;
[Link]("Strike no. " + ++count);
}
}
}
if (k==0)
[Link]("It was "+ch);
return i;
}
String check(int twt, int tlt)
{
if (twt < tlt)
return tw;
else if (tlt < twt)
return tl;
else
return "nobody";
}
char ask()
{ char ch;
[Link]("Do you wanna play again ? \n Enter 'Y' to continue. \n Enter 'N' to stop.");
ch = [Link]().charAt(0);
return ch;
}
int choice(String p)
{ char ch; int c=0;
[Link](p + " do you wanna play other games ? \n Press any key to continue. \n Enter 'N' to stop.");
ch = [Link]().charAt(0);
if (ch == 'N')
[Link](0);
else
{
[Link]("Let's proceed then. The different games are:");
[Link]("1. Guess the number");
[Link]("2. Hangman");
[Link]("Enter your choice "+p);
c = [Link]();
}
return c;
}
void main()
{ String winner; char choice = 'Y'; int twt, tlt; int n;
while (choice == 'Y')
{
JavaGame ob = new JavaGame();
[Link]();
[Link]();
[Link]([Link]);
n = [Link]([Link]);
switch(n)
{
case 1: [Link]();
twt = [Link]([Link]);
tlt = [Link]([Link]);
winner = [Link](twt,tlt);
[Link](winner);
break;
case 2 : ob.rules2();
twt = [Link]([Link]);
tlt = [Link]([Link]);
winner = [Link](twt,tlt);
[Link](winner);
break;
default: [Link]("Wrong choice. Enter again.");
n = [Link]([Link]);
}
choice = [Link]();
}
if (choice == 'N')
[Link](0);
}
}

You might also like