Skip to content

Commit b56cabb

Browse files
Merge pull request #86 from ssachis/main
sudoku game
2 parents 30c9105 + 6c775f4 commit b56cabb

File tree

4 files changed

+441
-0
lines changed

4 files changed

+441
-0
lines changed

Games/Game_01/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
https://user-images.githubusercontent.com/89895559/148755705-4ddf9cf7-bc77-42d4-a912-962035dbafe7.mp4
4+

Games/Game_01/assets/index.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
const easy = [
2+
"6------7------5-2------1---362----81--96-----71--9-4-5-2---651---78----345-------",
3+
"685329174971485326234761859362574981549618732718293465823946517197852643456137298"
4+
];
5+
const medium = [
6+
"--9-------4----6-758-31----15--4-36-------4-8----9-------75----3-------1--2--3--",
7+
"619472583243985617587316924158247369926531478734698152891754236365829741472163895"
8+
];
9+
const hard = [
10+
"-1-5-------97-42----5----7-5---3---7-6--2-41---8--5---1-4------2-3-----9-7----8--",
11+
"712583694639714258845269173521436987367928415498175326184697532253841769976352841"
12+
];
13+
var timer;
14+
var timeRemaining;
15+
var lives;
16+
var selectedNum;
17+
var selectedTiles;
18+
var disableSelect;
19+
window.onload=function(){
20+
21+
22+
id("start").addEventListener("click",startGame);
23+
for(let i=0;i<id("number-container").children.length;i++){
24+
id("number-container").children[i].addEventListener("click",function(){
25+
26+
if(!disableSelect){
27+
if(this.classList.contains("selected")){
28+
this.classList.remove("selected");
29+
selectedNum=null;
30+
}else{
31+
for(let i=0;i<9;i++)
32+
{
33+
id("number-container").children[i].classList.remove("selected");
34+
}
35+
36+
this.classList.add("selected");
37+
selectedNum=this;
38+
updateMove();
39+
40+
41+
}
42+
}
43+
});
44+
45+
}
46+
}
47+
48+
49+
function startGame(){
50+
51+
id("lives").textContent="";
52+
let board;
53+
if(id("diff-1").checked) board=easy[0];
54+
else if (id("diff-2").checked) board=medium[0];
55+
else board=hard[0];
56+
57+
disableSelect=false;
58+
generateBoard(board);
59+
startTimer();
60+
if(id("theme-1").checked){
61+
qs("body").classList.remove("dark");
62+
}else{
63+
qs("body").classList.add("dark");
64+
}
65+
id("number-container").classList.remove("hidden");
66+
67+
}
68+
function startTimer(){
69+
if(id("time-1").checked) timeRemaining=300;
70+
else if(id("time-2").checked) timeRemaining=600;
71+
else if(id("time-3").checked) timeRemaining=900;
72+
73+
id("timer").textContent=timeConversion(timeRemaining);
74+
timer=setInterval(function(){
75+
timeRemaining--;
76+
if(timeRemaining==0) endGame();
77+
id("timer").textContent=timeConversion(timeRemaining);
78+
},1000)
79+
}
80+
function timeConversion(time){
81+
let minutes=Math.floor(time/60);
82+
if(minutes<10) minutes="0"+minutes;
83+
let seconds=time%60;
84+
if(seconds<10) seconds="0"+seconds;
85+
return minutes+":"+seconds;
86+
87+
88+
}
89+
90+
function generateBoard(board){
91+
clearPrevious();
92+
let idCount=0;
93+
for(let i=0;i<81;i++){
94+
let tile=document.createElement("p");
95+
if(board.charAt(i)!="-"){
96+
tile.textContent=board.charAt(i);
97+
}else{
98+
tile.addEventListener("click",function(){
99+
if(!disableSelect){
100+
if(tile.classList.contains("selected")){
101+
tile.classList.remove("selected");
102+
selectedTiles=null;
103+
}else{
104+
for(let i=0;i<81;i++){
105+
qsa(".tile")[i].classList.remove("selected");
106+
107+
}
108+
tile.classList.add("selected");
109+
selectedTiles=tile;
110+
updateMove();
111+
}
112+
}
113+
114+
});
115+
}
116+
tile.id=idCount;
117+
idCount++;
118+
tile.classList.add("tile");
119+
if((tile.id>17 && tile.id<27)||(tile.id>44 && tile.id<54)){
120+
tile.classList.add("bottomborder");
121+
}
122+
if((tile.id+1)%9==3 ||(tile.id+1)%9==6 ){
123+
tile.classList.add("rightborder");
124+
}
125+
id("board").appendChild(tile);
126+
}
127+
128+
129+
}
130+
131+
function updateMove(){
132+
if(selectedTiles && selectedNum){
133+
selectedTiles.textContent=selectedNum.textContent;
134+
if(checkCorrect(selectedTiles)){
135+
136+
selectedTiles.classList.remove("selected");
137+
138+
selectedNum.classList.remove("selected");
139+
selectedTiles=null;
140+
selectedNum=null;
141+
if(checkDone()){
142+
endGame();
143+
}
144+
145+
}else{
146+
disableSelect=true;
147+
selectedTiles.classList.add("incorrect");
148+
setTimeout(function(){
149+
disableSelect=false;
150+
selectedTiles.classList.remove("incorrect");
151+
selectedTiles.classList.remove("selected");
152+
selectedTiles.classList.remove("selected");
153+
selectedTiles.textContent="";
154+
selectedTiles=null;
155+
selectedNum=null;
156+
157+
},1000);
158+
159+
160+
}
161+
162+
}
163+
}
164+
function checkDone(){
165+
let tile=qsa(".tile");
166+
for(let i=0;i<tile.length;i++){
167+
if(tile[i].textContent==="")
168+
return false;
169+
}
170+
return true;
171+
172+
}
173+
174+
175+
function endGame(){
176+
disableSelect=true;
177+
clearTimeout(timer);
178+
if(timeRemaining === 0){
179+
id("lives").textContent="you lost!";
180+
181+
}else{
182+
183+
id("lives").textContent="you won!";
184+
}
185+
}
186+
function checkCorrect(tile){
187+
let solution;
188+
if(id("diff-1").checked) solution=easy[1]
189+
else if (id("diff-2").checked) solution=medium[1];
190+
else solution=hard[1];
191+
if(solution.charAt(tile.id)==tile.textContent){
192+
return true;
193+
194+
}else{
195+
return false;
196+
}
197+
}
198+
199+
function clearPrevious() {
200+
201+
let tiles= qsa(".tile");
202+
for(let i=0;i<tiles.length;i++){
203+
tiles[i].remove();
204+
205+
}
206+
if (timer) clearTimeout(timer);
207+
for(let i=0;i<id("number-container").children.length;i++){
208+
id("number-container").children[i].classList.remove("selected");
209+
}
210+
selectedTiles=null;
211+
selectedNum=null;
212+
}
213+
function qs(selector){
214+
215+
return document.querySelector(selector);
216+
}
217+
function qsa(selector){
218+
219+
220+
return document.querySelectorAll(selector);
221+
}
222+
223+
function id(id){
224+
return document.getElementById(id);
225+
}

