1999-10-12 00:53:36 -04:00
|
|
|
|
#! /usr/bin/ruby -Ke
|
2009-05-15 22:00:42 -04:00
|
|
|
|
# -*- encoding: euc-jp -*-
|
1999-10-12 00:53:36 -04:00
|
|
|
|
|
1999-01-19 23:59:32 -05:00
|
|
|
|
class Board
|
|
|
|
|
def clr
|
|
|
|
|
print "\e[2J"
|
|
|
|
|
end
|
|
|
|
|
def pos(x,y)
|
|
|
|
|
printf "\e[%d;%dH", y+1, x*2+1
|
|
|
|
|
end
|
|
|
|
|
def colorstr(id,s)
|
|
|
|
|
printf "\e[%dm%s\e[0m", id, s
|
|
|
|
|
end
|
|
|
|
|
def put(x, y, col, str)
|
|
|
|
|
pos(x,y); colorstr(43,str)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
pos(0,@hi); print "<EFBFBD>Ĥ<EFBFBD>:",@mc,"/",@total," "
|
1999-01-19 23:59:32 -05:00
|
|
|
|
pos(x,y)
|
|
|
|
|
end
|
|
|
|
|
private :clr, :pos, :colorstr, :put
|
1999-08-13 01:45:20 -04:00
|
|
|
|
CHR=["<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","<EFBFBD><EFBFBD>","@@"]
|
1999-01-19 23:59:32 -05:00
|
|
|
|
COL=[46,43,45] # default,opened,over
|
|
|
|
|
def initialize(h,w,m)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>פ<EFBFBD><D7A4><EFBFBD><EFBFBD><EFBFBD>(h:<3A>ġ<EFBFBD>w:<3A><><EFBFBD><EFBFBD>m:<3A><><EFBFBD>Ƥο<C6A4>)
|
1999-01-19 23:59:32 -05:00
|
|
|
|
@hi=h; @wi=w; @m=m
|
|
|
|
|
reset
|
|
|
|
|
end
|
|
|
|
|
def reset
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>פ<EFBFBD>(<28><>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
srand()
|
|
|
|
|
@cx=0; @cy=0; @mc=@m
|
|
|
|
|
@over=false
|
|
|
|
|
@data=Array.new(@hi*@wi)
|
|
|
|
|
@state=Array.new(@hi*@wi)
|
|
|
|
|
@total=@hi*@wi
|
|
|
|
|
@total.times {|i| @data[i]=0}
|
|
|
|
|
@m.times do
|
|
|
|
|
loop do
|
|
|
|
|
j=rand(@total-1)
|
|
|
|
|
if @data[j] == 0 then
|
|
|
|
|
@data[j]=1
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
clr; pos(0,0)
|
|
|
|
|
@hi.times{|y| pos(0,y); colorstr(COL[0],CHR[0]*@wi)}
|
|
|
|
|
pos(@cx,@cy)
|
|
|
|
|
end
|
|
|
|
|
def mark
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD>ߤΥ<DFA4><CEA5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤˥ޡ<CBA5><DEA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĥ<EFBFBD><C4A4><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
if @state[@wi*@cy+@cx] != nil then return end
|
|
|
|
|
@state[@wi*@cy+@cx] = "MARK"
|
|
|
|
|
@mc=@mc-1;
|
|
|
|
|
@total=@total-1;
|
|
|
|
|
put(@cx, @cy, COL[1], CHR[9])
|
|
|
|
|
end
|
|
|
|
|
def open(x=@cx,y=@cy)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD>ߤΥ<DFA4><CEA5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<D6A4><F2A5AAA1>ץ<EFBFBD><D7A5>ˤ<EFBFBD><CBA4><EFBFBD>
|
|
|
|
|
# <20><><EFBFBD>Ƥ<EFBFBD><C6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Х<EFBFBD><D0A5><EFBFBD><EFBFBD>४<EFBFBD><E0A5AA><EFBFBD>С<EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
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
|
|
|
|
|
@state[@wi*y+x]="OPEN"
|
|
|
|
|
if fetch(x,y) == 1 then @over = 1; return end
|
|
|
|
|
c = count(x,y)
|
|
|
|
|
put(x, y, COL[1], CHR[c])
|
|
|
|
|
return 0 if c != 0
|
|
|
|
|
if x > 0 && y > 0 then open(x-1,y-1) end
|
|
|
|
|
if y > 0 then open(x, y-1) end
|
|
|
|
|
if x < @wi-1 && y > 0 then open(x+1,y-1) end
|
|
|
|
|
if x > 0 then open(x-1,y) end
|
|
|
|
|
if x < @wi-1 then open(x+1,y) end
|
|
|
|
|
if x > 0 && y < @hi-1 then open(x-1,y+1) end
|
|
|
|
|
if y < @hi -1 then open(x,y+1) end
|
|
|
|
|
if x < @wi-1 && y < @hi-1 then open(x+1,y+1) end
|
|
|
|
|
pos(@cx,@cy)
|
|
|
|
|
end
|
|
|
|
|
def fetch(x,y)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# (x,y)<29>ΰ<EFBFBD><CEB0>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>Ƥο<C6A4>(0 or 1)<29><><EFBFBD>֤<EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
if x < 0 then 0
|
|
|
|
|
elsif x >= @wi then 0
|
|
|
|
|
elsif y < 0 then 0
|
|
|
|
|
elsif y >= @hi then 0
|
|
|
|
|
else
|
1999-12-01 04:24:48 -05:00
|
|
|
|
@data[y*@wi+x]
|
1999-01-19 23:59:32 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def count(x,y)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# (x,y)<29><><EFBFBD><EFBFBD><EFBFBD>ܤ<EFBFBD><DCA4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥο<C6A4><CEBF><EFBFBD><EFBFBD>֤<EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
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)
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ν<EFBFBD>λ
|
1999-01-19 23:59:32 -05:00
|
|
|
|
quit
|
|
|
|
|
unless win
|
|
|
|
|
pos(@cx,@cy); print CHR[11]
|
|
|
|
|
end
|
|
|
|
|
pos(0,@hi)
|
|
|
|
|
if win then print "*** YOU WIN !! ***"
|
|
|
|
|
else print "*** GAME OVER ***"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def over?
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ν<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>å<EFBFBD>
|
|
|
|
|
# <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƤӽФ<D3BD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
remain = (@mc+@total == 0)
|
|
|
|
|
if @over || remain
|
|
|
|
|
over(remain)
|
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def quit
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>ޤ<EFBFBD><DEA4>Ͻ<EFBFBD>λ)
|
|
|
|
|
# <20><><EFBFBD>̤<EFBFBD><CCA4><EFBFBD><EFBFBD>Ƹ<EFBFBD><C6B8><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
@hi.times do|y|
|
|
|
|
|
pos(0,y)
|
|
|
|
|
@wi.times do|x|
|
|
|
|
|
colorstr(if @state[y*@wi+x] == "MARK" then COL[1] else COL[2] end,
|
|
|
|
|
if fetch(x,y)==1 then CHR[10] else CHR[count(x,y)] end)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def down
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
if @cy < @hi-1 then @cy=@cy+1; pos(@cx, @cy) end
|
|
|
|
|
end
|
|
|
|
|
def up
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
if @cy > 0 then @cy=@cy-1; pos(@cx, @cy) end
|
|
|
|
|
end
|
|
|
|
|
def left
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
if @cx > 0 then @cx=@cx-1; pos(@cx, @cy) end
|
|
|
|
|
end
|
|
|
|
|
def right
|
1999-08-13 01:45:20 -04:00
|
|
|
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
1999-01-19 23:59:32 -05:00
|
|
|
|
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
|
|
|
|
|
loop do
|
1999-08-13 01:45:20 -04:00
|
|
|
|
case STDIN.getc
|
1999-01-19 23:59:32 -05:00
|
|
|
|
when ?n # new game
|
|
|
|
|
bd.reset
|
|
|
|
|
when ?m # mark
|
|
|
|
|
bd.mark
|
|
|
|
|
when ?j
|
|
|
|
|
bd.down
|
|
|
|
|
when ?k
|
|
|
|
|
bd.up
|
|
|
|
|
when ?h
|
|
|
|
|
bd.left
|
|
|
|
|
when ?l
|
|
|
|
|
bd.right
|
|
|
|
|
when ?\s
|
|
|
|
|
bd.open
|
|
|
|
|
when ?q,?\C-c # quit game
|
|
|
|
|
bd.quit
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
if bd.over?
|
1999-08-13 01:45:20 -04:00
|
|
|
|
if STDIN.getc == ?q then break end
|
1999-01-19 23:59:32 -05:00
|
|
|
|
bd.reset
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
ensure
|
1999-08-13 01:45:20 -04:00
|
|
|
|
system("stty -raw echo")
|
1999-01-19 23:59:32 -05:00
|
|
|
|
end
|
|
|
|
|
print "\n"
|