|  |
11-18-2005, 07:54 AM
|
#1 (permalink)
|
Monster Techie Join Date: Sep 2005 Location: Boston, MA Posts: 1,791
| ignore this 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);
}
}
__________________ Lenovo Thinkpad T60
Intel Core 2 Duo T7200 @ 2.00 Ghz
2 GB DDR-2 memory
ATI Mobility Radeon x1400
60 GB 7200 rpm Hard Drive
Windows Vista Business SP1 32 Bit |
| |
12-06-2005, 07:51 AM
|
#2 (permalink)
|
Monster Techie Join Date: Sep 2005 Location: Boston, MA Posts: 1,791
| 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);
}
}
__________________ Lenovo Thinkpad T60
Intel Core 2 Duo T7200 @ 2.00 Ghz
2 GB DDR-2 memory
ATI Mobility Radeon x1400
60 GB 7200 rpm Hard Drive
Windows Vista Business SP1 32 Bit |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |