-
Notifications
You must be signed in to change notification settings - Fork 1
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
Additional improvements #16
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,116 +49,54 @@ button { | |
} | ||
|
||
.game-canvas { | ||
display: flex; | ||
flex-direction: column; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way we don't need all the specific classes further down |
||
margin: 20px auto; | ||
padding: 5px 0 0 5px; | ||
background-color: #55b4ff; | ||
justify-content: center; | ||
width: 500px; | ||
height: 500px; | ||
} | ||
|
||
.row { | ||
display: flex; | ||
flex: auto; | ||
flex-wrap: nowrap; | ||
} | ||
|
||
[class^="cell"] { | ||
background-color: rgba(255, 255, 255, 0.6); | ||
text-align: center; | ||
} | ||
|
||
[class^="cell"]:hover { | ||
background-color: rgba(255, 255, 255, 0.8);; | ||
} | ||
|
||
.cell-8 { | ||
margin-right: 5px; | ||
margin-bottom: 5px; | ||
height: 60px; | ||
width: 60px; | ||
flex: auto; | ||
} | ||
|
||
.cell-10 { | ||
margin-right: 5px; | ||
margin-bottom: 5px; | ||
height: 60px; | ||
width: 60px; | ||
} | ||
|
||
.cell-12 { | ||
margin-right: 5px; | ||
margin-bottom: 5px; | ||
height: 60px; | ||
width: 60px; | ||
} | ||
|
||
@media screen and (max-width: 1200px) { | ||
.cell-12 { | ||
height: 50px; | ||
width: 50px; | ||
} | ||
} | ||
|
||
@media screen and (max-width: 992px) { | ||
.cell-10 { | ||
height: 50px; | ||
width: 50px; | ||
} | ||
|
||
.cell-12 { | ||
height: 40px; | ||
width: 40px; | ||
} | ||
[class^="cell"]:hover { | ||
background-color: rgba(255, 255, 255, 0.8); | ||
} | ||
|
||
@media screen and (max-width: 768px) { | ||
.cell-8 { | ||
height: 50px; | ||
width: 50px; | ||
margin-right: 2px; | ||
margin-bottom: 2px; | ||
} | ||
.game-canvas { | ||
padding: 2px 0 0 2px; | ||
width: 450px; | ||
height: 450px; | ||
|
||
.cell-10 { | ||
height: 40px; | ||
width: 40px; | ||
margin-right: 2px; | ||
margin-bottom: 2px; | ||
} | ||
|
||
.cell-12 { | ||
height: 35px; | ||
width: 35px; | ||
[class^="cell"] { | ||
margin-right: 2px; | ||
margin-bottom: 2px; | ||
} | ||
|
||
.game-canvas { | ||
padding: 2px 0 0 2px; | ||
} | ||
} | ||
|
||
@media screen and (max-width: 567px) { | ||
.cell-8 { | ||
height: 40px; | ||
width: 40px; | ||
margin-right: 1px; | ||
margin-bottom: 1px; | ||
.game-canvas { | ||
padding: 1px 0 0 1px; | ||
width: 400px; | ||
height: 400px; | ||
} | ||
|
||
.cell-10 { | ||
height: 35px; | ||
width: 35px; | ||
[class^="cell"] { | ||
margin-right: 1px; | ||
margin-bottom: 1px; | ||
} | ||
|
||
.cell-12 { | ||
height: 30px; | ||
width: 30px; | ||
margin-right: 1px; | ||
margin-bottom: 1px; | ||
} | ||
|
||
.game-canvas { | ||
padding: 1px 0 0 1px; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,25 +7,28 @@ import { table } from "./app.js"; | |
|
||
//// Left click | ||
|
||
export function clickOnCell(event){ | ||
export function clickOnCell(e){ | ||
|
||
if (!table.timeCounterIsStart){startTimer()} | ||
if (table.won === undefined && !table.timeCounterIsStart){startTimer()} | ||
|
||
let cellId = event.target.id; | ||
let cellId = e.target.id; | ||
let cellElement = document.getElementById(cellId); | ||
let cell = table.tableCells[cellId]; | ||
|
||
if (!cell || cell.haveFlag || table.won !== undefined) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is newly introduced to realise event delegation. Same further down in |
||
|
||
if (!table.firstClick) { | ||
firstClick(table, cellElement, cellId) | ||
} else { | ||
let haveMine = table.tableCells[cellId].haveMine; | ||
let amountNeighborsWithMine = table.tableCells[cellId].amountNeighborsWithMine; | ||
let haveMine = cell.haveMine; | ||
let amountNeighborsWithMine = cell.amountNeighborsWithMine; | ||
|
||
if (haveMine){ | ||
openCellWithMine(cellElement, cellId, table); | ||
gameOver(); | ||
} else if(!haveMine && amountNeighborsWithMine > 0){ | ||
getNumberToCell(amountNeighborsWithMine, cellElement); | ||
table.tableCells[cellId].isOpen = true; | ||
cell.isOpen = true; | ||
} else { | ||
openEmptyCell(cellElement, cellId, table); | ||
} | ||
|
@@ -43,9 +46,11 @@ export function clickOnCell(event){ | |
export function rightClickOnCell(e) { | ||
e.preventDefault(); | ||
|
||
let cellId = event.target.id; | ||
let cellId = e.target.id; | ||
let cellElement = document.getElementById(cellId); | ||
|
||
if (!table.tableCells[cellId] || table.won !== undefined) return; | ||
|
||
let haveFlag = table.tableCells[cellId].haveFlag; | ||
|
||
if (!table.timeCounterIsStart){startTimer()} | ||
|
@@ -89,9 +94,6 @@ function firstClick(table, cellElement, cellId){ | |
table.tableCells[cellId].firstClickOpen = true; | ||
table.tableCells[cellId].haveFlag = false; | ||
cellElement.style.backgroundImage = ''; | ||
cellElement.removeEventListener("contextmenu", rightClickOnCell, false); | ||
cellElement.removeEventListener('touchstart', onTouchStart,false); | ||
cellElement.removeEventListener('touchend', onTouchEnd,false); | ||
|
||
let neighbors = table.tableCells[cellId].neighbors; | ||
for (const neighbor in neighbors){ | ||
|
@@ -173,19 +175,13 @@ function getNumberToCell(amountNeighborsWithMine, cellElement){ | |
cellElement.style.backgroundSize = "cover"; | ||
cellElement.style.backgroundColor = "rgba(255, 255, 255, 0.3)"; | ||
cellElement.style.backgroundImage = urlImg; | ||
cellElement.removeEventListener("contextmenu", rightClickOnCell, false); | ||
cellElement.removeEventListener('touchstart', onTouchStart,false); | ||
cellElement.removeEventListener('touchend', onTouchEnd,false); | ||
} | ||
|
||
function openEmptyCell(cellElement, cellId, table){ | ||
cellElement.style.backgroundColor = "rgba(255, 255, 255, 0.3)"; | ||
table.tableCells[cellId].isOpen = true; | ||
table.tableCells[cellId].haveFlag = false; | ||
cellElement.style.backgroundImage = ''; | ||
cellElement.removeEventListener("contextmenu", rightClickOnCell, false); | ||
cellElement.removeEventListener('touchstart', onTouchStart,false); | ||
cellElement.removeEventListener('touchend', onTouchEnd,false); | ||
checkNeighborsWithNeighborsWithMine(table, cellId); | ||
} | ||
|
||
|
@@ -232,14 +228,12 @@ function checkAndAddNumberCellsWithFlag() { | |
} | ||
|
||
function getFlagToCell(cellElement, table, cellId){ | ||
cellElement.removeEventListener("click", clickOnCell); | ||
cellElement.style.backgroundSize = "cover"; | ||
cellElement.style.backgroundImage = "url('./src/img/flag.png')"; | ||
table.tableCells[cellId].haveFlag = true; | ||
} | ||
|
||
function removeFlagFromCell(cellElement, table, cellId){ | ||
cellElement.addEventListener("click", clickOnCell); | ||
cellElement.style.backgroundSize = ""; | ||
cellElement.style.backgroundImage = ""; | ||
table.tableCells[cellId].haveFlag = false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,18 @@ | ||
import { | ||
buildGameTable | ||
} from "./gameBuilder.js"; | ||
import { removeGameTable } from "./gameState"; | ||
import { buildGameTable } from "./gameBuilder.js"; | ||
import { stopTimer, resetTimeCounter } from "./timer"; | ||
|
||
var level; | ||
setLevel(); | ||
const restartElement = document.getElementById("restart"); | ||
const levelElement = document.getElementById('level'); | ||
|
||
export var table = buildGameTable(level); | ||
export let table = {}; | ||
|
||
document.getElementById("restart").addEventListener("click", restartGame); | ||
restartElement.addEventListener("click", restartGame); | ||
levelElement.addEventListener("change", restartGame); | ||
|
||
restartGame(); | ||
|
||
function restartGame() { | ||
setLevel(); | ||
removeGameTable(); | ||
table = buildGameTable(level); | ||
table = buildGameTable(levelElement.value.split(',').map(item => +item)); | ||
stopTimer(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We directly pass the value from the |
||
resetTimeCounter(); | ||
} | ||
|
||
function setLevel() { | ||
level = document.getElementById('level').value; | ||
|
||
switch (level) { | ||
case "easy": | ||
level = { | ||
rows: 8, | ||
cols: 8, | ||
maxMines:10 | ||
} | ||
break; | ||
case "normal": | ||
level = { | ||
rows: 10, | ||
cols: 10, | ||
maxMines: 14 | ||
} | ||
break; | ||
case "hard": | ||
level = { | ||
rows: 12, | ||
cols: 12, | ||
maxMines: 20 | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
app.js
line 15 to see how this helps.