-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.js
159 lines (157 loc) · 4.39 KB
/
TicTacToe.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
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
var gameState= {
player: [],
cpu: [],
clicks : 0,
winningPositions : [["0","1","2"],["3","4","5"],["6","7","8"],["0","3","6"],["1","4","7"],["2","5","8"],
["0","4","8"],["2","4","6"]]
};
window.onload = function(){
var canvas = document.getElementById("GameBoard");
drawBoard(canvas);
var tds = document.getElementsByTagName("td");
for(var i = 0 ; i < tds.length; i++){
tds[i].onclick = drawX;
}
}
function drawBoard(canvas){
//Vertical lines
//Line 1
var context = canvas.getContext("2d");
var width = canvas.offsetWidth;
var height = canvas.offsetHeight;
var verticalLineCoords = Math.floor(width/3);
var horizontalLineCoords = Math.floor(height/3);
context.beginPath();
context.moveTo(verticalLineCoords, 0);
context.lineTo(verticalLineCoords, 800);
context.stroke();
//Line 2
context.beginPath();
context.moveTo(2*verticalLineCoords, 0);
context.lineTo(2*verticalLineCoords,800);
context.stroke();
//Horizontal lines
//Line 1
context.beginPath()
context.moveTo(0,horizontalLineCoords);
context.lineTo(1200, horizontalLineCoords);
context.stroke();
//Line 2
context.beginPath()
context.moveTo(0, 2*horizontalLineCoords);
context.lineTo(1200, 2*horizontalLineCoords);
context.stroke();
}
function drawX(e){
var canvas = e.target;
var canvasId = canvas.getAttribute("id");
if(gameState.player.indexOf(canvasId)>=0 || gameState.cpu.indexOf(canvasId)>=0){
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = "Invalid move!";
}
else{
gameState.clicks++;
gameState.player.push(canvasId);
var context = canvas.getContext("2d");
context.beginPath();
context.moveTo(70,25);
context.lineTo(220,120);
context.strokeStyle = "black"
context.stroke();
context.beginPath();
context.moveTo(70,120);
context.lineTo(220,25);
context.stroke();
if(gameState.player.length>=3){
if(checkWinner(gameState.player)){
winningMessage();
return true;
}else{
if(checkDraw()){
drawMessage();
}
}
}
if(gameState.clicks<5){
drawO();
}
}
}
function drawO (){
var random = Math.floor(Math.random()* 9);
cell = random.toString();
console.log(cell);
if(gameState.player.indexOf(cell)>=0 || gameState.cpu.indexOf(cell)>=0){
drawO();
}else{
gameState.cpu.push(cell);
var canvas = document.getElementById(cell);
var context = canvas.getContext("2d");
context.beginPath()
context.arc(150,80,60,0,2*Math.PI,true);
context.strokeStyle = "red";
context.stroke();
if(gameState.cpu.length>=3){
if(checkWinner(gameState.cpu)){
loosingMessage();
return true;
}else{
if(checkDraw()){
drawMessage();
}
}
}
}
}
function checkWinner(array){
var count;
for(var j = 0 ; j < gameState.winningPositions.length;j++){
var winner = gameState.winningPositions[j];
count = 0;
for(var i = 0 ; i < array.length; i++){
var pos = array[i];
if(winner.indexOf(pos)>=0){
count++;
if(count==3){
return true;
}
}
}
}
return false;
}
function winningMessage(){
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = "You won the game!";
var tds = document.getElementsByTagName("td");
for(var i = 0 ; i < tds.length; i++){
tds[i].onclick = function(){
winningMessage();
}
}
}
function loosingMessage(){
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = "You lost the game!";
var tds = document.getElementsByTagName("td");
for(var i = 0 ; i < tds.length; i++){
tds[i].onclick = function(){
loosingMessage();
}
}
}
function drawMessage(){
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = "It's a draw!";
var tds = document.getElementsByTagName("td");
for(var i = 0 ; i < tds.length; i++){
tds[i].onclick = function(){
drawMessage();
}
}
}
function checkDraw(){
if(gameState.clicks >=5 && !checkWinner(gameState.player) && !checkWinner(gameState.cpu)){
return true;
}
}