Games/Game_01/assets/sodoku.html

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Sodoku</title>
8+
<link
9+
rel="stylesheet"
10+
href="C:\Users\sachi\OneDrive\Desktop\sodoku\styles.css"
11+
/>
12+
<script src="index.js"></script>
13+
</head>
14+
15+
<body>
16+
<header class="header">
17+
<h1 id="A">SUDOKU</h1>
18+
<div id="setup-game">
19+
<div id="diff">
20+
<h2 id="B">Choose difficulty:</h2>
21+
<label id="label">
22+
<input
23+
type="radio"
24+
id="diff-1"
25+
name="diff"
26+
value="easy"
27+
checked
28+
/>Easy</label
29+
>
30+
<label id="label">
31+
<input
32+
type="radio"
33+
id="diff-2"
34+
name="diff"
35+
value="easy"
36+
/>Medium</label
37+
>
38+
<label id="label">
39+
<input
40+
type="radio"
41+
id="diff-3"
42+
name="diff"
43+
value="easy"
44+
/>Hard</label
45+
>
46+
</div>
47+
<div id="time">
48+
<h2 id="B">Choose time:</h2>
49+
<label id="label">
50+
<input type="radio" id="time-1" name="time" value="five" checked />5
51+
min</label
52+
>
53+
<label id="label">
54+
<input type="radio" id="time-2" name="time" value="ten" />10
55+
min</label
56+
>
57+
<label id="label">
58+
<input type="radio" id="time-3" name="time" value="fifteen" />15
59+
min</label
60+
>
61+
</div>
62+
<div id="theme">
63+
<h2 id="B">Choose theme:</h2>
64+
<label id="label"
65+
><input
66+
type="radio"
67+
id="theme-1"
68+
name="theme"
69+
value="light"
70+
checked
71+
/>light</label
72+
>
73+
<label id="label"
74+
><input
75+
type="radio"
76+
id="theme-2"
77+
name="theme"
78+
value="Dark"
79+
/>Dark</label
80+
>
81+
</div>
82+
</div>
83+
<button id="start">New Game</button>
84+
</header>
85+
<div id="stats">
86+
<p id="timer"></p>
87+
<p id="lives"></p>
88+
</div>
89+
<div ig="game">
90+
<div id="board"></div>
91+
92+
<div id="number-container" class="hidden">
93+
<p id="one">1</p>
94+
<p id="two">2</p>
95+
<p id="three">3</p>
96+
<p id="four">4</p>
97+
<p id="five">5</p>
98+
<p id="six">6</p>
99+
<p id="seven">7</p>
100+
<p id="eight">8</p>
101+
<p id="nine">9</p>
102+
</div>
103+
</div>
104+
</body>
105+
</html>

0 commit comments

Comments
 (0)