-
Checkers/Tic-Tac-Toe
Hi everyone, is there anyone out there who would like to write a checkers or tic tac toe game, or maybe both? I'm looking for someone on the upper side of begginer who would like to write these games with me. Like I said, I'm still a begginer but would like to learn more, and not that the tutorials on this site aren't great, but I figure the best way to learn is hands on experiance, or start something like this. I know checkers or tic tac toe doesn't sound to hard, but most of the games I've seen people use random numbers to place were the peices go, but I'd like to try to use some AI. If anyone wants to help, reply here, or email me if you like, [email protected]
-
A brief note
If you want to try your hand at AI, look towards the checkers idea. Tictactoe really is, in my opinion, too simple for AI and a reasonable level of intelligence could be reached in very few lines of code.
Of course I may be wrong, and if thats the case I have an MFC TicTacToe game you can modify if you so wish...
-
well here is my pitiful attempt at tictactoe no ai yet its 100% random :(
Code:
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
char board[3][3];
void FillBoard();
void DisplayBoard();
void FirstPlayer();
bool NoWinner();
void GetPlayerMove();
int GetComputerMove();
int main()
{
srand(time(NULL));
FillBoard();
FirstPlayer();
while(NoWinner())
{
GetPlayerMove();
if(NoWinner()==false)
{
break;
}
while(GetComputerMove()==0)
{
}
}
return (0);
}
//---------------------------------------------------------------
void FillBoard()
{
int i, j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
board[i][j]=' ';
}
//---------------------------------------------------------------
void DisplayBoard()
{
int i;
for(i=0;i<3;i++)
cout<<"["<<board[i][0]<<"]"<<"["<<board[i][1]<<"]"<<"["<<board[i][2]<<"]"<<endl;
}
//---------------------------------------------------------------
void GetPlayerMove()
{
int movetobox;
do
{
cout<<"Enter the box you want to move to (1-9): ";
cin>>movetobox;
}while((movetobox<1)||(movetobox>9));
if((movetobox==1)&&(board[0][0]==' '))
{
board[0][0]='X';
DisplayBoard();
}
else if((movetobox==2)&&(board[0][1]==' '))
{
board[0][1]='X';
DisplayBoard();
}
else if((movetobox==3)&&(board[0][2]==' '))
{
board[0][2]='X';
DisplayBoard();
}
else if((movetobox==4)&&(board[1][0]==' '))
{
board[1][0]='X';
DisplayBoard();
}
else if((movetobox==5)&&(board[1][1]==' '))
{
board[1][1]='X';
DisplayBoard();
}
else if((movetobox==6)&&(board[1][2]==' '))
{
board[1][2]='X';
DisplayBoard();
}
else if((movetobox==7)&&(board[2][0]==' '))
{
board[2][0]='X';
DisplayBoard();
}
else if((movetobox==8)&&(board[2][1]==' '))
{
board[2][1]='X';
DisplayBoard();
}
else if((movetobox==9)&&(board[2][2]==' '))
{
board[2][2]='X';
DisplayBoard();
}
else
{
cout<<"Illegal Move!"<<endl;
GetPlayerMove();
}
}
//---------------------------------------------------------------
void FirstPlayer()
{
int random;
srand(time(NULL));
random=(rand () % 2);
if(random==0)
{
cout<<"Computer goes first!"<<endl;
while(GetComputerMove()==0)
{
}
}
else if(random==1)
{
cout<<"Player goes first!"<<endl;
DisplayBoard();
GetPlayerMove();
while(GetComputerMove()==0)
{
}
}
}
//---------------------------------------------------------------
int GetComputerMove()
{
int movetobox;
movetobox=(rand () % 9);
if((movetobox==1)&&(board[0][0]==' '))
{
board[0][0]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==2)&&(board[0][1]==' '))
{
board[0][1]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==3)&&(board[0][2]==' '))
{
board[0][2]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==4)&&(board[1][0]==' '))
{
board[1][0]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==5)&&(board[1][1]==' '))
{
board[1][1]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==6)&&(board[1][2]==' '))
{
board[1][2]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==7)&&(board[2][0]==' '))
{
board[2][0]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==8)&&(board[2][1]==' '))
{
board[2][1]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
else if((movetobox==9)&&(board[2][2]==' '))
{
board[2][2]='O';
cout<<"Computer moves to box "<<movetobox<<endl;
DisplayBoard();
return (1);
}
return (0);
}
//---------------------------------------------------------------
bool NoWinner()
{
if((board[0][0]=='O')&&(board[0][1]=='O')&&(board[0][2]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[1][0]=='O')&&(board[1][1]=='O')&&(board[1][2]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[2][0]=='O')&&(board[2][1]=='O')&&(board[2][2]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[0][0]=='O')&&(board[1][0]=='O')&&(board[2][0]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[0][1]=='O')&&(board[1][1]=='O')&&(board[2][1]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[0][2]=='O')&&(board[1][2]=='O')&&(board[2][2]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[0][0]=='O')&&(board[1][1]=='O')&&(board[2][2]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
else if((board[0][2]=='O')&&(board[1][1]=='O')&&(board[2][0]=='O'))
{
cout<<"Computer Wins!"<<endl;
return (false);
}
if((board[0][0]=='X')&&(board[0][1]=='X')&&(board[0][2]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[1][0]=='X')&&(board[1][1]=='X')&&(board[1][2]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[2][0]=='X')&&(board[2][1]=='X')&&(board[2][2]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[0][0]=='X')&&(board[1][0]=='X')&&(board[2][0]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[0][1]=='X')&&(board[1][1]=='X')&&(board[2][1]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[0][2]=='X')&&(board[1][2]=='X')&&(board[2][2]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[0][0]=='X')&&(board[1][1]=='X')&&(board[2][2]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
else if((board[0][2]=='X')&&(board[1][1]=='X')&&(board[2][0]=='X'))
{
cout<<"Player Wins!"<<endl;
return (false);
}
if((board[0][0]!=' ')&&(board[0][1]!=' ')&&(board[0][2]!=' ')&&(board[1][0]!=' ')&&(board[1][1]!=' ')&&(board[1][2]!=' ')&&(board[2][0]!=' ')&&(board[2][1]!=' ')&&(board[2][2]!=' '))
{
cout<<"Game ends in a draw!"<<endl;
return (false);
}
return (true);
}
-