-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.ts
164 lines (132 loc) · 4.68 KB
/
board.ts
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
/**
* board.ts
*
* class Board
* class Square
*/
interface BoardInterface {
game: Game;
}
interface SquareInterface {
readonly id : string;
readonly bonus : number;
readonly row : number;
readonly column : number;
board : Board;
tile : LetterTile;
}
class Board implements BoardInterface {
public game: Game = null;
/*
squares
0 = standard (no bonus)
1 = double letter
2 = triple letter
3 = double word
4 = triple word
*/
public static bonuses: NumberMatrix = [
[4,0,0,1,0,0,0,4,0,0,0,1,0,0,4],
[0,3,0,0,0,2,0,0,0,2,0,0,0,3,0],
[0,0,3,0,0,0,1,0,1,0,0,0,3,0,0],
[1,0,0,3,0,0,0,1,0,0,0,3,0,0,1],
[0,0,0,0,3,0,0,0,0,0,3,0,0,0,0],
[0,2,0,0,0,2,0,0,0,2,0,0,0,2,0],
[0,0,1,0,0,0,1,0,1,0,0,0,1,0,0],
[4,0,0,1,0,0,0,3,0,0,0,1,0,0,4],
[0,0,1,0,0,0,1,0,1,0,0,0,1,0,0],
[0,2,0,0,0,2,0,0,0,2,0,0,0,2,0],
[0,0,0,0,3,0,0,0,0,0,3,0,0,0,0],
[1,0,0,3,0,0,0,1,0,0,0,3,0,0,1],
[0,0,3,0,0,0,1,0,1,0,0,0,3,0,0],
[0,3,0,0,0,2,0,0,0,2,0,0,0,3,0],
[4,0,0,1,0,0,0,4,0,0,0,1,0,0,4]
];
public squares: Square[][] = [];
public constructor() {
this.createSquares();
}
public createSquares() {
let row: number;
let column : number;
let rowData: Square[];
let newSquare: Square;
for(row = 0; row < 15; row++) {
rowData = [];
for(column = 0; column < 15; column++) {
newSquare = new Square(row, column);
newSquare.board = this;
rowData.push(newSquare);
}
this.squares.push(rowData);
}
}
public html(): string {
let boardView: string = "";
let row: number;
let column : number;
let currentSquare: Square;
for(row = 0; row < 15; row++) {
for(column = 0; column < 15; column++) {
currentSquare = this.squares[row][column];
boardView += currentSquare.html();
}
}
return '<div class="board">' + boardView + '</div>';
}
}
class Square implements SquareInterface {
readonly id : string;
readonly bonus : number;
readonly row : number;
readonly column : number;
public board : Board = null;
public tile : LetterTile = null;
public constructor(row, column) {
this.id = 'sq-' + row + '-' + column;
this.row = row;
this.column = column;
this.bonus = Board.bonuses[row][column];
}
public static allowDrop(ev) {
if(ev.target.childNodes.length == 0) {
ev.preventDefault();
}
}
public static drop(ev) {
ev.preventDefault();
let data: string = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
let squareIndex: Array<any> = ev.target.id.split("-");
let targetSquare: Square = (<any>window).scrabble.board.squares[squareIndex[1]][squareIndex[2]];
let currentPlayer: Player = (<any>window).scrabble.getCurrentPlayer();
for(let tileIndex = 0; tileIndex < currentPlayer.letters.length; tileIndex++) {
if(currentPlayer.letters[tileIndex].id == parseInt(data)) {
let droppedTile: LetterTile = currentPlayer.letters[tileIndex];
if(droppedTile.square != null) {
droppedTile.square.tile = null; // clear old square
}
targetSquare.tile = droppedTile;
droppedTile.square = targetSquare;
droppedTile.status = 2; // Mark as being played but not locked
break;
}
}
}
public html(): string {
let bgcolour: number;
let output: string;
switch(this.bonus) {
case 0: bgcolour = Colour.Grey; break;
case 1: bgcolour = Colour.LightBlue; break;
case 2: bgcolour = Colour.DarkBlue; break;
case 3: bgcolour = Colour.Pink; break;
case 4: bgcolour = Colour.Red; break;
case 5: bgcolour = Colour.Yellow; break;
}
output = '<div id="' + this.id + '" class="square" ondrop="Square.drop(event)" ondragover="Square.allowDrop(event)" style="background-color:' + Game.colours[bgcolour] + '">';
if(this.tile != null) output += this.tile.html();
output += '</div>';
return output;
}
}