Tuesday, July 17, 2012

Coin Game in Java

import java.util.Scanner;
import java.util.Random;
public class CoinGame
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter '0' if you want to play against a computer or '1' if you want to play against another player.");
int start = keyboard.nextInt();
int score = 0;
int n = 1;
String a;
String b;
String ht = "ht";
Random coin = new Random();
if (start == 1) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Player 1's objective is to guess te opposite of what Player 2 will guess.");
System.out.println("Player 2's objective is to guess what Player 1 has guess that Player 2 will not guess.");
System.out.println(" In other words, Player 2 wants to guess Player 1's input.");
System.out.println("Every set that goes in Player 1's favor will decrease the score by 1.");
System.out.println("Every set that goes in Player 2's favor will increase the score by 1.");
System.out.println("Player 1 wins if the score reaches -5 while Player 2 wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Now, Player 1, with Player 2 not looking, enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("Player 1, you did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("40 blank lines were just printed so that Player 2 should not see Player 1's input.");
System.out.println("Now, Player 2, guess Player 1's input by entering 'h' (for heads) or 't' (for tails).");
b = keyboard.next();
char d = b.charAt(0);
System.out.println("Player 1 had entered " + c + " while Player 2 had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in Player 2's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in Player 1's favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Player 2 Wins!");
else
System.out.println("Player 1 Wins!");
}
if (start == 0) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Your objective is to guess te opposite of what the computer will guess.");
System.out.println("The computer's guesses will be randomly generated.");
System.out.println("Every set that goes in your favor will decrease the score by 1.");
System.out.println("Every set that goes in the computer's favor will increase the score by 1.");
System.out.println("You win if the score reaches -5 while the computer wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Please enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("You did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
int f = coin.nextInt(2);
String e = ht.substring(f,f+1);
char d = e.charAt(0);
System.out.println("You entered " + c + " while the computer had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in the computer's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in your favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Sorry, You Lost.");
else
System.out.println("You Win!");
}
}
}

No comments:

Post a Comment