summaryrefslogtreecommitdiff
path: root/sample/mine.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/mine.rb')
-rwxr-xr-x[-rw-r--r--]sample/mine.rb47
1 files changed, 24 insertions, 23 deletions
diff --git a/sample/mine.rb b/sample/mine.rb
index 8fc27e0c6d..77e0204bf6 100644..100755
--- a/sample/mine.rb
+++ b/sample/mine.rb
@@ -1,4 +1,7 @@
-#! /usr/bin/ruby -Ke
+#! /usr/bin/ruby -Ku
+# -*- coding: utf-8 -*-
+
+require 'io/console'
class Board
def clr
@@ -12,19 +15,19 @@ class Board
end
def put(x, y, col, str)
pos(x,y); colorstr(43,str)
- pos(0,@hi); print "Ĥ:",@mc,"/",@total," "
+ pos(0,@hi); print "残り:",@mc,"/",@total," "
pos(x,y)
end
private :clr, :pos, :colorstr, :put
- CHR=["","","","","","","","","","","","@@"]
+ CHR=["・","1","2","3","4","5","6","7","8","★","●","@@"]
COL=[46,43,45] # default,opened,over
def initialize(h,w,m)
- # פ(h:ġw:m:Ƥο)
+ # ゲーム盤の生成(h:縦,w:横,m:爆弾の数)
@hi=h; @wi=w; @m=m
reset
end
def reset
- # פ()
+ # ゲーム盤を(再)初期化する
srand()
@cx=0; @cy=0; @mc=@m
@over=false
@@ -46,7 +49,7 @@ class Board
pos(@cx,@cy)
end
def mark
- # ߤΥ֤˥ޡĤ
+ # 現在のカーソル位置にマークをつける
if @state[@wi*@cy+@cx] != nil then return end
@state[@wi*@cy+@cx] = "MARK"
@mc=@mc-1;
@@ -54,8 +57,8 @@ class Board
put(@cx, @cy, COL[1], CHR[9])
end
def open(x=@cx,y=@cy)
- # ߤΥ֤򥪡ץˤ
- # ƤХ४С
+ # 現在のカーソル位置をオープンにする
+ # 爆弾があればゲームオーバー
if @state[@wi*y+x] =="OPEN" then return 0 end
if @state[@wi*y+x] == nil then @total=@total-1 end
if @state[@wi*y+x] =="MARK" then @mc=@mc+1 end
@@ -75,7 +78,7 @@ class Board
pos(@cx,@cy)
end
def fetch(x,y)
- # (x,y)ΰ֤Ƥο(0 or 1)֤
+ # (x,y)の位置の爆弾の数(0 or 1)を返す
if x < 0 then 0
elsif x >= @wi then 0
elsif y < 0 then 0
@@ -85,13 +88,13 @@ class Board
end
end
def count(x,y)
- # (x,y)ܤƤο֤
+ # (x,y)に隣接する爆弾の数を返す
fetch(x-1,y-1)+fetch(x,y-1)+fetch(x+1,y-1)+
fetch(x-1,y) + fetch(x+1,y)+
fetch(x-1,y+1)+fetch(x,y+1)+fetch(x+1,y+1)
end
def over(win)
- # νλ
+ # ゲームの終了
quit
unless win
pos(@cx,@cy); print CHR[11]
@@ -102,8 +105,8 @@ class Board
end
end
def over?
- # νλå
- # λƤӽФ
+ # ゲームの終了チェック
+ # 終了処理も呼び出す
remain = (@mc+@total == 0)
if @over || remain
over(remain)
@@ -113,8 +116,8 @@ class Board
end
end
def quit
- # (ޤϽλ)
- # ̤Ƹ
+ # ゲームの中断(または終了)
+ # 盤面を全て見せる
@hi.times do|y|
pos(0,y)
@wi.times do|x|
@@ -124,26 +127,26 @@ class Board
end
end
def down
- # 򲼤
+ # カーソルを下に
if @cy < @hi-1 then @cy=@cy+1; pos(@cx, @cy) end
end
def up
- #
+ # カーソルを上に
if @cy > 0 then @cy=@cy-1; pos(@cx, @cy) end
end
def left
- # 򺸤
+ # カーソルを左に
if @cx > 0 then @cx=@cx-1; pos(@cx, @cy) end
end
def right
- # 򱦤
+ # カーソルを右に
if @cx < @wi-1 then @cx=@cx+1; pos(@cx, @cy) end
end
end
bd=Board.new(10,10,10)
-system("stty raw -echo")
-begin
+
+IO.console.raw do
loop do
case STDIN.getc
when ?n # new game
@@ -169,7 +172,5 @@ begin
bd.reset
end
end
-ensure
- system("stty -raw echo")
end
print "\n"