-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part3_FrontEndProject.js
170 lines (145 loc) · 5.16 KB
/
Part3_FrontEndProject.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
// We need to use jQuery for the following:
var player1 = prompt("Player One: Enter Your Name , you will be Blue");
var player1Color = 'rgb(86, 151, 255)';
var player2 = prompt("Player Two: Enter Your Name, you will be Red");
var player2Color = 'rgb(237, 45, 73)';
var game_on = true;
var table = $('table tr');
// http://stackoverflow.com/questions/6139407/getting-td-by-index-with-jquery
function reportWin(rowNum,colNum) {
console.log("You won starting at this row,col");
console.log(rowNum);
console.log(colNum);
}
// Change the color of a button
function changeColor(rowIndex,colIndex,color) {
return table.eq(rowIndex).find('td').eq(colIndex).find('button').css('background-color',color);
}
// Report Back to current color of a button
function returnColor(rowIndex,colIndex) {
return table.eq(rowIndex).find('td').eq(colIndex).find('button').css('background-color');
}
// Take in column index, returns the bottom row that is still gray
function checkBottom(colIndex) {
var colorReport = returnColor(5,colIndex);
for (var row = 5; row > -1; row--) {
colorReport = returnColor(row,colIndex);
if (colorReport === 'rgb(128, 128, 128)') {
return row
}
}
}
// Check to see if 4 inputs are the same color
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');
reportWin(row,col);
return true;
}else {
continue;
}
}
}
}
// Check for Vertical Wins
function verticalWinCheck() {
for (var col = 0; col < 7; col++) {
for (var row = 0; row < 3; row++) {
if (colorMatchCheck(returnColor(row,col), returnColor(row+1,col) ,returnColor(row+2,col), returnColor(row+3,col))) {
console.log('vertical');
reportWin(row,col);
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;
}
}
}
}
// Game End
function gameEnd(winningPlayer) {
for (var col = 0; col < 7; col++) {
for (var row = 0; row < 7; row++) {
$('h3').fadeOut('fast');
$('h2').fadeOut('fast');
$('h1').text(winningPlayer+" has won! Refresh your browser to play again!").css("fontSize", "50px")
}
}
}
// Start with Player One
var currentPlayer = 1;
var currentName = player1;
var currentColor = player1Color;
// Start with Player One
$('h3').text(player1+": it is your turn, please pick a column to drop your blue chip.");
$('.board button').on('click',function() {
// Recognize what column was chosen
var col = $(this).closest("td").index();
// Get back bottom available row to change
var bottomAvail = checkBottom(col);
// Drop the chip in that column at the bottomAvail Row
changeColor(bottomAvail,col,currentColor);
// Check for a win or a tie.
if (horizontalWinCheck() || verticalWinCheck() || diagonalWinCheck()) {
gameEnd(currentName);
}
// If no win or tie, continue to next player
currentPlayer = currentPlayer * -1 ;
// Re-Check who the current Player is.
if (currentPlayer === 1) {
currentName = player1;
$('h3').text(currentName+": it is your turn, please pick a column to drop your blue chip.");
currentColor = player1Color;
}else {
currentName = player2
$('h3').text(currentName+": it is your turn, please pick a column to drop your red chip.");
currentColor = player2Color;
}
})
// Helper function to help you understand Rows and Columns From A Table
// http://stackoverflow.com/questions/788225/table-row-and-column-number-in-jquery
//
// $('.board button').on('click',function(){
// // This is the Column Number (starts at zero):
// console.log('This is the Column:');
// console.log($(this).closest("td").index());
// // This is the Row Number:
// console.log("This is the Row:");
// console.log($(this).closest("tr").index());
// console.log('\n');
// // This is a way to grab a particular cell (replace):
// // $('table').eq(rowIndex).find('td').eq(colIndex)
// });
// // Change color on click
// $('.board button').on('click',function() {
// if($(this).css('background-color') === 'rgb(51, 51, 51)'){
// $(this).css('background-color','rgb(86, 151, 255)');
// }else if ($(this).css('background-color') === 'rgb(86, 151, 255)'){
// $(this).css('background-color','rgb(237, 45, 73)');
// }else{
// $(this).css('background-color','rgb(51, 51, 51)');
// }
// });