Skip to content

Commit

Permalink
Merge pull request #22 from KerorinNorthFox/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KerorinNorthFox authored Sep 18, 2023
2 parents f9e1d15 + 33fff6e commit e1273d3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# CLI MineSweeper
Play Minesweeper on CLI.

<!-- # Attention
Do not use Nimble to install the game because I do not know why but Latest Version recognized old version v1.0.0(true Latest Version is v1.1.0) so you can not play the climinesweeper.
Please try [From Source](#from-source) method. -->

# Installation
```bash
$ nimble install climinesweeper
```

# Usage
```bash
$ climinesweeper
$ climinesweeper [Options]
```
```
options:
Options:
-h, --help display the help.
-v, --version display the version.
--noColor play without colors.
Expand Down
2 changes: 1 addition & 1 deletion climinesweeper.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.1.1"
version = "1.2.1"
author = "kerorinnf"
description = "Play MineSweeper on CLI"
license = "MIT"
Expand Down
15 changes: 9 additions & 6 deletions src/climinesweeper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ usage:
options:
-h, --help display the help.
-v, --version display the version.
--noColor play without colors.
--continue [number] play with a set number of continue.
--infinite play without Boom!!.
--noColor Play without colors.
--continue [number] Play with a set number of continue.
--infinite Play without Boom!!.
--new Discard save file and play a new game
[5 <= number <= 20] Set the number of vert and hor cells and start the game.
None Set the min number (5) of vert and hor cells and start the game.
Expand All @@ -29,14 +30,14 @@ proc exitProc() {.noconv.} =
showCursor()
quit(0)

proc main(blc:int, defaultContinue: int, isInfinity:bool, isNoColor:bool): void =
proc main(blc:int, defaultContinue: int, isInfinity:bool, isNoColor:bool, isNew:bool): void =
illwillInit(fullscreen=true)
setControlCHook(exitProc)
hideCursor()
# illwillの画面作成
var tb: TerminalBuffer = newTerminalBuffer(terminalWidth(), terminalHeight())
# minesweeper初期化
var game: MineSweeper = MineSweeper.init(tb, blc, defaultContinue, isInfinity, isNoColor)
var game: MineSweeper = MineSweeper.init(tb, blc, defaultContinue, isInfinity, isNoColor, isNew)

game.start()
while(true):
Expand All @@ -53,6 +54,7 @@ when isMainModule:
isNoColor: bool = false
defaultContinue: int = 3
isInfinity: bool = false
isNew: bool = false
defaultBlc: int = 5
for i, arg in args:
if isSkip:
Expand All @@ -65,6 +67,7 @@ when isMainModule:
of "--noColor": isNoColor = true
of "--continue": defaultContinue = args[i+1].parseInt; isSkip = true
of "--infinite": isInfinity = true
of "--new": isNew = true
else:
let blc: int = arg.parseInt
if blc>=MIN_BLOCK and blc<=MAX_BLOCK:
Expand All @@ -76,4 +79,4 @@ when isMainModule:
quit(1)

if isQuit: quit(0)
main(defaultBlc, defaultContinue, isInfinity, isNoColor)
main(defaultBlc, defaultContinue, isInfinity, isNoColor, isNew)
80 changes: 60 additions & 20 deletions src/pkg/minesweeper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import
std/random,
./utils

from jsony import toJson, fromJson

#================================================================
#
# Decralation
Expand All @@ -17,7 +19,7 @@ import
# Consts
#----------------------------------------------------------------
const
VERSION*: string = "v1.2.0"
VERSION*: string = "v1.2.1"
BLC_LIM_ARRAY: array[7,int] = [7, 9, 11, 13, 15, 17, 20]
BOMB_LIM_ARRAY: array[7,int] = [2, 3, 4, 5, 6, 7, 8]
WINDOW_WIDTH: int = 40 # ウィンドウの横幅
Expand Down Expand Up @@ -71,6 +73,8 @@ type MineSweeper* = ref object
doubleBlc: int
placedTotalFlags: int # 現在立っている旗の総数
remainingContinue: int # 残りコンティニュー数
isInfinity: bool
isNoColor: bool
remainingBombs: int # 現在の残り爆弾総数

mainWindow: MainWindow
Expand All @@ -84,9 +88,6 @@ type MineSweeper* = ref object
var
tb: TerminalBuffer
game: MineSweeper
remainingContinue: int
isInfinity: bool
isNoColor: bool

#----------------------------------------------------------------
# Public proc
Expand All @@ -100,6 +101,13 @@ proc showCursorPosDebug(): void {.used.} =
tb.write(1, 30, " ".repeat(100))
tb.write(1, 30, "cursorXPos:", $game.mainWindow.cursor.x, ", cursorYPos:", $game.mainWindow.cursor.y, ", oldCursorXPos:", $game.mainWindow.cursor.preX, ", oldCursorYPos:", $game.mainWindow.cursor.preY)

when hostOS == "windows":
proc removeSaveFile(): void =
discard execShellCmd("del save.txt")
else:
proc removeSaveFile(): void =
removeFile("save.txt")

#----------------------------------------------------------------
# Main Window Dec
#----------------------------------------------------------------
Expand Down Expand Up @@ -163,9 +171,9 @@ proc resetMessage(self:MessageWindow): void
#----------------------------------------------------------------
# MineSweeper Dec
#----------------------------------------------------------------
proc init*(_:type MineSweeper, terminalbuffer:var TerminalBuffer, blc:int, continueNum:int, isInf:bool, noColorFlag:bool): MineSweeper
proc init*(_:type MineSweeper, terminalbuffer:var TerminalBuffer, blc:int, continueNum:int, isInfinity:bool, isNoColor:bool, isNew:bool): MineSweeper

proc setting(self:MineSweeper, blc:int): void
proc setting(self:MineSweeper, blc:int, remainingContinue:int, isInfinity:bool, isNoColor:bool): void

proc makeBlocks(self:MineSweeper): void

Expand All @@ -187,14 +195,17 @@ proc releaseCell(self:MineSweeper, pos:int): void

proc countBombAroundCell(self:MineSweeper, pos:int): void

proc saveProgress(self:MineSweeper): void

proc loadProgress(self:var MineSweeper, isNew:bool): bool
#----------------------------------------------------------------
# Template
#----------------------------------------------------------------
template Draw(fg:ForegroundColor, bg:BackgroundColor, isBright:bool, body: untyped): untyped =
var bright = isBright
if IS_BRIGHT:
bright = false
if isNoColor:
if game.isNoColor:
tb.setForegroundColor(if fg==fgBlack: fg else: fgNone)
tb.setBackgroundColor(if bg==bgWhite: bg else: bgNone)
else:
Expand All @@ -205,9 +216,9 @@ template Draw(fg:ForegroundColor, bg:BackgroundColor, isBright:bool, body: untyp

template DrawAnimation(fgColor: ForegroundColor, isBright: bool, count: int, body: untyped): untyped =
var
fg: ForegroundColor = if isNoColor: fgWhite else: fgColor
fg: ForegroundColor = if game.isNoColor: fgWhite else: fgColor
bg: BackgroundColor = bgNone
bright: bool = if isNoColor: false else: isBright
bright: bool = if game.isNoColor: false else: isBright
if IS_BRIGHT:
bright = false
for _ in 1..count:
Expand Down Expand Up @@ -258,6 +269,7 @@ proc moveCursor(self:MainWindow): void =
"Arrow key : Move cursor",
"HJKL key: Move cursor",
"Enter key: Select the cell",
"S key: Save the progress",
"Ctrl+c : Quit the game"
])

Expand All @@ -270,6 +282,12 @@ proc moveCursor(self:MainWindow): void =
# ---決定---
of Key.Enter:
return
# ---セーブ---
of Key.S:
game.saveProgress()
illwillDeinit()
showCursor()
quit(0)
# ---上移動---
of Key.Up, Key.K:
if self.cursor.y == 0: # yが-1になるため処理しない
Expand Down Expand Up @@ -418,7 +436,7 @@ proc drawRemainingContinues(self:MenuWindow): void =
yPos: int = self.pos.y + yOffset
Draw(fgCyan, bgNone, isBright=true):
tb.write(xPos, yPos, " ".repeat(3))
if isInfinity:
if game.isInfinity:
tb.write(xPos, yPos, "Infinity!!")
else:
tb.write(xPos, yPos, game.remainingContinue.`$`)
Expand Down Expand Up @@ -603,13 +621,15 @@ proc resetMessage(self:MessageWindow): void =
# MineSweeper Impl
#----------------------------------------------------------------
# プロパティに値をセットしたり
proc setting(self:MineSweeper, blc:int): void =
proc setting(self:MineSweeper, blc:int, remainingContinue:int, isInfinity:bool, isNoColor:bool): void =
self.blc = blc
self.doubleBlc = blc*2
self.makeBlocks()
self.placeBombs()
self.placedTotalFlags = 0
self.remainingContinue = remainingContinue
self.isInfinity = isInfinity
self.isNoColor = isNoColor
self.makeBlocks()
self.placeBombs()
self.mainWindow = MainWindow.init(self)
self.menuWindow = MenuWindow.init(self.mainWindow)
self.instructionsWindow = InstructionsWindow.init(self.menuWindow)
Expand Down Expand Up @@ -768,19 +788,39 @@ proc countBombAroundCell(self:MineSweeper, pos:int): void =

self.blocks[pos].bombsAround = bombCount

# ゲームをセーブする
proc saveProgress(self:MineSweeper): void =
var f: File = open("save.txt", FileMode.fmWrite)
let data: string = self.toJson()
f.writeLine(data)
f.close()

# ゲームをロードする
proc loadProgress(self:var MineSweeper, isNew:bool): bool =
if isNew: return false
var f: File
try:
f = open("save.txt", FileMode.fmRead)
except IOError:
return false
let data: string = f.readAll()
self = data.fromJson(MineSweeper)
f.close()
removeSaveFile()
return true

#================================================================
#
# Utilization
#
#================================================================
# ゲーム初期化処理
proc init*(_:type MineSweeper, terminalbuffer:var TerminalBuffer, blc:int, continueNum:int, isInf:bool, noColorFlag:bool): MineSweeper =
remainingContinue = continueNum
isInfinity = isInf
isNoColor = noColorFlag
proc init*(_:type MineSweeper, terminalbuffer:var TerminalBuffer, blc:int, continueNum:int, isInfinity:bool, isNoColor:bool, isNew:bool): MineSweeper =
tb = terminalbuffer
let ms = MineSweeper()
ms.setting(blc)
var ms = MineSweeper()
var isLoaded = ms.loadProgress(isNew)
if not isLoaded:
ms.setting(blc, continueNum, isInfinity, isNoColor)
game = ms
return ms

Expand Down Expand Up @@ -839,7 +879,7 @@ proc update*(self:MineSweeper): bool =
elif cellBlock.isBomb: # 爆弾に当たった時
self.messageWindow.drawMessage("Boom!!", fg=fgRed)
sleep(2000)
if (self.remainingContinue != 0 or isInfinity == true) and self.menuWindow.selectContinue(): # コンティニュー処理
if (self.remainingContinue != 0 or self.isInfinity == true) and self.menuWindow.selectContinue(): # コンティニュー処理
self.messageWindow.resetMessage()
self.remainingContinue.dec()
return false
Expand Down

0 comments on commit e1273d3

Please sign in to comment.