The Multiplayer Game of tic-tac-toe

**Abstract:**

The game is built over a 3X3 matrix which is used by two players use it alternatively. The two players alternate between ‘X’ and ‘O’ symbols. The game works on a few rules the first player to cross the following boxes win.

**Conditions for winning:**

- All three cells in first row
- All three cells in second row
- All three cells in third row
- All three cells in first column
- All three cells in second column
- All three cells in third column
- All three cells in leading diagnol
- All three cells in opposite diagnol

The first person to finish any of these patterns will win

**Program working:**

The program first gets the input of the address of the place where the first player wants to make a mark, then the computer moves onto the next player. Like the first step, the process is repeated until any one of the conditions is satisfied or all blocks are filled.

The second player can also be substituted for a system by using a randomiser function which will generate random numbers for the addresses and then marks the respective block instead of a player.

#### note: this is a c++ prog.

#include

#include

#include

#include

#include

a1 = ‘7’;

a2 = ‘8’; //these display the corresponding number for each space

a3 = ‘9’;

b1 = ‘4’;

b2 = ‘5’;

b3 = ‘6’;

c1 = ‘1’;

c2 = ‘2’;

c3 = ‘3’;

win = 0; //0 means no one has won yet

turn = first; //starts it as o’s turn but will then switch it to x’s

turns = 0; //no turns have been taken yet

time(&seconds); //gets value from clock and puts it in seconds variable

srand((unsigned int) seconds); //converts it into an unsigned int

//this works by giving more randomized values because the clock

//wont be the same the next time the program is run

strategy = rand() % 2 + 1; //gives comp a rand offensive strategy

//only used if it goes first

clrscr();

cout << “Do you want to play against a…\n1 – Computer\n2 – Human\n”; cin >> players; //how many people are playing

do

