-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·420 lines (407 loc) · 20.2 KB
/
index.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<html>
<head>
<title>Othello</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
#applet{
width: 400px;
background-color: #EEE;
text-align: center;
}
#gameState{
text-align: center;
}
#board{
margin-top: 5%;
padding-bottom: 5%;
}
#LEFT, #CENTER, #RIGHT{
display: inline;
padding-left: 5px;
padding-right: 5px;
}
#LEFT{
float:left;
padding-right: 15%;
}
#RIGHT{
float:right;
padding-left: 15%;
}
button{
margin: 1px;
padding: 5%;
}
</style>
</head>
<body>
<h1>Othello</h1>
<h2>CS 201 Final Project 12/M3/2007</h2>
<h3>By Kazuaki Okumura & Michael Luby</h3>
<h2>Ported to JavaScript 7/T17/2014</h2>
<h3>by Michael Luby</h3>
<div id="applet"></div>
<div style="text-align: left"><b><a href="Othello.java">Two Player Code</a> - <a href="Othello2.java">Player Vs CPU Code</a></b></div>
<h1 style="color:red;"><script type="text/javascript">
if (navigator.appVersion.indexOf("Mac")!=-1) document.write("Java applets don't work on Mac browsers. Hopefully they'll fix that soon...");
</script></h1>
<h2>Two Player</h2>
<applet code="Othello.class" width=600 height=600></applet>
<h2>Player vs CPU</h2>
<applet code="Othello2.class" width=600 height=600></applet>
<script>
// Othello Applet (graphics code adapted from Postfix Calculator Applet
// 12/M3/2007
// CS 201 Final Project - Kazuaki Okumura & Michael Luby
$(document).ready(function(){
//***numRanks and numFiles control the dimensions of the gameboard
//***the four starting pieces are initialized in "initializeGame()"
//***the computer player can be set to black(1), white(2) or off (!=1,2)
//***by changing the integer value in "if(currentPlayer == 2)" in "placeTile()"
// instance variables
var player1sum; // label used to show player 1's total tiles
var gameState; // label used to show game state
var player2sum; // label used to show player 2's total tiles
var numRanks = 8; // ranks are horizontal
var numFiles = 8; // files are vertical
var currentPlayer; //1 = black, 2 = white
var boardArray = [];
var tempBoard= [];
var flippable; //boolean
// local color constants
var black = "black";
var white = "white";
var green = "green";
var dgreen = "gray";
//code to set up the applet, including layout
var applet = $("#applet");
applet.css("background-color", "black");
applet.append("<div id='gameState'>");
gameStatePanel();
applet.append("<div id='board'>");
initializeGame();
function gameStatePanel(){
//dynamic text fields that display the state of the game (in progress, black won,
// white won, draw) and sum of player 1's tiles, sum of player 2's tiles
var resultPanel = $("#gameState");
resultPanel.css("background-color",dgreen);
resultPanel.append("<div id='LEFT'>");
resultPanel.append("<div id='CENTER'>");
resultPanel.append("<div id='RIGHT'>");
//displays the sum of player 1 (black)'s tiles in the upper left
player1sum = $("#LEFT");
//player1sum.css("fontTimesRoman", Font.BOLD, 30));
player1sum.css("background", white);
player1sum.css("color", black);
player1sum.text("Black: ");
//displays game state in upper center: in progress, black won, white won, or draw)
gameState = $("#CENTER");
//gameState.setFont(new Font("TimesRoman", Font.BOLD, 30));
gameState.css("background", dgreen);
gameState.css("color", green);
gameState.text("Black's Turn");
//displays the sum of player 2 (white)'s tiles in the upper right
player2sum = $("#RIGHT");
//player2sum.setFont(new Font("TimesRoman", Font.BOLD, 30));
player2sum.css("background", black);
player2sum.css("color", white);
player2sum.text("White: ");
}
function board(){
//creates the main board
var centerPanel = $("#board");
console.log('called twice?');
for (var i = 0; i < numRanks; i++) {
boardArray[i] = [];
for (var j = 0; j < numFiles; j++) {
boardArray[i][j] = 0;
centerPanel.append("<button id='" + i +""+ j+"' >")
$("#"+i+""+j+"").css({"background": dgreen, "color": dgreen});
}
}
// Hide duplicated #gameState, #board duplicated by JS VM.
$("button:not([style])").remove();
// $("#board").next().remove();
// $("#board").next().remove();
}
function initializeGame(){
//places the starting four pieces, and sets currentPlayer = 1 (black)
currentPlayer = 1;
board();
boardArray[3][3] = 1;
boardArray[3][4] = 2;
boardArray[4][3] = 2;
boardArray[4][4] = 1;
sumTiles(1);
sumTiles(2);
rePaint();
}
//-----------------------------Button Methods---------------------------------
$("button").click(function(){
//This method handles button clicks
var id = $(this).attr("id");
var c = id.charAt(0);
var k = id.charAt(1);
var rank = Number(c);
var file = Number(k);
if (boardArray[rank][file] == 0) {
placeTile(rank, file);
}
});
//-----------------------------Functionality Methods--------------------------
function placeTile(rank, file){
//places a tile (black or white) depending on the currentPlayer)
//at position (rank, file), and toggles currentPlayer
if (moveExists(rank, file)) {
boardArray[rank][file] = currentPlayer;
flipTilesFrom(rank, file);
rePaint();
sumTiles(currentPlayer);
checkVictory();
togglePlayer();
}
if (currentPlayer == 2) {
try {
sleep(1000, computerPlay); //sleep for 1.000 seconds
} catch (ie) {}
}
}
function sleep(millis, callback) {
setTimeout(function(){ callback(); }, millis);
}
function rePaint(){
for (var i = 0; i < numRanks; i++) {
for (var j = 0; j < numFiles; j++) {
var centerPanel = $("#"+i+""+j+"");
if (boardArray[i][j] == 1) {
centerPanel.css("background-color", black);
centerPanel.css("color", black);
}
if (boardArray[i][j] == 2) {
centerPanel.css("background-color", white);
centerPanel.css("color", white);
}
}
}
}
function togglePlayer(){
if (currentPlayer == 1) {
currentPlayer = 2;
gameState.text("White's Turn");
} else {
currentPlayer = 1;
gameState.text("Black's Turn");
}
}
function checkVictory(){
//if the game is over, declare a winner (or a draw)
if (sumTiles(1) + sumTiles(2) == numRanks * numFiles) { //Game Over
if (sumTiles(1) == sumTiles(2)) {
gameState.text("Draw...");
} else if (sumTiles(1) > sumTiles(2)) {
gameState.text("Black Wins!");
} else {
gameState.text("White Wins!");
}
}
}
function sumTiles(currentPlayer){
//determine the total number of tiles of currentPlayer's color on the board
var playerSum = 0;
for (var i = 0; i < numRanks; i++) {
for (var j = 0; j < numFiles; j++) {
if (boardArray[i][j] == currentPlayer) {
playerSum++;
}
}
}
if (currentPlayer == 1) {
player1sum.text("Black: " + playerSum);
} else {
player2sum.text("White: " + playerSum);
}
return playerSum;
}
function moveExists(rank, file){
// checks to see if the currentPlayer has a valid move, IE placing a piece would
// result in at least one flip for currentPlayer
var tempBoard = [];
for (var i = 0; i < numRanks; i++) {
tempBoard[i] = [];
for (var j = 0; j < numFiles; j++) {
tempBoard[i][j] = boardArray[i][j];
}
}
flippable = false;
flipTilesFrom(rank, file);
for (var i = 0; i < numRanks; i++) {
for (var j = 0; j < numFiles; j++) {
boardArray[i][j] = tempBoard[i][j];
}
}
if (flippable == true) {
return true;
} else {
return false;
}
}
function computerPlay(){
//searches (exhaustively) for greatest number of tiles flipped, then makes that move
var originalSum = sumTiles(currentPlayer);
var bestGain = 0; // best currentSum-originalSum
var bestRank = 0; // best move rank
var bestFile = 0; // best move file
for (var i = 0; i < numRanks; i++) {
for (var j = 0; j < numFiles; j++) {
console.log("computer playing "+i+","+j);
//create temp board from original board
var tempBoard = [];
for (var k = 0; k < numRanks; k++) {
tempBoard[k] = [];
for (var l = 0; l < numFiles; l++) {
tempBoard[k][l] = boardArray[k][l];
}
}
console.log("created tempBoard");
//hypothetical: more tiles flipped by playing here?");
flipTilesFrom(i, j);
if (sumTiles(currentPlayer) - originalSum > bestGain) {
bestGain = sumTiles(currentPlayer) - originalSum;
bestRank = i;
bestFile = j;
}
console.log("bestGain so far: "+bestGain+" at "+bestRank+","+bestFile);
//restore original board from temp board
for (var m = 0; m < numRanks; m++) {
for (var n = 0; n < numFiles; n++) {
boardArray[m][n] = tempBoard[m][n];
}
}
console.log("restored board");
}
}
console.log("best gain is "+bestGain+" at "+bestRank+","+bestFile);
placeTile(bestRank, bestFile);
}
function flipTilesFrom(rank, file){
//toggles a tile's color at position (rank, file)
var newRank;
var newFile;
//Flip Eastward
newRank = rank;
newFile = file + 1;
while (newRank != numRanks && newFile != numFiles && newRank != -1 && newFile != -1 && boardArray[rank][newFile] != currentPlayer && boardArray[rank][newFile] != 0) {
newFile = newFile + 1;
if (boardArray[rank][newFile] == currentPlayer) {
for (var i = newFile - 1; i != file; i--) {
boardArray[rank][i] = currentPlayer;
flippable = true;
}
}
}
//Flip Southward
newRank = rank + 1;
newFile = file;
while (newRank != numRanks && newFile != numFiles && newRank != -1 && newFile != -1 && boardArray[newRank][file] != currentPlayer && boardArray[newRank][file] != 0) {
newRank = newRank + 1;
if (boardArray[newRank][file] == currentPlayer) {
for (var i = newRank - 1; i != rank; i--) {
boardArray[i][file] = currentPlayer;
flippable = true;
}
}
}
//Flip Westward
newRank = rank;
newFile = file - 1;
while (newRank != numRanks && newFile != numFiles && newRank != -1 && newFile != -1 && boardArray[rank][newFile] != currentPlayer && boardArray[rank][newFile] != 0) {
newFile = newFile - 1;
if (boardArray[rank][newFile] == currentPlayer) {
for (var i = newFile + 1; i != file; i++) {
boardArray[rank][i] = currentPlayer;
flippable = true;
}
}
}
//Flip Northward
newRank = rank - 1;
newFile = file;
while (newRank != numRanks && newFile != numFiles && newRank != -1 && newFile != -1 && boardArray[newRank][file] != currentPlayer && boardArray[newRank][file] != 0) {
newRank = newRank - 1;
if (boardArray[newRank][file] == currentPlayer) //<-- Occasionally OutOfBounds
{
for (var i = newRank + 1; i != rank; i++) {
boardArray[i][file] = currentPlayer;
flippable = true;
}
}
}
//Flip Southeastward
//South East
newRank = rank + 1;
newFile = file + 1;
while (newRank < numRanks && newFile < numFiles && newRank >= 1 && newFile >= 1 && boardArray[newRank][newFile] != currentPlayer && boardArray[newRank][newFile] != 0) {
newRank = newRank + 1;
newFile = newFile + 1;
gameState.text("e@" + newRank + "," + newFile);
if (boardArray[newRank][newFile] == currentPlayer) //<-- Occasionally OutOfBounds
{
for (var i = newRank - 1; i != rank; i--) {
newFile = newFile - 1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Northeastward
newRank = rank - 1;
newFile = file + 1;
while (newRank < numRanks && newFile < numFiles && newRank >= 1 && newFile >= 1 && boardArray[newRank][newFile] != currentPlayer && boardArray[newRank][newFile] != 0) {
newRank = newRank - 1;
newFile = newFile + 1;
gameState.text("e@" + newRank + "," + newFile);
if (boardArray[newRank][newFile] == currentPlayer) //<-- Occasionally OutOfBounds
{
for (var i = newRank + 1; i != rank; i++) {
newFile = newFile - 1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Northwestward
newRank = rank - 1;
newFile = file - 1;
while (newRank < numRanks && newFile < numFiles && newRank >= 1 && newFile >= 1 && boardArray[newRank][newFile] != currentPlayer && boardArray[newRank][newFile] != 0) {
newRank = newRank - 1;
newFile = newFile - 1;
gameState.text("e@" + newRank + "," + newFile);
if (boardArray[newRank][newFile] == currentPlayer) //<-- Occasionally OutOfBounds
{
for (var i = newRank + 1; i != rank; i++) {
newFile = newFile + 1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
//Flip Southwestward
newRank = rank + 1;
newFile = file - 1;
while (newRank < numRanks && newFile < numFiles && newRank >= 1 && newFile >= 1 && boardArray[newRank][newFile] != currentPlayer && boardArray[newRank][newFile] != 0) {
newRank = newRank + 1;
newFile = newFile - 1;
gameState.text("e@" + newRank + "," + newFile);
if (boardArray[newRank][newFile] == currentPlayer) {
for (var i = newRank - 1; i != rank; i--) {
newFile = newFile + 1;
boardArray[i][newFile] = currentPlayer;
flippable = true;
}
}
}
}
});
</script>
</body>
</html>