TicTacToe illegal moves and winner, array, functions

so i am in a beginners c++ class, and we have an assignment where we make a tic tac toe game with arrays, but our next step is to find illegal moves and cout illegal move. We also have to determine how to find a winner. any help would be appreciated!!

This is what i have so far.


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
#include <iostream>

using namespace std;

void updateBoard(char[], int, int);

int main() {
    int move, turn = 0;
    char board[] = " | | \n-----\n | | \n-----\n | | \n";

    cout << board;

    for (int i = 0; i < 9; ++i) {
        cout << "Player" << turn + 1 << " enter your move: ";
        cin >> move;

        updateBoard(board, move, turn);

        turn ^= 1;
    }
}

void updateBoard(char board[], int move, int turn) {
    int index = ((move / 10) - 1) * 12 + ((move % 10) - 1) * 2;
    board[index] = turn ? 'O' : 'X';
    system("cls");
    cout << board;
}
Last edited on
Please edit for readability
https://www.cplusplus.com/articles/jEywvCM9/
How would you do this if playing using paper/pen? How would you tell some else how to check for a winner etc? Write down exactly how this would be done so that someone else who knows nothing about TicTacToe can follow it and undertake the process correctly.

The first part of programming is to design the program - not coding! Only when you have a program design do you then start to code the program from the design. The design includes the processes needed to do what is required - such as determining if/who is the winner. If you can't specify how something is to be done, then you can't code it - the program just does what it's told!

Once you have the design then if you have issues converting to code, then we'll be able to advise on this.

@ccorkran,
Try something like this:
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
// for a two-dimensional array, 
// gameboard[3][3],
//
// 1  2  3
// 4  5  6
// 7  8  9
//
// then...
// This is to check for a top-row win.
if (gameboard[0][0] == gameboard[0][1] && gameboard[0][0] == gameboard[0][2] && gameboard[0][0] != '1')
{
	if (gameboard[0][0] == 'X')
	{
		xWin = true;
	}
	else if (gameboard[0][0] == 'O')
	{
		oWin = true;
	}
}
// Add more if statements to check for each row, column, and diagonal wins.
// You will need 7 more if statements to check all of them.

// xWin and oWin are booleans that cause another function to output "X wins!" or "O wins!" respectively.


Good luck,
max
Thank you all! I’ll be back to let you know how it goes!
I think I did it with a 1d array

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
#include <iostream>

using namespace std;

void updateBoard(char[], int, int);

int main() {
    int move, turn = 0;
    bool finishGame = false;
    char gameBoard[] = " | | \n-----\n | | \n-----\n | | \n";

    cout << gameBoard;

    for (int i = 0; i < 9; ++i) {
        cout << "Player" << turn + 1 << " enter your move: ";
        cin >> move;
        updateBoard(gameBoard, move, turn);

        if (gameBoard[0] == gameBoard[2] && gameBoard[2] == gameBoard[4] && gameBoard[0] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[12] == gameBoard[14] && gameBoard[14] == gameBoard[16] && gameBoard[12] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[24] == gameBoard[26] && gameBoard[26] == gameBoard[28] && gameBoard[24] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[0] == gameBoard[12] && gameBoard[12] == gameBoard[24] && gameBoard[0] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[2] == gameBoard[14] && gameBoard[14] == gameBoard[26] && gameBoard[2] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[4] == gameBoard[16] && gameBoard[16] == gameBoard[28] && gameBoard[4] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[24] == gameBoard[14] && gameBoard[14] == gameBoard[4] && gameBoard[4] != ' ') {
            finishGame = true;
        }
        else if (gameBoard[28] == gameBoard[14] && gameBoard[14] == gameBoard[0] && gameBoard[0] != ' ') {
            finishGame = true;
        }

        if (finishGame) {
            cout << "Player" << turn + 1 << " has won the game!" << endl;
            break;
        }
        else if (!finishGame && i == 8) {
            cout << "The game is drawn." << endl;
        }
        else {
            turn ^= 1;
        }
    }

    return 0;
}

void updateBoard(char board[], int move, int turn) {
    int index = ((move / 10) - 1) * 12 + ((move % 10) - 1) * 2;

    while (board[index] != ' ') {
        cout << "That was an illegal move!" << endl;
        cout << "Player" << turn + 1 << " enter a legal move: ";
        cin >> move;
        index = ((move / 10) - 1) * 12 + ((move % 10) - 1) * 2;
    }

    board[index] = turn ? 'O' : 'X';
    system("cls");
    cout << board;
}
Topic archived. No new replies allowed.