-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
125 lines (115 loc) · 3.15 KB
/
main.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
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
var canvas = document.getElementById("main");
canvas.height = 400;
canvas.width = 400;
const lw = (canvas.width / 3);
const lh = (canvas.height / 3);
var v = canvas.getContext("2d");
v.strokeRect(0, 0, canvas.width, canvas.height);
var board;
var players = ["O", "X"];
var currPlayer;
var finished;
document.addEventListener('click', (e) => {
forEachPos((i, j) => {
if(e.clientX >= j*lw && e.clientX < (j+1)*lw && e.clientY >= i*lh && e.clientY < (i+1)*lh && board[i][j] == ""){
board[i][j] = players[currPlayer];
if(++currPlayer >= players.length)
currPlayer = 0;
}
});
if(finished){
reset();
}
tick();
});
reset();
function reset(){
clearScreen();
board = [
["", "", ""],
["", "", ""],
["", "", ""]
];
currPlayer = 0;
finished = false;
for(var i = 1; i < 3; i++){
v.moveTo(i * lw, 0);
v.lineTo(i * lw, canvas.height);
v.moveTo(0, i * lh);
v.lineTo(canvas.width, i * lh);
v.stroke();
}
}
function clearScreen(){
v.beginPath();
v.clearRect(1, 1, canvas.width - 2, canvas.height - 2);
v.fill();
}
function drawBoard(){
forEachPos((i, j) => {
const initialX = j * lw + (lw / 2);
const initialY = i * lh + (lh / 2);
v.clearRect(j * lw + 1, i * lh + 1, lw - 2, lh - 2);
v.beginPath();
if(board[i][j] == players[0]){
v.arc(initialX, initialY, lw / 3, 0, Math.PI*2, false);
} else if(board[i][j] == players[1]) {
const w = (lw / 3);
const h = (lh / 3);
v.moveTo(initialX - w, initialY - h);
v.lineTo(initialX + w, initialY + h);
v.moveTo(initialX + w, initialY - h);
v.lineTo(initialX - w, initialY + h);
}
v.stroke();
});
}
function checkWinner(){
for(var j = 0; j < players.length; j++){
for(var i = 0; i < board.length; i++){
if(hasLine(board[i][0], board[i][1], board[i][2], j)){
return players[j];
}
}
for(var i = 0; i < board.length; i++){
if(hasLine(board[0][i], board[1][i], board[2][i], j)){
return players[j];
}
}
if(hasLine(board[0][0], board[1][1], board[2][2], j)) {
return players[j];
}
if(hasLine(board[0][2], board[1][1], board[2][0], j)) {
return players[j];
}
}
var x = "Tie";
forEachPos((i, j) => {
if(board[i][j] == ""){
x = null;
return;
}
});
return x;
}
function tick(){
drawBoard();
var winner = checkWinner();
if(winner != null) {
clearScreen();
v.textAlign = 'center';
v.font = canvas.height/7.5 + 'px Arial';
v.fillText((winner != "Tie" ? "Winner is " : "") + winner, (canvas.width/2), (canvas.height/2));
finished = true;
}
}
function hasLine(x, y, z, v){
return x == y && y == z && z == players[v];
}
function forEachPos(f){
for(var i = 0; i < board.length; i++){
for(var j = 0; j < board[0].length; j++){
f(i, j);
}
}
}