-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay
223 lines (186 loc) · 4.33 KB
/
play
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
//play.js
//opening message
function welcome() {
console.log("Welcome. Moves are entered with a single number between 1 to 9.");
console.log("Please enter 'X' or 'O' to indicate which player you are.");
}
//set up
var Nethan = require("./nethan");
var nethan_description = "./nethan.json";
var nethan = new Nethan(nethan_description);
var BOARD_LENGTH = 9;
var cells = [BOARD_LENGTH];
var setting_up = true;
var x_to_play = true;
var game_over = false;
var number_of_moves = 0;
var MAX_NUMBER_MOVES = 9;
function empty() {
this.cell = "[ ]";
}
function x() {
this.cell = "[ X ]";
}
function o() {
this.cell = "[ O ]";
}
function init_board() {
for(var i = 0; i < BOARD_LENGTH; i++)
cells[i] = new empty();
}
//checking for victory
function check_for_victory(is_human) {
if(victory_in_row() || victory_in_col() || victory_in_diag()) {
if(is_human)
console.log("You win!");
else
console.log("Nethan wins!");
process.exit(0);
}
}
function victory_in_row() {
for(var row = 0; row < BOARD_LENGTH; row += 3) {
var x_victory = true, o_victory = true;
for(var col = 0; col < BOARD_LENGTH / 3; col++) {
var cell = cells[row + col];
if(cell instanceof x)
o_victory = false;
else if(cell instanceof o)
x_victory = false;
else
x_victory = o_victory = false;
}
if(x_victory || o_victory)
return true;
}
return false;
}
function victory_in_col() {
for(var row = 0; row < BOARD_LENGTH / 3; row++) {
var x_victory = true, o_victory = true;
for(var col = 0; col < BOARD_LENGTH; col += 3) {
var cell = cells[row + col];
if(cell instanceof x)
o_victory = false;
else if(cell instanceof o)
x_victory = false;
else
x_victory = o_victory = false;
}
if(x_victory || o_victory)
return true;
}
return false;
}
function victory_in_diag() {
if(cells[0].cell === cells[4].cell && cells[4].cell === cells[8].cell && !(cells[0] instanceof empty)) //first diag (1, 5, 9)
return true;
else if(cells[2].cell === cells[4].cell && cells[4].cell === cells[6].cell && !(cells[2] instanceof empty)) //second diag (3, 5, 7)
return true;
else
return false;
}
//validation
function not_empty_at(input) {
return !(cells[input - 1] instanceof empty);
}
function valid_input(input) {
if(not_empty_at(input))
return false;
else
return true;
}
//modification
function update_board(input) {
if(x_to_play)
cells[input - 1] = new x();
else
cells[input - 1] = new o();
}
//io
var stdin = process.stdin;
var input = "";
var symbol_expression = /^[XO]$/;
var move_expression = /^[1-9]$/;
function select_symbol(input) {
if(!symbol_expression.test(input)) {
console.log("Please choose to play either 'X' or 'O'.\n");
process.exit(0);
}
if(input == "X")
x_to_play = true;
else
x_to_play = false;
nethan.set_symbol(!x_to_play);
setting_up = false;
console.log("Now enter your first move.");
}
function add_players() {
welcome();
stdin.addListener("data", callback);
}
function callback(d) {
input = d.toString().trim();
if(setting_up)
select_symbol(input);
var message = "";
if(!move_expression.test(input)) {
message = "Please enter a number between 1 and 9, and press enter.\n";
print_board(message);
return;
}
input = input.replace(/ /g, "");
if(valid_input(input)) {
update_board(input);
number_of_moves++;
print_board(message);
check_for_victory(true);
if(number_of_moves == MAX_NUMBER_MOVES) {
console.log("It's a draw!");
process.exit();
}
}
else
message = "Invalid move. Please try again.\n";
cells = convert_from(nethan.makeMove(cells));
number_of_moves++;
print_board(message);
check_for_victory(false);
}
function convert_from(nethan_response) {
for(var i = 0; i < BOARD_LENGTH; i++) {
if(nethan_response[i] >= 0.33333 && nethan_response[i] <= 0.66666)
cells[i] = new empty();
else if(x_to_play) {
if(nethan_response[i] < 0.33333)
cells[i] = new o();
else
cells[i] = new x();
}
else
if(nethan_response[i] < 0.33333)
cells[i] = new x();
else
cells[i] = new o();
}
return cells;
}
function print_board(message) {
var line = "";
for(var i = 0; i < BOARD_LENGTH; i++) {
line += cells[i].cell + " ";
if((i + 1) % 3 == 0) {
console.log(line);
console.log();
line = "";
}
}
if(message == undefined)
message = "";
if(!setting_up)
console.log(message);
}
//logic
init_board();
print_board();
add_players();