aboutsummaryrefslogtreecommitdiffstats
path: root/Gameboard.cpp
blob: 387e68dafbb863777dd7437b1a57c1fe4956aa48 (plain)
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
#include "Gameboard.h"

Gameboard::Gameboard() {
    for(int y=0;y<3;y++) {
        for(int x=0;x<3;x++) {
            _gameboard[y][x] = _defaultSymbol;
        }
    }
}

void Gameboard::printGameboard() {
    cout << "Game State:" << endl;
    cout << " ABC" << endl;
    for(int y=0;y<3;y++) {
        cout << y+1;
        for(int x=0;x<3;x++) {
            cout << _gameboard[y][x];
        }
        cout << endl;
    }
}

bool Gameboard::playMove(char symbol,int y,int x) {
    if(_gameboard[y-1][x-1] == _defaultSymbol) {
        _gameboard[y-1][x-1] = symbol;
        return true;
    }
    else {
        return false;
    }
}

bool Gameboard::checkWin(int playerID,char symbol) {
    string  horizontalTop,horizontalMid,horizontalBottom,
            verticalLeft,verticalMid,verticalRight,
            diagonalTopLeft,diagonalTopRight,symbolRow = "";

    symbolRow += symbol;
    symbolRow += symbol;
    symbolRow += symbol;

    horizontalTop += _gameboard[0][0];
    horizontalTop += _gameboard[0][1];
    horizontalTop += _gameboard[0][2];

    horizontalMid += _gameboard[1][0];
    horizontalMid += _gameboard[1][1];
    horizontalMid += _gameboard[1][2];

    horizontalBottom += _gameboard[2][0];
    horizontalBottom += _gameboard[2][1];
    horizontalBottom += _gameboard[2][2];

    verticalLeft += _gameboard[0][0];
    verticalLeft += _gameboard[1][0];
    verticalLeft += _gameboard[2][0];

    verticalMid += _gameboard[0][1];
    verticalMid += _gameboard[1][1];
    verticalMid += _gameboard[2][1];

    verticalRight += _gameboard[0][2];
    verticalRight += _gameboard[1][2];
    verticalRight += _gameboard[2][2];

    diagonalTopLeft += _gameboard[0][0];
    diagonalTopLeft += _gameboard[1][1];
    diagonalTopLeft += _gameboard[2][2];

    diagonalTopRight += _gameboard[0][2];
    diagonalTopRight += _gameboard[1][1];
    diagonalTopRight += _gameboard[2][0];

    if(horizontalTop == symbolRow) {
        return playerID;
    }
    else if(horizontalMid == symbolRow) {
        return playerID;
    }
    else if(horizontalBottom == symbolRow) {
        return playerID;
    }
    else if(verticalLeft == symbolRow) {
        return playerID;
    }
    else if(verticalMid == symbolRow) {
        return playerID;
    }
    else if(verticalRight == symbolRow) {
        return playerID;
    }
    else if(diagonalTopLeft == symbolRow) {
        return playerID;
    }
    else if(diagonalTopRight == symbolRow) {
        return playerID;
    }
    else {
        return 0;
    }
}