Skip to content

Instantly share code, notes, and snippets.

@douglas-vaz
Last active December 16, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglas-vaz/5507962 to your computer and use it in GitHub Desktop.
Save douglas-vaz/5507962 to your computer and use it in GitHub Desktop.
Simple AI implementation of Tic-Tac-Toe
#include <iostream>
using namespace std;
enum players {X = 1, O = 2};
class TicTacToe{
short state[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
const short strategy[9] = { 1, 3, 1, 3, 5, 3, 1, 3, 1};
public:
/**
* X = 1
* O = 2
*/
bool move(int player, int pos)
{
if(pos < 0 || pos > 8 || state[pos] != 0)
return false;
state[pos] = player;
}
short possibleWin()
{
//Check possible win for each player
for (int p = 1; p < 3; ++p)
{
for(int i = 0; i < 9; i += 3){
//Check for win horizontally
if(state[0+i] == p && state[1+i] == p && state[2+i] == 0)
return 2+i;
else if(state[1+i] == p && state[2+i] == p && state[0+i] == 0)
return 0+i;
else if(state[0+i] == p && state[2+i] == p && state[1+i] == 0)
return 1+i;
//Check for win vertically
if(state[0+i/3] == p && state[3+i/3] == p && state[6+i/3] == 0)
return 6+i/3;
else if(state[0+i/3] == p && state[6+i/3] == p && state[3+i/3] == 0)
return 3+i/3;
else if(state[3+i/3] == p && state[6+i/3] == p && state[0+i/3] == 0)
return 0+i/3;
}
//Check for win diagonally
if(state[0] == p && state[4] == p && state[8] == 0)
return 8;
else if(state[4] == p && state[8] == p && state[0] == 0)
return 0;
else if(state[2] == p && state[4] == p && state[6] == 0)
return 6;
else if(state[4] == p && state[6] == p && state[2] == 0)
return 2;
else if(((state[0] == p && state[8]) || (state[2] == p && state[6] == p)) && state[4] == 0)
return 4;
}
//Win not possible
return -1;
}
/**
* 1 = X wins
* 2 = O wins
* 3 = Nobody wins
* 0 = Game in progress
*/
short gameOver()
{
for (int p = 1; p < 3; ++p)
{
for(int i = 0; i < 9; i+=3){
if(state[0+i] == p && state[1+i] == p && state[2+i] == p)
return p;
else if(state[0+i/3] == p && state[3+i/3] == p && state[6+i/3] == p)
return p;
}
if((state[0] == p && state[4] == p && state[8] == p) || (state[2] == p && state[4] == p && state[6] == p))
return p;
}
for (int i = 0; i < 9; ++i)
{
if(state[i] == 0)
return 0;
}
return 3;
}
void showBoard()
{
for (int i = 0; i < 9; ++i)
{
if(state[i] == 0)
cout<<" - ";
else if(state[i] == 1)
cout<<" X ";
else if(state[i] == 2)
cout<<" O ";
if((i+1)%3 == 0)
cout<<endl;
}
cout<<endl;
}
short getNextMove()
{
short highestPossibleMove = 0;
short highestPossiblePos = -1;
short possible;
if(gameOver())
return -1;
else{
if((possible = possibleWin()) != -1){
return possible;
}
for (int i = 0; i < 9; ++i){
if(state[i] == 0 && strategy[i] > highestPossibleMove){
highestPossibleMove = strategy[i];
highestPossiblePos = i;
}
}
return highestPossiblePos;
}
}
};
int main(int argc, char** args)
{
TicTacToe lol;
lol.move(O, 0);
while(lol.gameOver() == 0)
{
lol.move(X, lol.getNextMove());
lol.showBoard();
lol.move(O, lol.getNextMove());
lol.showBoard();
}
cout<<lol.gameOver()<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment