ignore this

Status
Not open for further replies.

aaronkupen

Fully Optimized
Messages
1,848
Location
Pittsburgh, PA
Code:
/*

Date: 11/17/05
Filename: GuessingGame.java
Purpose: a game to guess a number between 1-15
*/

import javax.swing.*;

public class GuessingGame
{
	public static void main(String[] args)
	{
		JOptionPane.showMessageDialog(null,"Welcome to the guessing game, guess a number between 1 and 15","Intro",JOptionPane.PLAIN_MESSAGE);

		int nNumber = (int)(Math.random() * 15 +1);
		int nGuess, nCounter = 0;
		int[] nPrevious;
		nPrevious = new int[15];

		String strNumbers = "\n";

		do
		{

			String strGuess = JOptionPane.showInputDialog(null,"Enter a number" + strNumbers,"Number",JOptionPane.PLAIN_MESSAGE);
			nGuess = Integer.parseInt(strGuess);

			if (nGuess < 1)
			{
				JOptionPane.showMessageDialog(null,"Number out of range","Error",JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				for (int i = 0; i++ <= nCounter;)
				{
					if (nGuess == nPrevious[i]
					{
						JOptionPane.showMessageDialog(null,"You entered the same number","Error",JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						nPrevious[nCounter] = nGuess;
					strNumbers += strGuess + "\n";
					++nCounter;
					}
				}
			}

		}
		while (nGuess != nNumber);

		JOptionPane.showMessageDialog(null,"You are correct. It took you " + nCounter + "time(s) to get it","Answer",JOptionPane.PLAIN_MESSAGE);

		System.exit(0);
	}
}
 
Code:
/*

Filename: 21Stones.java
Last Updated: 12/5/05
Purpose: A game
*/

import javax.swing.*;

public class Stones21
{
	public static void main(String[] args)
	{
		JOptionPane.showMessageDialog(null,"Welcome to 21 Stones, the object of the game is to take away \nup to 3 stones at a time, and whomever(the computer or you) \ntakes away the last stone wins the game","Directions",JOptionPane.INFORMATION_MESSAGE);


		int nStones = 21, nGuess, nWin = 0;
		String strWinner;

		//keeps program running until there are zero stones
		while (nStones != 0)
		{
			nStones = UserGuess(nStones);

			if (nStones == 0)
			{
				nWin = 0;
				break;
			}
			else
			{
				nWin = 1;
				nStones = CompGuess(nStones);
			}
		}

		if (nWin == 0)
		{
			strWinner = "You are the winner!";
		}
		else
		{
			strWinner = "The Computer Wins!";
		}

		JOptionPane.showMessageDialog(null,strWinner,"End Message",JOptionPane.PLAIN_MESSAGE);

		finish();
	}

	//recieves the user's guess
	public static int UserGuess(int nStones)
	{
		int newStones = 0, nDone = 0;

		while (nDone == 0)
		{
			String strGuess = JOptionPane.showInputDialog(null,"  You have " + nStones + " stone(s).  Continue Playing", "Guess",JOptionPane.PLAIN_MESSAGE);

			//validates user input was acceptable
			try
			{
				if (strGuess == null)
				{
					finish();
				}

				int nGuess = Integer.parseInt(strGuess);

				if (nGuess < 1 || nGuess > 3)
				{
					throw new NumberFormatException();
				}
				else
				{
					//validates if new stones is 0 or above
					try
					{
						newStones = nStones - nGuess;

						if (newStones < 0)
						{
							throw new NumberFormatException();
						}
						else
						{
							nDone = 1;
						}
					}
					catch (NumberFormatException e)
					{
						JOptionPane.showMessageDialog(null,"Enter a Number that doesnt make the total less than 0","Error",JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null,"Invalid Input","Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		JOptionPane.showMessageDialog(null,"You have " + newStones + " left.","Update",JOptionPane.PLAIN_MESSAGE);

		return newStones;
	}

	//computer performs its guess
	public static int CompGuess(int nStones)
	{
		int newStones, nGuess;

		if (nStones == 5 || nStones == 6 || nStones == 7)
		{
			nGuess = nStones - 4;
		}
		else
		{
			if (nStones % 3 == 0)
			{
				nGuess = 3;
			}
			else if (nStones % 2 == 0)
			{
				nGuess = 2;
			}
			else
			{
				nGuess = 1;
			}
		}

		newStones = nStones - nGuess;

		JOptionPane.showMessageDialog(null, "The computer took " + nGuess + " stone(s).","Update",JOptionPane.PLAIN_MESSAGE);

		return newStones;
	}

	//closes program
	public static void finish()
	{
		System.exit(0);
	}
}
 
Status
Not open for further replies.
Back
Top Bottom