-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (69 loc) · 1.91 KB
/
index.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
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function Game() {
var state,
symbols,
turn;
function Game$initialize() {
state = [];
symbols = ['X', 'O'];
turn = 0;
}
function Game$start() {
rl.on('line', function(line){
Game$command(line.trim());
Game$print();
if (Game$check()) {
console.log("O jogador %s ganhou!", symbols[+!turn]);
process.exit(0);
}
rl.setPrompt("Player " + symbols[turn] + " > ");
rl.prompt();
});
Game$print();
rl.setPrompt("Player " + symbols[turn] + " > ");
rl.prompt();
}
function Game$command(command) {
if (/^[1-9]$/.test(command) && state[--command] === undefined) {
state[command] = turn;
turn = +!turn;
} else if (command === 'q') {
process.exit(0);
}
}
function Game$check() {
var i;
for (i = 0; i < 3; i++) {
if ((state[i*3] !== []._ && state[i*3] === state[i*3+1] && state[i*3] === state[i*3+2]) ||
(state[i] !== []._ && state[i] === state[i+3] && state[i] === state[i+6])) {
return true;
}
}
if (state[4] !== []._ && ((state[0] === state[4] && state[0] === state[8]) ||
(state[2] === state[4] && state[2] === state[6]))) {
return true;
}
return false;
}
function Game$print() {
console.log(" %s | %s | %s", Game$get(0), Game$get(1), Game$get(2));
console.log("-----------");
console.log(" %s | %s | %s", Game$get(3), Game$get(4), Game$get(5));
console.log("-----------");
console.log(" %s | %s | %s", Game$get(6), Game$get(7), Game$get(8));
}
function Game$get(index) {
return [symbols[state[index]]] + '' || (index + 1);
}
Game$initialize.apply(this, arguments);
return {
start: Game$start,
get state() { return state; }
};
}
var game = new Game();
game.start();