-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift.js
322 lines (236 loc) · 6.36 KB
/
shift.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
NOTE:
===============================================================================================
This Project`s code is compared to the previous projects a bit more complicated.
(https://github.com/Incrementis/Repository-INDEX-/wiki/Effective-Javascript)
I recommend reading the code in baby steps and using google chrome`s debugging tools if needed.
As often mentioned the code could be optimized in many different ways, e.g. improving Error messages,
implementing a real A.I., code abstraction, winning conditions
and many other ideas which possibly could came up.
This mini project`s code structure serves only demonstration purposes.
===============================================================================================
*/
var gameBar =
{
DivContainer: document.getElementById('bar'),
Star: "Star.png",
Circle: "Circle.png",
Rounds: [] //Will be filled with objects
}
var gameBoard =
{
firstROW: document.getElementById('first-row').children,
secondROW: document.getElementById('second-row').children,
thirdROW: document.getElementById('third-row').children,
Rows:[] //Will be filled with bool values
}
//Purpose: Calculates a random integer value to decide where to put the cross mark
function Brain()
{
//Number between 0 and 8
return Math.floor((Math.random() * 9));
}
//Purpose: Resets the old game and/or starts a new game
function newGame()
{
var btnShift = document.getElementById('btn-shift');
var Star = '<img src="' + gameBar.Star + ' "style="border:none;margin-left:-6px">';
var Circle = '<img src="' + gameBar.Circle + ' "style="border:none;margin-left:-6px">';
var AI = Brain();
//The maximum rounds of a new game is always 9, due to 9 fields!
var rounds = 9;
//Reseting the game for a new start
gameBar.DivContainer.innerHTML = "";
gameBar.Rounds = [];
gameBoard.Rows = [];
//Filling the board with empty fields
for(var field = 0; field < 3; field++)
{
gameBoard.firstROW[field].src = "Empty.png"
gameBoard.secondROW[field].src = "Empty.png";
gameBoard.thirdROW[field].src = "Empty.png";
}
//Filling the game bar and preparing game board
for(var round = 0; round < rounds; round++)
{
//Checking if "round" is even - for player & system turns
if( (round+1) % 2 == 0 )
{
//Filling the browser
gameBar.DivContainer.innerHTML += Circle;
/*
ATTENTION:
Will be Needed to update the player and system turns and for shifting
*/
gameBar.Rounds.push
({
image: Circle,
turn: "Player"
});
}
else
{
//Filling the Browser
gameBar.DivContainer.innerHTML += Star;
/*
ATTENTION:
Will be Needed to update the turns and for shifting
*/
gameBar.Rounds.push
({
image: Star,
turn: "Rival"
});
}
//Setting all the game board fields to true, to check later if a field can be filled
gameBoard.Rows.push(true);
}
//----INIT first turn of pseudo AI----//
/*
ATTENTION:
Shifting the game bar array.
*/
gameBar.Rounds.shift();
//Clearing game bar
gameBar.DivContainer.innerHTML = "";
//Updating game bar (turns/rounds) and showing in browser
for(var round = 0; round < gameBar.Rounds.length; round++)
{
gameBar.DivContainer.innerHTML += gameBar.Rounds[round].image;
}
//Updating field occupation
gameBoard.Rows[AI] = false;
//Visualising field occupation
if(AI >= 0 && AI <= 2)
{
gameBoard.firstROW[AI].src = gameBar.Star;
}
else if(AI >= 3 && AI <= 5)
{
//Setting AI to a valid index
AI -= 3;
gameBoard.secondROW[AI].src = gameBar.Star;
}
else if(AI >= 6 && AI <= 8)
{
//Setting AI to a valid index
AI -= 6;
gameBoard.thirdROW[AI].src = gameBar.Star;
}
//Activating "SHIFT" button
btnShift.style = "visibility:visible; margin-top:30px";
/*
RESUME:
This function prepares two important variables for the later use.
1. gameBar.Rounds
2. gameBoard.Rows
*/
}
//Purpose: Shifting the player`s turn
function playerPosition(ID)
{
//Checking if it is the player`s turn and if the field is empty
if( gameBar.Rounds[0].turn === "Player" && gameBoard.Rows[ID] === true )
{
/*
ATTENTION:
Shifting the game bar array "Rounds".
*/
gameBar.Rounds.shift();
//Clearing game bar in browser
gameBar.DivContainer.innerHTML = "";
//Updating game bar (turns/rounds) and showing in browser
for(var round = 0; round < gameBar.Rounds.length; round++)
{
gameBar.DivContainer.innerHTML += gameBar.Rounds[round].image;
}
//Updating field occupation
gameBoard.Rows[ID] = false;
//Visualising field occupation
if(ID >= 0 && ID <= 2)
{
gameBoard.firstROW[ID].src = gameBar.Circle;
}
else if(ID >= 3 && ID <= 5)
{
//Setting ID to a valid index
ID -= 3;
gameBoard.secondROW[ID].src = gameBar.Circle;
}
else if(ID >= 6 && ID <= 8)
{
//Setting ID to a valid index
ID -= 6;
gameBoard.thirdROW[ID].src = gameBar.Circle;
}
}
else if(gameBar.Rounds[0].turn === "Player" && gameBoard.Rows[ID] === false)
{
alert("Field already taken!");
}
else
{
alert("Please SHIFT turn!");
}
}
//Purpose: Shifting the system`s turn
function systemPosition()
{
var AI = 0;
var Run = false;
//Checking if there is any empty field available
for(var field = 0; field < 9; field++)
{
if(gameBoard.Rows[field] === true)
{
Run = true;
}
}
//Error-Message if not System`s turn
if(gameBar.Rounds[0].turn !== "Rival")
{
Run = false;
alert("Player`s turn!");
}
//Searching for an empty field
while(Run)
{
AI = Brain();
if( gameBoard.Rows[AI] === true)
{
//Stop searching for an index
Run = false;
/*
ATTENTION:
Shifting the game bar array.
*/
gameBar.Rounds.shift();
//Clearing game bar
gameBar.DivContainer.innerHTML = "";
//Updating game bar (turns/rounds) and showing in browser
for(var round = 0; round < gameBar.Rounds.length; round++)
{
gameBar.DivContainer.innerHTML += gameBar.Rounds[round].image;
}
//Updating field occupation
gameBoard.Rows[AI] = false;
//Visualising field occupation
if(AI >= 0 && AI <= 2)
{
gameBoard.firstROW[AI].src = gameBar.Star;
}
else if(AI >= 3 && AI <= 5)
{
//Setting AI to a valid index
AI -= 3;
gameBoard.secondROW[AI].src = gameBar.Star;
}
else if(AI >= 6 && AI <= 8)
{
//Setting AI to a valid index
AI -= 6;
gameBoard.thirdROW[AI].src = gameBar.Star;
}
}
}
}