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

}

Recommended Answers

All 4 Replies

goto? really? Why not put the switch code into a function bool ValidInput(...) and then loop for each user's input until it is valid?

while (!ValidInput(...))
{
    cout<<"...make your move...";
    cin>>input;
}

You could then reuse one set of the switch code for each user input call. Though it is possible to do this with goto statements as well, I won't help you with that--very nasty stuff!:P

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 ?

Yes, an array would be MUCH better, but if you're not supposed to use one, then you're not supposed to use one.

#2 - As you have it now, each trip through the for-loop has an X move and a Y move. The loop won't execute nine times. By the middle of the fifth trip through the loop, the program will have terminated with your function call.

#6 - You can definitely do a single switch case rather than two switch cases:

int playerNum;
char playerSymbol;

// decide whose turn it is.
if (playerNum == 1)
    playerSymbol = 'X';
else
    playerSymbol = 'Y';

cout<<"\nPlayer " << playerNum << " make your move. Choose locations 1 to 9 \n";
cin>>input;
switch (input)
{
    // same switch code, but don't hard-code 'X' or 'Y'.  Replace 'X' and 'Y' in code with playerSymbol.
}
int playerNum;
char playerSymbol;

// decide whose turn it is.
if (playerNum == 1)
    playerSymbol = 'X';
else
    playerSymbol = 'Y';

cout<<"\nPlayer " << playerNum << " make your move. Choose locations 1 to 9 \n";
cin>>input;
switch (input)
{
    // same switch code, but don't hard-code 'X' or 'Y'.  Replace 'X' and 'Y' in code with playerSymbol.
}

ELEGANT!!!!!!!!!!

int playerNum;
char playerSymbol;

// decide whose turn it is.
if (playerNum == 1)
    playerSymbol = 'X';
else
    playerSymbol = 'Y';

cout<<"\nPlayer " << playerNum << " make your move. Choose locations 1 to 9 \n";
cin>>input;
switch (input)
{
    // same switch code, but don't hard-code 'X' or 'Y'.  Replace 'X' and 'Y' in code with playerSymbol.
}

Modified to show how you can stop using goto statements:

bool ValidateMove (int location, char playerSymbol)
{
	switch (location)
	{
		// same switch code, but don't hard-code 'X' or 'Y'.  Replace 'X' and 'Y' in code with playerSymbol.

		// return false if invalid move and such, or true if valid
	}
}

void TicTacToe (void)
{

	int playerNum;
	char playerSymbol;
	bool done = false;
	int input;

	do
	{
		// decide whose turn it is.
		if (playerNum == 1)
			playerSymbol = 'X';
		else
			playerSymbol = 'Y';

		cout<<"\nPlayer " << playerNum << " make your move. Choose locations 1 to 9 \n";
		cin>>input;

		// TODO: use input to get sentinel to determine to exit

		// if valid, change players...otherwise, just loop and ask location again...
		if (ValidateMove(intput, playerSymbol))
		{
			// setup for next player
			if (1 == playerNum)
				playerNum = 2;
			else
				playerNum = 1;

			continue;
		}

		// TODO: if game is over, set done = true or just break

	} while (!done);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.