I need help with my tic-tac-toe game, please!

It is almost complete! The only thing left that I am trying to accomplish is to initialize all of the elements of the array with an "*" symbol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//*************************************************************************************************************************************************************************
// tutor.cpp - this program allows two players to play a game of tic-tac-toe. 								     	   			   							   			   
//		Input: (1):  player 1 inputs their first move as a coordinate (row x column) of numbers 1 through 9. (2): player 2 inputs their first move as a coordinate (row x  
//			column) of numbers 1 through 9. (3) Input continues to go back and forth between the two players until a winner is found or a draw happens in the game.		   
//		Output: (1):  A tic-tac-toe gameboard is drawn and player 1 is prompted for their input. (2): After each successfull move input, a new board is drawn replacing    
//			"*" symbol with either a "X" or "O" depending on which player is using their turn. (3): Player 1 is output as the winner if they are able to mark the board    
//			with 3 "X's" in a row, column, or diagonal / Player 2 is output as the winner if they are able to mark the board with 3 "O's" in a row, column, or diagonal.   
//			(4): 																																						   
//		Assumptions: (1):  A valid input for row and column is 1-9, anything else results in an "invalid move" and player is prompted for input again.					   
//*************************************************************************************************************************************************************************

#include <iostream>
#include <string>
using namespace std;

char num[10] = {'o','1','2','3','4','5','6','7','8','9'};

string whiteSpace = "                ";  

int win()																									// Win function returns the winner of the game, either:   
{																											// -->  returns -1: for game in progress           		  
    if (num[1] == num[2] && num[2] == num[3])																// -->  returns  0: for game over with draw situation 	  
        return 1;																							// -->  returns  1: for game over with a success      	  
    else if (num[4] == num[5] && num[5] == num[6])
        return 1;
    else if (num[7] == num[8] && num[8] == num[9])
        return 1;
    else if (num[1] == num[4] && num[4] == num[7])
        return 1;
    else if (num[2] == num[5] && num[5] == num[8])
        return 1;
    else if (num[3] == num[6] && num[6] == num[9])
        return 1;
    else if (num[1] == num[5] && num[5] == num[9])
        return 1;
    else if (num[3] == num[5] && num[5] == num[7])
        return 1;
    else if (num[1] != '1' && num[2] != '2' && num[3] != '3' && num[4] != '4' && num[5] != '5' && num[6] != '6' && num[7] != '7' && num[8] != '8' && num[9] != '9')
        return 0;
    else
        return -1;
}
  
void game()     																		// Game void function draws the game board along with player 1 and 2's input  
{
    system("cls");
    cout << "\n\n\t" << whiteSpace << "    Coy's Tic Tac Toe Game\n\n" << whiteSpace;
    cout << "        Player 1 (X)  -  Player 2 (O)\n\n\n";
    cout << whiteSpace << "                   |     |     " << endl;
    cout << whiteSpace << "                " << num[1] << "  |  " << num[2] << "  |  " << num[3] << endl;
    cout << whiteSpace << "              _____|_____|_____" << endl;
    cout << whiteSpace << "                   |     |     " << endl;
    cout << whiteSpace << "                " << num[4] << "  |  " << num[5] << "  |  " << num[6] << endl;
    cout << whiteSpace << "              _____|_____|_____" << endl;
    cout << whiteSpace << "                   |     |     " << endl;
    cout << whiteSpace << "                " << num[7] << "  |  " << num[8] << "  |  " << num[9] << endl;
    cout << whiteSpace << "                   |     |     " << "\n\n\n";
}
 
int main()
{
    int player = 1,i;
	int row, column;																	// Variables for row and column in array					   				  
    char m;																				// Variable for marker (X = Player 1, O = Player 2)						      
    do
    {
        game();
        player=(player%2)?1:2;

        cout << "   Player " << player 													// User is prompted for input of their move by corresponding row & column     
			 << ", enter a (1-3) row & column number separated by a single space: ";
        cin >> row >> column;

        m=(player == 1) ? 'X' : 'O';													// Player 1 = X | Player 2 = O												  

        if (row == 1 && column == 1)													// If statements mark the corresponding element in the array according to     
            num[1] = m;																	// the row and column number that is input by user							  
        else if (row == 1 && column == 2)
            num[2] = m;
        else if (row == 1 && column == 3)
            num[3] = m;
        else if (row == 2 && column == 1)
            num[4] = m;
        else if (row == 2 && column == 2)
            num[5] = m;
        else if (row == 2 && column == 3)
            num[6] = m;
        else if (row == 3 && column == 1)
            num[7] = m;
        else if (row == 3 && column == 2)
            num[8] = m;
        else if (row == 3 && column == 3)
            num[9] = m;
        else
        {
            cout<<"\aInvalid move, please input position again: ";						// If anything outside of 1-3 is input for the row & column, that is invalid  
            player--;																	// input. Error message is output and user is prompted for aditional input    
            cin.ignore();
            cin.get();
        }
        i=win();
        player++;
    }while(i==-1);
    
    game();
    if(i==1)
        cout << whiteSpace << "          ==>\aPlayer "<< --player 						// If 3 "X's" or "O's" are found in a row, column, or diagonal then a winner  
			 <<" is the winner! " << endl;												// is found and output to the users											  
    else
        cout << whiteSpace << "              ==>\aGame is a draw! \n";					// If every element in the array is filled and there are not 3 "X's" or "O's" 
    cin.ignore();																		// marked either in a row, column, or diagonal: THE GAME IS OUTPUT AS A DRAW. 
    cin.get();
   }


I did try using the code char num[10] = {'o','*','*','*','*','*','*','*','*','*'};
instead of char num[10] = {'o','1','2','3','4','5','6','7','8','9'};

This did give me (*)'s in the elements of the array, however doing it this way results in an immediate win for Player 1 because of the way my if statements are structured in the win function...

I am sure there is another way to do this, but I am pretty stumped at this point. Any help would greatly be appreciated. Thanks in advance!
check for valid symbols in your win function. I.e:

1
2
if (num[1] == num[2] && num[2] == num[3] && (num[3] == 'X' ||num[3] == 'O') )																// -->  returns  0: for game over with draw situation 	  
        return 1;	


With this you can tell which was the winner:

1
2
if (num[1] == num[2] && num[2] == num[3] && (num[3] == 'X' ||num[3] == 'O') )																// -->  returns  0: for game over with draw situation 	  
        return (num[1] == 'X' ? 1 : 2);	


returns the no of the winner.
Last edited on
Topic archived. No new replies allowed.