Also, arrays are 0-based, so the indices for board go from 0 to 8.
And this won't work: strcpy(board, "0123456789");
You're trying to store 11 characters (10 digits and the terminating \0) in an array with 9 elements.
Edit: more stuff:
Don't do this: if(isover == true)
If you want to check whether a boolean expression is true, use if (isover) or if (!isover) if you want to check whether it's false.
In moveX, moveO get rid of goto. The correct solution here is to use a loop. Never use goto within the first five years of learning C++ (after that, I assume that you'll know enough to make the decision yourself).
Edit: more stuff:
Don't do this: if(isover == true)
If you want to check whether a boolean expression is true, use if (isover) or if (!isover) if you want to check whether it's false.
Before I compiled this program for first time, there was if(isover). However, strange problem would happen after first move; while loop would instantly break. Thats why I added == true, and after that program worked fine!
this was compiled in MS VC++ 2008
EDIT:
Also, arrays are 0-based, so the indices for board go from 0 to 8.
And this won't work: strcpy(board, "0123456789");
You're trying to store 11 characters (10 digits and the terminating \0) in an array with 9 elements.
But then again, why did my program compile and work fine?
But then again, why did my program compile and work fine?
Not all errors lead to compiler errors. Some lead to undefined behavior, which can result in anything from crashes to different and unexpected program behavior. Writing beyond array bounds is such an error, which causes memory corruption.
And this is likely a direct result from memory corruption:
Before I compiled this program for first time, there was if(isover). However, strange problem would happen after first move; while loop would instantly break. Thats why I added == true, and after that program worked fine!
Since isover comes directly after board, it's likely that your strcpy tramples all over these variables.
Not all errors lead to compiler errors. Some lead to undefined behavior, which can result in anything from crashes to different and unexpected program behavior. Writing beyond array bounds is such an error, which causes memory corruption.
Since isover comes directly after board, it's likely that your strcpy tramples all over these variables.
For my version of TicTacToe i used 3x3 array of integers to define game board (0=empty, 1=X, 2=O)
X(=1) starts the game and after each move game checks if CURRENT player has won and after that checks if board is full (game end with no winner).
#include <iostream>
usingnamespace std; // Normally i don't use this...
// Normally i REALLY use std::cout version
void init(int a[]);
void print(int a[]);
int main()
{
// Create and initialize board
int board[9];
init(board);
// FOR DEMO PURPOSES!
board[2]=1;
board[5]=2;
board[6]=1;
// print board
print(board);
}
void init(int a[])
{
for (int i=0; i<9; i++)
a[i]=0;
}
void print(int a[])
{
for (int i=0; i<9; i++) {
// print new row if necessary
if (!(i%3))
cout << endl << "|";
// if value in board is 0 print " ",
// 1 print X and if 2 print O
if (!a[i])
cout << " |";
elseif (a[i]==1)
cout << "X|";
else
cout << "O|";
}
// make sure we have clean row after returning to main()
cout << endl;
}
My math teacher showed us a way that each grid can be assigend a number and a winning line -- either horizontal, vertical or diagonal -- has the sum of 15 for the three pieces. It sucks I forgot to save the notes.
@eraggo
I tried editing your code and managed to finish it without using any global variables. (How does my code look like to you?)
But why is it so bad to use global variables in such small program?
Maybe it is farewell to use globals in small projects but some habits will stay. So, learning good practices is good for later on.
Have you compiled and runned your code succesfully?
About line 22: Where do you change state of forever-variable so loop can exit?
//Only X can win cuz im too lazy to make 16 possible win conditions
16? 3 ways to win by rows, 3 ways to win vertically, and 2 ways to win diagonally = 8 ways ;) Keep it simple.
Good(ish) prototype for checking win condition: bool CheckWinCondition(int a[], int turn); // turn=who turn it is currently
I dont change state of forever. While loop should break when CheckWinCondition returns true value. Actually... I read the code again and it seems I forgot to break that loop.