Skip to content

Commit 09abbcb

Browse files
Merge pull request #133 from Shauryaditya/main
ConnectFour
2 parents 24fb690 + e2f5e98 commit 09abbcb

File tree

5 files changed

+263
-3
lines changed

5 files changed

+263
-3
lines changed

ConnectFour/Part3_FrontEndProject.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.board button {
2+
width: 100px;
3+
height: 100px;
4+
background-color: gray;
5+
border-radius: 50%;
6+
border: 4px solid black; /*this gets rid of bootstrap shadow*/
7+
margin: 1px;
8+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Connect Four</title>
6+
7+
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
9+
<link rel="stylesheet" href="Part3_FrontEndProject.css">
10+
11+
</head>
12+
<body>
13+
<div class="container" align='center'>
14+
<h1>Welcome to Connect Four!</h1>
15+
<h2>The object of this game is to connect four of your chips in a row!</h2>
16+
<h3>Let's get started!</h3>
17+
<table class="board">
18+
<tr>
19+
<td><button type="button"></button></td>
20+
<td><button type="button"></button></td>
21+
<td><button type="button"></button></td>
22+
<td><button type="button"></button></td>
23+
<td><button type="button"></button></td>
24+
<td><button type="button"></button></td>
25+
<td><button type="button"></button></td>
26+
</tr>
27+
<tr>
28+
<td><button type="button"></button></td>
29+
<td><button type="button"></button></td>
30+
<td><button type="button"></button></td>
31+
<td><button type="button"></button></td>
32+
<td><button type="button"></button></td>
33+
<td><button type="button"></button></td>
34+
<td><button type="button"></button></td>
35+
</tr>
36+
<tr>
37+
<td><button type="button"></button></td>
38+
<td><button type="button"></button></td>
39+
<td><button type="button"></button></td>
40+
<td><button type="button"></button></td>
41+
<td><button type="button"></button></td>
42+
<td><button type="button"></button></td>
43+
<td><button type="button"></button></td>
44+
</tr>
45+
<tr>
46+
<td><button type="button"></button></td>
47+
<td><button type="button"></button></td>
48+
<td><button type="button"></button></td>
49+
<td><button type="button"></button></td>
50+
<td><button type="button"></button></td>
51+
<td><button type="button"></button></td>
52+
<td><button type="button"></button></td>
53+
</tr>
54+
<tr>
55+
<td><button type="button"></button></td>
56+
<td><button type="button"></button></td>
57+
<td><button type="button"></button></td>
58+
<td><button type="button"></button></td>
59+
<td><button type="button"></button></td>
60+
<td><button type="button"></button></td>
61+
<td><button type="button"></button></td>
62+
</tr>
63+
<tr>
64+
<td><button type="button"></button></td>
65+
<td><button type="button"></button></td>
66+
<td><button type="button"></button></td>
67+
<td><button type="button"></button></td>
68+
<td><button type="button"></button></td>
69+
<td><button type="button"></button></td>
70+
<td><button type="button"></button></td>
71+
</tr>
72+
</table>
73+
74+
</div>
75+
76+
</script>
77+
<script
78+
src="https://code.jquery.com/jquery-3.1.1.min.js"
79+
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
80+
crossorigin="anonymous">
81+
</script>
82+
<script src='Part3_FrontEndProject.js'>
83+
84+
</script>
85+
</body>
86+
</html>

ConnectFour/Part3_FrontEndProject.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// We need to use jQuery for the following:
2+
3+
var player1 = prompt("Player One: Enter Your Name , you will be Blue");
4+
var player1Color = 'rgb(86, 151, 255)';
5+
6+
var player2 = prompt("Player Two: Enter Your Name, you will be Red");
7+
var player2Color = 'rgb(237, 45, 73)';
8+
9+
var game_on = true;
10+
var table = $('table tr');
11+
12+
// http://stackoverflow.com/questions/6139407/getting-td-by-index-with-jquery
13+
function reportWin(rowNum, colNum) {
14+
console.log("You won starting at this row,col");
15+
console.log(rowNum);
16+
console.log(colNum);
17+
}
18+
// Change the color of a button
19+
function changeColor(rowIndex, colIndex, color) {
20+
return table.eq(rowIndex).find('td').eq(colIndex).find('button').css('background-color', color);
21+
}
22+
23+
// Report Back to current color of a button
24+
function returnColor(rowIndex, colIndex) {
25+
return table.eq(rowIndex).find('td').eq(colIndex).find('button').css('background-color');
26+
}
27+
28+
// Take in column index, returns the bottom row that is still gray
29+
function checkBottom(colIndex) {
30+
var colorReport = returnColor(5, colIndex);
31+
for (var row = 5; row > -1; row--) {
32+
colorReport = returnColor(row, colIndex);
33+
if (colorReport === 'rgb(128, 128, 128)') {
34+
return row
35+
}
36+
}
37+
}
38+
39+
// Check to see if 4 inputs are the same color
40+
function colorMatchCheck(one, two, three, four) {
41+
return (one === two && one === three && one === four && one !== 'rgb(128, 128, 128)' && one !== undefined);
42+
}
43+
44+
// Check for Horizontal Wins
45+
function horizontalWinCheck() {
46+
for (var row = 0; row < 6; row++) {
47+
for (var col = 0; col < 4; col++) {
48+
if (colorMatchCheck(returnColor(row, col), returnColor(row, col + 1), returnColor(row, col + 2), returnColor(row, col + 3))) {
49+
console.log('horiz');
50+
reportWin(row, col);
51+
return true;
52+
} else {
53+
continue;
54+
}
55+
}
56+
}
57+
}
58+
59+
// Check for Vertical Wins
60+
function verticalWinCheck() {
61+
for (var col = 0; col < 7; col++) {
62+
for (var row = 0; row < 3; row++) {
63+
if (colorMatchCheck(returnColor(row, col), returnColor(row + 1, col), returnColor(row + 2, col), returnColor(row + 3, col))) {
64+
console.log('vertical');
65+
reportWin(row, col);
66+
return true;
67+
} else {
68+
continue;
69+
}
70+
}
71+
}
72+
}
73+
74+
// Check for Diagonal Wins
75+
function diagonalWinCheck() {
76+
for (var col = 0; col < 5; col++) {
77+
for (var row = 0; row < 7; row++) {
78+
if (colorMatchCheck(returnColor(row, col), returnColor(row + 1, col + 1), returnColor(row + 2, col + 2), returnColor(row + 3, col + 3))) {
79+
console.log('diag');
80+
reportWin(row, col);
81+
return true;
82+
} else if (colorMatchCheck(returnColor(row, col), returnColor(row - 1, col + 1), returnColor(row - 2, col + 2), returnColor(row - 3, col + 3))) {
83+
console.log('diag');
84+
reportWin(row, col);
85+
return true;
86+
} else {
87+
continue;
88+
}
89+
}
90+
}
91+
}
92+
93+
// Game End
94+
function gameEnd(winningPlayer) {
95+
for (var col = 0; col < 7; col++) {
96+
for (var row = 0; row < 7; row++) {
97+
$('h3').fadeOut('fast');
98+
$('h2').fadeOut('fast');
99+
$('h1').text(winningPlayer + " has won! Refresh your browser to play again!").css("fontSize", "50px")
100+
}
101+
}
102+
}
103+
104+
// Start with Player One
105+
var currentPlayer = 1;
106+
var currentName = player1;
107+
var currentColor = player1Color;
108+
109+
// Start with Player One
110+
$('h3').text(player1 + ": it is your turn, please pick a column to drop your blue chip.");
111+
112+
$('.board button').on('click', function() {
113+
114+
// Recognize what column was chosen
115+
var col = $(this).closest("td").index();
116+
117+
// Get back bottom available row to change
118+
var bottomAvail = checkBottom(col);
119+
120+
// Drop the chip in that column at the bottomAvail Row
121+
changeColor(bottomAvail, col, currentColor);
122+
123+
// Check for a win or a tie.
124+
if (horizontalWinCheck() || verticalWinCheck() || diagonalWinCheck()) {
125+
gameEnd(currentName);
126+
}
127+
128+
// If no win or tie, continue to next player
129+
currentPlayer = currentPlayer * -1;
130+
131+
// Re-Check who the current Player is.
132+
if (currentPlayer === 1) {
133+
currentName = player1;
134+
$('h3').text(currentName + ": it is your turn, please pick a column to drop your blue chip.");
135+
currentColor = player1Color;
136+
} else {
137+
currentName = player2
138+
$('h3').text(currentName + ": it is your turn, please pick a column to drop your red chip.");
139+
currentColor = player2Color;
140+
}
141+
142+
})

ConnectFour/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Connect Four
2+
3+
- This is a simple game which can be played between two players.The player who connects the four bubbles first wins the game . The bubbles could be connected horizontally , vertically or diagonally.The players have to be together since multiplayer feature is not available .
4+
5+
## Technologies Used :
6+
7+
- HTML
8+
9+
- CSS
10+
11+
- Javascript
12+
13+
- JQuery
14+
15+
## How to set-up:
16+
17+
- Clone this repo
18+
19+
- Open it in any Text Editor
20+
21+
- Enjoy it !!!
22+
23+
![ConnectFour](https://user-images.githubusercontent.com/68684482/124650666-98456c00-deb7-11eb-80e7-87e999d8ab96.png)

Index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1 align="center">Web-dev-mini-projects</h1>
1+
<h1 align="center">Web-dev-mini-projects</h1>
22
<br>
33
<div align="center">
44
<a href="https://github.com/topics/html"><img alt="HTML" src="https://img.shields.io/badge/HTML%20-%23E34F26.svg?&style=for-the-badge"/></a>
@@ -48,10 +48,11 @@
4848
| [TO-DO List](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/To-Do%20List) | Simple TO-DO List written in HTML, CSS, and JavaScript |
4949
| [Weather-App](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/Weather-App) | Simple Weather app written in HTML, CSS, and JavaScript using the APIs for fetching weather and geolocation information |
5050
| [News Webapp](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/flask_news_project) | A web app for searching news made using flask |
51-
| Score Keeper |A web Page where user can keep a count of their scores and will tell which player won.|
51+
| Score Keeper |A web Page where user can keep a count of their scores and will tell which player won.|
5252
| [Image Filter App]() |An Awesome Image Filter App written in Html,Css,Javascript and CamanJS.|
5353
| [Form Validation](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/form-validation) |A basic sign up page with all the validations in javascript. |
5454
| [Age Calculator](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/age-calculator) | A website which can be used to calculate age of a person in days,months and years . |
5555
| [BMI Calculator](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/bmi-calculator) | A basic website which can be used to calculate body mass index |
5656
| [Glassmorphism Animation Form](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/Glassmorphism-Form) |A basic login page with Glassmorphism Animations |
57-
| [Battery Percentage Indicator](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/Battery%20Indicator) |This is a battery indicator app which is used to know battery percentage of the system. |
57+
| [Battery Percentage Indicator](https://github.com/Ayushparikh-code/Web-dev-mini-projects/tree/main/Battery%20Indicator) |This is a battery indicator app which is used to know battery percentage of the system. |
58+
| [ConnectFour](https://github.com/Shauryaditya/Web-dev-mini-projects/tree/main/ConnectFour) | This is a simple game which can be played between two players.The player who connects the four bubbles first wins the game . The bubbles could be connected horizontally , vertically or diagonally.The players have to be together since multiplayer feature is not available .|

0 commit comments

Comments
 (0)