/
Turn.js
200 lines (180 loc) · 4.56 KB
/
Turn.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
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
/*****************************
Variables
*******************************/
var totalTurns = 0;
var playerTurn = 0; //0 means positive (blue), 1 negative (red)
var gameOver = false;
//var canResetImg = false;
/******************************************
Turn Hanlders
********************************************/
/*
controls who's turn it is to make a move. also handles board display in turn change
*/
function changeTurn(){
if (playerTurn ==0) {
//if single player mode, just let the computer make a move
if (isSinglePlay){
setPlayerTurn(1);
//SetCanResetImg(false); //make sure visible units stay visible here
//RefreshBoard();
printScore();
//SetCanResetImg(true); //opponent's visible units can be set back to "?"
//Wait half a second before letting the computer start making its move
setTimeout("ComputerTurn()", 500);
//ComputerTurn();
}
else{ //multiplayer mode. User will now have to wait for friend's move
setPlayerTurn(1);
//SetCanResetImg(false); //make sure visible units stay visible here
if(!isPlayer1) {
nextTurn();
}
//RefreshBoard();
printScore();
}
//RefreshBoard();
}
else {
//Wait a little over half a second. The opponent has already moved, but there is a 500 ms delay so that the user can better see the move
//SetCanResetImg(true); //opponent's visible units can be set back to "?"
setTimeout("MultiPlayerMyTurn()", 550);
}
//RefreshBoard();
//nextTurn();
};
/*
Called when the human player just made a move in single player mode
Computer makes it's move, board is refreshed
*/
function ComputerTurn(){
ForgetUnitRanks(); //AIDifficulty.js, will cause AI to forget some opposing units' ranks
decideMove();
//wait another half a second. The computer made it's move, but takes 500 ms to highlight it's unit and move for the person to see
if (!isGameOver()){
setTimeout("FinishComputerTurn()", 550); //The board is refreshed after move.
}
else {
//if the computer isnt out of turns, it caught the flag.
if (lblWarning.innerText != strCompSurrender) DisplayWarning(strCompCapture);
DisplayEndBoard();
}
};
/*
After the computer has finally made its move, time to refresh the board, go to next turn
*/
function FinishComputerTurn(){
//chack to make sure the human player can still move
nextTurn();
if (!CanMove()){
//Todo: handle flag capture case
setGameOver(1); //The game is over, no more turns
//alert(strYouCantMove);
DisplayWarning(strYouCantMove);
DisplayEndBoard();
}
else{
RefreshBoard();
printScore();
}
setPlayerTurn(0);
};
/*
Called in multiplayer mode, when ready to switch turn back to local machine
User is no longer waiting, but ready to go.
*/
function MultiPlayerMyTurn(){
/*if (isGameOver()){
//setGameOver(1); //The game is over, no more turns
alert(strFriendWon);
DisplayEndBoard();
}*/
if (TestCanMove()){
if (isPlayer1){
nextTurn();
}
RefreshBoard();
printScore();
}
setPlayerTurn(0);
};
function TestCanMove(){
if (!CanMove()){
//Todo: handle flag capture case
setGameOver(1); //The game is over, no more turns
//alert(strYouCantMove);
DisplayWarning(strYouCantMove);
try{
googleTalk.SendTalkData(friend_user_id, msgNoMoves);
} catch (e){
return 0;
}
DisplayEndBoard();
return 0;
}
return 1;
};
/*
Increment turns
*/
function nextTurn(){
totalTurns++;
};
/********************************************
Utilities
********************************************/
/*
returns the total number of turns in this game so far
*/
function getTotalTurns(){
return totalTurns;
};
/*
sets the turn to the provided player
*/
function setPlayerTurn(turn){
playerTurn = turn;
var items = boardArea.contentItems.toArray();
if (items.length > 102){
if (playerTurn == 0){
items[102].heading = strGo;
}
else if (playerTurn == 1){
items[102].heading = strWait;
}
else {
//alert("display game over note");
items[102].heading = strGameOver;
items[102].setRect(0,0,70,textheight);
}
}
};
function SetStartTurn(){
if (isSinglePlay) playerTurn = 0;
else if (isPlayer1) playerTurn = 0;
else playerTurn = 1;
};
function getPlayerTurn(){
return playerTurn;
};
/*
sets the status of the game, if it is over or still running
*/
function setGameOver(over){
gameOver = over;
};
/*
These are used to justify setting an opponent's visible unit back to "?"
*/
/*function SetCanResetImg(val){
canResetImg = val;
}
function GetCanResetImg(){
return canResetImg;
}*/
/*
returns whether or not the game is over (or still running)
*/
function isGameOver(){
return gameOver;
}