Ok guys, here's how i understand it:
games like chess and tic-tac-toe use a Minimax tree as Sean said.
I decided that because chess is merely a ridiculously more complex form of tic-tac-toe (if you can say merely). My point is that they seem to be very similar yet one is much more complex than the other. So i decided to make a tic-tac-toe game.
I have just finished programming all of the program except for the AI. I have win parameters coded in and everything else. In fact here is my code. The only 2 things i need are the AI and i have to get the computer to randomly pick who goes first (i have a program or a tut around here somewhere where it shows you how to do that). So heres the code for the tic-tac-toe game:
Code:
#include <iostream>
#include <windows.h>
using namespace std;
void dispboard(char board[3][3]);
int main()
{
//tic-tac-toe game
//infinite loop
while(1)
{
//clear screen;
system("cls");
//title
cout<<"##########################\n";
cout<<"##########################\n";
cout<<"## TIC - TAC - TOE ##\n";
cout<<"## VERSION 1.0 ##\n";
cout<<"##########################\n";
cout<<"##########################\n\n";
//menu
cout<<"(1) Play\n";
cout<<"(2) Exit\n";
//get choice
int mchoice;
cout<<"Choice: ";
cin>>mchoice;
cin.ignore();
//evaluate
if(mchoice==2)
{
//exit
cout<<"Exitting...";
Sleep(1000);
break;
}
if(mchoice!=1)
{
cout<<"Incorrect Choice";
cin.ignore();
continue;
}
//play game
//initialize vairiables***************
//init board
char board[3][3];
//fill board
int bfi;
int bf;
for(bfi=1; bfi<4; bfi++)
{
for(bf=1; bf<4; bf++)
{
board[bfi][bf]='\0';
}
}
//init piece vars
char player;
char computer;
//************************************
//get which the player wants
cout<<"\n\nWhich Piece would you like to be?\n";
cout<<"(1) X 's\n";
cout<<"(2) O 's\n\n";
//get
int piecechoice;
cout<<"Choice: ";
cin>>piecechoice;
cin.ignore();
//evaluate
if(piecechoice==1)
{
player='X';
computer='O';
}
else if(piecechoice==2)
{
player='O';
computer='X';
}else{
cout<<"Incorrect Choice!";
cin.ignore();
continue;
}
//begin game
//vars
int column;
int row;
int win;
win=0;
while(win==0)
{
system("cls");
column=0;
row=0;
dispboard(board);
//check for winner
char checkletter;
for(bf=1; bf<4; bf++)
{
for(bfi=1; bfi<3; bfi++)
{
if(bfi==1)
{
checkletter=player;
}else if(bfi==2)
{
checkletter=computer;
}
if((board[bf][1]==checkletter && board[bf][2]==checkletter && board[bf][3]==checkletter) ||
(board[1][bf]==checkletter && board[2][bf]==checkletter && board[3][bf]==checkletter) ||
(board[1][1]==checkletter && board[2][2]==checkletter && board[3][3]==checkletter) ||
(board[3][1]==checkletter && board[2][2]==checkletter && board[1][3]==checkletter))
{
//winner
if(checkletter==player)
{
//human won
win=1;
}
else if(checkletter==computer)
{
//computer won
win==2;
}
}
}
}
if(win!=0)
{
break;
}
cout<<"\n\n\n";
cout<<"Where would You Like To Move?\n";
cout<<"Column: ";
cin>>column;
cin.ignore();
cout<<"Row: ";
cin>>row;
cin.ignore();
board[column][row]=player;
}
//write winner
if(win==1)
{
//human won
cout<<"\n\nCongratulations You Won!\n\n";
cin.ignore();
continue;
}
if(win==2)
{
cout<<"Sorry You Lost!\n\n";
cin.ignore();
continue;
}
}
return 0;
}
void dispboard(char board[3][3])
{
cout<<" 1 2 3 \n";
cout<<" | | \n";
cout<<"1 "<<board[1][1]<<" | "<<board[2][1]<<" | "<<board[3][1]<<" \n";
cout<<" ____|____|____\n";
cout<<" | | \n";
cout<<"2 "<<board[1][2]<<" | "<<board[2][2]<<" | "<<board[3][2]<<" \n";
cout<<" ____|____|____\n";
cout<<" | | \n";
cout<<"3 "<<board[1][3]<<" | "<<board[2][3]<<" | "<<board[3][3]<<" \n";
cout<<" | | \n";
}
So anyone who can help me out/ point me in the right direction it would be greatly appreciated. Thanks!