LOST, PLEASE HELP! - thankyou

Hi, this is my tic tac toe game disaster.I've restarted twice already & I'm what one would call "lost in the trees."
I'm having trouble with my functions, the assignments of "X" & "O" aren't global & I'm unsure how to make them "global" without actually making them global. Same with trying to determine who goes first.
This is the most code I've written in C++ & I'm not sure how to manage the functions in my main.
My game is using the "MagicSquareBox" technique for my AI.
Right now my code doesn't check for wins, or draws nor is it in a loop I don't know where to add these functions without jumbling up my main.

Any help is greatly appreciated, thanks a lot! -thankyou
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  
void Showblankguide();
void board(char array [][3], int size);
int player_turn(int num);
int Showmenu(int num);
int choicefunc();
int cpuMove(int userMove);
int whoAreYou(char letter);
bool isPlaying();
char plotter();


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


int main(int argc, char const *argv[]) {
  bool isPlaying = true;
  cout << fixed << showpoint << setprecision(0);         // Set up numeric output formatting                                                                                                       
  choicefunc();
  plotter();

  return 0;
}


void Showblankguide()
{
	cout <<"Player1 vs. CPU" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << "A1" << " |  " << "A2" << " |  " <<    "A3" << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << "B1" << " |  "    "C2"  << " |  " << "C3" << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << "C1" << " |  " << "C2" << " |  " <<   "C3" << endl;

	cout << "     |     |     " << endl << endl;
}

int Showmenu(int choice)
{
  const int X_CHOICE = 1,                                                       //Constants for the menu choices
            O_CHOICE = 2,
            QUIT_CHOICE = 3;

            Showblankguide();
       cout << "\n\t\tTIC TAC TOE MENU \n\n"
            << "\n\t\t1. I want to be X \n"
            << "\n\t\t2. I want to be O \n"
            << "\n\t\t3. Quit the program.\n"
            << "\n\t\t===================================\n"
            << "\n\t\tEnter you choice: ";
            cin >> choice;
            while (choice < X_CHOICE || choice > QUIT_CHOICE)
            {
              cout << "Enter a valid choice: ";
              cin >> choice;
            }
          return choice;
        }
int choicefunc(){
  char player;
  char cpu;

  int choice = Showmenu(choice);
  const int X_CHOICE = 1,                                                       //Constants for the menu choices
            O_CHOICE = 2,
            QUIT_CHOICE = 3;

    switch (choice)
    {
      case X_CHOICE:player = 'X';whoAreYou(player); cpu = 'O'; break;

      case O_CHOICE:player = 'O';whoAreYou(player); cpu = 'X'; break;
    }

return player;
}

int whoAreYou(char player)
{
  int anarray[5];
  int turn;
  char cpu = 'X';
  if (player == 'X') cpu = 'O';
  cout << "   \n";
  cout << "You are " << player << endl;
  cout << "Designate where you want " << player << " to move:  \n\n";
  plotter();
}
int cpuMove(int userMove)
{                                                                      //subtracts usersMove from 15, 2 numbers equal the difference, 1 is randomly chosen
    int starter = 15-userMove; 
    char cpu = 'O';                                              // <<<<<<<<<<< THIS IS WRONG, HARD CODE, BAD CODE :(
    srand(time(NULL));
    int number1 = rand() % starter + 1;
    int counterMove = starter - number1;
    if (userMove == 0) counterMove = number1;
    switch (counterMove)
{   //assigns cpu to [x][y] and shows updated board
    case 8: grid4[0][0] = cpu ; board(grid4, 3); break ;
    case 1: grid4[0][1] = cpu ; board(grid4, 3); break ;
    case 6: grid4[0][2] = cpu ; board(grid4, 3); break ;
    case 3: grid4[1][0] = cpu ; board(grid4, 3); break ;
    case 5: grid4[1][1] = cpu ; board(grid4, 3); break ;
    case 7: grid4[1][2] = cpu ; board(grid4, 3); break ;
    case 4: grid4[2][0] = cpu ; board(grid4, 3); break ;
    case 9: grid4[2][1] = cpu ; board(grid4, 3); break ;
    case 2: grid4[2][2] = cpu ; board(grid4, 3); break ;
  }
   
  }
                                                                                     //Constants for array input, assigns player to position in the grid4 array, gives counterMove the int value of coordinate, & updates board

