Skip to content

Commit

Permalink
update: able to cheat
Browse files Browse the repository at this point in the history
  • Loading branch information
leyiang committed Feb 20, 2024
1 parent a95eae9 commit 12cd056
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/minesweeper/core/Arena.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,33 @@ export default class Arena {
mark( x, y ) {
const cell = this.get( x, y );
if( ! cell ) return;
if( cell.open ) return;
if( cell.open ) return this.help(x, y);

cell.flag = ! cell.flag;
}

help(x, y) {
console.log(x, y);
const list = this.mine_list.filter(mine => {
if( mine.flag ) return false;
if( mine.help ) return false;

return true;
});

list.sort( (a, b) => {
const distA = Math.hypot( (a.x - x), (a.y - y) );
const distB = Math.hypot( (b.x - x), (b.y - y) );

return distA - distB;
});

const mine = list[0];
if( ! mine ) return;

mine.flag = true;
}

floodFill(x, y) {
let res = [ this.get(x, y) ];
let list = [ [x, y] ];
Expand Down
1 change: 1 addition & 0 deletions src/minesweeper/core/Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Cell {
this.diff = .1;
this.speed = -.15;
this.speedX = Math.random() % .05
this.help = false;

this.cover = {
x: this.x,
Expand Down
10 changes: 8 additions & 2 deletions src/minesweeper/core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class Game {
constructor() {
this.over = false;
this.ribbon_list = [];
this.help_list = [];
}

init() {
Expand All @@ -20,14 +21,19 @@ export default class Game {
if( this.over ) return;

// Generate Arena
this.arena.initArena( x, y );
if( ! this.arena.initialized ) {
this.arena.initArena( x, y );
this.help_list = this.arena.mine_list.slice();
console.log( this.help_list );
}

this.arena.click( x, y );
});

game.event.on("right", (x, y) => {
if( this.over ) return;
if( ! this.arena.initialized ) return;

this.arena.initArena( x, y );
this.arena.mark( x, y );
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/minesweeper/game/interact.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export default function interact(el=document.body, mode ) {
el.addEventListener("mousemove", function( e ) {
const x = Math.floor( e.clientX / mode.size );
const y = Math.floor( e.clientY / mode.size );
const x = Math.floor( e.offsetX / mode.size );
const y = Math.floor( e.offsetY / mode.size );

game.event.emit("move", x, y );
});

el.addEventListener("click", function( e ) {
const x = Math.floor( e.clientX / mode.size );
const y = Math.floor( e.clientY / mode.size );
const x = Math.floor( e.offsetX / mode.size );
const y = Math.floor( e.offsetY / mode.size );

game.event.emit("click", x, y );
});

el.addEventListener("contextmenu", function( e ) {
e.preventDefault();

const x = Math.floor( e.clientX / mode.size );
const y = Math.floor( e.clientY / mode.size );
const x = Math.floor( e.offsetX / mode.size );
const y = Math.floor( e.offsetY / mode.size );

game.event.emit("right", x, y );
});
Expand Down
4 changes: 4 additions & 0 deletions src/minesweeper/game/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ const rand = max => Math.floor( Math.random() * max );

const dist = (x1, y1, x2, y2) => {
return Math.sqrt( (x2-x1) ** 2 + (y2 - y1) ** 2);
}

function getRandomItem( arr ) {
return arr[ Math.floor( Math.random() * arr.length ) ];
}
10 changes: 10 additions & 0 deletions src/minesweeper/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@
transform: translate3d(2px, 2px, 0);
}
}

canvas {
display: block;
}

html {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-size: 20px;
}
</style>
</head>
<body>

<canvas id="screen"></canvas>
<span>If u need help, right click on opend cells</span>
<script src="./app.js" type="module"></script>

</body>
Expand Down

0 comments on commit 12cd056

Please sign in to comment.