Author |
Message |
S_Grimm
|
Posted: Tue Dec 09, 2008 6:58 pm Post subject: Mastermind V1.0 |
|
|
So......
Mastermind. For those of you who don't know what it is, it is a game where you guess the numbers.
This version is a four number version. (as in guess 4 numbers ie 8862 or 1497). I would love suggestions on how to improve the code.
Next Version to include: GUI interface. Instead of numbers, colors!
BTW: If anyone in my class (rto) takes this and submits it as there own......
Java: |
/***********************
* The Mastermind Game *
* *
* Shane Grimminck *
* Copyright 2008 *
***********************/
import java.io.*;
public class mastermind
{
public static void main (String[] args) throws IOException
{
//Variable Declarations
long masterNumbers;
String masterString;
String guessString = "";
int guessInt;
int count = 0;
char[] masterChar = {'a', 'a', 'a', 'a'};
char[] guessChar = new char [4];
boolean[] posRight = new boolean [4]; // will be used to determine if the number is in the right position.
boolean[] numRight = new boolean [4]; // will be used to determine if the number is correct.
//Buffered Reader and Input Stream Reader Declarations
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (isr);
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
if (masterChar [0] == 'a')
{
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
}
if (masterChar [1] == 'a')
{
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
}
if (masterChar [2] == 'a')
{
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
}
if (masterChar [3] == 'a')
{
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
}
//System.out.println (masterString);
while ((!(masterString.equals (guessString))) && count < 3)
{
System.out.print ("Your guess : ");
guessInt = Integer.parseInt (input.readLine ());
guessString = String.valueOf (guessInt);
guessChar = guessString.toCharArray ();
count++;
/* for (int i = 0 ; i < 4 ; i++)
{
System.out.println ("Master : " + masterChar [i] + " Guess : " + guessChar [i]);
}
*/
for (int i = 0 ; i < 4 ; i++)
{
if (masterChar [i] == guessChar [i])
{
posRight [i] = true;
System.out.println ("Num " + (i + 1) + " is correct!");
}
}
for (int i = 0 ; i < 4 ; i++)
{
if ((masterChar [i] == guessChar [0]) && posRight [i] == false)
{
numRight [i] = true;
System.out.println (guessChar [0] + " is not in the correct position.");
}
}
for (int i = 0 ; i < 4 ; i++)
{
if ((masterChar [i] == guessChar [1]) && posRight [i] == false)
{
numRight [i] = true;
System.out.println (guessChar [1] + " is not in the correct position.");
}
}
for (int i = 0 ; i < 4 ; i++)
{
if ((masterChar [i] == guessChar [2]) && posRight [i] == false)
{
numRight [i] = true;
System.out.println (guessChar [2] + " is not in the correct position.");
}
}
for (int i = 0 ; i < 4 ; i++)
{
if ((masterChar [i] == guessChar [3]) && posRight [i] == false)
{
numRight [i] = true;
System.out.println (guessChar [3] + " is not in the correct position.");
}
}
}
if (masterString.equals (guessString))
{
System.out.println ("You Win!");
}
else
{
System.out.println ("You Lose");
System.out.println ("The answer was : " + masterString);
}
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Tue Dec 09, 2008 7:06 pm Post subject: RE:Mastermind V1.0 |
|
|
You have a lot of repeating code. Instead of
code: |
if (masterChar [0] == 'a')
if (masterChar [1] == 'a')
...
|
where each block has a redundant block of code, you can iterate over the conditions with a for-loop.
Same goes for your 4 copies of the "for (int i = 0 ; i < 4 ; i++)" loops. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
S_Grimm
|
Posted: Tue Dec 09, 2008 7:22 pm Post subject: RE:Mastermind V1.0 |
|
|
the "for (int i = 0; i < 4; i++){}" loops are separate so that they individually check. Stupid way to do it, I know, but it works. The "if masterChar..." stuff I will change though. |
|
|
|
|
|
Tony
|
Posted: Tue Dec 09, 2008 7:26 pm Post subject: RE:Mastermind V1.0 |
|
|
Java: |
for(int j= 0; j < 4; j++ ){
for (int i = 0 ; i < 4 ; i++ )
{
if ((masterChar [i ] == guessChar [j ]) && posRight [i ] == false)
{
numRight [i ] = true;
System. out. println (guessChar [j ] + " is not in the correct position.");
}
}
}
|
no? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
S_Grimm
|
Posted: Tue Dec 09, 2008 7:35 pm Post subject: RE:Mastermind V1.0 |
|
|
lol. I am reaaaally stupid. thanks tony. What did that just cut out? ummm..
57. wow. Just wow. Thanks Tony.
Heres the updated copy
Java: |
/***********************
* The Mastermind Game *
* v2 *
* Shane Grimminck *
* Copyright 2008 *
***********************/
//Thanks to Tony for helping shorten this to 97 lines instead of 141
import java.io.*;
public class mastermind2
{
public static void main (String[] args) throws IOException
{
//Variable Declarations
long masterNumbers;
String masterString;
String guessString = "";
int guessInt;
int count = 0;
char[] masterChar = {'a', 'a', 'a', 'a'};
char[] guessChar = new char [4];
boolean[] posRight = new boolean [4]; // will be used to determine if the number is in the right position.
boolean[] numRight = new boolean [4]; // will be used to determine if the number is correct.
//Buffered Reader and Input Stream Reader Declarations
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader input = new BufferedReader (isr);
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
for (int i = 0 ; i < 4 ; i++0)
{
if (masterChar [i] == 'a')
{
masterNumbers = (Math.round (Math.random () * 10000)); // returns a Random 4 digit number
masterString = String.valueOf (masterNumbers); // converts the number into a string
masterChar = masterString.toCharArray (); // converts the String into an array of char
}
}
while ((!(masterString.equals (guessString))) && count < 3)
{
guessInt = Integer.parseInt (input.readLine ());
guessString = String.valueOf (guessInt);
guessChar = guessString.toCharArray ();
count++;
/* for (int i = 0 ; i < 4 ; i++)
{
System.out.println ("Master : " + masterChar [i] + " Guess : " + guessChar [i]);
}
*/
for (int i = 0 ; i < 4 ; i++)
{
if (masterChar [i] == guessChar [i])
{
posRight [i] = true;
System.out.println ("Num " + (i + 1) + " is correct!");
}
}
for (int o = 0 ; o < 4 ; o++)
{
for (int i = 0 ; i < 4 ; i++)
{
if ((masterChar [i] == guessChar [o]) && posRight [i] == false)
{
numRight [i] = true;
System.out.println (guessChar [o] + " is not in the correct position.");
}
}
}
}
if (masterString.equals (guessString))
{
System.out.println ("You Win!");
}
else
{
System.out.println ("You Lose");
System.out.println ("The answer was : " + masterString);
}
}
}
|
|
|
|
|
|
|
S_Grimm
|
Posted: Tue Dec 09, 2008 7:36 pm Post subject: RE:Mastermind V1.0 |
|
|
Actually, I don't think that i need the boolean statements either.......
edit. Nvrmind i do need them. Anyone have any other suggestions? |
|
|
|
|
|
rto
|
Posted: Wed Dec 17, 2008 9:10 am Post subject: RE:Mastermind V1.0 |
|
|
God this program is completely and utterly garbage -.-
haha jk, goodly done |
|
|
|
|
|
S_Grimm
|
Posted: Wed Dec 17, 2008 11:19 am Post subject: RE:Mastermind V1.0 |
|
|
Well considering MINE is completed AND handed in to the teacher and YOURS still isn't.......... I'd say mine is better than yours.
jk. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|