#include<iostream> // my first EVER game 27/03/12 // update in 08/04/12.Attempting to make an artificial intellgence in it.
#include<ctime>
usingnamespace std;
int main() {
char board[3][3] = {0,0,0,0,0,0,0,0,0};
char mark;
int turn = 0;
int player;
bool gameover = false;
do {
cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "------------" << endl;
cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "------------" << endl;
cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl; // the board
turn++;
if(turn % 2 == 0) {
player = 2;
mark = 'O'; // the 'turn'
}
else {
player = 1;
mark = 'X';
}
int x = 0,y = 0;
bool valid = false;
do {
if(player == 2) {
srand(time(0));
x = rand() % 2 + 1; // inperfect artificial intelligence
y = rand() % 2 + 1;
}
else {
cout << "enter row (1 - 3)." << endl;
cin >> x;
cout << "enter column (1 - 3)." << endl;
cin >> y;
}
if(x == 1 && y == 1 && board[0][0] == 0) { // start of the move check
board[0][0] = mark;
valid = true;
}
elseif(x == 1 && y == 2 && board[0][1] == 0) {
board[0][1] = mark;
valid = true;
}
elseif(x == 1 && y == 3 && board[0][2] == 0) {
board[0][2] = mark;
valid = true;
}
elseif(x == 2 && y == 1 && board[1][0] == 0) {
board[1][0] = mark;
valid = true;
}
elseif(x == 2 && y == 2 && board[1][1] == 0) {
board[1][1] = mark;
valid = true;
}
elseif(x == 2 && y == 3 && board[1][2] == 0) {
board[1][2] = mark;
valid = true;
}
elseif(x == 3 && y == 1 && board[2][0] == 0) {
board[2][0] = mark;
valid = true;
}
elseif(x == 3 && y == 2 && board[2][1] == 0) {
board[2][1] = mark;
valid = true;
}
elseif(x == 3 && y == 3 && board[2][2] == 0) {
board[2][2] = mark;
valid = true;
} // end of move check.
else {
cout << "invalid move,try again." << endl;
valid = false;
}
}while(valid == false); // loop until the move is valid.
if (board[0][0] == mark && board[0][1] == mark && board[0][2] == mark) { // all the first row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[1][0] == mark && board[1][1] == mark && board[1][2] == mark) { // all the second row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[2][0] == mark && board[2][1] == mark && board[2][2] == mark) { // all the third row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board [0][0] == mark && board[1][0] == mark && board[2][0] == mark) { // all the first row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[0][1] == mark && board[1][1] == mark && board[2][1] == mark) { // all the second row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[0][2] == mark && board[1][2] == mark && board[1][2] == mark) { // all the third row complete
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) { // horizontal combination
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[2][0] == mark && board[1][1] == mark && board[0][2]== mark) { // another horizontal combination
cout << "player" << player << " wins!" << endl;
gameover = true;
}
elseif (board[0][0] != 0 && board[0][1] != 0 && board[0][2] != 0 && board[1][0] != 0 && // draw situation
board[1][1] != 0 && board[1][2] != 0 && board[2][0] != 0 && board[2][1] != 0 && board[2][2] != 0 ) {
cout << "draw for both players!" << endl;
gameover = true;
}
}while(gameover == false);
cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "------------" << endl;
cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "------------" << endl;
cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
system("pause");
return 0;
}
Okay.I'll be totally honest with you.It doesn't work.Yeah,it does the job,it IS artificial intelligence,just a dumb one.I have no idea how to make it check player's moves.I'm a beginner,so please understand and help me.I really need help.Thank you in advance.
if i remember correctly there is an artificial intelligence engine called minimax. that is a very simple artificial intelligence that is really well made for this. also using functions and/or classes makes this so much simpler.
Minimax huh...I guess I went to far ahead beyond my abilities.I'll probaly leave this project for a while until I grasp more knowledge from C++.Thank you for you reply: D