Skip to content
Open
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
25 changes: 23 additions & 2 deletions src/composables/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface GameState {
board: BlockState[][]
mineGenerated: boolean
status: GameStatus
mines: number
startMS?: number
endMS?: number
}
Expand Down Expand Up @@ -53,6 +54,7 @@ export class GamePlay {
this.mines = mines

this.state.value = {
mines: this.mines,
mineGenerated: false,
status: 'ready',
board: Array.from({ length: this.height }, (_, y) =>
Expand Down Expand Up @@ -111,8 +113,8 @@ export class GamePlay {
})
}

expendZero(block: BlockState) {
if (block.adjacentMines)
expendZero(block: BlockState, flag: boolean = true) {
if (flag && (block.adjacentMines || block.mine))
return

this.getSiblings(block)
Expand Down Expand Up @@ -156,6 +158,25 @@ export class GamePlay {
this.expendZero(block)
}

onDblClick(block: BlockState) {
if (this.state.value.status !== 'play' || !block.revealed)
return

const { mineNum, flagNum, trueFlag } = this.getSiblings(block).reduce((prev, cur) => {
if (cur.mine)
prev.mineNum++
if (cur.flagged)
prev.flagNum++
if (cur.mine && cur.flagged)
prev.trueFlag++
if (!cur.mine && cur.flagged)
this.onGameOver('lost')
return prev
}, { mineNum: 0, flagNum: 0, trueFlag: 0 })
if (mineNum === flagNum && flagNum === trueFlag)
this.expendZero(block, false)
}

getSiblings(block: BlockState) {
return directions.map(([dx, dy]) => {
const x2 = block.x + dx
Expand Down
7 changes: 4 additions & 3 deletions src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const state = $computed(() => play.board)

const mineRest = $computed(() => {
if (!play.state.value.mineGenerated)
return play.mines
return play.blocks.reduce((a, b) => a - (b.flagged ? 1 : 0), play.mines)
return play.state.value.mines
return play.blocks.reduce((a, b) => a - (b.flagged ? 1 : 0), play.state.value.mines)
})

function newGame(difficulty: 'easy' | 'medium' | 'hard') {
Expand All @@ -25,7 +25,7 @@ function newGame(difficulty: 'easy' | 'medium' | 'hard') {
play.reset(16, 16, 40)
break
case 'hard':
play.reset(16, 30, 99)
play.reset(30, 16, 99)
break
}
}
Expand Down Expand Up @@ -78,6 +78,7 @@ watchEffect(() => {
@click="play.onClick(block)"
@lrclick="play.autoExpand(block)"
@contextmenu.prevent="play.onRightClick(block)"
@dblclick="play.onDblClick(block)"
/>
</div>
</div>
Expand Down