Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new project - N_Queen #464

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions public/N_Queen/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>N-Queens Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">

<h1>N-Queens Game</h1>

<div class="borad-details">
<div class="board-size">
<label for="boardSize">Choose Board Size (N):</label>
<input type="number" id="boardSize" min="4" value="4">
</div>
<div class="start-reset-button">
<button id="startGame">Start Game</button>
</div>
</div>

<button id="resetGame" class="reset-button" style="display: none;">Reset Game</button>
<p id="message"></p>
<div id="gameBoard"></div>

</div>
<script src="script.js"></script>
</body>
</html>
81 changes: 81 additions & 0 deletions public/N_Queen/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

const startGameButton = document.getElementById("startGame");
const resetGameButton = document.getElementById("resetGame");
const boardInput = document.getElementById("boardSize");
const board = document.getElementById("gameBoard");
const message = document.getElementById("message");

let queens = [];

startGameButton.addEventListener("click", startGame);
resetGameButton.addEventListener("click", resetGame);

function startGame() {
const n = parseInt(boardInput.value);
if (isNaN(n) || n < 4) {
alert("Please enter a valid board size (minimum 4).");
return;
}

resetGame();

board.style.gridTemplateColumns = `repeat(${n}, 60px)`;
board.innerHTML = "";
message.textContent = "";

for (let i = 0; i < n * n; i++) {
const square = document.createElement("div");
square.classList.add("square");
square.addEventListener("click", () => placeQueen(i, n));
board.appendChild(square);
}

queens = [];
startGameButton.style.display = "none";
resetGameButton.style.display = "inline-block";
}

function placeQueen(index, n) {
const row = Math.floor(index / n);
const col = index % n;

if (queens.some(([r, c]) => r === row || c === col || Math.abs(r - row) === Math.abs(c - col))) {
message.textContent = "Invalid placement! Queens are attacking each other. Try again.";
message.classList.add("error");
highlightConflicts(row, col, n);
return;
}

queens.push([row, col]);
const squares = document.querySelectorAll(".square");
squares[index].innerHTML = `<span class="queen">&#9819;</span>`;
message.textContent = "";

if (queens.length === n) {
message.textContent = "Congratulations! You've placed all queens safely!";
message.classList.remove("error");
}
}

function highlightConflicts(row, col, n) {
const squares = document.querySelectorAll(".square");
squares.forEach((square, index) => {
const r = Math.floor(index / n);
const c = index % n;

if (r === row || c === col || Math.abs(r - row) === Math.abs(c - col)) {
square.classList.add("highlight");
} else {
square.classList.remove("highlight");
}
});
}

function resetGame() {
board.innerHTML = "";
queens = [];
message.textContent = "";
message.classList.remove("error");
startGameButton.style.display = "inline-block";
resetGameButton.style.display = "none";
}
136 changes: 136 additions & 0 deletions public/N_Queen/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

body {
font-family: 'Arial', sans-serif;
background-color: #121212;
color: #ffffff;
text-align: center;
margin: 0;
padding: 0;
}

.container {
max-width: fit-content;
margin: 20px auto;
padding: 20px;
background: #1e1e2f;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
border-radius: 15px;
}

h1 {
font-size: 2.5em;
color: #ff6347;
margin-bottom: 20px;
}

label {
font-weight: bold;
font-size: 1.2em;
color: #ffa07a;
padding-bottom: 20px;
}

input[type="number"] {
margin: 10px 0;
padding: 8px;
font-size: 1.1em;
border: 2px solid #ffa07a;
border-radius: 8px;
width: 80px;
text-align: center;
background-color: #282c34;
color: #f6f6f6;
}

button {
background-color:#48bb78 ;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 1.2em;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #6cde9b;
}

#gameBoard {
display: grid;
margin: 20px auto;
border: 4px solid #ff6347;
background: #2c2f40;
width: fit-content;
border-radius: 10px;
}

.square {
width: 60px;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
border: 0.5px solid #e4806e;
cursor: pointer;
transition: background-color 0.3s;
}

.square:nth-child(odd) {
background-color: #495057;
}

.square:nth-child(even) {
background-color: #343a40;
}

.queen {
font-size: 32px;
color: #ffa07a;
pointer-events: none;
}

#message {
font-size: 1.4em;
font-weight: bold;
margin-top: 20px;
color: #48bb78;
}

#message.error {
color: #e53e3e;
}

.highlight {
background-color: rgba(229, 62, 62, 0.6);
}

.reset-button {
background-color: #ff6347;
margin-top: 10px;
padding: 10px 20px;
color: #fff;
border: none;
border-radius: 8px;
font-size: 1.2em;
cursor: pointer;
transition: background-color 0.3s;

}

.reset-button:hover {
background-color:#f34b2e;
}

.board-details {
display: flex;
flex-direction: column;
}

.board-size{
padding-bottom:20px;
}


Loading