Tic Tac Toe console game help

I've been making my own little tic tac toe game lately but ive come across some problems, i feel like a lot of the code im making could easily be compressed into much easier code. the "i"'d code is the code i currently feel could have a much easier way of writing, any ideas? Thanks!

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
#include <iostream>
#include <windows.h>
#define Rows 3
#define Cols 3
/* Layout of game(when i run in my console)
GAME        CONTROLS        ****TICTACTOE****
*|*|*        1  |  2  |  3
-------     --------------
*|*|*       11  | 22  | 33
-------     --------------
*|*|*       111 | 222 | 333
    PLAYER ONE'S TURN:

*/
void printMap(char Map[][3]);
void Movep(int Input, char Map[][3], char Player);
void cls(); //windows function to clear screen
void Controls(int r, int c); //takes rows and columns as params
void CheckThree(char Map[][3], char Player); //check for 3 in a row(unfinished)
int main()
{
    int Input; //input to decide where to go
    bool Playerone, PlayerTwo, Game; //used for while loops
    Playerone  = Game = true; //set playerone to go first and the game to run
    PlayerTwo = false; //player two starts out false
    char Map[Rows][Cols];
    //this compressed for loop runs through and sets everything to '*' in the array
    for(int r = 0; r < Rows; r++){for(int c = 0; c < Cols; c++){Map[r][c] = '*';}}
    //print the map:
    printMap(Map);
    while(Game)
    {
        while(Playerone) //PLAYER ONE LOOP
        {
            std::cout << "\n PLAYER ONE'S TURN: ";
            std::cin >> Input;
            Movep(Input, Map, 'X');
            cls();
            printMap(Map);
            CheckThree(Map, 'X');
            PlayerTwo = true; //playertwo's turn now
            Playerone = false; //playerone's turn ends
        }

        while(PlayerTwo) //PLAYER TWO LOOP
        {
            std::cout << "\n PLAYER TWO'S TURN: ";
            std::cin >> Input;
            Movep(Input, Map, 'O');
            cls();
            printMap(Map);
            CheckThree(Map,'O');
            Playerone = true; //restart the cycle again.
            PlayerTwo = false;
        }
    }
}

void printMap(char Map[][3])
{
    std::cout << "GAME\t\t   CONTROLS\t\t****TICTACTOE****\n"; //title of console
    for(int r = 0; r < Rows; r++){
        for(int c = 0; c < Cols; c++){
            std::cout << Map[r][c];
            if(c<2){std::cout << "|";}
            Controls(r,c);
        }
        if(r>1){continue;}
        std::cout << "\n-------\t\t--------------\n"; //the dashes separating rows for contols + game
    }
}

void Movep(int x, char Map[][3], char Player)
{
    switch(x) //cumbersome and repetitve, only way to do this?
    //im taking the input and setting the square he inputed to his player
    //i use a case to check every option.
    {
        case 1:
            if(Map[0][0] == '*') // used to make sure you arent overlapping squares
                Map[0][0] = Player;
            break;
        case 2:
            if(Map[0][1] == '*')
                Map[0][1] = Player;
            break;
        case 3:
            if(Map[0][2] == '*')
                Map[0][2] = Player;
            break;
        case 11:
            if(Map[1][0] == '*')
                Map[1][0] = Player;
            break;
        case 22:
            if(Map[1][1] == '*')
                Map[1][1] = Player;
            break;
        case 33:
            if(Map[1][2] == '*')
                Map[1][2] = Player;
            break;
        case 111:
            if(Map[2][0] == '*')
                Map[2][0] = Player;
            break;
        case 222:
            if(Map[2][1] == '*')
                Map[2][1] = Player;
            break;
        case 333:
            if(Map[2][2] == '*')
                Map[2][2] = Player;
            break;
    }
}
void cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}

void Controls(int r, int c) //PRINT OUT CONTROLS
{
    if(c < 2){return;} //end function aftet columns > 2
    switch(r){
    case 0:
    std::cout << "\t\t" << "  1 |   2 |   3";
    break;
    case 1:
    std::cout << "\t\t" << " 11 |  22 |  33";
    break;
    case 2:
    std::cout << "\t\t" << "111 | 222 | 333";
    break;
    }
}

void CheckThree(char Map[][3], char Player) //on clue how to go about this
{
    for(int r = 0; r < Rows;r++){
        for(int c = 0; c< Cols; c++){
            if(Map[r][c] == Player) //should i just make a ton of if statements? what do i do?
                if(Map[r+1][c] == Player)
                    if(Map[r+2][c] == Player)
                        std::cout << "\n\n" << Player << " WINS THE GAME!";
        }
    }
}
Last edited on
Tip: Text that looks like this is called 'italic'. ;)
Movep: Make an array of the 1, 2, 3, 11, 22, 33, 111, 222, 333 (why such a strange system btw?) and then make a matching array of coordinate pairs (or two arrays if you don't know structs/classes yet) and have their indexes just match up correctly. You could search through the 1, 2, 3, 11, 22... array for the user input then use the index for the coordinates.

CheckThree: There are only eight ways to win, right? Set up pre-made winning grids (arrays of booleans) and search through them to see if the grids match. You can convert the player grid to a boolean grid by checking if each grid cell is for the player you're checking.
Last edited on
Thanks a lot L B, saves me a lot of time programming this!
Topic archived. No new replies allowed.