Sin
2013-04-13 14:56:19
//Title: Red Yellow Green Game
//Author: *name omitted*, last updated April 12th, 2013
//Purpose: To learn about GUIs/Marks
//Image download and save method thanks to a thread on StackOverflow
//CreateImageIcon method thanks to Oracle
//Tutorial on Threads() and how to use them thanks to a video tutorial
//Type in 'master' into to the text field to enable the hint button
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.lang.Math;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class RYG extends Applet implements ActionListener
{
long thenum = randNum ();
String imageUrl = "http://i47.tinypic.com/11hzb48.png";
String destinationFile = "RYG.png";
int x = 0;
JLabel pic, instruction;
JTextField guessField;
JButton hint, guess, newNum;
JLabel title[] = new JLabel [4];
JPanel f = new JPanel ();
JPanel t = new JPanel ();
JPanel button = new JPanel (new BorderLayout ());
public void init ()
{
setSize (380, 240);
setBackground (Color.lightGray);
t.setBackground (Color.lightGray);
f.setBackground (Color.lightGray);
button.setOpaque (false);
//JLabel Definitions
title [0] = new JLabel ("Red");
title [1] = new JLabel ("Yellow");
title [2] = new JLabel ("Green");
title [3] = new JLabel ("Game");
title [0].setForeground (Color.red);
title [1].setForeground (Color.yellow);
title [2].setForeground (Color.green);
title [3].setForeground (Color.blue);
for (int i = 0 ; i < title.length ; i++)
{
title [i].setFont (new Font ("Serif", Font.PLAIN, 26));
t.add (title [i]);
}
pic = new JLabel (createImageIcon (destinationFile));
instruction = new JLabel ("Enter your guess: ");
//JButton definitions
hint = new JButton ("Hint.");
hint.setEnabled (false);
hint.setActionCommand ("hint");
hint.addActionListener (this);
guess = new JButton ("Guess.");
guess.setActionCommand ("guess");
guess.addActionListener (this);
newNum = new JButton ("New number");
newNum.setActionCommand ("new");
newNum.addActionListener (this);
//JTextField definitions
guessField = new JTextField (18);
//JPanel additions
f.add (instruction);
f.add (guessField);
button.add (guess, BorderLayout.CENTER);
button.add (hint, BorderLayout.SOUTH);
button.add (newNum, BorderLayout.EAST);
//Applet additions
add (t);
add (pic);
add (f);
add (button);
}
public void actionPerformed (ActionEvent e)
{
//If the Guess button is clicked
if (e.getActionCommand ().equals ("guess"))
{
showStatus ("Guess command received");
String myG = guessField.getText ();
//Creates a new thread, was just an experiment
Thread t = new Thread ();
{
if (myG.equalsIgnoreCase ("master"))
{
showStatus ("Master command recieved");
hint.setEnabled (true);
button.revalidate ();
button.repaint ();
}
}
//If textfield text is 'master', run the master Thread
if (myG.equalsIgnoreCase ("master"))
{
t.run ();
}
if (!myG.equals ("master"))
{
try
{
x = Integer.parseInt (myG, 10); //ParseInt inside a try/catch, so that incase the user enters a letter, it throws an exception
}
catch (NumberFormatException ex)
{
//shows an error message
JOptionPane.showMessageDialog (null, "No letters can be entered, only numbers!", "Error!", JOptionPane.INFORMATION_MESSAGE);
}
//displays the color codes
showStatus ("Red: " + red (x) + " Yellow: " + yellow (x) + " Green: " + green (x));
if (green (x) == 3) //If winning condition is met
{
JOptionPane.showMessageDialog (null, "", "", JOptionPane.INFORMATION_MESSAGE, createImageIcon ("firework.gif"));
int menu = JOptionPane.showConfirmDialog (null, "Congratulations! You won! \n To play again, please click 'Yes'", "Winner!", JOptionPane.INFORMATION_MESSAGE);
if (menu == (JOptionPane.YES_OPTION))
{
//creates a new number
thenum = randNum ();
}
else if (menu == (JOptionPane.NO_OPTION))
{
//Exits the program
System.exit (0);
}
}
}
}
if (e.getActionCommand ().equals ("hint"))
{
//Master hint
JOptionPane.showMessageDialog (null, "The secret number is: " + thenum, "Hint", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getActionCommand ().equals ("new"))
{
//Creates a new number
thenum = randNum ();
}
}
public int green (long guess)
{
//divide guess into digits
double g1 = Math.floor (guess / 100);
double g2 = Math.floor (guess / 10 % 10);
double g3 = Math.floor (guess % 10);
//divide the secret num into digits
double s1 = Math.floor (thenum / 100);
double s2 = Math.floor (thenum / 10 % 10);
double s3 = Math.floor (thenum % 10);
//make a counter to count the green digits
int r = 0;
if (g1 == s1 && (g1 != s2 && g1 != s3))
r++;
if (g2 == s2 && (g2 != s1 && g2 != s3))
r++;
if (g3 == s3 && (g3 != s1 && g3 != s2))
r++;
//return the number of green digits
return r;
}
public int red (long guess)
{ //divide guess into digits
double g1 = Math.floor (guess / 100);
double g2 = Math.floor (guess / 10 % 10);
double g3 = Math.floor (guess % 10);
//divide the secret num into digits
double s1 = Math.floor (thenum / 100);
double s2 = Math.floor (thenum / 10 % 10);
double s3 = Math.floor (thenum % 10);
//make a counter to count the red digits
int r = 0;
if (g1 != s1 && g1 != s2 && g1 != s3)
r++;
if (g2 != s1 && g2 != s2 && g2 != s3)
r++;
if (g3 != s1 && g3 != s2 && g3 != s3)
r++;
//return the number of red digits
return r;
}
public int yellow (long guess)
{
//divide guess into digits
double g1 = (guess / 100);
double g2 = (guess / 10 % 10);
double g3 = (guess % 10);
//divide the secret num into digits
double s1 = (thenum / 100);
double s2 = (thenum / 10 % 10);
double s3 = (thenum % 10);
//make a counter to count the red digits
int r = 0;
if (g1 != s1 && (g1 == s2 || g1 == s3))
r++;
if (g2 != s2 && (g2 == s1 || g2 == s3))
r++;
if (g3 != s3 && (g3 == s2 || g3 == s1))
r++;
//return the number of red digits
return r;
}
//Creates a random number between 100 to 1000
public static long randNum ()
{
long secretnum = Math.round (Math.random () * 1000);
while (secretnum < 100)
{
secretnum = Math.round (Math.random () * 1000);
}
return secretnum;
}
public static void saveImage (String imageUrl, String destinationFile) throws IOException
{
URL url = new URL (imageUrl);
InputStream is = url.openStream ();
OutputStream os = new FileOutputStream (destinationFile);
byte[] b = new byte [2048];
int length;
while ((length = is.read (b)) != -1)
{
os.write (b, 0, length);
}
is.close ();
os.close ();
}
protected ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = getClass ().getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
showStatus ("Couldn't find file: " + path + ". Downloading now.");
try
{
saveImage (imageUrl, destinationFile);
return new ImageIcon (imgURL);
}
catch (Exception ex)
{
}
return null;
}
}
}
Assignment:
A simple guessing game is the red-yellow-green game. It is a two person game where one person picks a number between 100 and 999 and the other person tries to guess it. Every time a guess is made, the picker responds to the guesser by telling them the number of red digits, yellow digits and green digits in the guess.
A guess digit is a green digit if the guess digit and the corresponding answer digit are the same and in the same position. A guess digit is a yellow digit if the digit is in the secret number, but it is not in the right position. A guess digit is a red digit if it is not any of the digits in the secret number. Suppose the secret number is 123 and the guess is 422. The reply would be one red, one yellow and one green. The initial guess digit, 4, is a red digit because it is not 1, 2, or 3. The middle guess digit is green because it matches the corresponding answer digit. The final guess digit, 2, is a yellow digit because 2 is in the number, but it is not in the final position.
When supplying the red-yellow-green information in response to a guess, the reply gives the number of red digits first, the number of yellow digits next and then the number of green digits. By giving totals only, the game is made harder. Suppose the answer is 492 and the guess is 249; the picker would reply zero red, three yellow and zero green.
What should I add?