動物将棋 (6)
持ち駒の処理について書きたいと思います。持ち駒は、先手、後手をそれぞれMochi[1]とMochi[2]の配列に入れることとしています。そこで、グローバル変数として下記の宣言をしています。
--グローバル変数宣言---------------------- Mochi = {} Mochi[1] = {0,0,0,0,0,0} Mochi[2] = {0,0,0,0,0,0}
値 0が持ち駒無しを表します。持ち駒の最大値は6個なので、このような感じになっています。
持ち駒の位置に持ち駒の絵を描く
持ち駒の位置に持ち駒の絵を描く関数を作りました。mput()です。持ち駒は、先手、後手それぞれ、自分の前に2列に取った駒を並べることにしました。
駒のデータが0のときは、駒を消す意味で、ワーク画面の48*5の絵柄をputします。これは、背景画像です。この使用を追加したため、読み込み画像を変更しました。ソースの後に示します。
また、先手(plyer=1)と後手(plyer=2)で描く場所が違うので、if文で分かれています。
------------------------------------------ -- 持ち駒の位置に持ち駒の絵を描く -- num: Mochi[][num], doubutsu==0なら背景を描く ------------------------------------------ function mput( plyer, num, doubutsu ) local px local py local wx = 48*(doubutsu-plyer*10 - 1) local wy = 48*(2-plyer) + 192 if( doubutsu==0 )then wx = 48*5 end if( plyer==1 )then if( num<4 )then px = Center.x+128 py = Center.y-96+15+(num-1)*55 canvas.putg( px, py, px+47, py+47, wx, wy, wx+47, wy+47 ) else px = Center.x+128+55 py = Center.y-96+15+(num-4)*55 canvas.putg( px, py, px+47, py+47, wx, wy, wx+47, wy+47 ) end elseif( plyer==2 )then if( num<4 )then px = Center.x-129-48 py = Center.y-96+15+(num-1)*55 canvas.putg( px, py, px+47, py+47, wx, wy, wx+47, wy+47 ) else px = Center.x-129-55-48 py = Center.y-96+15+(num-4)*55 canvas.putg( px, py, px+47, py+47, wx, wy, wx+47, wy+47 ) end end end
持ち駒として追加する
Mochi配列に駒を追加する関数です。配列の最後尾の6番目に追加した後、ソートしています。
------------------------------------------ -- 持ち駒を追加する ------------------------------------------ function addMochiKoma( plyer, koma ) local koma1 = plyer*10 + (koma % 10) --にわとりはひよこにする if( koma1==12 )then koma1 = 11 end if( koma1==22 )then koma1 = 21 end Mochi[plyer][6] = koma1 sortMochiKoma( plyer ) --持ち駒をソートします end
持ち駒から削除する
Mochi配列から駒を削除する関数です。削除する駒の配列箇所を0にした後、ソートしています。
------------------------------------------ -- 持ち駒を削除する ------------------------------------------ function delMochiKoma( plyer, num ) Mochi[plyer][num] = 0 sortMochiKoma( plyer ) --持ち駒をソートします end
持ち駒をソートする。
値の大きい順にソートしています。Luaのテーブルソートは使わずに、超単純にソートしています。6個のデータなのでこれでいいかなというところです。値の入れ替えが、○,○=○,○でできるのがLuaの特徴でもあります。
------------------------------------------ --持ち駒をソートします ------------------------------------------ function sortMochiKoma( plyer ) local i,j for j=1,5 do for i=j+1,6 do if( Mochi[plyer][j]<Mochi[plyer][i] )then Mochi[plyer][i],Mochi[plyer][j] = Mochi[plyer][j],Mochi[plyer][i] end end end end
タッチによる駒操作
タッチによる駒操作は、今までの説明で全てなので、ソースを欠いておきます。
------------------------------------------ -- タッチ操作処理 ------------------------------------------ function doTouchAction() local hx = 1 local hy = 1 local ret = 0 local x,y,s local i,j local hx1, hy1,ret1 while(true)do while(true)do --駒にタッチするまで待つ x,y = touch(1) ret, hx, hy = getKomaNum() if( ret~=0 and ret~=-1 )then break end end if( hy~=-1 )then --盤上の駒を消す dput( hx, hy, 0 ) else --持ち駒の絵を消す mput( math.floor(ret/10), hx, 0 ) end --現在の画面をワークにキャッシュする canvas.getg( 0, 0, ViewWide-1, ViewHeight-1, 260, 0, 259+ViewWide, ViewHeight-1 ) while(true)do --駒移動ループ drawKoma( x, y, ret ) x,y,s = touch() --ワークから描き戻し canvas.putg( 0, 0, ViewWide-1, ViewHeight-1, 260, 0, 259+ViewWide, ViewHeight-1 ) if( s==1 )then break end --指が離れたら抜ける end ret1, hx1, hy1 = getKomaNum() --盤面から指が離れたときの状態が返ってくる if( ret1~=-1 )then if( hy==-1 and hy1~=-1 )then --持ち駒を打った if(ret1==0)then --空マスに打った --打てたので、持ち駒を削除します delMochiKoma( math.floor(ret/10), hx ) Ban[hx1][hy1] = ret end elseif( hy1~=-1 )then --盤上の駒を動かした if( chkMove( hx, hy, hx1, hy1, ret )==1 )then if( ret==11 and hy1==1 )then --ひよこの場合 --最前列ではにわとりになる ret = 12 elseif( ret==21 and hy1==4 )then --ひよこの場合 --最前列ではにわとりになる ret = 22 end --Ban[hx1][hy1]に駒がある場合は、自分の持ち駒となる if( Ban[hx1][hy1]~=0 )then --持ち駒に追加します addMochiKoma( math.floor(Ban[hx][hy]/10), Ban[hx1][hy1] ) end Ban[hx][hy] = 0 Ban[hx1][hy1] = ret end end end --盤上の駒を描く for j=1,4 do for i=1,3 do dput( i, j, Ban[i][j] ) end end --持ち駒を描く for i=1,6 do mput( 1, i, Mochi[1][i] ) mput( 2, i, Mochi[2][i] ) end canvas.putflush() end end