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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chess Clock 9000</title>
<script>
var SPACEBAR = 32;
var ESCAPE = 27;
var ARROWS = function(key) {
return (key >= 37 && key <= 40) || (key >= 63232 && key <= 63235)
};
var initial_time = 10 * 60*5;
var game = null;
function createGame() {
game = new Game(initial_time, 'info', 'player1clock', 'player2clock');
game.resetClocks()
}
formatTime = function(tenths) {
var minutes = String(Math.floor(tenths/600));
var seconds = String(Math.floor(tenths/10) % 60);
if (seconds.length == 1) { seconds = "0" + seconds;}
var decimal = String(tenths % 10);
return (minutes != '0') ?
(minutes + ":" + seconds) :
(seconds + '.' + decimal);
}
function Player(clock_id, initial_time, game) {
this.clock = document.getElementById(clock_id);
this.initial_time = initial_time;
this.time = this.initial_time;
this.game = game;
this.opponent = null;
}
Player.prototype.play = function() {
var opponent = this.opponent;
var game = this.game;
if (!game.game_over) {
clearTimeout(game.timer_loop);
game.timer_loop = setInterval(function(){
opponent.time -= 1;
if (opponent.time == 0) {
clearTimeout(game.timer_loop);
game.game_over = true;
game.info.innerHTML = 'GAME OVER';
game.info.className = '';
}
game.displayTimers();
}, 100);
opponent.clock.className = 'now_playing';
this.clock.className = '';
game.info.className = 'hidden';
}
}
function Game(initial_time, info_id, p1_clock_id, p2_clock_id) {
this.info = document.getElementById(info_id);
this.player1 = new Player(p1_clock_id, initial_time, this);
this.player2 = new Player(p2_clock_id, initial_time, this);
this.player1.opponent = this.player2;
this.player2.opponent = this.player1;
this.timer_loop = null;
this.game_over = false;
}
Game.prototype.keypress = function(e) {
var key = e.charCode || e.keyCode || 0;
var keychar = String.fromCharCode(key)
if ("qwertasdfgzxcvb".indexOf(keychar) != -1) {
this.player1.play(); }
else if (("yuiop[]\hjkl;'nm,./".indexOf(keychar) != -1)
|| ARROWS(key)) {
this.player2.play(); }
else if (key == SPACEBAR) {
this.pause(); }
else if (key == ESCAPE) {
this.resetClocks(); }
else if ("=+".indexOf(keychar) != -1) {
this.player1.initial_time += 300;
this.player2.initial_time += 300;
this.resetClocks(); }
else if ("-_".indexOf(keychar) != -1) {
if (this.player1.initial_time >= 600) {
this.player1.initial_time -= 300;
}
if (this.player2.initial_time >= 600) {
this.player2.initial_time -= 300;
}
this.resetClocks();
}
}
Game.prototype.pause = function() {
if (!this.game_over) {
var info = this.info;
info.innerHTML = 'PAUSED'
info.className = '';
clearTimeout(this.timer_loop);
this.timer_loop = setInterval(function(){
if (info.className == '') {
info.className = 'hidden';
}
else {
info.className = '';
}
}, 500);
}
}
Game.prototype.displayTimers = function() {
this.player1.clock.innerHTML = formatTime(this.player1.time);
this.player2.clock.innerHTML = formatTime(this.player2.time);
}
Game.prototype.resetClocks = function() {
clearTimeout(this.timer_loop);
this.player1.clock.className = '';
this.player2.clock.className = '';
this.info.className = 'hidden';
this.player1.time = this.player1.initial_time;
this.player2.time = this.player2.initial_time;
this.game_over = false;
this.displayTimers();
}
</script>
<style>
body {
background-color: #FFFFAA;
font-family: 'Trebuchet MS', sans-serif;
margin: 50px 50px;
font-size: 1000%;
}
#player1clock {
color: #000088;
float: left;
}
#player2clock {
color: #005500;
float: right;
}
.now_playing {
border-bottom: .2em solid #aa0000;
}
#info {
font-size: 80%;
color: #AA0000;
text-align: center;
width: 100%;
}
.hidden {
visibility: hidden;
}
</style>
</head>
<body style="" onkeypress="game.keypress(event)" onload="createGame()">
<div id="info" class="hidden">-</div>
<div id="player1clock">0</div>
<div id="player2clock">0</div>
</body>
</html>
|