-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmyScripts.js
More file actions
207 lines (172 loc) · 5.46 KB
/
myScripts.js
File metadata and controls
207 lines (172 loc) · 5.46 KB
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
// We prefer to use let keyword instead of var!
let playerone,playertwo;
let player1color = 'rgb(244, 65, 65)';
let player2color = 'rgb(66, 134, 244)';
const startBtn= document.getElementById("start");
const nameInput= document.getElementById('nameInput')
let playtTime = document.getElementById("timeforplay")
var timer;
var timeLeft;
async function inputs(){
startBtn.style.display="none";
nameInput.style.display="block";
}
function Close(){
nameInput.style.display="none";
startBtn.style.display="block";
}
var currentPlayer=1;
var currentName=playerone;
var currentColor=player1color;
// Used to make changes when a move is made or timer runs out
function makeMove(){
currentPlayer = currentPlayer * -1;
if (currentPlayer === 1){
currentName = playerone;
$('.plyr1').text(currentName + ", it is your turn!").css('color','red');
$('#timer-wrap').css('background-color', 'red');
$('.plyr1').css('font-weight','bolder');
$('.plyr2').text(playertwo).css('font-weight','lighter');
currentColor = player1color;
}
else {
currentName = playertwo;
$('.plyr2').text(currentName + ", it is your turn!").css('color','blue');
$('#timer-wrap').css('background-color', 'blue');
$('.plyr2').css('font-weight','bolder');
$('.plyr1').text(playerone).css('font-weight','lighter');
currentColor = player2color;
}
}
async function namesInput(){
const nameInput=await document.getElementById('nameInput')
nameInput.style.display="none";
const footer=await document.querySelectorAll('.footer')[0];
footer.style.position="unset";
const container= await document.getElementById("gamepage");
container.style.display="block";
const plr1= await document.getElementById("player1")
playerone=plr1.value;
const plr2= await document.getElementById("player2")
playertwo=plr2.value;
if(playerone=="" || playerone==null)
playerone="player 1"
if(playertwo==="" || playertwo==null)
playertwo="player 2"
// Start with Player 1
$('.plyr1').text(playerone + ", it is your turn!").css('color','red');
$('.plyr2').text(playertwo).css('color','blue');
$('.plyr1').css('font-weight','bolder');
$('.plyr2').css('font-weight','lighter');
$('.board button').on('click', function(){
var col = $(this).closest('td').index();
var bottomAvail = checkBottom(col);
changeColor(bottomAvail, col, currentColor);
if(horizontalWinCheck() || verticalWinCheck() || diagonalWinCheck()){
$('h1').text(currentName + " You have won!");
$('h3').fadeOut('fast');
$('h2').fadeOut('fast');
clearInterval(timer);
document.getElementById("timer-wrap").style.display = "none";
}
makeMove();
});
}
//grab elements on table
var game_on = true;
var table = $('table tr');
function reportWin(rowNum, colNum) {
console.log("You won starting at this row, col");
console.log(rowNum);
console.log(colNum);
}
//toggle color for element
function changeColor(row,col,color){
return table.eq(row).find('td').eq(col).find('button').css('background-color',color);
}
function returnColor(row,col){
return table.eq(row).find('td').eq(col).find('button').css('background-color');
}
function checkBottom(col){
var colorReport=returnColor(5,col);
for (var row = 5; row > -1; row--) {
colorReport=returnColor(row,col);
if (colorReport ==='rgb(128, 128, 128)'){
return row
}
}
}
function colorMatchCheck(one,two,three,four){
return (one===two && one===three && one===four && one !== 'rgb(128, 128, 128)' && one !== undefined);
}
// Check for Horizontal Wins
function horizontalWinCheck() {
for (var row = 0; row < 6; row++) {
for (var col = 0; col < 4; col++) {
if (colorMatchCheck(returnColor(row,col), returnColor(row,col+1) ,returnColor(row,col+2), returnColor(row,col+3))) {
console.log('horiz');
return true;
}else {
continue;
}
}
}
}
// Check for vertical wins
function verticalWinCheck() {
for (var row = 0; row < 6; row++) {
for (var col = 0; col < 7; col++) {
if (colorMatchCheck(returnColor(row,col), returnColor(row+1,col) ,returnColor(row+2,col), returnColor(row+3,col))) {
console.log('vertical');
return true;
}else {
continue;
}
}
}
}
// Check for diagonal wins
function diagonalWinCheck(){
for (var col = 0; col < 5; col++) {
for (var row = 0; row < 7; row++) {
if (colorMatchCheck(returnColor(row,col), returnColor(row+1,col+1) ,returnColor(row+2,col+2), returnColor(row+3,col+3))) {
console.log('diag');
reportWin(row,col);
return true;
}else if (colorMatchCheck(returnColor(row,col), returnColor(row-1,col+1) ,returnColor(row-2,col+2), returnColor(row-3,col+3))) {
console.log('diag');
reportWin(row,col);
return true;
}else {
continue;
}
}
}
}
// Skips the current player if the timer runs out
function skipCurrentPlayer() {
// this.currentPlayer = this.currentPlayer * -1;
console.log(this.currentPlayer);
makeMove();
startTimer();
}
//Updates timer on the UI
function updateTimer() {
timeLeft = timeLeft - 1;
if(timeLeft >= 0)
$('#timer').html(timeLeft);
else {
skipCurrentPlayer();
}
}
// Starts the timer when the move is made
function startTimer() {
// timeLeft is assigned the value given by the user
if(playtTime.value == ""){
timeLeft = 31;
}
else timeLeft = playtTime.value;
clearInterval(timer);
timer = setInterval(updateTimer, 1000);
updateTimer();
}