tic tac toe double error

I started writing a two players tictactoe program, even if the program isn't complete i have an idea of how to write in grids and how to check winners(i check for 3 same input in a horizontal, vertical or oblique line).
What i can't get is how to get error if i write on a grid where i already gave an input.
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
#include <iostream>
#include <string>
using namespace std;

int main (){
int x;
int y;
string gr1="1";
string gr2="2";
string gr3="3";
string gr4="4";
string gr5="5";
string gr6="6";
string gr7="7";
string gr8="8";
string gr9="9";
cout << gr1 << "|" << gr2 << "|" << gr3 << "\n" << gr4 << "|" << gr5 << "|" << gr6 << "\n" << gr7 << "|" << gr8 << "|" << gr9 << endl;
cout << "player 1 you are X, select" << endl;
gr1=" ";
gr2=" ";
gr3=" ";
gr4=" ";
gr5=" ";
gr6=" ";
gr7=" ";
gr8=" ";
gr9=" ";
cin >> x;
switch(x){
case 1: gr1="X";
break;
case 2: gr2="X";
break;
case 3: gr3="X";
break;
case 4: gr4="X";
break;
case 5: gr5="X";
break;
case 6: gr6="X";
break;
case 7: gr7="X";
break;
case 8: gr8="X";
break;
case 9: gr9="X";
break;
}
cout << gr1 << "|" << gr2 << "|" << gr3 << "\n" << gr4 << "|" << gr5 << "|" << gr6 << "\n" << gr7 << "|" << gr8 << "|" << gr9 << endl;
cout << "payer 2 you are O, select" << endl;
cin >> y;
switch(y){
case 1: gr1="O";
break;
case 2: gr2="O";
break;
case 3: gr3="O";
break;
case 4: gr4="O";
break;
case 5: gr5="O";
break;
case 6: gr6="O";
break;
case 7: gr7="O";
break;
case 8: gr8="O";
break;
case 9: gr9="O";
break;
}
cout << gr1 << "|" << gr2 << "|" << gr3 << "\n" << gr4 << "|" << gr5 << "|" << gr6 << "\n" << gr7 << "|" << gr8 << "|" << gr9 << endl;
}
You can look at my old tic tac toe game state checking algorithm.
Note, that it is horribly written and possibly inefficient:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum fieldelement {O = -1, NONE, X, TIE};

fieldelement checkGameState(fieldelement const (&board)[3][3])
{
    fieldelement winner = TIE;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; ++j)
            if(board[i][j] == NONE)
                winner = NONE;
    for(int i = 0; i < 3; ++i)
        if ( ((board[i][0]==board[i][1])&&(board[i][0]==board[i][2])) ||
             ((board[0][i]==board[1][i])&&(board[0][i]==board[2][i])) ) {
            winner = board[i][i];
            break;
        }
    if ( ((board[1][1]==board[0][0])&&(board[1][1]==board[2][2])) ||
         ((board[1][1]==board[0][2])&&(board[1][1]==board[2][0])) )
        winner = board[1][1];
    return winner;
}


What i can't get is how to get error if i write on a grid where i already gave an input.
And you can have that code (all said about previous code is valid here too):
1
2
3
4
5
6
7
8
9
10
11
12
13
void do_turn(const fieldelement player, fieldelement (&board)[3][3])
{
    std::cout << "Your move '" << getSymbol(player) << "'" << std::endl;
    int x, y;
    while(true) {
        std::cin >> x >> y;
        if(0 < x && x < 4 && 0 < y && y < 4)
            if(board[x-1][y-1] == NONE)
                break;
        std::cout << "Wrong move, try again" << std::endl;
    }
    board[x-1][y-1] = player;
}
Last edited on
Topic archived. No new replies allowed.