Hi I am very new to C++ and programming in general. I decided after reading that it would be best to write my own code as thats the best way of re-inforcing what you have read and finding out what you actually did not understand. So here goes:
#include<iostream>
#include <cstdlib> // for rand()
usingnamespace std;
int check();
int win();
char turn();
char draw();
void ai();
char start();
char draw(char b[8])
{
int i = 0;
for (i; i<9;i++)
if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7)
{
cout<<b[i];
printf("|");
}
else {
cout<<b[i];
printf("\n");
}
return *b;
}
int win(char b[8], int e)
{
//how to make a clean simplistic way of the 3 in a row and draw statement? without a load of if statements
return e;
}
int check(int& c, char b[8])
{
int t= 0;
do{
if (b[c-1] == 'X' || b[c-1] == 'O')
{
printf("please try again\n\n");
cin>>c;
}
else
{
t = 1;
}
}while(t==0);
return c;
}
int turn(int& i)
{
printf("Please enter a number\n\n");
cin>>i;
return i;
}
void ai(char b[8])// needs to be advanced temporary ai
{
printf("AI is making it's move\n\n");
int c = rand() % (9);
check(c,b);
b[c-1] = 'O';
}
char start(char b[8])
{
printf("Welcome to O and X - AI only game\nPlease select an option:\n1. New Game\n2. Load game\n");
char i = '0';
cin>>i;
if (i == '2')
{
//read file and place data into array b place into a char fucntion
return *b;
}else
{
return *b;
}
}
int main()
{
int endgame = 0;
char board[9] = {'1','2','3',
'4','5','6',
'7','8','9'};
start(board);
do
{
draw(board);
int choice = 0;
turn(choice);
check(choice,board);
board[choice - 1] = 'X';
printf("\n");
win(board, endgame);
ai(board);
}while(endgame == 0);
}
I have some questions that need to be answered before I move on.
1)Why is it that when I am passing a variable to a function that is not void in order to use the value in the variable I have to place an "&" sign after declaring the variable type. Simialr question about arrays instead when returning the value why do you use a "*"? I think it has something to do with pointers but I would like this explained please.
2)How would I go about making a very 'clean' win function, that checks for 3 in a row of either X or O.
3) How would I go about making an actual ai? All I can do is make the ai choose a random number between 1 - 9 and then check to see if its taken. This is not an ai as far as I am concerned.
4)Arrays why is it when I declare it it only allows 9 or higher but when used it wants 8. Basicly what is it about declaring that makes it not count from 0 upwards but rather 1 upwards?
5)Finaly how would I go about condensing this and are there any bad habbits that I have picked up? Or any other comments that would help me improve?
Hey, sorry I won't actually be able to help you with most of your problems, C++ is still very new to me as well. What I can say though is I don't think you've done your AI function correctly. From what I understand that would give you the exact same random number each time. I know that your probably going to completely change this function anyway if you find out a way to make a better AI but thought I'd mention it anyway. Here's a link about generating random numbers.
1. For a parameter definition, the & means it's a referenece. So the function uses the same variable as the calling function. Without the &, the function is given a copy of it.
for the function turn(), it uses int& i so when cin updates i, the value get updated in main, too.
If it was just int i, turn() would update its own copy of i and the new result would not get back to main.
2. You need to scan each row and col, and then the diags!
Think of how few tests you need for each row!
"Make it work, Make it right!"
3. Too big a question to fully answer...
I would first get your random code to work. It can't call check() as the PC can't use the keyboard!
Then look at a rule based system. You can experiment with different rules. Play the game yourself, or against a friend, and record the moves. Use that to come up with rules.
Once you have a version which can complete a game, even if it looses, freeze it. Then come up with a new version. Then maybe you could modify your code so the two AIs can play each other!
(Look up how to use command line parameters)
4. I don't get this?
Are you talking about the rand() code?
5. Misc
- make the formatting consistent
- ensure all your array are big enough! e.g. a[4] has 4 elems; a[0], a[1], a[2], a[3]
- replace all printf with cout
- why return *b; (from draw();)
- sort out your board drawing logic : what shape is the board?
- eliminate spurious returns!
- ...
Use commenting to block out most of you code, getting each bit to work in turn (depedencies allowing).
Then put it back together and retest.
Andy
P.S. As drunken meerkat said, you need a call to srand(time(0)) at the start of your program.
P.P.S. Do you understand pointer derferencing? (like *b) If not yet, don't use it (yet)!
For your AI question, there are many ways to do it, with varying difficulties.
One of the easiest is of course just having it choose a random position on the board.
From there you could work your way up to something like andywestken suggested and use rules, for example if the computer could win the game on its turn, it will always take the winning position instead of moving completely randomly, or you could have it block your possible wins. You could even build an entire instruction set that makes the computer unbeatable by setting up a few rules in a specific order,
for example:
1. If AI can win, take the win.
2. If player wins on next turn, block.
3. If player can fork on next turn, block...
And so on.
You could also give each square a specific value (ie. middle is worth the most, then the corners, then the edges), and have the AI pick from the highest value available squares.
A variation on this is to save each game played, and change values of squares based on previous games and their results.
You could simply write a list of ever possible game possible (It's not as grandiose as it sounds, since you can rotate the board during each check to cut the number down to 1/4 of the actual possible games) and give the computer instructions on how to play based on each board.
Etc.
The list goes on, so just come up with a basic idea and it'll begin to take shape in code.