#include <iostream>
using namespace std;
class TicTacToe
{
char* table[9];
public:
TicTacToe() {
table[0] = "1";
table[1] = "2";
table[2] = "3";
table[3] = "4";
table[4] = "5";
table[5] = "6";
table[6] = "7";
table[7] = "8";
table[8] = "9";
}
void player_choise(int pos) {
if(table[pos-1] != "X" && table[pos-1] != "O")
table[pos-1] = "X";
}
void comput_choise()
{
int r = rand() % 8 + 0;
if(table[r] != "X" && table[r] != "O")
table[r] = "O";
else
comput_choise();
}
void print_table()
{
cout << "++-----++-----++-----++" << endl;
cout << "|| " << table[0] << " || " << table[1] << " || " << table[2] << " ||" << endl;
cout << "++-----++-----++-----++" << endl;
cout << "|| " << table[3] << " || " << table[4] << " || " << table[5] << " ||" << endl;
cout << "++-----++-----++-----++" << endl;
cout << "|| " << table[6] << " || " << table[7] << " || " << table[8] << " ||" << endl;
cout << "++-----++-----++-----++" << endl;
}
bool is_winner()
{
if( (table[0] == "X" && table[1] == "X" && table[2] == "X") ||
(table[3] == "X" && table[4] == "X" && table[5] == "X") ||
(table[6] == "X" && table[7] == "X" && table[8] == "X") ||
(table[0] == "X" && table[3] == "X" && table[6] == "X") ||
(table[1] == "X" && table[4] == "X" && table[7] == "X") ||
(table[2] == "X" && table[5] == "X" && table[8] == "X") ||
(table[0] == "X" && table[4] == "X" && table[8] == "X") ||
(table[2] == "X" && table[4] == "X" && table[6] == "X")) { return true; } else { return false; }
}
bool is_looser()
{
if( (table[0] == "O" && table[1] == "O" && table[2] == "O") ||
(table[3] == "O" && table[4] == "O" && table[5] == "O") ||
(table[6] == "O" && table[7] == "O" && table[8] == "O") ||
(table[0] == "O" && table[3] == "O" && table[6] == "O") ||
(table[1] == "O" && table[4] == "O" && table[7] == "O") ||
(table[2] == "O" && table[5] == "O" && table[8] == "O") ||
(table[0] == "O" && table[4] == "O" && table[8] == "O") ||
(table[2] == "O" && table[4] == "O" && table[6] == "O")) { return true; } else { return false; }
}
};
int main()
{
srand(static_cast<int>(time(NULL)));
TicTacToe game;
int pos;
while(true)
{
game.print_table();
if (game.is_winner())
{
cout << "You win!" << endl;
break;
}
else if (game.is_looser())
{
cout << "You lose!" << endl;
break;
}
else
{
cout << "Make your choise: ";
cin >> pos;
game.player_choise(pos);
game.comput_choise();
continue;
}
}
return 0;
}
[C++ min-game] tic tac toe, how to improve it?
Started by Jacki, Nov 07 2009 07:44 AM
2 replies to this topic
#1
Posted 07 November 2009 - 07:44 AM
Hi people, I was seeing my old c++ programms and I found a tic tac toe game, it work quite fine I think but I believe it can be better, specially the AI. Please, can you tell me how you improve it? Thanks...

|
|
|
#2
Posted 07 November 2009 - 09:15 AM
Since I use C and not C++, I am not sure what you can do in classes and stuff, but this could probably be done with a for loop:
TicTacToe() {
table[0] = "1";
table[1] = "2";
table[2] = "3";
table[3] = "4";
table[4] = "5";
table[5] = "6";
table[6] = "7";
table[7] = "8";
table[8] = "9";
}
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Download the new operating system programming kit! (some assembly required)
#3
Posted 07 November 2009 - 10:06 AM
And also when you try if someone won or not you don't have to test victory first and the failure you can test them together and if the game is over you can just get which player's turn it is since that must have won when it did the last move. And the actual test could be done with for loops.


Sign In
Create Account


Back to top









