-
Mastermind
i am having a spot of trouble making this program, and i would like as much as possible, i have consulted many, including my teacher and all of my fellow students. THe job is to make a program to have the computer play a game of Mastermind against a human opponent. The
computer will select 4 coloured pegs at random from 6 possible choices (red, green, yellow, blue,
orange, purple). A colour may be chosen more than once. You will then try and guess what pegs the
computer has chosen by selecting a guess of 4 pegs. Each time you make a guess the computer will
score the guess by showing white and black pegs. A white peg means you have the right colour in the
right position. A black peg means you have the right colour at the wrong position. You have 10
chances to guess the pegs the computer has chosen.
i ahev already done this much:
// game1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
void main()
{
char lmargin[5]=" ";
char guess[10] [5];
char score[10] [5];
int j,k;
// initialize guess and score arrays with some values
// values should be blank spaces in actual game
void getGuess(char g[5])
{
char x;
int n,count=0;
cin.get(g,5);
cin.ignore(80,'\n');
cout << '/'<<g<<'/'<<endl;
x=g[count];
do
{
switch (x)
{
case 'r':
case 'y':
case 'b':
case 'o':
case 'g':
case 'p': count=count+1;
break;
default:count=8;
break;
}
x=g[count];
}
while (count < 4);
if (count==8)
{
cout <<"\nInvalid characters were entered."<<endl;
cout << "Press any key to continue"<<endl;
n=getch();
strcpy(g," ");
}
}
for (j=0; j<=9; j++)
{
strcpy(guess[j],"4243");
strcpy(score[j],"5678");
}
// top line
cout <<lmargin<<char(-38);
for (j=1; j<7; j++) cout <<char(-60);
cout <<char(-62);
for (j=1; j<7; j++) cout <<char(-60);
cout <<char(-65)<<endl;
// title
cout <<lmargin<<char(-77)<<" Guess"<<char(-77)<<" Score"<<char(-77)<<endl;
// line under title
cout <<lmargin<<char(-61);
for (j=1; j<7; j++) cout <<char(-60);
cout <<char(-59);
for (j=1; j<7; j++) cout <<char(-60);
cout<<char(-76)<<endl;
// body
for (k=0;k<=9;k++)
{
cout <<setw(4)<<k+1<<char(-77)<<' '<<guess[k]<<' '<<char(-77)<<' '<<score[k]<<' '
<<char(-77)<<endl;
}
// bottom line
cout<<lmargin<<char(-64);
for(j=1;j<14;j++) cout<<char(-60);
cout<<char(-39)<<endl;
}
i have no idea how to make the get guess function work nevermind the rest of the program. I would appreciate all help possible.
Thank you.
-
I looked through the code briefly. My guess is that you should try not to use so much void, make that get_guess function return the guessed value or something.
-
are you using lawrenceville press by chance?