-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
239 lines (210 loc) · 9.18 KB
/
app.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
233
234
235
236
237
238
239
var app=angular.module("TicTacApp", ['firebase']);
app.controller("TicTacCtrl", function($scope, $firebase){
//Credit to Sam and Wendy for this general setup.
//Sync the board:
var boardRef = new Firebase("https://tic-tac-angular.firebaseio.com/board");
var boardSync = $firebase(boardRef);
$scope.board = boardSync.$asArray();
//Sync the turnNumber:
var turnNumRef = new Firebase("https://tic-tac-angular.firebaseio.com/turnNum");
var turnNumSync = $firebase(turnNumRef);
$scope.turnNumber = turnNumSync.$asArray();
//Sync up player true and false (to NOT be updated later)
var playerRef = new Firebase('https://tic-tac-angular.firebaseio.com/players');
var playerSync = $firebase(playerRef);
$scope.players = playerSync.$asArray();
//declare as false
$scope.players.$loaded(function(){
if($scope.players.length === 0){
$scope.players.$add({playerOne: false, playerTwo: false});
}
else {
$scope.players[0].playerOne = false;
$scope.players[0].playerTwo = false;
$scope.players[0].$save(0);
}
});
//Make a board:
$scope.board.$loaded(function(){
if($scope.board.length == 0){
for(var i = 0; i < 9; i++){
$scope.board.$add({GamesIcon: " "});
}
}
else{
for(var i = 0; i < 9; i++){
$scope.board[i].GamesIcon = " ";
$scope.board.$save(i);
}
}
});
// Make up a turn counter:
$scope.turnNumber.$loaded(function(){
if($scope.turnNumber.length === 0){
$scope.turnNumber.$add({turnNum: 0});
}
else{
$scope.turnNumber[0].turnNum = 0;
$scope.turnNumber.$save(0);
}
});
$scope.win1 = [0,1,2,null,null,null,null,null,null]; //check
$scope.win2 = [null,null,null,3,4,5,null,null,null]; //check
$scope.win3 = [null,null,null,null,null,null,6,7,8]; //check
$scope.win4 = [0,null,null,3,null,null,6,null,null]; //check
$scope.win5 = [null,1,null,null,4,null,null,7,null]; //check
$scope.win6 = [null,null,2,null,null,5,null,null,8]; //check
$scope.win7 = [0,null,null,null,4,null,null,null,8]; //check
$scope.win8 = [null,null,2,null,4,null,6,null,null]; //check
//defines empty varables with 8 places for the two users:
$scope.user1 = [null,null,null,null,null,null,null,null,null];
$scope.user2 = [null,null,null,null,null,null,null,null,null];
// $scope.gameBoard = [];
$scope.user1Name = "Player 1";
$scope.user2Name = "Player 2";
$scope.gameIcon1 = "X";
$scope.gameIcon2 = "O";
$scope.user1ID = 1;
$scope.user2ID = 2;
$scope.winner = false;
$scope.makeMove = function(idx){
if($scope.turnNumber[0].turnNum == 0){
$scope.players[0].playerOne = true;
}
else if(($scope.turnNumber[0].turnNum == 1) && ($scope.players[0].playerOne !== true)){
$scope.players[0].playerTwo = true;
}
if(($scope.board[idx].GamesIcon == " ") && ($scope.turnNumber[0].turnNum % 2 === 0) && ($scope.players[0].playerOne == true)){
$scope.gameIcon = ($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.gameIcon1 : $scope.gameIcon2;
$scope.board[idx].GamesIcon = $scope.gameIcon;
$scope.board.$save(idx);
$scope.currentUser = (($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.user1 : $scope.user2);
$scope.currentUser[idx] = idx;
$scope.currentUserName = (($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.user1Name : $scope.user2Name);
console.log("new move:");
console.log($scope.turnNumber.turnNum)
console.log($scope.currentUserName);
console.log($scope.currentUser);
console.log($scope.board);
console.log($scope.win1);
console.log(idx);
console.log($scope.turnNumber[0].turnNum);
if(
(($scope.win1[0] == $scope.currentUser[0]) && ($scope.win1[1] == $scope.currentUser[1]) && $scope.win1[2] == $scope.currentUser[2]) ||
(($scope.win2[3] == $scope.currentUser[3]) && ($scope.win2[4] == $scope.currentUser[4]) && $scope.win2[5] == $scope.currentUser[5]) ||
(($scope.win3[6] == $scope.currentUser[6]) && ($scope.win3[7] == $scope.currentUser[7]) && $scope.win3[8] == $scope.currentUser[8]) ||
(($scope.win4[0] == $scope.currentUser[0]) && ($scope.win4[3] == $scope.currentUser[3]) && $scope.win4[6] == $scope.currentUser[6]) ||
(($scope.win5[1] == $scope.currentUser[1]) && ($scope.win5[4] == $scope.currentUser[4]) && $scope.win5[7] == $scope.currentUser[7]) ||
(($scope.win6[2] == $scope.currentUser[2]) && ($scope.win6[5] == $scope.currentUser[5]) && $scope.win6[8] == $scope.currentUser[8]) ||
(($scope.win7[0] == $scope.currentUser[0]) && ($scope.win7[4] == $scope.currentUser[4]) && $scope.win7[8] == $scope.currentUser[8]) ||
(($scope.win8[2] == $scope.currentUser[2]) && ($scope.win8[4] == $scope.currentUser[4]) && $scope.win8[6] == $scope.currentUser[6])
) {
console.log($scope.currentUserName + " Wins this round");
alert($scope.currentUserName + " Wins this round");
$scope.endGame();
$scope.square = 'Z';
}
else if ($scope.turnNumber[0].turnNum == 8){
console.log("Tie Game");
console.log($scope.turnNumber[0].turnNum);
alert("It's a tie!");
}
else {
console.log("No news, the game goes on");
}
$scope.turnNumber[0].turnNum++;
$scope.turnNumber.$save(0);
$scope.endGame = function(){
$scope.turnNumber[0].turnNum=0;
if($scope.gameIcon == "X"){
for (var i=0; i < 9; i++){
// $scope.board[i].GamesIcon = "xWin";
// $scope.board.$save(i);
$scope.user1[i] = "";
$scope.user2[i] = "";
}
}
else if($scope.gameIcon == "O"){
for (var i=0; i < 9; i++){
// $scope.board[i].GamesIcon = "yWin";
// $scope.board.$save(i);
$scope.user1[i] = "";
$scope.user2[i] = "";
}
}
}
//
}
//Code goes here:
if(($scope.board[idx].GamesIcon == " ") && ($scope.turnNumber[0].turnNum % 2 == 1) && $scope.players[0].playerTwo == true){
$scope.gameIcon = ($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.gameIcon1 : $scope.gameIcon2;
$scope.board[idx].GamesIcon = $scope.gameIcon;
$scope.board.$save(idx);
$scope.currentUser = (($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.user1 : $scope.user2);
$scope.currentUser[idx] = idx;
$scope.currentUserName = (($scope.turnNumber[0].turnNum % 2) === 0 ? $scope.user1Name : $scope.user2Name);
console.log("new move:");
console.log($scope.turnNumber.turnNum)
console.log($scope.currentUserName);
console.log($scope.currentUser);
console.log($scope.board);
console.log($scope.win1);
console.log(idx);
console.log($scope.turnNumber[0].turnNum);
if(
(($scope.win1[0] == $scope.currentUser[0]) && ($scope.win1[1] == $scope.currentUser[1]) && $scope.win1[2] == $scope.currentUser[2]) ||
(($scope.win2[3] == $scope.currentUser[3]) && ($scope.win2[4] == $scope.currentUser[4]) && $scope.win2[5] == $scope.currentUser[5]) ||
(($scope.win3[6] == $scope.currentUser[6]) && ($scope.win3[7] == $scope.currentUser[7]) && $scope.win3[8] == $scope.currentUser[8]) ||
(($scope.win4[0] == $scope.currentUser[0]) && ($scope.win4[3] == $scope.currentUser[3]) && $scope.win4[6] == $scope.currentUser[6]) ||
(($scope.win5[1] == $scope.currentUser[1]) && ($scope.win5[4] == $scope.currentUser[4]) && $scope.win5[7] == $scope.currentUser[7]) ||
(($scope.win6[2] == $scope.currentUser[2]) && ($scope.win6[5] == $scope.currentUser[5]) && $scope.win6[8] == $scope.currentUser[8]) ||
(($scope.win7[0] == $scope.currentUser[0]) && ($scope.win7[4] == $scope.currentUser[4]) && $scope.win7[8] == $scope.currentUser[8]) ||
(($scope.win8[2] == $scope.currentUser[2]) && ($scope.win8[4] == $scope.currentUser[4]) && $scope.win8[6] == $scope.currentUser[6])
) {
console.log($scope.currentUserName + " Wins this round");
alert($scope.currentUserName + " Wins this round");
$scope.endGame();
$scope.square = 'Z';
}
else if ($scope.turnNumber[0].turnNum == 8){
console.log("Tie Game");
console.log($scope.turnNumber[0].turnNum);
alert("It's a tie!");
}
else {
console.log("No news, the game goes on");
}
$scope.turnNumber[0].turnNum++;
$scope.turnNumber.$save(0);
$scope.endGame = function(){
$scope.turnNumber[0].turnNum=0;
if($scope.gameIcon == "X"){
for (var i=0; i < 9; i++){
// $scope.board[i].GamesIcon = "xWin";
// $scope.board.$save(i);
$scope.user1[i] = "";
$scope.user2[i] = "";
}
}
else if($scope.gameIcon == "O"){
for (var i=0; i < 9; i++){
// $scope.board[i].GamesIcon = "yWin";
// $scope.board.$save(i);
$scope.user1[i] = "";
$scope.user2[i] = "";
}
}
}
//
}
};
}
);
//===================================
// functionality needed: tie condition:
// If the gameboard is full, declare tie and run endGame function.
// reset button:
// This can work by resetting user arrays and the board to all null.
// Make an endGame function:
// //do an ng-hide?
//===================================