-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
137 lines (123 loc) · 3.56 KB
/
Board.cpp
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
//
// Created by riham on 24/10/18.
//
#include "Board.h"
Board::Board() {
// init multiplier letter as 1
for (auto& i: multiplier_letter) {
for (auto& j: i) {
j = 1;
}
}
for (auto& i: multiplier_word) {
for (auto& j: i) {
j = 1;
}
}
for (auto &i: board) {
for (auto &j: i) {
j = -1;
}
}
}
void Board::initBoard() {
// update board with 3xL
for (auto &it: _3xL) {
multiplier_letter[it.first-1][it.second-1] = 3;
}
// update board with 2xL
for (auto &it: _2xL) {
multiplier_letter[it.first-1][it.second-1] = 2;
}
////////////////////////////////////////////
// update board with 3xW
// it triples the score of the current word
for (auto &it: _3xW) {
multiplier_word[it.first-1][it.second-1] = 3;
}
// update board with 2xW
// it doubles the score of the current word
for (auto it: _2xW) {
multiplier_word[it.first-1][it.second-1] = 2;
}
}
ostream& operator<<(ostream& os, Board const& myObj) {
for (auto &i : myObj.board) {
for (int j : i) {
os << char(j+'A') << " ";
}
os << endl;
}
return os;
}
bool Board::putFirstTie(int tie) {
if (board[starPos][starPos] != -1) return false;
board[starPos][starPos] = tie;
ties_count++;
return true;
}
bool Board::putTie(int posX, int posY, int tie) {
if (board[posX][posY] != -1 || !isValidMove(posX, posY, tie)) return false;
board[posX][posY] = tie;
ties_count++;
return true;
}
bool Board::putTieMove(int posX, int posY, int tie) {
if (board[posX][posY] != -1) return false;
board[posX][posY] = tie;
ties_count++;
return true;
}
bool Board::isValidMove(int posX, int posY, int tie) {
board[posX][posY] = tie;
string horizontalWord = getHorizontalWord(posX, posY), verticalWord = getVerticalWord(posX, posY);
cout << endl << horizontalWord << " " << verticalWord << endl;
// call the dictionary module to make sure that horizontal and vertical words are correct
// and make sure that the current tie is appended to at least one another tie
if ((horizontalWord.size() > 1 || verticalWord.size() > 1) && isValidWords(horizontalWord, verticalWord))
return true;
board[posX][posY] = -1;
return false;
}
string Board::getHorizontalWord(int posX, int posY) {
string horizontalWord;
int r = posX, c = posY;
// if it's a blank tie it should be handled to be replaced by a suitable char
while (c >= 0 && board[r][c] != -1) {
horizontalWord = char(board[r][c]+'A') + horizontalWord;
c--;
}
c = posY + 1;
while (c < BOARD_SIZE && board[r][c] != -1) {
horizontalWord += char(board[r][c]+'A');
c++;
}
return horizontalWord;
}
string Board::getVerticalWord(int posX, int posY) {
string verticalWord;
int r = posX, c = posY;
while (r >= 0 && board[r][c] != -1) {
verticalWord = char(board[r][c]+'A') + verticalWord;
r--;
}
r = posX+1;
while (r < BOARD_SIZE && board[r][c] != -1) {
verticalWord += char(board[r][c]+'A');
r++;
}
return verticalWord;
}
bool Board::isValidWords(string& horWord, string& verWord) {
// it calls the dictionary to make sure that these words exist in it
return true;
}
int Board::getMultiplierLetter(int posX, int posY) {
return multiplier_letter[posX][posY];
}
int Board::getMultiplierWord(int posX, int posY) {
return multiplier_word[posX][posY];
}
int Board::tiesCount() {
return ties_count;
}