char plotter(){
    string coordinates;
    int counterMove;
    char player = 'X';             // <<<<<<<<<<< THIS IS WRONG, HARD CODE, BAD CODE :(
    Showblankguide();
    getline(cin,coordinates);
    if (coordinates == "A1") {
      grid4[0][0] = player ; counterMove = cpuMove(8); board(grid4, 3);
    }
    else if (coordinates == "A2") {
      grid4[0][1] = player ; counterMove = cpuMove(1); board(grid4, 3);
    }
    else if (coordinates == "A3") {
      grid4[0][2] = player ; counterMove = cpuMove(6); board(grid4, 3);
    }
    else if (coordinates == "B1") {
      grid4[1][0] = player ; counterMove = cpuMove(3); board(grid4, 3);
    }
    else if (coordinates ==  "B2") {
      grid4[1][1] = player ; counterMove = cpuMove(5); board(grid4, 3);
    }
    else if (coordinates ==  "B3") {
      grid4[1][2] = player ; counterMove = cpuMove(7); board(grid4, 3);
    }
    else if (coordinates ==  "C1") {
      grid4[2][0] = player ; counterMove = cpuMove(4); board(grid4, 3);
    }
    else if (coordinates ==  "C2" ) {
      grid4[2][1] = player ; counterMove = cpuMove(9); board(grid4, 3);
    }
    else if (coordinates == "C3" ) {
      grid4[2][2] = player ; counterMove = cpuMove(2); board(grid4, 3);

  return 0;
}

}
void board(char array [][3], int size)
{
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1         vs.          CPU " << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << array[0][0] << "  |  " << array[0][1] << "  |  " << array[0][2] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << array[1][0] << "  |  " << array[1][1] << "  |  " << array[1][2] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << array[2][0] << "  |  " << array[2][1] << "  |  " << array[2][2] << endl;

	cout << "     |     |     " << endl << endl;
}

Last edited on
This would definitely be a lot easier to manage in a class. You could declare your variables as properties at the class level. This will solve your "global" issue but still have your variables encapsulated in your class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Showblankguide()
{
	cout <<"Player1 vs. CPU" << endl;
	cout << "     |     |     " << endl;
	cout << "  " << "A1" << " |  " << "A2" << " |  " <<    "A3" << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << "B1" << " |  "    "C2"  << " |  " << "C3" << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << "C1" << " |  " << "C2" << " |  " <<   "C3" << endl;

	cout << "     |     |     " << endl << endl;
}


There is a bug in the second row, it outputs Cx instead of Bx.

And what are you trying to do here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int cpuMove(int userMove)
{                                                                      //subtracts usersMove from 15, 2 numbers equal the difference, 1 is randomly chosen
    int starter = 15-userMove;
    char cpu = 'O';                                              // <<<<<<<<<<< THIS IS WRONG, HARD CODE, BAD CODE :(
    srand(0);
    int number1 = rand() % starter + 1;
    int counterMove = starter - number1;
    if (userMove == 0) counterMove = number1;
    switch (counterMove)
    {   //assigns cpu to [x][y] and shows updated board
        case 8: grid4[0][0] = cpu ; board(grid4, 3); break ;
        case 1: grid4[0][1] = cpu ; board(grid4, 3); break ;
        case 6: grid4[0][2] = cpu ; board(grid4, 3); break ;
        case 3: grid4[1][0] = cpu ; board(grid4, 3); break ;
        case 5: grid4[1][1] = cpu ; board(grid4, 3); break ;
        case 7: grid4[1][2] = cpu ; board(grid4, 3); break ;
        case 4: grid4[2][0] = cpu ; board(grid4, 3); break ;
        case 9: grid4[2][1] = cpu ; board(grid4, 3); break ;
        case 2: grid4[2][2] = cpu ; board(grid4, 3); break ;
    }
}

Tic-tac-toe is a "tactical" game. You can (and should) define rules that if the computer follows them will result in the computer winning or a draw (but the computer will never loose).
To start your computer should probably:
- if available claim the center position (because the maximum of 4 lines going through a point only occurs there).
- if the center position has been claimed:
--- check if there is a row with 2 similar symbols. In there is, claim the third position in that row.
--- check if there is a column with 2 similar symbols. In there is, claim the third position in that column.
--- check if there is a diagonal with 2 similar symbols. In there is, claim the third position in that diagonal.
- if there are no two identical symbols in line in any direction
--- check if there is a corner position left (because those are part of three lines)
------ if there are multiple corners available, prefer the one that is in line with another symbol from the computer.
- if there are no corner positions available, select one of the open positions.

For checking if somebody won, it is probably a good idea to tell that function what the last move was, then you only have to check the row, column and diagonals that go through that one position.

And int player_turn(int num); is declared but seems to be missing in the implementation.

I would recommend removing all code that you currently don't use and fixing the indenting, to give you a bit more overview. Also, I would like to suggest that you describe what should happen (in your own words) with references to the functions.

For example:
The program starts with the main function. This first calls the printHelloWorld function. The printHelloWorld function will output "Hello World" to the console. Subsequently the program will return to the main function, that will return 0 and exit.

While making such a description you will most likely find things that you have to write multiple times, that is a strong indicator that you may want to create a function for those things.
Last edited on
Topic archived. No new replies allowed.