ok, wernt sure were to post this, but i think here is best.
i have just started learning c++, i am still a newb, and this is my first game!
try it out, see what you can score, i did it in 2 goes! (jammy)
here is the
#include <cstdlib>
#include <iostream>
using namespace std;
int check_hit(int c[],int guess[]);
int check_touch(int c[],int guess[]);
int main(int argc, char *argv[])
{
cout << "here is hit and touch, a code cracking game \nto play, enter four numbers, pressing enter after each.\nthe hits counter will tell you how many numbers are \nin the right place.\nthe touch counter will tell you how many numbers\nare right, but in the wrong space.\nno numbers are used twice in any one code.\n\n\n";
srand((unsigned)time(NULL));
int c[5];
char cross[10]={'t'};
for (int r=1;r<=4;r++)
{
do
{
c[r]=rand()%9;
}while(cross[c[r]]=='f');
cross[c[r]]='f';
}
int goes=0;
int pass=1;
do
{
cout << "please enter a four number guess, press enter after each number: \n";
int guess[5];
cin >> guess[1];
cin >> guess[2];
cin >> guess[3];
cin >> guess[4];
int hits=check_hit(c,guess);
int touch=check_touch(c,guess);
cout <<"hits: "<<hits<<endl;
cout <<"touch: "<<touch<<endl<<endl;
if (hits==4)
{
pass=0;
}
goes++;
} while(pass);
cout << "the number was: "<<c[1]<<c[2]<<c[3]<<c[4]<<endl;
cout << "you took "<<goes<<" goes." <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int check_hit(int c[],int guess[])
{
int count=0;
for (int d=1;d<=4;d++){if (guess[d]==c[d]){count++;}}
return count;
}
int check_touch(int c[],int guess[])
{
int count=0;
for (int val1=1;val1<=4;val1++)
{
for (int val2=1;val2<=4;val2++)
{
if ((guess[val1]==c[val2]) && !(val1==val2))
{
count++;
}
}
}
return count;
}
and the exe is included.
cheers