-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
213 lines (164 loc) · 6.5 KB
/
Board.cs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Scrabble.Game_Logic {
class Board {
private Cell[,] grid;
private int SIZE;
int direction;
public Board(int size) {
SIZE = size;
grid = new Cell[SIZE,SIZE];
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
grid[row, col] = new Cell(row, col);
}
}
}
// CHANGE
// maybe set to current until move is solidified,
// just set to current for now
// create new method to solidify move
public bool playTile(Tile tile, int row, int col) {
if(!grid[row,col].IsOccupied && !grid[row, col].IsCurrent) {
grid[row, col].Tile = tile;
grid[row, col].IsCurrent = true;
return true;
}
return false;
}
public void solidifyPlay(LinkedList mainWord) {
LinkedList.Node head = new LinkedList.Node();
head = mainWord.Head;
while(head.next != null) {
grid[head.next.row, head.next.col].IsOccupied = true;
head = head.next;
}
}
// this needs to run every single time a tile is played to check for all valid words
public List<string> getWords(LinkedList mainWord, int direction) {
List<string> allWords = new List<string>();
LinkedList.Node head = mainWord.Head;
if(direction <= 2) {
// direction: 1 if vertical
// if vertical, run vertWord once and horizWord for as many letters
if (direction == 1) {
allWords.Add(vertWord(head.next.row, head.next.col));
while(head.next != null) {
allWords.Add(horizWord(head.next.row, head.next.col));
head = head.next;
}
}
// not vertical means horizontal
// run horizontal once and vertical for as many letters
else {
allWords.Add(horizWord(head.next.row, head.next.col));
while(head.next != null) {
allWords.Add(vertWord(head.next.row, head.next.col));
head = head.next;
}
}
}
return allWords;
}
// go all the way north until it hits a blank space or out of range
// then go all the way south and create a word out of it
public string vertWord(int row, int col) {
for(int x=row; x>-1; x--) {
if (!grid[x, col].IsOccupied && !grid[x,col].IsCurrent) {
row = x+1;
break;
}
if(x == 0) {
row = 0;
break;
}
}
LinkedList word = new LinkedList();
while (grid[row, col].IsOccupied || grid[row,col].IsCurrent) {
word.insertOrder(grid[row, col].Tile, row, col);
row++;
}
return word.toWord();
}
// go all the way west until it hits a blank space or out of range
// then go all the way east until it hits blank and create word
public string horizWord(int row, int col) {
for(int y = col; y>-1; y--) {
if(!grid[row, y].IsOccupied && !grid[row, y].IsCurrent) {
col = y + 1;
break;
}
if (y == 0) {
col = 0;
break;
}
}
LinkedList word = new LinkedList();
while (grid[row, col].IsOccupied || grid[row, col].IsCurrent) {
word.insertOrder(grid[row, col].Tile, row, col);
col++;
}
return word.toWord();
}
public int getDirection(LinkedList mainWord) {
if (mainWord.Head.next.next == null) {
direction = 1;
}
else {
LinkedList.Node head = mainWord.Head;
if(head.next.row == head.next.next.row && head.next.col != head.next.next.col) {
direction = 2;
}
else if(head.next.col == head.next.next.col && head.next.row != head.next.next.row) {
direction = 1;
}
else {
direction = 3;
}
}
return direction;
}
// have to run for each tile
public bool borders(int row, int col) {
if (grid[row - 1, col].IsOccupied || grid[row - 1, col].IsCurrent) {
return true;
}
else if (grid[row + 1, col].IsOccupied || grid[row + 1, col].IsCurrent) {
return true;
}
else if (grid[row, col - 1].IsOccupied || grid[row, col - 1].IsCurrent) {
return true;
}
else if (grid[row, col+1].IsOccupied || grid[row, col + 1].IsCurrent) {
return true;
}
return false;
}
public void returnTiles(LinkedList mainWord, Player player) {
LinkedList.Node head = mainWord.Head;
while (head.next != null) {
player.draw(head.next.data);
grid[head.next.row, head.next.col].IsOccupied = false;
grid[head.next.row, head.next.col].IsCurrent = false;
head = head.next;
}
// todo
}
// This is used for TESTING
public void printBoard() {
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
if (grid[row, col].IsOccupied || grid[row,col].IsCurrent) {
Console.Write($" {grid[row, col].Tile} ");
}
else {
Console.Write(" - ");
}
}
Console.WriteLine();
}
}
}
}