#include<iostream> // my first EVER game 27/03/12
usingnamespace std;
int main() {
char a = '1';
char b = '2';
char c = '3';
char d = '4';
char e = '5';
char f = '6';
char g = '7';
char h = '8';
char i = '9';
bool gameover = false;
char move;
bool won = false;
int turn = 1;
int player;
char choice;
do { // loops until game is not over
char mark;
cout << a << " | " << b << " | " << c << endl; // the board
cout << "-----------" << endl;
cout << d << " | " << e << " | " << f << endl;
cout << "-----------" << endl;
cout << g << " | " << h << " | " << i << endl;
turn++; // turn = 2
if(turn % 2 == 0) { // if the turn is multiples of 2,then it'll be player 1.
mark = 'X';
player = 1;
}
else {
mark = 'O';
player = 2;
}
cin >> move;
if(move == '1' && a == '1') { // first check = if the player puts in 1. Second check = if the space is available
a = mark;
}
elseif(move == '2' && b == '2') {
b = mark;
}
elseif(move == '3' && c == '3') {
c = mark;
}
elseif(move == '4' && d == '4') {
d = mark;
}
elseif(move == '5' && e == '5') {
e = mark;
}
elseif(move == '6' && f == '6') {
f = mark;
}
elseif(move == '7' && g == '7') {
g = mark;
}
elseif(move == '8' && h == '8') {
h = mark;
}
elseif(move == '9' && i == '9') {
i = mark;
}
else {
cout << "invalid move!" << endl; // if the spaces aren't available or player typed in invalid choice
}
if(a == mark && b == mark && c == mark) { // win conditions
won = true;
}
elseif(a == mark && d == mark && g == mark) {
won = true;
}
elseif(a == mark && e == mark && i == mark) {
won = true;
}
elseif(d == mark && e == mark && f == mark) {
won = true;
}
elseif (b == mark && e == mark && h == mark) {
won = true;
}
elseif(c == mark && f == mark && i == mark) {
won = true;
}
elseif(g == mark && e == mark && c == mark) {
won = true;
}
elseif(g == mark && h == mark && i == mark) {
won = true;
}
elseif (a != '1' && b != '2' && c != '3' && d != '4' && e != '5' && f != '6' && g != '7' && h != '8' && i != '9') {
won = false; // if all the boxes are taken and there is no win condition,it's surely a draw.
gameover = true;
cout << "It's draw for both players!" << endl;
}
if (won == true) {
cout << "player " << player << " won!" << endl; // winner will surely have been the last player.
gameover = true;
cout << a << " | " << b << " | " << c << endl;
cout << "-----------" << endl;
cout << d << " | " << e << " | " << f << endl;
cout << "-----------" << endl;
cout << g << " | " << h << " | " << i << endl; // the ending board.
}
}while(gameover == false); // the loop breaks if gameover is true :D
system("pause");
return 0;
}
the code is VERY long because I'm a beginner and there MIGHT be unecessary codes in there as well.three questions-
1.) How to make computer play against me.
2.) What can be improved.
3.) Basic feedbacks.