PDA

Click to See Complete Forum and Search --> : Mastermind game...


Dec 10th, 2000, 10:40 AM
this is reall wierd,
I have this program, a mastermind game,

it should be working perfect... but it's not..


#include <iostream.h>
#include <stdlib.h>

int numguess = 0;
void getguess();

int main()
{
getguess();
return 0;
}

void getguess()
{

randomize();
int peg[2];
peg[0] = random(5) + 1;
peg[1] = random(5) + 1;
peg[2] = random(5) + 1;

int guess[2];
int rightpeg = 0;
int rightcolor = 0;
//checking for dupes
if (peg[0] == peg[1])
{
if (peg[1] > 1) peg[1]--;
else peg[1] ++;
}

if(peg[0] == peg[2])
{
if (peg[2] > 1) peg[2]--;
else peg[2] ++;
}

if (peg[1] == peg[2])
{
if (peg[2] > 1) peg[2]--;
else peg[2] ++;
}
cout << peg[0] << endl << peg[1] << endl << peg[2] << endl;
while(rightcolor < 3)
{

numguess++;
cout << "Guess " << numguess << ":" << endl;
cout << "First Peg: ";
cin >> guess[0];
cout << "Second Peg: ";
cin >> guess[1];
cout << "Third Peg: ";
cin >> guess[2];

if (guess[0] == peg[1])
rightpeg++;

if (guess[0] == peg[2])
rightpeg++;

if (guess[1] == peg[0])
rightpeg++;

if (guess[1] == peg[2])
rightpeg++;

if (guess[2] == peg[0])
rightpeg++;

if (guess[2] == peg[1])
rightpeg++;


if (guess[0] == peg[0])
{
rightpeg++;
rightcolor++;
}

if (guess[1] == peg[1])
{
rightpeg++;
rightcolor++;
}

if (guess[2] == peg[2])
{
rightpeg++;
rightcolor++;
}


cout << "You have " << rightpeg << " correct peg(s) and " << rightcolor;
cout << " correct color(s)" << endl;
rightcolor = 0;
rightpeg = 0;
}
cout << "You have broken the code in " << numguess << "guesses.";
}


lets say the order of the pegs is
2
1
3


I enter
2
1
3

it says all the pegs are correct, but only 2 colors are correct...

it seems to ignore the first number...


Any help is appreciated,

Thanks,
Dennis

Dec 10th, 2000, 11:11 AM
<edited>

Ok I got it to work now



#include <iostream.h>
#include <stdlib.h>

int numguess = 0;

void getguess();

int main()
{
getguess();
return 0;
}

void getguess()
{

randomize();

int peg1 = random(5) + 1;
int peg2 = random(5) + 1;
int peg3 = random(5) + 1;

int guess1 = 0;
int guess2 = 0;
int guess3 = 0;

int rightpeg = 0;
int rightcolor = 0;
while((peg1 == peg2) || (peg1 == peg3) || (peg2 == peg3))
{
while (peg1 == peg3)
{
peg3 = random(5) + 1;
}

while (peg1 == peg2)
{
peg2 = random(5) + 1;
}


while (peg2 == peg3)
{
peg2 = random(5) + 1;
}
}


while(rightcolor < 3)
{
rightcolor = 0;
rightpeg = 0;
numguess++;
cout << "Guess " << numguess << ":" << endl;
cout << "First Peg: ";
cin >> guess1;
cout << "Second Peg: ";
cin >> guess2;

while (guess2 == guess1)
{
cout << "Please enter a unique number: ";
cin >> guess2;
}

cout << "Third Peg: ";
cin >> guess3;
while ((guess3 == guess2) || (guess3 == guess1))
{
cout << "Please enter a unique number: ";
cin >> guess3;
}

if (guess1 == peg2)
rightpeg++;

if (guess1 == peg3)
rightpeg++;

if (guess2 == peg1)
rightpeg++;

if (guess2 == peg3)
rightpeg++;

if (guess3 == peg1)
rightpeg++;

if (guess3 == peg2)
rightpeg++;


if (guess1 == peg1)
{
rightpeg++;
rightcolor++;
}

if (guess2 == peg2)
{
rightpeg++;
rightcolor++;
}

if (guess3 == peg3)
{
rightpeg++;
rightcolor++;
}


cout << "You have " << rightpeg << " correct peg(s) and " << rightcolor;
cout << " correct color(s)" << endl;

}
cout << "You have broken the code in " << numguess << " guesses.";
}


[Edited by denniswrenn on 12-10-2000 at 12:28 PM]

SteveCRM
Dec 10th, 2000, 11:54 AM
--------------------Configuration: mastermind - Win32 Debug--------------------
Compiling...
mastermind.cpp
c:\windows\profiles\steve\desktop\c c++\mastermind\mastermind.cpp(17) : error C2065: 'randomize' : undeclared identifier
c:\windows\profiles\steve\desktop\c c++\mastermind\mastermind.cpp(19) : error C2065: 'random' : undeclared identifier
Error executing cl.exe.

mastermind.exe - 2 error(s), 0 warning(s)

Dec 10th, 2000, 12:23 PM
I guess the random functions are only in borland's stdlib.h