001 | <html>
002 | <head>
003 |     <meta http-equiv="Content-Style-Type" content="text/css" charset=Shift_JIS"/>
004 |     <link rel="stylesheet" type="text/css" href="style.css" />
005 |     <title>Ruby・イン・ワンダーランド【三山崩し】</title>
006 | <script language="RubyScript">
007 | ##############################
008 | # 三山崩し
009 | # By 実装者 2008/12
010 | ##############################
011 | ActiveScriptRuby.settrace false
012 | #定数定義
013 | Nuts = [ 3, 5, 7 ]  #皿別の木の実の数
014 | PlatePos = [[150,5],[35,180],[255,180]]
015 | #インスタンス変数定義
016 | @state = false  #ゲームステータス true=実行中、false=初期状態
017 | @plates = [] #皿の配列
018 | #木の実クラスの定義
019 | class Nut
020 |     #アクセサの定義
021 |     attr_reader:id              #id
022 |     attr_reader:nn              #木の実番号
023 |     attr_reader:csrc            #カレント画像
024 |     attr_reader:posx            #座標X
025 |     attr_reader:posy            #座標y
026 |     attr_reader:sel             #選択状態
027 |     #初期化
028 |     def initialize(pn, nn, x, y)
029 |         @posx   = x         #x座標
030 |         @posy   = y         #y座標
031 |         @pn     = pn        #皿番号
032 |         @nn     = nn        #木の実番号
033 |         @id     = "mi_#{pn.to_s}_#{nn.to_s}"    #id
034 |         @src    = "nut.gif" #画像
035 |         @rsrc   = "nutr.gif"    #選択時画像
036 |         @csrc   = @src      #カレントの画像
037 |         @sel    = false     #選択中かどうか
038 |     end
039 |     #クリック時の処理
040 |     def selfClick
041 |         @sel = !@sel    #選択状態の変更
042 |         @csrc = @sel ? @rsrc : @src #画像の変更
043 |     end
044 | end
045 | #皿クラスの定義
046 | class Plate
047 |     #アクセサの定義
048 |     attr_reader:id      #id
049 |     attr_reader:pn      #皿番号
050 |     attr_reader:nn      #オリジナルの木の実の数
051 |     attr_reader:cnn     #現在乗っている木の実の数
052 |     attr_reader:src     #画像
053 |     attr_reader:posx    #座標X
054 |     attr_reader:posy    #座標y
055 |     #初期化
056 |     def initialize(pn, nn, x, y)
057 |         @pn = pn            #皿番号
058 |         @nn = nn            #木の実の数(初期)
059 |         @cnn = nn           #木の実の数(現在)
060 |         @posx = x           #y座標
061 |         @posy = y           #y座標
062 |         @src = "plate.gif"  #画像
063 |         @id = "di_#{pn.to_s}"   #id
064 |         #木の実を構築
065 |         @nuts = []  #木の実配列を定義
066 |         restart #初期状態設定処理を実行
067 |     end
068 |     #初期状態設定
069 |     def restart
070 |         @cnn = @nn
071 |         reset
072 |     end
073 |     #リセット処理
074 |     def reset
075 |         @nuts.clear     #木の実配列を初期化
076 |         #木の実の生成
077 |         x=@posx+50
078 |         y=@posy+100
079 |         for i in 0..@cnn-1
080 |             @nuts << Nut.new(pn, i, x, y)
081 |             x+=28
082 |         end
083 |     end
084 |     
085 |     #木の実表示HTML取得
086 |     def getNutsHtml
087 |         htmlStr = ""
088 |         @nuts.each { |nut|
089 |             htmlStr += "<img src='#{nut.csrc}' id='#{nut.id}' style='position: absolute; left: #{nut.posx.to_s}px; top: #{nut.posy.to_s}px;' width='30' height='20' /onmousedown='mouseDown(#{@pn.to_s},#{nut.nn.to_s})'>"
090 |         }
091 |         htmlStr
092 |     end
093 |     #木の実選択チェック
094 |     def isNutSel
095 |         @nuts.each { |nut|
096 |             return true if nut.sel
097 |         }
098 |         false
099 |     end
100 |     #木の実クリック時の処理
101 |     def nutClick( n )
102 |         @nuts[n].selfClick
103 |     end
104 |     #木の実を取り除く処理
105 |     def removeNuts
106 |         r = 0
107 |         @nuts.each { |nut|
108 |             r+=1 if nut.sel
109 |         }
110 |         @cnn = @cnn - r
111 |         reset
112 |     end
113 |     #木の実取り除き処理(コンピュータ用)
114 |     def directRemove(nn)
115 |         @cnn -= nn
116 |         reset
117 |     end
118 | end
119 | #木の実クリック処理
120 | def mouseDown(pn, nn)
121 |     return if !@state   #実行中でなければ何もしない
122 |     @plates.each { |plate|
123 |         if plate.isNutSel && ( plate.pn != pn ) then
124 |             @window.alert "その山からは選択できません"
125 |             return
126 |         end
127 |     }
128 |     @plates[pn].nutClick(nn)
129 |     buildScreen
130 | end
131 | #画面表示処理
132 | def buildScreen()
133 |     htmlStr = ""
134 |     @plates.each { |plate|
135 |         #プレートを表示
136 |         htmlStr += "<img src='#{plate.src}' id='#{plate.id}' style='position: absolute; left: #{plate.posx.to_s}px; top: #{plate.posy.to_s}px;' width=300 height='225' />"
137 |         #木の実の表示
138 |         htmlStr+=plate.getNutsHtml
139 |     }
140 |     #取り除きボタンの表示
141 |     #もし、どれかの皿で木の実が選択されていれば取り除きボタンを表示
142 |     if @state && (@plates[0].isNutSel || @plates[1].isNutSel || @plates[2].isNutSel) then
143 |     htmlStr += "<input id='btnRemove' type='button' value='取り除く' style='position: absolute; left:480px; top:370px; width: 89px' onclick='remove' language='RubyScript'/>"
144 |     end
145 |     @window.document.getElementById("fields").innerHTML = htmlStr
146 | end
147 | #オブジェクト初期化処理
148 | def initObjects()
149 |     #皿を構築
150 |     @plates.clear
151 |     for i in 0..2
152 |         @plates << Plate.new(i, Nuts[i], PlatePos[i][0], PlatePos[i][1])
153 |     end
154 | end
155 | # 初期化処理
156 | def init()
157 |     @state = false
158 |     initObjects() #各オブジェクト生成
159 |     buildScreen() #画面の構築
160 | end
161 | #ゲーム開始処理
162 | def start
163 |     @state = !@state
164 |     @window.document.getElementById("btnStart").value = @state ? "リセット" : "スタート"
165 |     if @state then
166 |         @window.alert "私が先行です"
167 |         comTurn
168 |     else
169 |         #皿の状態をリセットする
170 |         @plates.each { |plate|
171 |             plate.restart
172 |         }
173 |         buildScreen
174 |     end
175 | end
176 | #取り除きボタンの押下
177 | def remove
178 |     @plates.each { |plate|
179 |         plate.removeNuts
180 |     }
181 |     #画面再構築
182 |     buildScreen
183 |     #勝敗の判定
184 |     judge(1)
185 | end
186 | #コンピュータのターン
187 | def comTurn
188 |     x = @plates[0].cnn ^ @plates[1].cnn ^ @plates[2].cnn
189 |     j = 0
190 |     for i in 0..2
191 |         if @plates[i].cnn > (@plates[i].cnn ^ x) then
192 |              j = i;
193 |         end
194 |     end
195 |     r = @plates[j].cnn - (@plates[j].cnn ^ x)
196 |     @window.alert "私は" + (j+1).to_s + "番の山から" + r.to_s + "個取ります"
197 |     @plates[j].directRemove(r)
198 |     buildScreen
199 |     judge(2)
200 | end
201 | #勝敗の判定
202 | def judge(pc)
203 |     n = 0
204 |     @plates.each { |plate|
205 |         n += plate.cnn.to_i
206 |     }
207 |     if n == 0 then
208 |         if pc == 1 then #プレイヤーターン
209 |             @window.alert("残念ながら貴方の勝ちです。\nでも次は負けない…")
210 |         else
211 |             @window.alert("私の勝ちです。わははは♪")
212 |         end
213 |         start
214 |     else
215 |         comTurn if pc == 1
216 |     end
217 | end
218 | </script>
219 | 
220 | </head>
221 | <body onload="init()" language="RubyScript">
222 |     <div id="fields" style="width: 600px; height: 400px; background-image:url(back.jpg); background-position: 0% 0%; background-attachment: fixed; position:relative">
223 |     </div>
224 |     <br/>
225 |     <div align="center" style="width: 600px; height: 24px">
226 |         <input id="btnStart" type="button" value="スタート" style="width: 89px" onclick="start" language="RubyScript"/>
227 |     </div>
228 |     <!--
229 |     <h1>Ruby三山崩しゲーム</h1>
230 |     -->
231 |     <br />
232 |     <br />
233 |     <hr>
234 | </body>
235 | </html>
最終更新:2008年12月30日 23:19