-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomokuJS.html
155 lines (146 loc) · 4.41 KB
/
gomokuJS.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Gomoku (since 2017)</title>
<link rel="stylesheet" type="text/css" href="gomokuJS.css"/>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="gomokuJS.js"></script>
</head>
<body>
<div>
<div id="header"></div>
<div id="main">
<img id='board' src='images/board.png'></img>
<img id='pointer' src='images/pointer.png'></img>
<span id='gameover'></span>
</div>
<div id="panel">
<span id='control'>
You: Black<br/>
Machine: White<br/>
Move: Up, Down, Left, Right<br/>
Put: Space<br/>
Replay: Refresh the page<br/>
</span>
</div>
<div id="debug">
<span id="debugmsg"></span>
</div>
</div>
<script>
var msgBox = new MessageBox();
var board = new Board();
var COLOR = GLOBAL.color.BLACK;
var man = new Player('man');
var machine = new Player('machine');
function win(player, reason)
{
// window.alert(dir == 0 ? 'Black wins' : 'White wins');
var span = document.querySelector('#gameover');
if (player === "man") {
span.textContent = 'You win';
}
else {
span.textContent = 'Machine wins';
}
span.style.display = 'inline';
span.style.position = 'absolute';
span.style.left = span.parentElement.offsetWidth / 3 + 'px';
span.style.bottom = span.parentElement.offsetHeight * 2 / 3 + 'px';
}
function lose(dir, error)
{
var reason = "";
switch (error) {
case GLOBAL.boardStatus.PARAM_ERROR:
reason = "Parameter error";
break;
case GLOBAL.boardStatus.FORBIDDEN_LONG_CHAIN:
reason = "Long chain";
break;
case GLOBAL.boardStatus.FORBIDDEN_3_3:
reason = "3-3 forbidden";
break;
case GLOBAL.boardStatus.FORBIDDEN_4_4:
reason = "4-4 forbidden";
break;
default:
break;
}
window.alert((dir == 0 ? 'Black lose: ' : 'White lose: ') + reason);
}
function handleKeyPress(key)
{
var pointer = document.getElementById('pointer');
var background = document.getElementById('board');
var backgroundHeight = background.offsetHeight;
var backgroundWidth = background.offsetWidth;
var left = parseInt(pointer.style.left);
var bottom = parseInt(pointer.style.bottom);
if (key.code == 'ArrowUp') {
if (bottom + 32 < backgroundHeight) {
bottom += 32;
}
pointer.style.bottom = bottom + 'px';
}
else if (key.code == 'ArrowDown') {
if (bottom - 32 >= 5) {
bottom -= 32;
}
pointer.style.bottom = bottom + 'px';
}
else if (key.code == 'ArrowLeft') {
if (left - 32 >= 3) {
left -= 32;
}
pointer.style.left = left + 'px';
}
else if (key.code == 'ArrowRight') {
if (left + 32 < backgroundWidth) {
left += 32;
}
pointer.style.left = left + 'px';
}
else if (key.code == 'Space') {
var x = (left - 3) / 32;
var y = (bottom - 5) / 32;
if (man.go(x, y) != 0) {
return false;
}
var ret = board.judge(man.color, x, y);
if (ret == GLOBAL.boardStatus.WIN) {
win(man.name);
}
else if (ret == GLOBAL.boardStatus.FORBIDDEN_LONG_CHAIN) {
lose(man.name, ret);
}
var point = machine.defensiveMoveLevel1();
pointer.style.left = point.x * 32 + 3 + 'px';
pointer.style.bottom = point.y * 32 + 5 + 'px';
ret = board.judge(machine.color, point.x, point.y);
if (ret == GLOBAL.boardStatus.WIN) {
win(machine.name);
}
else if (ret == GLOBAL.boardStatus.FORBIDDEN_LONG_CHAIN) {
lose(machine.name, ret);
}
}
return false;
}
function main()
{
man.setColor(GLOBAL.color.BLACK);
man.associateBoard(board);
machine.setColor(GLOBAL.color.WHITE);
machine.associateBoard(board);
var pointer = document.getElementById('pointer');
var left = 3 + Math.floor(board.DIMENSION / 2) * 32;
var bottom = 5 + Math.floor(board.DIMENSION / 2 ) * 32;
pointer.style.left = left + 'px';
pointer.style.bottom = bottom + 'px';
document.body.addEventListener('keydown', handleKeyPress);
}
main();
</script>
</body>
</html>