Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
05f16aa
s/Exception/StandardError/
yskuniv Jan 17, 2016
534a3a8
s/@map/@field/g
yskuniv Jan 17, 2016
00513bd
メソッド名を変更
yskuniv Jan 17, 2016
a82b045
くっそきもいグローバル変数を撲滅
yskuniv Jan 17, 2016
69cdb48
s/&&/and/g
yskuniv Jan 17, 2016
b323971
くそ煩わしいロジックを排除
yskuniv Jan 17, 2016
10e2510
位置おかしいだろJK
yskuniv Jan 17, 2016
486ba51
header生成部分をリファクタリング
yskuniv Jan 17, 2016
f487c97
{} -> do
yskuniv Jan 17, 2016
e2087ad
cursesのくそきもいコードをリファクタリング
yskuniv Jan 17, 2016
a12cf65
条件演算子を排除
yskuniv Jan 17, 2016
411c958
elsifきもいのでcaseに修正
yskuniv Jan 17, 2016
7e8accd
headerまわりを再度リファクタリング
yskuniv Jan 17, 2016
5e65c32
sprintfのあたりをリファクタリング
yskuniv Jan 17, 2016
da35c78
'' に統一
yskuniv Jan 17, 2016
2d4bcb6
定数化
yskuniv Jan 17, 2016
54e0feb
因数分解してみた
yskuniv Jan 17, 2016
b7b101f
s/offset/color_offset/g
yskuniv Jan 17, 2016
2cb2864
ちょっときもいけどまぁ
yskuniv Jan 17, 2016
36d5414
カーソルの近傍判定コードをリファクタリング
yskuniv Jan 17, 2016
880a533
レンダリング機能をClass化
yskuniv Jan 17, 2016
a1e35ad
メソッド化
yskuniv Jan 17, 2016
486560f
定数化
yskuniv Jan 17, 2016
43ded53
while true -> loop
yskuniv Jan 17, 2016
68bd43c
game over / clearの処理を共通化
yskuniv Jan 17, 2016
127fabf
rename variable
yskuniv Jan 17, 2016
0a61ae4
rename constants
yskuniv Jan 17, 2016
810faa8
print_fieldメソッドの型変更
yskuniv Jan 17, 2016
1a7448b
パラメータ化
yskuniv Jan 17, 2016
d9d7d00
定数定義の位置を変更
yskuniv Jan 17, 2016
b358449
Cursorクラスをリファクタリング
yskuniv Jan 17, 2016
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
46 changes: 23 additions & 23 deletions mswp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ def isDoubted
end
end

class GameOverException < Exception
class GameOverException < StandardError
end

class GameClearException < Exception
class GameClearException < StandardError
end

def initialize(length, nr_mines)
@map = MDArray.new(length)
@field = MDArray.new(length)
@nr_mines = nr_mines
if @nr_mines >= @map.size
if @nr_mines >= @field.size
raise ArgumentError.new
end

@map.fill { |i| Cell.new }
@field.fill { |i| Cell.new }
@active = false
end

Expand All @@ -75,15 +75,15 @@ def isTouched(pos)
return
end

@map[pos].isTouched
@field[pos].isTouched
end

def touch(pos)
if ! @active
setup(pos)
end

cell = @map[pos]
cell = @field[pos]
if cell.isFlagged || cell.isTouched
return
end
Expand All @@ -109,7 +109,7 @@ def toggleFlag(pos)
return
end

cell = @map[pos]
cell = @field[pos]
if cell.isTouched
return
end
Expand All @@ -128,7 +128,7 @@ def toggleDoubt(pos)
return
end

cell = @map[pos]
cell = @field[pos]
if cell.isTouched
return
end
Expand All @@ -148,12 +148,12 @@ def touchNeighbors(pos)
return
end

cell = @map[pos]
cell = @field[pos]
if ! cell.isTouched
return
end

nr_flagged_cells = @map.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
nr_flagged_cells = @field.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
sum + (neighbor.isFlagged ? 1 : 0)
}
if nr_flagged_cells != cell.getNumberOfNeighborMines
Expand All @@ -168,19 +168,19 @@ def flagNeighbors(pos)
return
end

cell = @map[pos]
cell = @field[pos]
if ! cell.isTouched
return
end

nr_untouched_cells = @map.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
nr_untouched_cells = @field.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
sum + (neighbor.isTouched ? 0 : 1)
}
if nr_untouched_cells != cell.getNumberOfNeighborMines
return
end

@map.neighbor8_with_index(pos).each { |(neighbor, i)|
@field.neighbor8_with_index(pos).each { |(neighbor, i)|
if ! (neighbor.isTouched || neighbor.isFlagged)
neighbor.flag
@nr_flagged_cells += 1
Expand All @@ -189,13 +189,13 @@ def flagNeighbors(pos)
end

def neighbors(pos)
@map.neighbor8_with_index(pos).map { |(neighbor, i)|
@field.neighbor8_with_index(pos).map { |(neighbor, i)|
[Marshal.load(Marshal.dump(neighbor)).freeze, i]
}
end

def each
@map.each_with_index { |cell, pos|
@field.each_with_index { |cell, pos|
yield(Marshal.load(Marshal.dump(cell)).freeze, pos)
}
end
Expand All @@ -206,25 +206,25 @@ def each

def setup(pos)
# 地雷の配置
(@map.all - [@map[pos]] - (@nr_mines <= @map.size - 3 ** @map.dimension ?
@map.neighbor8_with_index(pos).map { |(cell, i)| cell } :
[])).sort_by { rand }[0...@nr_mines].each { |cell| cell.mine }
(@field.all - [@field[pos]] - (@nr_mines <= @field.size - 3 ** @field.dimension ?
@field.neighbor8_with_index(pos).map { |(cell, i)| cell } :
[])).sort_by { rand }[0...@nr_mines].each { |cell| cell.mine }
# 近傍地雷数の計算
@map.each_with_index { |cell, i|
@field.each_with_index { |cell, i|
if ! cell.isMined
cell.setNumberOfNeighborMines(@map.neighbor8_with_index(i).inject(0) { |sum, (neighbor, j)|
cell.setNumberOfNeighborMines(@field.neighbor8_with_index(i).inject(0) { |sum, (neighbor, j)|
sum += neighbor.isMined ? 1 : 0
})
end
}

@nr_untouched_cells = @map.size
@nr_untouched_cells = @field.size
@nr_flagged_cells = 0
@active = true
end

def __touchNeighbors(pos)
@map.neighbor8_with_index(pos).each { |(cell, i)|
@field.neighbor8_with_index(pos).each { |(cell, i)|
touch(i)
}
end
Expand Down
Loading