-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
120 lines (97 loc) · 2.08 KB
/
board.js
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
var fs = require("fs");
var wordList = fs.readFileSync("content/words.txt").toString().split(/\r?\n/);
var squareTeamList = [];
class Square {
constructor(team, word) {
this.team = team;
this.word = word;
this.selected = false;
}
}
class Board {
constructor() {
squareTeamList = [];
for (var i = 0; i < 10; i++)
{
squareTeamList.push(Board.RED);
squareTeamList.push(Board.BLUE);
}
for (var i = 0; i < 5; i++)
{
squareTeamList.push(0);
}
console.log(squareTeamList);
this.score = [];
this.score[Board.RED] = 0;
this.score[Board.BLUE] = 0;
this.size = 5;
this.board = this.create_board(this.size);
}
update_board(from){
Object.assign(this, from);
}
create_board(size) {
shuffle(wordList);
shuffle(squareTeamList);
var m = [];
for (var i = 0; i < size; i++) {
m[i] = [];
for (var j = 0; j < size; j++) {
m[i][j] = new Square(squareTeamList[i * size + j], wordList[i * size + j]);
}
}
return m;
}
select(i, j) {
var sq = this.board[i][j];
if (!sq.selected) {
sq.selected = true;
this.score[sq.team]++;
return true;
}
return false;
}
getSquare(i, j) {
return this.board[i][j];
}
}
Board.RED = 1;
Board.BLUE = 2;
module.exports = Board;
/*
//
//
//MAIN
//
//
var testBoard = new Board();
//
//
//LOGGING
//
//
testBoard.board.forEach(function(arr, index1){
arr.forEach(function(square, index2){
console.log("Square " + index1+index2 + ": " + square.word + " " + square.team);
})
});
*/
//
//
//UTILITY
//
//
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};