Skip to content

Commit

Permalink
bulls and cows game added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankitapanda25 committed May 12, 2024
1 parent 256686c commit c071ae7
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Games/Bulls_and_Cows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**Description**
It is a game where the user takes turns to guess a four digit number generated by the computer

**Rules**
Guess a 4-digit code where each digit is unique.
You'll receive feedback after each guess:
Bulls: Correct digit in the correct position.
Cows: Correct digit in the wrong position.
Keep guessing until you get 4 bulls!
Binary file added Games/Bulls_and_Cows/assets/bulls-and-cows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Games/Bulls_and_Cows/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bulls and Cows Game</title>
<link rel="stylesheet" href="./styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
</head>
<body>
<div class="wrapper">
<div class="navbar">
<h1>Bulls and Cows Game</h1>
<div style="text-align:center;
font-size: 30px; "><a href="https://kunjgit.github.io/GameZone/"><i style="color:white;" class="fas fa-home home-icon"></i></a></div>

</div>
<div id="console"></div>
<input type="text" id="try" placeholder="e.g. 1234" onkeyup="if (event.keyCode == 13) document.getElementById('submit').click()">
<button id="submit" onclick="check()">Submit</button>
<div id="history"></div>
<div id="rules">
<strong>Rules:</strong>
<ul>
<li>Guess a 4-digit code where each digit is unique.</li>
<li>You'll receive feedback after each guess:</li>

<li><strong>Bulls:</strong> Correct digit in the correct position.</li>
<li><strong>Cows:</strong> Correct digit in the wrong position.</li>

<li>Keep guessing until you get 4 bulls!</li>
</ul>
</div>
</div>

<script src="./script.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions Games/Bulls_and_Cows/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let secretCode = generateRandomCode();
let guesses = [];

function generateRandomCode() {
const uniqueDigits = new Set();
while (uniqueDigits.size < 4) {
uniqueDigits.add(Math.floor(Math.random() * 10));
}
return Array.from(uniqueDigits).join("");
}

function check() {
const guess = document.getElementById("try").value;
let bulls = 0;
let cows = 0;

for (let i = 0; i < 4; i++) {
if (guess[i] === secretCode[i]) {
bulls++;
} else if (secretCode.includes(guess[i]) && guess[i] !== secretCode[i]) {
cows++;
}
}

const result = `${bulls} bull${bulls !== 1 ? "s" : ""} and ${cows} cow${cows !== 1 ? "s" : ""}`;
const guessInfo = `Your guess: ${guess} - ${result}`;
guesses.push(guessInfo);

document.getElementById("console").innerText = guessInfo;
document.getElementById("history").innerHTML = `<strong>Guess History:</strong><br>${guesses.join("<br>")}`;

if (bulls === 4) {
alert("Congratulations, you guessed it!");
// Reset the game
secretCode = generateRandomCode();
guesses = [];
document.getElementById("try").value = ""; // Clear the guess input
document.getElementById("console").innerText = "";
document.getElementById("history").innerHTML = "";
}
}
56 changes: 56 additions & 0 deletions Games/Bulls_and_Cows/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
html, body {
height: 100%;
margin: 0;
text-align: center;
}
.wrapper {
background-color: lightgrey;
height: 100%;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
.navbar{
display: flex;
background-color: black;
color: white;
padding-top: 1em;
padding-bottom: 1em;
position: relative;
top: 0;
justify-content: space-between;
padding-inline: 5vh;
align-items: center;
}
h1 {


}
#console {
text-align: left;
}
#try {
width: 150px;
margin: 2em;
}
li{
list-style: none;
margin-top: 3px;
/* width: fit-content; */
}
#history {
margin-top: 1em;

}
#rules {
margin-top: 1em;
display: flex;
flex-direction: column;
justify-content: center;
gap: 4px;

}

