-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic.js
232 lines (185 loc) · 4.84 KB
/
tic.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
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
224
225
226
227
228
229
230
231
232
var ticdbscore=0;
$(document).ready(function(){
$("#board, #scores").hide();
/* VARIABLES INITIATED IN HERE */
var human;
var comp;
var p1 = [1,'X',0]; // [player,sign,wins]
var p2 = [2,'O',0];
var current;
var firstTurn;
var winners = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 1, 2],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
var boardArr = ["#", "#", "#", "#", "#", "#", "#", "#", "#"];
var gameWon = false;
var turnCount=0;
/*CODE TO BEGIN GAME, PLAYER NEEDS TO CHOOSE X OR O*/
$('#chooseP > .btn').click(function(){
if($(this).attr('id') === "chooseP1"){
human = p1;
comp = p2;
current = human;
firstTurn = 'human';
} else {
human = p2;
comp = p1;
current = comp;
firstTurn = 'comp';
//need to run computer first turn
var x = setTimeout(compTurn, 500);
}
$('#chooseP').hide();
$("#board, #scores").show();
});
/*CODE TO MAKE FREE TILES CHANGE MOUSE ON HOVER SO PLAYER CAN SEE IF AVAILABLE*/
$("td").hover(function() {
var pos = $(this).attr("id");
//checks if square is empty using board array, if it is empty, changes mouse to crosshair when hovering
if (boardArr[pos] == "#") {
$(this).css("cursor", "crosshair");
}
});
//CODE FOR HUMAN CLICKING EMPTY SQUARE
$("td").click(function(){
var tile = $(this).attr('id');
if(boardArr[tile]==='#'){
humanTurn(tile);
}
})
/*FUNCTIONS TO BE CALLED FOR TURNS*/
//pickEmpty, used for computers turn to pick a random empty square.
function pickEmpty(){
var emptyArr = [];
for(i=0;i<boardArr.length;i++){
if(boardArr[i] === '#'){
emptyArr.push(i);
}
}
var x = Math.round(Math.random() * emptyArr.length);
var choice = emptyArr[x];
if (choice === undefined) {
choice = emptyArr[x - 1];
}
return choice;
};
/********************************************************/
/**************COMPUTERS TURN****************************/
function compTurn(){
let current = comp;
var tile = pickEmpty();
$('#'+tile).html(comp[1]);
boardArr[tile] = comp[1];
checkWin(current);
if(gameWon === 1 || gameWon ===2){
alert("Computer wins!");
winReset(comp);
}
else {
turnCount ++;
checkDraw();
if(turnCount===0 && human[0]===2){
compTurn();
}
}
};
/*********************HUMANS TURN**************************/
function humanTurn(tile){
let current = human;
$('#'+tile).html(human[1]);
boardArr[tile] = human[1];
checkWin(current);
if(gameWon === 1 || gameWon ===2){
alert("Nice one, you win!");
ticdbscore=ticdbscore+20;
console.log("+++",ticdbscore);
var reset = setTimeout(winReset(human),1000);
}
else {
turnCount ++;
checkDraw();
if(turnCount!== 0){
var compGo = setTimeout(compTurn,800);
}else if(turnCount===0 && human[0]===2){
compTurn();
}
}
}
/**********************Function to check for a win*********************/
function checkWin(curr){
var currentTiles = [];
//compile array of all tiles taken by the current player
for(i = 0; i<boardArr.length; i++){
if(boardArr[i]===curr[1]){
currentTiles.push(i);
}
}
//Check the current tiles array agains each winning combo to find a match.
for(x=0; x<winners.length; x++){
var count = 0;
for(j=0;j<winners[x].length;j++){
if(currentTiles.indexOf(winners[x][j])!= -1){
count ++;
}
if(count == 3){
gameWon = current[0];
}
}
}
}
/**************Upon win, updates scores and resets board for new game********/
function winReset(winner){
winner[2]=winner[2]+20;
$('#scores').html('P1: '+p1[2]+' <br>P2: '+p2[2]);
turnCount=0;
$('td').html('');
boardArr = ["#", "#", "#", "#", "#", "#", "#", "#", "#"];
if(human === p1){
current = human;
}else{
let current = comp;
var tile = pickEmpty();
$('#'+tile).html(comp[1]);
boardArr[tile] = comp[1];
turnCount++;
}
gameWon = false;
}
/*******************CHECKS FOR DRAW*********************/
function checkDraw(){
if(turnCount > 8){
alert("It's a Draw!");
$('td').html('');
boardArr = ["#", "#", "#", "#", "#", "#", "#", "#", "#"];
if(human === p1){
current = human;
}else{
current = comp;
}
turnCount = 0;
}
}
});
function ticscoredata()
{
console.log("++++++",ticdbscore);
var name=localStorage.getItem(1);
console.log("===",name);
var request = new XMLHttpRequest();
request.open('POST', 'http://localhost:8080/MINI/rest/hello/ticscore', true);
request.setRequestHeader("Content-Type","application/json");
var body= '{"ticdbscore":"'+ticdbscore+'", "userName":"'+name+'"}';
request.onload = function () {
var data = this.response;
console.log("received: ",data);
}
request.send(body);
}