I know that an array matrix would have been better but Im taking a freshman course so coulnt use it.
1- Tic tac toe grid is defined from locations 1 to 9
2- For loop executes 9 times for 9 turns
3- A functions takes all 9 locations as argument to compute result after every turn
4- a switch() is used to assign X/O to the grid
5- every case in the switch first checks of location is already used or not- i really want this check to happen only once instead of in every case
6- Can I use just one swicth for both p1 and p2 ?
#include <iostream>
#include "conio.h"
#include "stdlib.h"
using namespace std;
void checkResult(char, char, char, char, char, char, char, char, char);
// this function checks whether P1 or P1 has won; it also displays the grid everytime it is called
int main()
{
char c1='-',c2='-',c3='-',c4='-',c5='-',c6='-', c7='-', c8='-', c9='-';
// 9 chars initialized. alternative could have been an array matrix[3][3]
int input; // location input
cout<<"\nNOTE:Position in Grid is allocated by numbers 1 to 9 from left to right.\n";
for ( int i=0; i<9; i++)
{
p1_input:
cout<<"\nPlayer 1 make your move. Choose locations 1 to 9 \n";
cin>>input;
switch (input)
{
case 1:
if (c1=='-')
c1='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 2:
if (c2=='-')
c2='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 3:
if (c3=='-')
c3='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 4:
if (c4=='-')
c4='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 5:
if (c5=='-')
c5='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 6:
if (c6=='-')
c6='X';
else
{ cout<<"\n ***Error: already chosen***"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 7:
if (c7=='-')
c7='X';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 8: if (c8=='-')
c8='X';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 9: if (c9=='-')
c9='X';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p1_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
default: cout<<"\n**********invalid input*********\n"; goto p1_input;
}
p2_input:
cout<<"\nPlayer 2 make your move. Choose locations 1 to 9 \n";
cin>>input;
switch (input)
{ case 1:
if (c1=='-')
c1='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 2:
if (c2=='-')
c2='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 3:
if (c3=='-')
c3='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 4:
if (c4=='-')
c4='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 5:
if (c5=='-')
c5='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 6:
if (c6=='-')
c6='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 7:
if (c7=='-')
c7='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 8: if (c8=='-')
c8='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
case 9: if (c9=='-')
c9='O';
else
{ cout<<"\n ***Error: already chosen***\n"; goto p2_input;}
checkResult(c1,c2,c3,c4,c5,c6,c7,c8,c9); break;
default: cout<<"\n**********invalid input*********\n"; goto p2_input;
}
} //end of for loop. 9 tries over. result should be draw now if checkResult has not declared a winner yet
return 0;
}
void checkResult (char c1 , char c2, char c3, char c4, char c5, char c6, char c7, char c8, char c9)
{
cout<<"\t"<<c1<<"\t"<<c2<<"\t"<<c3<<"\n\t"<<c4<<"\t"<<c5<<"\t"<<c6<<"\t\n\t"<<c7<<"\t"<<c8<<"\t"<<c9<<"\n";
if (
(c1=='X' && c2=='X' && c3=='X') ||
(c4=='X' && c5=='X' && c6=='X') ||
(c7=='X' && c8=='X' && c9=='X') ||
(c1=='X' && c4=='X' && c7=='X') ||
(c2=='X' && c5=='X' && c8=='X') ||
(c3=='X' && c6=='X' && c9=='X') ||
(c1=='X' && c5=='X' && c9=='X') ||
(c3=='X' && c5=='X' && c7=='X')
)
{ cout<<"\n PLAYER 1 Wins!!!"; getch(); exit(0); } // the above 8 cases are when P1 wins; 3 vertical cases, 3 horizontal and 2 diagonal
else
if (
(c1=='O' && c2=='O' && c3=='O') ||
(c4=='O' && c5=='O' && c6=='O') ||
(c7=='O' && c8=='O' && c9=='O') ||
(c1=='O' && c4=='O' && c7=='O') ||
(c2=='O' && c5=='O' && c8=='O') ||
(c3=='O' && c6=='O' && c9=='O') ||
(c1=='O' && c5=='O' && c9=='O') ||
(c3=='O' && c5=='O' && c7=='O')
)
{ cout<<"\n PLAYER 2 Wins!!!"; getch(); exit(0);} // the above 8 cases are when P2 wins; 3 vertical cases, 3 horizontal and 2 diagonal
else
if (c1!='-'&& c2!='-'&& c3!='-'&& c4!='-' && c5!='-' && c6!='-' && c7!='-' && c8!='-' && c9!='-')
{ cout<<"\n Draw..."; getch(); exit(0);} //over here the 3x3 matrix is full and none of the win cases exist for P1 and P2
}