{

turns++; //adds to number of turns taken

if(turn==pl2) //this switches whose turn it is

turn=pl1; //by checking to see whose it is and the

else if(turn==pl1) //switching it to the other one

turn=pl2;

tries=0; //they havent tried to pick a space yet

if(players==2||(players==1&&turn==pl1)) //if there are 2 people or 1 person where

//its X’s turn then let the player pick

//the square to move in

{

do{

clrscr(); //clear the screen for the board

tries++; //adds to how many tries theve taken

if(tries>1) //if theyve taken more than one try tell them

cout << “Space if full pick another.\n”; //the space is full cout << “\n | | \n” << ” ” <<>> move; //user input of where they want to go

if((move==7 && a1==’7′)||(move==8 && a2==’8′)||(move==9 && a3==’9′)||

(move==4 && b1==’4′)||(move==5 && b2==’5′)||(move==6 && b3==’6′)||

(move==1 && c1==’1′)||(move==2 && c2==’2′)||(move==3 && c3==’3′))

empty = 1; //makes sure that where they picked is empty

else

empty = 0; //if its not empty then its full

}while(empty==0); //if its full give them another try to pick an empty spot

}

if(players==1&&turn==pl2)//if there is 1 player and its O’s turn then do AI

{

if(first==pl2) //if user goes first do this AI which is more defensive

{

if(a1==’7’&&((a2==pl2&&a3==pl2)||(b1==pl2&&c1==pl2)||(b2==pl2&&c3==pl2)))

a1 = turn; //if it can win by taking space a1 then do it

else if(a2==’8’&&((a1==pl2&&a3==pl2)||(b2==pl2&&c2==pl2)))

a2 = turn; //if it can win by taking space a2 then do it

else if(a3==’9’&&((a1==pl2&&a2==pl2)||(b3==pl2&&c3==pl2)||(b2==pl2&&c1==pl2)))

a3 = turn; //if it can win by taking space a3 then do it

else if(b1==’4’&&((a1==pl2&&c1==pl2)||(b2==pl2&&b3==pl2)))

b1 = turn; //if it can win by taking space b1 then do it

else if(b2==’5’&&((b1==pl2&&b3==pl2)||(a2==pl2&&c2==pl2)||

(a1==pl2&&c3==pl2)||(a3==pl2&&c1==pl2)))

b2 = turn; //if it can win by taking space b2 then do it

else if(b3==’6’&&((b1==pl2&&b2==pl2)||(a3==pl2&&c3==pl2)))

b3 = turn; //if it can win by taking space b3 then do it

else if(c1==’1’&&((c2==pl2&&c3==pl2)||(a1==pl2&&b1==pl2)||(b2==pl2&&a3==pl2)))

c1 = turn; //if it can win by taking space c1 then do it

else if(c2==’2’&&((c1==pl2&&c3==pl2)||(a2==pl2&&b2==pl2)))

c2 = turn; //if it can win by taking space c2 then do it

else if(c3==’3’&&((c1==pl2&&c2==pl2)||(a3==pl2&&b3==pl2)||(b2==pl2&&a1==pl2)))

c3 = turn; //if it can win by taking space c3 then do it

else if(a1==’7’&&((a2==pl1&&a3==pl1)||(b1==pl1&&c1==pl1)||(b2==pl1&&c3==pl1)))

a1 = turn; //if it can block a win by taking space a1 then do it

else if(a2==’8’&&((a1==pl1&&a3==pl1)||(b2==pl1&&c2==pl1)))

a2 = turn; //if it can block a win by taking space a2 then do it

else if(a3==’9’&&((a1==pl1&&a2==pl1)||(b3==pl1&&c3==pl1)||(b2==pl1&&c1==pl1)))

a3 = turn; //if it can block a win by taking space a3 then do it

else if(b1==’4’&&((a1==pl1&&c1==pl1)||(b2==pl1&&b3==pl1)))

b1 = turn; //if it can block a win by taking space b1 then do it

else if(b2==’5’&&((b1==pl1&&b3==pl1)||(a2==pl1&&c2==pl1)||

(a1==pl1&&c3==pl1)||(a3==pl1&&c1==pl1)))

b2 = turn; //if it can block a win by taking space b2 then do it

else if(b3==’6’&&((b1==pl1&&b2==pl1)||(a3==pl1&&c3==pl1)))

b3 = turn; //if it can block a win by taking space b3 then do it

else if(c1==’1’&&((c2==pl1&&c3==pl1)||(a1==pl1&&b1==pl1)||(b2==pl1&&a3==pl1)))

c1 = turn; //if it can block a win by taking space c1 then do it

else if(c2==’2’&&((c1==pl1&&c3==pl1)||(a2==pl1&&b2==pl1)))

c2 = turn; //if it can block a win by taking space c2 then do it

else if(c3==’3’&&((c1==pl1&&c2==pl1)||(a3==pl1&&b3==pl1)||(b2==pl1&&a1==pl1)))

c3 = turn; //if it can block a win by taking space c3 then do it

//the following keeps the user from doing any type of strategy that will

//get the 2 places with 2 in a row that the computer cant block

else if(turns<=2&&a1==pl1&&a2==’8′) a2 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&a1==pl1&&b1==’4′) b1 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&a3==pl1&&b3==’6′) b3 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&a3==pl1&&a2==’8′) a2 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&c1==pl1&&c2==’2′) c2 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&c1==pl1&&b1==’4′) b1 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&c3==pl1&&c2==’2′) c2 = turn; //keep them from setting it up using 3 corners else if(turns<=2&&c3==pl1&&b3==’6′) b3 = turn; //keep them from setting it up using 3 corners else if(a2==pl1&&b1==pl1&&a1==’7′) a1 = turn; //keeps them from setting it up using sides else if(a2==pl1&&b3==pl1&&a3==’9′) a3 = turn; //keeps them from setting it up using sides else if(b3==pl1&&c2==pl1&&c3==’3′) c3 = turn; //keeps them from setting it up using sides else if(c2==pl1&&b1==pl1&&c1==’1′) c1 = turn; //keeps them from setting it up using sides else if(turns<=4&&a1==pl1&&c3==pl1&&a3==’9′) a3 = turn; //keeps them form setting it up using corners else if(turns<=4&&a1==pl1&&c3==pl1&&c1==’1′) c1 = turn; //keeps them form setting it up using corners else if(turns<=4&&a3==pl1&&c1==pl1&&a1==’7′) a1 = turn; //keeps them form setting it up using corners else if(turns<=4&&a3==pl1&&c1==pl1&&c3==’3′) c3 = turn; //keeps them form setting it up using corners else if(b2==’5′) //if middle space is open, take it b2 = turn; else { do //if it cant win or block a win then do something different { move = (rand() % 9) + 1; //gets a random number then %s by 9 and adds 1 //which will end up with a random number from 1 – 9 if((move==7 && a1==’7′)||(move==8 && a2==’8′)||(move==9 && a3==’9′)|| (move==4 && b1==’4′)||(move==5 && b2==’5′)||(move==6 && b3==’6′)|| (move==1 && c1==’1′)||(move==2 && c2==’2′)||(move==3 && c3==’3′)) empty = 1; //makes sure that where it picked is empty else empty = 0; //if its not empty then its full }while(empty==0); //if its full give it another try to pick an empty spot } } else if(first==pl1) //if computer goes first do this AI which is more aggresive { if(strategy==1) { //this strategy tries to set them up like this // 3 turn|1 turn| win the third turn and win depends on //——————— where they go the first 2 turns //2 turn|3 turn| //——————— // win| | if(turns==1&&b1==’4′) b1 = turn; else if(turns==3&&a1==’7’&&a2==’8’&&a3==’9′) a2 = turn; else if(turns==3&&c1==’1’&&c2==’2’&&c3==’3′) c2 = turn; else if(turns==5&&b1==pl2&&a2==pl2&&a1==’7′) a1 = turn; else if(turns==5&&b1==pl2&&c2==pl2&&c1==’1′) c1 = turn; else if(turns==5&&b1==pl2&&c2==pl2&&b2==’5′) b2 = turn; else if(turns==5&&b1==pl2&&a2==pl2&&b2==’5′) b2 = turn; else //if it cant continue with the strategy it will move strategy = 0; //to the default strategy which is the defensive one } else if(strategy==2) { //for this strategy it will try to set them up like this // | |2 turn //———————- // | |win //——————— //1 turn| win |3 turn //but if they move in the top right on thier first turn it will move to this //2 turn | | //———————— // | win | //———————– //1 turn| win | 3 turn //this is needed for this strategy because this one leaves more room //for the user to win while it is still setting them up, so this keeps //them from winnign during its strategy if(a1==’7’&&((a2==pl2&&a3==pl2)||(b1==pl2&&c1==pl2)||(b2==pl2&&c3==pl2))) a1 = turn; //if it can win by taking space a1 then do it else if(a2==’8’&&((a1==pl2&&a3==pl2)||(b2==pl2&&c2==pl2))) a2 = turn; //if it can win by taking space a2 then do it else if(a3==’9’&&((a1==pl2&&a2==pl2)||(b3==pl2&&c3==pl2)||(b2==pl2&&c1==pl2))) a3 = turn; //if it can win by taking space a3 then do it else if(b1==’4’&&((a1==pl2&&c1==pl2)||(b2==pl2&&b3==pl2))) b1 = turn; //if it can win by taking space b1 then do it else if(b2==’5’&&((b1==pl2&&b3==pl2)||(a2==pl2&&c2==pl2)|| (a1==pl2&&c3==pl2)||(a3==pl2&&c1==pl2))) b2 = turn; //if it can win by taking space b2 then do it else if(b3==’6’&&((b1==pl2&&b2==pl2)||(a3==pl2&&c3==pl2))) b3 = turn; //if it can win by taking space b3 then do it else if(c1==’1’&&((c2==pl2&&c3==pl2)||(a1==pl2&&b1==pl2)||(b2==pl2&&a3==pl2))) c1 = turn; //if it can win by taking space c1 then do it else if(c2==’2’&&((c1==pl2&&c3==pl2)||(a2==pl2&&b2==pl2))) c2 = turn; //if it can win by taking space c2 then do it else if(c3==’3’&&((c1==pl2&&c2==pl2)||(a3==pl2&&b3==pl2)||(b2==pl2&&a1==pl2))) c3 = turn; //if it can win by taking space c3 then do it else if(a1==’7’&&((a2==pl1&&a3==pl1)||(b1==pl1&&c1==pl1)||(b2==pl1&&c3==pl1))) a1 = turn; //if it can block a win by taking space a1 then do it else if(a2==’8’&&((a1==pl1&&a3==pl1)||(b2==pl1&&c2==pl1))) a2 = turn; //if it can block a win by taking space a2 then do it else if(a3==’9’&&((a1==pl1&&a2==pl1)||(b3==pl1&&c3==pl1)||(b2==pl1&&c1==pl1))) a3 = turn; //if it can block a win by taking space a3 then do it else if(b1==’4’&&((a1==pl1&&c1==pl1)||(b2==pl1&&b3==pl1))) b1 = turn; //if it can block a win by taking space b1 then do it else if(b2==’5’&&((b1==pl1&&b3==pl1)||(a2==pl1&&c2==pl1)|| (a1==pl1&&c3==pl1)||(a3==pl1&&c1==pl1))) b2 = turn; //if it can block a win by taking space b2 then do it else if(b3==’6’&&((b1==pl1&&b2==pl1)||(a3==pl1&&c3==pl1))) b3 = turn; //if it can block a win by taking space b3 then do it else if(c1==’1’&&((c2==pl1&&c3==pl1)||(a1==pl1&&b1==pl1)||(b2==pl1&&a3==pl1))) c1 = turn; //if it can block a win by taking space c1 then do it else if(c2==’2’&&((c1==pl1&&c3==pl1)||(a2==pl1&&b2==pl1))) c2 = turn; //if it can block a win by taking space c2 then do it else if(c3==’3’&&((c1==pl1&&c2==pl1)||(a3==pl1&&b3==pl1)||(b2==pl1&&a1==pl1))) c3 = turn; //if it can block a win by taking space c3 then do it else if(turns==1&&c1==’1′) c1 = turn; else if(turns==3&&c1==pl2&&a3==’9′) a3 = turn; else if(turns==5&&c1==pl2&&a3==pl2&&a1==’7′) a1 = turn; else if(turns==5&&c1==pl2&&a3==pl2&&c3==’3′) c3 = turn; else if(turns==3&&c1==pl2&&a3==pl1&&a1==’7′) a1 = turn; else if(turns==5&&c1==pl2&&a1==pl2&&c3==’3′) c3 = turn; else //if it cant continue with the strategy it will move strategy = 0; //to the default AI } if(strategy==0) { if(a1==’7’&&((a2==pl2&&a3==pl2)||(b1==pl2&&c1==pl2)||(b2==pl2&&c3==pl2))) a1 = turn; //if it can win by taking space a1 then do it else if(a2==’8’&&((a1==pl2&&a3==pl2)||(b2==pl2&&c2==pl2))) a2 = turn; //if it can win by taking space a2 then do it else if(a3==’9’&&((a1==pl2&&a2==pl2)||(b3==pl2&&c3==pl2)||(b2==pl2&&c1==pl2))) a3 = turn; //if it can win by taking space a3 then do it else if(b1==’4’&&((a1==pl2&&c1==pl2)||(b2==pl2&&b3==pl2))) b1 = turn; //if it can win by taking space b1 then do it else if(b2==’5’&&((b1==pl2&&b3==pl2)||(a2==pl2&&c2==pl2)|| (a1==pl2&&c3==pl2)||(a3==pl2&&c1==pl2))) b2 = turn; //if it can win by taking space b2 then do it else if(b3==’6’&&((b1==pl2&&b2==pl2)||(a3==pl2&&c3==pl2))) b3 = turn; //if it can win by taking space b3 then do it else if(c1==’1’&&((c2==pl2&&c3==pl2)||(a1==pl2&&b1==pl2)||(b2==pl2&&a3==pl2))) c1 = turn; //if it can win by taking space c1 then do it else if(c2==’2’&&((c1==pl2&&c3==pl2)||(a2==pl2&&b2==pl2))) c2 = turn; //if it can win by taking space c2 then do it else if(c3==’3’&&((c1==pl2&&c2==pl2)||(a3==pl2&&b3==pl2)||(b2==pl2&&a1==pl2))) c3 = turn; //if it can win by taking space c3 then do it else if(a1==’7’&&((a2==pl1&&a3==pl1)||(b1==pl1&&c1==pl1)||(b2==pl1&&c3==pl1))) a1 = turn; //if it can block a win by taking space a1 then do it else if(a2==’8’&&((a1==pl1&&a3==pl1)||(b2==pl1&&c2==pl1))) a2 = turn; //if it can block a win by taking space a2 then do it else if(a3==’9’&&((a1==pl1&&a2==pl1)||(b3==pl1&&c3==pl1)||(b2==pl1&&c1==pl1))) a3 = turn; //if it can block a win by taking space a3 then do it else if(b1==’4’&&((a1==pl1&&c1==pl1)||(b2==pl1&&b3==pl1))) b1 = turn; //if it can block a win by taking space b1 then do it else if(b2==’5’&&((b1==pl1&&b3==pl1)||(a2==pl1&&c2==pl1)|| (a1==pl1&&c3==pl1)||(a3==pl1&&c1==pl1))) b2 = turn; //if it can block a win by taking space b2 then do it else if(b3==’6’&&((b1==pl1&&b2==pl1)||(a3==pl1&&c3==pl1))) b3 = turn; //if it can block a win by taking space b3 then do it else if(c1==’1’&&((c2==pl1&&c3==pl1)||(a1==pl1&&b1==pl1)||(b2==pl1&&a3==pl1))) c1 = turn; //if it can block a win by taking space c1 then do it else if(c2==’2’&&((c1==pl1&&c3==pl1)||(a2==pl1&&b2==pl1))) c2 = turn; //if it can block a win by taking space c2 then do it else if(c3==’3’&&((c1==pl1&&c2==pl1)||(a3==pl1&&b3==pl1)||(b2==pl1&&a1==pl1))) c3 = turn; //if it can block a win by taking space c3 then do it else if(b2==’5′) //if middle space is open, take it b2 = turn; else { do //if it cant win or block a win then do something different { move = (rand() % 9) + 1; //gets a random number then %s by 9 and adds 1 //which will end up with a random number from 1 – 9 if((move==7 && a1==’7′)||(move==8 && a2==’8′)||(move==9 && a3==’9′)|| (move==4 && b1==’4′)||(move==5 && b2==’5′)||(move==6 && b3==’6′)|| (move==1 && c1==’1′)||(move==2 && c2==’2′)||(move==3 && c3==’3′)) empty = 1; //makes sure that where it picked is empty else empty = 0; //if its not empty then its full }while(empty==0); //if its full give it another try to pick an empty spot } } } } if(move==7 && a1==’7′)//if they picked 7 save top left variable a1 = turn; // as character of whos ever turn it is else if(move==8 && a2==’8′)//if they picked 8 save top middle variable a2 = turn; // as character of whos ever turn it is else if(move==9 && a3==’9′)//if they picked 9 save top right variable a3 = turn; // as character of whos ever turn it is else if(move==4 && b1==’4′)//if they picked 4 save middle left variable b1 = turn; // as character of whos ever turn it is else if(move==5 && b2==’5′)//if they picked 5 save middle variable b2 = turn; // as character of whos ever turn it is else if(move==6 && b3==’6′)//if they picked 6 save middle right variable b3 = turn; // as character of whos ever turn it is else if(move==1 && c1==’1′)//if they picked 1 save bottom left variable c1 = turn; // as character of whos ever turn it is else if(move==2 && c2==’2′)//if they picked 2 save bottom middle variable c2 = turn; // as character of whos ever turn it is else if(move==3 && c3==’3′)//if they picked 3 save bottom right variable c3 = turn; // as character of whos ever turn it is if(a1==a2&&a2==a3 || b1==b2&&b2==b3 || c1==c2&&c2==c3 || //if any row is all a1==b1&&b1==c1 || a2==b2&&b2==c2 || a3==b3&&b3==c3 || //same characters a1==b2&&b2==c3 || a3==b2&&b2==c1) win = 1; //win = 1 which means someone wins else if(a1!=’7’&&a2!=’8’&&a3!=’9’&&b1!=’4’&&b2!=’5’&&b3!=’6’&&c1!=’1’&&c2!=’2’&& c3!=’3′) //if all squares dont equal their starting values and neither win = 2; //player has won win = 2 which means its a cat game, no one wins }while(win==0);//do the loop while win still =s 0 which means no one has won clrscr(); //clear screen to display winner and winning board if(win==1) //if win = 1 cout << win=”=”2)” win =” 2″>> again; //ask if they want to play again and gets response

}while(again==1); //repeats loop while again = 1 which means they want to

//play again

return 0;

}