While this opening statement may sound cliche, I have used it for a very specific purpose - it's related to the main theme if this post. The phrase is a reference to the classic 'Hello World' computer program (Humorous wordplay. Ha ha). To the uninitiated, the 'Hello World' program is a computer program that displays the sentence 'Hello world!' on a display device - it is generally one of the easiest programs to write, irrespective of the language used. To those of you who got the reference, I suppose there is no point in explaining it and rendering the pun moot.
So I'm guessing that a lot of you have by now guessed what this post is all about. You're actually partially correct. Yes, this post is about computer programming. But no, it is not a generic post describing a programming language or its evolution.
In the 9th and 10 grade, I took a course in Java programming in high school. It was incredible fun - but I'm not going to focus on that right now (maybe in a later post). Anyway, after I started to get a feel of the language and had completed taking my first baby steps in program writing, I decided to get a little more adventurous. I consolidated my knowledge and Java coding skills and made up my mind - I was going to develop a . . . . DUN DUN DUN . . . . game.
For my ambitious project (or so it seemed to me), I decided to develop a text-based emulator of the hugely popular board game, Mastermind. (Yes, the one with the coloured pegs). If you haven't heard of or played it, I suggest that you read up on the game before going further into this post (the Wikipedia article on the game is excellent).
In my version of the game, the user plays against the computer. It is the computer that decides the sequence of colours and the user who has to deduce the colour code in 10 turns. As in the original game, the computer does provide a clue after each turn. I haven't kept any provision for it to be the other way around - maybe I'll come up with that one some day. My first beta testers were my parents (my father is the one who introduced me to mastermind in the first place, way back when I was a kid) and my high school course teacher.
I wrote the code using Java on BlueJ as an Integrated Development Environment (IDE). It turned out pretty well and I'm quite proud of it. While I haven't put it up on the Internet, I have put up the entire code below. Those of you who are interested in reading the code can scroll down the page.
import java.lang.*;
import java.io.*;
public class Mastermind
{
long noseq[];
int noseq2[];
String seq[];
String eseq[];
char tmpseq[];
int white;
int black;
static String
nm;
static String
lines[];
static int
wordcnt = 0;
public
Mastermind()
{
noseq = new
long[4];
noseq2 =
new int[4];
seq = new
String[4];
eseq = new
String[4];
tmpseq =
new char[4];
white = 0;
black = 0;
nm = new
String();
lines = new
String[20];
wordcnt =
0;
}
public static
void timeloop()
{
for(double
j = 0.0; j < 25000000.0; j++)
{
}
}
public void
timeloop2()
{
for(double
i = 0; i < 7500000000.0; i++)
{
}
}
public void
genSequence()
{
for(int i =
0; i < 4; i++)
{
noseq[i] = Math.round(Math.random()*10);
if(noseq[i] > 6)
noseq[i] -= 6;
if(noseq[i] < 1)
noseq[i] += 1;
}
for(int i =
0; i < 4; i++)
{
noseq2[i] = (int)(noseq[i]);
}
}
public void
saveSeq()
{
for(int i =
0; i < 4; i++)
{
switch(noseq2[i])
{
case 1: tmpseq[i] = 'G';
break;
case 2: tmpseq[i] = 'B';
break;
case 3: tmpseq[i] = 'R';
break;
case 4: tmpseq[i] = 'Y';
break;
case 5: tmpseq[i] = 'P';
break;
case 6: tmpseq[i] = 'O';
break;
}
}
}
public void
assignColours()
{
for(int i =
0; i < 4; i++)
{
if(noseq[i]
== 1)
{
seq[i] = "Green";
}
else
if(noseq[i] == 2)
{
seq[i] = "Blue";
}
else
if(noseq[i] == 3)
{
seq[i] = "Red";
}
else
if(noseq[i] == 4)
{
seq[i] = "Yellow";
}
else
if(noseq[i] == 5)
{
seq[i] = "Purple";
}
else
if(noseq[i] == 6)
{
seq[i] = "Orange";
}
}
}
public void
instructions()
{
System.out.println();
System.out.println("There are 6 main colours in this game: Green,
Blue, Red, Yellow, Purple & Orange.");
System.out.println("Each colour has a corresponding letter:");
System.out.println("G – Green");
System.out.println("B - Blue");
System.out.println("R - Red");
System.out.println("Y - Yellow");
System.out.println("P - Purple");
System.out.println("O - Orange");
System.out.println("The letters written next to the colours are
used to select the respective colours during the game.");
System.out.println("It is recommended that you write down the color
code(key) as it will not be displayed again.");
timeloop2();
System.out.println();
System.out.println("The computer will select a random sequence of 4
colours out of the 6 main ones.");
System.out.println("A colour may be repeated.");
System.out.println("Your job is to deduce the correct sequence of
colours in 10 chances.");
System.out.println("In each chance, you will have to enter your own
sequence to see if it matches with the computer’s sequence.");
System.out.println("After you have entered your sequence, the
computer will compare the 2 sequences & display a message.");
System.out.println("If
a colour in your sequence is present in the computer’s sequence at the same
position, you will be");
System.out.println("given a white peg (the word white will be
written on the screen).");
System.out.println("If a colour in your sequence is present in the
computer’s sequence but at a different position, you ");
System.out.println("will be given a black peg (the word black will
be written on the screen).");
timeloop2();
System.out.println();
System.out.println("Example 1:");
System.out.println("Computer’s sequence: Green Blue Red Yellow");
System.out.println("Your sequence: Green Orange Yellow Red");
System.out.println("Display: 1 white & 2 black");
System.out.println("Green is at the same position in both sequences
(1 white).");
System.out.println("Red is at different positions in the 2
sequences(1 black).");
System.out.println("Yellow is at different positions in the 2
sequences(1 black).");
timeloop2();
System.out.println();
System.out.println("Example 2: ");
System.out.println("Computer’s sequence: Orange Orange Purple Yellow");
System.out.println("Your sequence: Orange Red Purple Green");
System.out.println("Display: 2 whites");
System.out.println("Purple at position 3 in both – 1 white");
System.out.println("Orange at position 1(your sequence) & position
1(computer’s sequence) – 1 white");
System.out.println("The orange at position 2(computer's sequence)
will not give a black peg as the orange at 1 is giving a white.");
System.out.println("This holds true for all such cases in the
game.");
timeloop2();
System.out.println();
wordcnt =
0;
lines[0] =
"You will get 10 chances to solve the code.";
lines[1] =
"In every chance, you will have to enter a sequence based on the result of
the previous sequence.";
lines[2] =
"The sequence should consist of only the starting letters of 4 colours
based on the key displayed earlier.";
lines[3] =
"Example: For Green Blue Red Yellow, you will enter – GBRY (no spaces!!).
";
lines[4] =
"You are now ready to play the game. Good Luck!!! ";
while(wordcnt < 5)
{
timeloop();
for(int
i = 0; i < lines[wordcnt].length(); i++)
{
System.out.print(lines[wordcnt].charAt(i));
timeloop();
}
System.out.println();
wordcnt++;
}
System.out.println();
System.out.println();
}
public void game()throws
IOException
{
String ch =
new String();
char ch1[]
= new char[4];
white = 0;
int wh[] =
new int[4];
black = 0;
int bl[] =
new int[4];
int cnt =
1;
InputStreamReader
reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("The computer has selected a random sequence of
4 colours. You have 10 chances to deduce it.");
System.out.println("A final
reminder: the 6 colours are - Green, Blue, Red, Yellow, Purple &
Orange.");
System.out.println("PLEASE TURN ON THE CAPS LOCK ON YOUR KEYBOARD
AS ALL SEQUENCES HAVE TO BE ENTERED IN CAPITAL LETTERS ONLY!");
System.out.println();
outer:
do
{
saveSeq();
System.out.print("Chance " + cnt + ": ");
ch =
input.readLine();
for(int
i = 0; i < 4; i++)
{
ch1[i] = ch.charAt(i);
}
for(int
i = 0; i < 4; i++) //white
{
if(ch1[i] == tmpseq[i])
{
wh[i]++;
ch1[i] = 'Z';
tmpseq[i] = 'Z';
}
}
for(int
i = 0; i < 4; i++) //black
{
for(int j = 0; j < 4; j++)
{
if(i != j)
{
if((ch1[i] == tmpseq[j]) && (ch1[i] != 'Z'))
{
bl[i]++;
ch1[i] = 'Z';
tmpseq[j] = 'Z';
}
}
}
}
for(int
i = 0; i < 4; i++)
{
black += bl[i];
white += wh[i];
}
if(white == 4)
{
System.out.println();
System.out.println("Congratulations " + nm + "! You have
won the game.");
break outer;
}
else
{
System.out.print(" " + black
+ " Black & " + white + " White");
System.out.println();
System.out.println();
for(int i = 0; i < 4; i++)
{
wh[i] = 0;
bl[i] = 0;
}
}
black =
0;
white =
0;
cnt++;
}
while(cnt
<= 10);
System.out.println();
if(white !=
4)
{
System.out.println(nm + ", you have been unable to deduce the
correct code. You have lost the game.......");
System.out.println("The correct sequence was:");
for(int
i = 0; i < 4; i++)
{
System.out.print(seq[i] + " ");
}
}
}
public static
void main()throws IOException
{
Mastermind
mf4 = new Mastermind();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
int ch = 0;
wordcnt =
0;
lines[0] = "Welcome to Pratik Gandhi's
version of the all time classic game - MASTERMIND!!!!!!!";
lines[1] =
"This game is a challenge for people of age 10 & above.";
lines[2] =
"All you need to play this game is a peice of paper, a pen and some
logic.";
lines[3] =
"";
lines[4] =
"To get started, type in your name & then press enter.";
while(wordcnt < 5)
{
timeloop();
for(int
i = 0; i < lines[wordcnt].length(); i++)
{
System.out.print(lines[wordcnt].charAt(i));
timeloop();
}
System.out.println();
wordcnt++;
}
nm =
input.readLine();
System.out.println();
wordcnt =
0;
lines[0] =
"To play after reading the instructions, press 1 & then press
enter.";
lines[1] =
"To start the game straightaway, press 2 & then press enter.";
while(wordcnt < 2)
{
timeloop();
for(int
i = 0; i < lines[wordcnt].length(); i++)
{
System.out.print(lines[wordcnt].charAt(i));
timeloop();
}
System.out.println();
wordcnt++;
}
System.out.println();
wordcnt =
0;
ch =
Integer.parseInt(input.readLine());
switch(ch)
{
case 1:
{
mf4.instructions();
mf4.genSequence();
mf4.saveSeq();
mf4.assignColours();
mf4.game();
}
break;
case 2:
{
mf4.genSequence();
mf4.saveSeq();
mf4.assignColours();
mf4.game();
}
break;
}
}
}
CODE ENDS HERE.
That's all for this post.
Hope you enjoyed it!
Pratik.