Cell.cpp 3.08 KB
Newer Older
Jason Riggs's avatar
change  
Jason Riggs committed
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
#include <iostream>
#include <stdexcept>
#include <vector>
#include <cassert>
#include "Cell.h"
using namespace std;

bool Cell::isLegalPlacementfor(int x, int y, char board[][8], char playDisc, char oppDisc) {
    vector<Directions> directions;

    Cell current(x - 1, y - 1);
    
    if (current.isOccupied(board, playDisc) || current.isOccupied(board, oppDisc)) {
        return false;
    }
    Cell north(x - 1, y - 2);
    if (north.isOccupied(board, oppDisc)) {
        directions.push_back(north.NORTH);
    }
    Cell northEast(x, y - 2);
    if (northEast.isOccupied(board, oppDisc)) {
        directions.push_back(northEast.NORTHEAST);
    }
    Cell east(x, y - 1);
    if (east.isOccupied(board, oppDisc)) {
        directions.push_back(EAST);

    }
    Cell southEast(x, y);
    if (southEast.isOccupied(board, oppDisc)) {
        directions.push_back(SOUTHEAST);

    }
    Cell south(x - 1, y);
    if (south.isOccupied(board, oppDisc)) {
        directions.push_back(SOUTH);

    }
    Cell southWest(x - 2, y);
    if (southWest.isOccupied(board, oppDisc)) {
        directions.push_back(SOUTHWEST);

    }
    Cell west(x - 2, y - 1);
    if (west.isOccupied(board, oppDisc)) {
        directions.push_back(WEST);
    }
    Cell northWest(x - 2, y - 2);
    if (northWest.isOccupied(board, oppDisc)) {
        directions.push_back(NORTHWEST);

    }
Jason Riggs's avatar
change    
Jason Riggs committed
53
54
55
    // for (int i = 0; i < directions.size(); i++) {
    //     cout << directions.at(i) << endl;
    // }
Jason Riggs's avatar
change  
Jason Riggs committed
56
57
58
59
60
61
62
63
64
    for (auto direction : directions) {
        
        try{
            int value = current.sequenceLength(direction, board, oppDisc, "");
            if (value > 0) {
                return true;
            }
        }
        catch(runtime_error &excpt) {
Jason Riggs's avatar
change    
Jason Riggs committed
65
            //cout << excpt.what() << endl;
Jason Riggs's avatar
change  
Jason Riggs committed
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
        }
    }
    return false;
}

int Cell::sequenceLength(Directions direction, char board[][8], char playDisc, string indent) {
    int x1, y1;
    switch (direction) {
        case NORTH:
            x1 = x;
            y1 = y - 1;
            break;
        case NORTHEAST:
            x1 = x + 1;
            y1 = y - 1;
            break;
        case EAST:
            x1 = x + 1;
            y1 = y;
            break;
        case SOUTHEAST:
            y1 = y + 1;
            x1 = x + 1;
            break;
        case SOUTH:
            x1 = x;
            y1 = y + 1;
            break;
        case SOUTHWEST:
            y1 = y + 1;
            x1 = x - 1;
        case WEST:
            x1 = x - 1;
            y1 = y;
            break;
        case NORTHWEST:
            y1 = y - 1;
            x1 = x - 1;
            break;
        default:
            exit(1);
    }

    if (x1 < 0 || x1 >= 8 || y1 < 0 || y1 >= 8) {
        throw runtime_error("Invalid Sequence");
    }
    Cell sequence(x1, y1);
    if (board[x1][y1] == ' ') {
        throw runtime_error("Invalid Input");
    }

    if (board[x1][y1] != playDisc) {
        return 0;
    }
    int result = (1 + sequence.sequenceLength(direction, board, playDisc, "   " + indent));
    return result;
}

bool Cell::isOccupied(char board[][8], char disc) {
    return (board[x][y] == disc);
}