ul{
display: flex;
flex-direction: column;

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ This repository also provides one such platforms where contributers come over an
| [Rotating_Elements](https://github.com/tanujbordikar/GameZone/tree/Rotating_Elements) | [Chopsticks](https://github.com/kunjgit/GameZone/tree/main/Games/Chopsticks) | [Anime Clicker](https://github.com/kunjgit/GameZone/tree/main/Games/Anime_Clicker) | [3D Snake](https://github.com/kunjgit/GameZone/tree/main/Games/3d_Snake) | [Rocket_Showdown](https://github.com/tanujbordikar/GameZone/tree/Rocket_Showdown) |
| [Find Extra Cube](https://github.com/kunjgit/GameZone/tree/main/Games/Find_Extra_Cube) | [PathPlex](https://github.com/kunjgit/GameZone/tree/main/Games/Pathplex) | [CSS Select](https://github.com/kunjgit/GameZone/tree/main/Games/CSS_Select) | [Squid](https://github.com/kunjgit/GameZone/tree/main/Games/Squid_Game) | [CSS Crossword](https://github.com/kunjgit/GameZone/tree/main/Games/CSS_Crossword) |
| [CSS Select](https://github.com/kunjgit/GameZone/tree/main/Games/CSS_Select) | [Squid](https://github.com/kunjgit/GameZone/tree/main/Games/Squid_Game) | [Flip Coin](https://github.com/kunjgit/GameZone/tree/main/Games/Flip_Coin) | [Witty Word Quest](https://github.com/kunjgit/GameZone/tree/main/Games/witty_word_quest) | [Typing Game](https://github.com/Ishan-77/GameZone/tree/main/Games/Typing_Game) |
| [numeral-whiz](https://github.com/Ishan-77/GameZone/tree/main/Games/numeral-whiz) | [candy_match](https://github.com/kunjgit/GameZone/tree/main/Games/Candy_Match_Saga) | [Crossy_Road](https://github.com/tanujbordikar/GameZone/tree/Crossy_Road) | [HueHero](https://github.com/kunjgit/GameZone/tree/main/Games/HueHero) | [Puzzel_Winner](https://github.com/kunjgit/GameZone/tree/main/Games/Puzzel_Winner) |
| [numeral-whiz](https://github.com/Ishan-77/GameZone/tree/main/Games/numeral-whiz) | [candy_match](https://github.com/kunjgit/GameZone/tree/main/Games/Candy_Match_Saga) | [Crossy_Road](https://github.com/tanujbordikar/GameZone/tree/Crossy_Road) | [HueHero](https://github.com/kunjgit/GameZone/tree/main/Games/HueHero) | [Puzzel_Winner](https://github.com/kunjgit/GameZone/tree/main/Games/Puzzel_Winner) |
| [Emoji_Intruder](https://github.com/kunjgit/GameZone/tree/main/Games/Emoji_Intruder) | [Guess The Weapon](https://github.com/kunjgit/GameZone/tree/main/Games/Guess_The_Weapon) | [Guess Who](https://github.com/kunjgit/GameZone/tree/main/Games/Guess_Who) | | |


Expand Down
20 changes: 20 additions & 0 deletions assets/index_old.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ <h3 class="project-title"><a href="https://github.com/kunjgit/GameZone/tree/main

</a>
</li>


<li class="project-item active" data-filter-item data-category="open source">
<a href="./Games/Simon_Game_Challenge/">
Expand Down Expand Up @@ -2618,7 +2619,26 @@ <h3 class="project-title"><a href="https://github.com/kunjgit/GameZone/tree/main
</li>


<li class="project-item active" data-filter-item data-category="open source">
<a href="./Games/Bulls_and_cows/">

<figure class="project-img">
<div class="project-item-icon-box">
<img id="joystick"
src="https://raw.githubusercontent.com/Tarikul-Islam-Anik/Animated-Fluent-Emojis/master/Emojis/Activities/Video%20Game.png"
alt="Eye" width="3" />
</div>

<img src="./assets/images/bulls-and-cows.png" alt="bulls and cows game" loading="lazy">
</figure>

<h3 class="project-title"><a href="https://github.com/kunjgit/GameZone/tree/main/Games/Bulls_and_cows"
target="_blank">137. Bulls and Cows 🔗</a></h3>

<p class="project-category">Can you guess?</p>

</a>
</li>



Expand Down
7 changes: 5 additions & 2 deletions assets/js/gamesData.json
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,10 @@
"gameTitle": "Guess_The_Weapon",
"gameUrl": "Guess_The_Weapon",
"thumbnailUrl": "icon.png"
},
"386": {
"gameTitle": "Bulls_and_Cows",
"gameUrl": "Bulls_and_Cows",
"thumbnailUrl": "bulls-and-cows.png"
}


}

0 comments on commit c071ae7

Please sign in to comment.