-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbreakerCode.js
More file actions
173 lines (133 loc) · 4.93 KB
/
breakerCode.js
File metadata and controls
173 lines (133 loc) · 4.93 KB
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
//This function sets up the board for Breaker. It creates 5 rows,
//each of which contains 5 randomly chosen words. It also sets the button that
//allows the Breaker to end their turn.
function makeBreaker(){
turnOff(document.getElementsByName("ending"));
turnOn(document.getElementsByName("isBreaker"));
turnOn(document.getElementsByName("BreakerCounts"));
document.getElementById("endGameText").innerText="Would you like to play again?"
turnOn(document.getElementsByName("playing"));
document.getElementById("turn").style.width="200px"
document.getElementById("turn").style.borderColor="blue";
var BreakBoard=document.getElementById("BreakerBoard");
for(var i=0;i<5;i++){
newRow=document.createElement("TR");
for(var j=0;j<5;j++){
newRow.appendChild(pickWord());
}
BreakBoard.appendChild(newRow);
}
document.getElementById("turnText").innerText="It is "+playerNames['blue']+"'s turn."
document.getElementById("endTurn").onclick=function (){
BtoM.send(["turn",turn]);
makeTurn(turn);
}
}
//This function assigns the colors to the grid. The function creates a
//random permutation of [0,1,...,24], and assigns the first 9 words as blue
//the next 8 as red, the last as the assassin, all else as neutral.
function setColors(){
var wordElements=document.getElementsByName("wordBox");
var numbers = [];
for (var i = 0; i < wordElements.length; i++){
numbers.push(i);
}
var perms=permute(numbers);
//create onclick functions
for(var i=0;i<9;i++){
words["blue"].push(wordElements[perms[i]].innerText);
createClick("blue", wordElements[perms[i]]);
}
for(var i=9;i<17; i++){
words["red"].push(wordElements[perms[i]].innerText);
createClick("red", wordElements[perms[i]]);
}
for(var i=17;i<perms.length-1;i++){
words["grey"].push(wordElements[perms[i]].innerText);
createClick("grey", wordElements[perms[i]]);
}
//Automatically end the game if the assassin is clicked.
assassin=wordElements[perms[perms.length-1]].innerText;
wordElements[perms[perms.length-1]].onclick=function black(){
this.bgColor="black";
this.innerText="";
wins(players[(turn+1)%2]);
BreakerWin();
BtoM.send(["wins", players[(turn+1)%2]]);
}
createRemainingCount();
BtoM.send(["initialize",words,assassin]);
}
//Runs endgame; asks player if they want to play again, sets up new board
function BreakerWin(){
turnOff(document.getElementsByName("BreakerCounts"));
turnOff(document.getElementsByName("playing"));
turnOn(document.getElementsByName("ending"));
turnOn(document.getElementsByName("PlayAgain"));
document.getElementById("PlayYes").onclick=function(){
NewGame();
}
document.getElementById("PlayNo").onclick=function(){
BtoM.send("goodbye");
document.write("Goodbye!");
}
}
//Set Up New Game
function NewGame(){
reset();
turnOff(document.getElementsByName("PlayAgain"));
turnOff(document.getElementsByName("isBreaker"));
document.getElementById("endGameText").innerText="Waiting for Maker..."
breakerBoard = document.getElementById("BreakerBoard");
breakerBoard.innerHTML="";
breakerCount=document.getElementById("wordsRemaining");
breakerCount.innerHTML="";
}
//createClick adds the mechanism that interprets a selection on Breaker's
//board. It changes the color of the corresponding cells on Breaker's board,
//iterates the count, and sends relevant data to Maker to change color as well
function createClick(color, element){
element.onclick=function (){
if(this.innerText!=""){
BtoM.send([color,this.innerText]);
count[color]+=1;
this.bgColor=color;
this.innerText="";
document.getElementById(color+"Count").innerText = words[color].length-count[color];
//setTimeout is used here to ensure that the color on the board changes before
//the functions wins or endTurn run, which will pop up an alert
if(count[color]==words[color].length){
setTimeout(wins,100,color);
BtoM.send(["wins",color]);
}else if(players[turn]!=color){
setTimeout(endTurn,100);
}
}
}
}
//Adds the table of how many words are left on Breaker's board
function createRemainingCount(){
countWords = document.getElementById("wordsRemaining");
var Header = document.createElement("TR");
var headElement = document.createElement("TD");
headElement.innerText = "Words Remaining";
Header.appendChild(headElement);
countWords.appendChild(Header);
countWords.appendChild(countRow("blue","Blue"));
countWords.appendChild(countRow("red","Red"));
countWords.appendChild(countRow("grey","Grey"));
document.getElementById("greyCountRow").style.display="none"
}
//Rows for breaker count table
function countRow(color, capColor){
var newRow = document.createElement("TR");
newRow.setAttribute("id",color+"CountRow");
rowLabel=document.createElement("TD");
rowLabel.innerText = capColor;
rowCount = document.createElement("TD");
rowCount.setAttribute("id", color+"Count");
rowCount.innerText = words[color].length;
newRow.appendChild(rowLabel);
newRow.appendChild(rowCount);
return newRow;
}