Created
November 22, 2012 11:05
-
-
Save seventhsense/4130585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
game = new Main() | |
game.start() | |
#共通ライブラリ | |
#行列演算のための関数 | |
##行列の和 | |
addArray = (array1, array2) -> | |
array = [] | |
len = array1.length | |
for i in [0...len] | |
array[i] = array1[i] + array2[i] | |
array | |
##行列のすべての要素に掛ける | |
multipuleArray = (array1, int) -> | |
array = [] | |
len = array1.length | |
for i in [0...len] | |
array[i] = array1[i] * int | |
array | |
##与えた数だけ連続するベクトルをつくる | |
## example continueArray([1,1], 3) | |
## #=> [[1,1],[2,2],[3,3]] | |
continueArray = (array, int) -> | |
for i in [1...int] | |
multipuleArray(array, i) | |
#二次元配列用論理和 Ban用 ban[0][0]は使わない | |
orArray = (array1, array2) -> | |
array = [] | |
for arr, i in array1 | |
continue if i == 0 | |
array[i] ||= [] | |
for set, j in arr | |
continue if j == 0 | |
array[i][j] = array1[i][j] || array2[i][j] | |
array | |
getMargin = () -> | |
margin = $('#ban').position() | |
[margin.left , margin.top] | |
#描画の座標を棋譜の座標に変換する | |
# 8px 上マージン | |
# 8px 左マージン | |
# 1マス 52px * 52px | |
get_kifu = (x, y) -> | |
margin = getMargin() | |
m_x = margin[0] | |
m_y = margin[1] | |
kifu_x = Math.round(9 - (x - m_x) / 52) | |
kifu_y = Math.round((y - m_y) / 52 + 1) | |
{"x": kifu_x, "y": kifu_y} | |
#棋譜の座標を描画の座標に変換する | |
get_pos = (x, y) -> | |
margin = getMargin() | |
m_x = margin[0] | |
m_y = margin[1] | |
pos_x = (9 - x) * 52 + m_x | |
pos_y = (y - 1) * 52 + m_y | |
{"pos_x": pos_x, "pos_y": pos_y} | |
#デバッグ情報を画面に表示 | |
debugInfo = (message) -> | |
$("debug").text(message) | |
#9*9のマスを提供するクラス | |
#主に盤上の駒情報を保持する | |
class Ban | |
constructor:() -> | |
#9*9マスの配列 | |
#とりあえず[0][0]は使わない | |
@ban = [] | |
for i in [1..9] | |
for j in [1..9] | |
@ban[i] ||= [] | |
@ban[i][j] = 0 | |
#駒idを格納する | |
set: (koma_id, x, y) -> | |
@ban[x][y] = koma_id | |
false | |
setClass: (css_class) -> | |
for i in [1..9] | |
for j in [1..9] | |
$("#ban" + i + "-" + j).addClass(css_class) if @ban[i][j] != 0 | |
#駒idを返す | |
get: (x, y) -> | |
@ban[x][y] | |
setOrArray: (ban) -> | |
@ban = orArray(@ban, ban.ban) | |
#駒の情報を保持するクラス | |
class Koma | |
constructor:(@id) -> | |
#駒の固有番号 | |
#@id | |
#駒の種類 | |
switch @id | |
when 1 , 21 | |
@shurui = "Ou" | |
when 2 , 22 | |
@shurui = "Hisha" | |
when 3 , 23 | |
@shurui = "Kaku" | |
when 4,5 , 24,25 | |
@shurui = "Kin" | |
when 6,7 , 26,27 | |
@shurui = "Gin" | |
when 8,9 , 28,29 | |
@shurui = "Keima" | |
when 10,11 , 30,31 | |
@shurui = "Kyo" | |
else #12~20, 32~40 | |
@shurui = "Fu" | |
#成ることができるか 王と金以外 | |
switch @shurui | |
when "Hisha", "Kaku", "Gin", "Keima", "Kyo", "Fu" | |
@canNari = true | |
else | |
@canNari = false | |
#成っているか | |
@isNari = "Narazu" | |
#動ける範囲 (ベクトル) | |
##動ける範囲のための定義 | |
hidari_ue = [-1, -1] | |
hidari = [-1, 0] | |
hidari_sita = [-1, 1] | |
ue = [0, -1] | |
sita = [0, 1] | |
migi_ue = [1, -1] | |
migi = [1, 0] | |
migi_sita = [1, 1] | |
KIN_MOVE = [ | |
[hidari_ue] | |
[hidari] | |
[ue] | |
[sita] | |
[migi_ue] | |
[migi] | |
] | |
#駒の動きの定義 | |
switch @shurui | |
when "Ou" | |
@movable = | |
"Narazu": | |
[ | |
[hidari_ue] | |
[hidari] | |
[hidari_sita] | |
[ue] | |
[sita] | |
[migi_ue] | |
[migi] | |
[migi_sita] | |
] | |
when "Hisha" | |
@movable = { | |
"Narazu": | |
[ | |
continueArray(hidari, 9) | |
continueArray(ue, 9) | |
continueArray(sita, 9) | |
continueArray(migi, 9) | |
] | |
"Nari": | |
[ | |
continueArray(hidari, 9) | |
continueArray(ue, 9) | |
continueArray(sita, 9) | |
continueArray(migi, 9) | |
[hidari_ue] | |
[hidari_sita] | |
[migi_ue] | |
[migi_sita] | |
] | |
} | |
when "Kaku" | |
@movable = { | |
"Narazu": | |
[ | |
continueArray(hidari_ue, 9) | |
continueArray(hidari_sita, 9) | |
continueArray(migi_ue, 9) | |
continueArray(migi_sita, 9) | |
] | |
"Nari": | |
[ | |
continueArray(hidari_ue, 9) | |
continueArray(hidari_sita, 9) | |
continueArray(migi_ue, 9) | |
continueArray(migi_sita, 9) | |
[hidari] | |
[ue] | |
[sita] | |
[migi] | |
] | |
} | |
when "Kin" | |
@movable = { | |
"Narazu": | |
KIN_MOVE | |
} | |
when "Gin" | |
@movable = { | |
"Narazu": | |
[ | |
[hidari_ue] | |
[hidari_sita] | |
[ue] | |
[migi_ue] | |
[migi_sita] | |
] | |
"Nari": | |
KIN_MOVE | |
} | |
when "Keima" | |
@movable = { | |
"Narazu": | |
[ | |
[[-1, -2]] | |
[[ 1, -2]] | |
] | |
"Nari": | |
KIN_MOVE | |
} | |
when "Kyo" | |
@movable = { | |
"Narazu": | |
[ | |
continueArray(ue, 9) | |
] | |
"Nari": | |
KIN_MOVE | |
} | |
when "Fu" | |
@movable = { | |
"Narazu": | |
[ | |
[ue] | |
] | |
"Nari": | |
KIN_MOVE | |
} | |
#どのプレイヤーのものか | |
#@owner | |
#手駒かどうか | |
@isTegoma = false | |
#現在場所に駒を描画する | |
setPosi : (@x, @y) -> | |
id = @id | |
@x = Math.round(x) | |
@y = Math.round(y) | |
pos = get_pos(@x, @y) | |
pos_x = pos["pos_x"] | |
pos_y = pos["pos_y"] | |
$("#koma_" + id).css("position", "absolute") | |
$("#koma_" + id).css({ "top": pos_y, "left": pos_x }) | |
false | |
#駒の現在位置を回答する | |
getKifuPosition: () -> | |
{"x": @x, "y": @y} | |
setOwner: (@owner) -> | |
#その駒の実際の動きを取得(成りと先手後手を考慮) | |
getMovable: () -> | |
#駒が成っているか | |
#@isNari = "Narazu" or "Nari" | |
#駒の所有はどちらか goteなら動きを上下反転(全部-1をかける?) | |
movable = @movable[@isNari] | |
if @owner == "gote" | |
array = [] | |
i = 0 | |
for move in movable | |
in_array = [] | |
j = 0 | |
for vector in move | |
in_array[j] = multipuleArray(vector, -1) | |
j++ | |
array[i] = in_array | |
in_array = [] | |
i++ | |
movable = array | |
movable | |
setNari: () -> | |
@isNari = "Nari" | |
this.setImage() | |
setImage: () -> | |
switch @shurui | |
when "Ou" | |
if @owner == "sente" | |
img = "Sou" | |
else | |
img = "Gou" | |
when "Hisha" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Shi" | |
else | |
img = "Sryu" | |
else if @isNari == "Narazu" | |
img = "Ghi" | |
else | |
img = "Gryu" | |
when "Kaku" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Skaku" | |
else | |
img = "Suma" | |
else if @isNari == "Narazu" | |
img = "Gkaku" | |
else | |
img = "Guma" | |
when "Kin" | |
if @owner == "sente" | |
img = "Skin" | |
else | |
img = "Gkin" | |
when "Gin" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Sgin" | |
else | |
img = "Sngin" | |
else if @isNari == "Narazu" | |
img = "Ggin" | |
else | |
img = "Gngin" | |
when "Keima" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Skei" | |
else | |
img = "Snkei" | |
else if @isNari == "Narazu" | |
img = "Gkei" | |
else | |
img = "Gnkei" | |
when "Kyo" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Skyo" | |
else | |
img = "Snkyo" | |
else if @isNari == "Narazu" | |
img = "Gkyo" | |
else | |
img = "Gnkyo" | |
when "Fu" | |
if @owner == "sente" | |
if @isNari == "Narazu" | |
img = "Sfu" | |
else | |
img = "Sto" | |
else if @isNari == "Narazu" | |
img = "Gfu" | |
else | |
img = "Gto" | |
image_address = "img/" + img + ".png" | |
$("#koma_" + @id).children("img").attr("src",image_address) | |
class Kifu | |
constructor:() -> | |
@max_count = 0 | |
@kifu = [] | |
setKifu: (dm, label = '', message = '') -> | |
@kifu[@max_count] = new Kyokumen(dm.ban, dm.koma, dm.teban, label, message) | |
@max_count += 1 | |
$('#kifu-select') | |
.append("<option id=\"option_#{@max_count}\" value=\"#{@max_count}\">#{@max_count}:#{label}</option>") | |
.val(@max_count) | |
.jQselectable('refresh') | |
if @max_count % 5 == 0 | |
$("#option_#{@max_count}").addClass("br") | |
false | |
getAllKifuLabel:() -> | |
array = [] | |
for kyokumen in @kifu | |
array.push(kyokumen.label) | |
array | |
class Kyokumen | |
constructor:(@ban, @koma, @teban, @label = '', @message = '') -> | |
#駒と盤の関係や手番の管理などデータを総合的に管理する | |
class DataManager | |
constructor: () -> | |
@teban = "sente" | |
@ban = new Ban() | |
@koma = [] | |
@kifu = new Kifu() | |
for i in [1..40] | |
@koma[i] = new Koma(i) | |
setKomaInitial(@ban, @koma) | |
setKomaInitial = (ban, koma) -> | |
for i in [1..20] | |
koma[i].setOwner("sente") | |
koma[i].setImage() | |
$('#koma_' + i).addClass("sente") | |
for i in [21..40] | |
koma[i].setOwner("gote") | |
koma[i].setImage() | |
$('#koma_' + i).addClass("gote") | |
$.get("data/initial.csv", null, (data)-> | |
rows = data.split("\n") | |
for row, i in rows | |
arry = row.split(",") | |
break if row == "" | |
ban.set(i + 1 , arry[1], arry[2]) | |
koma[i + 1].setPosi(arry[1], arry[2]) | |
) | |
moveKomaPosition: (koma_id, x, y) -> | |
#駒がもといた場所を空にする | |
@ban.set(0, @koma[koma_id].x, @koma[koma_id].y) | |
#新しい場所に駒idを登録する | |
@ban.set(koma_id, x, y) | |
#駒構造体の座標も更新する | |
@koma[koma_id].setPosi(x, y) | |
false | |
getKomaPosition: (koma_id) -> | |
hash = @koma[koma_id].getKifuPosition() | |
[hash["x"], hash["y"]] | |
setTeban: (@teban) -> | |
getKomaOwner: (koma_id) -> | |
@koma[koma_id].owner | |
getKomaMovable: (koma_id) -> | |
# new Banを作る 9×9の配列で中身は0 | |
# これから動ける場所に1をつけていく | |
movable_ban = new Ban | |
# もとの座標はそのドラッグのキャンセルの意味でドラッグを認める | |
movable_ban.set(1, @koma[koma_id].x, @koma[koma_id].y) | |
#駒の動ける範囲(手駒でなければ先手後手、成りを考慮) | |
movable = @koma[koma_id].getMovable() | |
#ループ開始 | |
#move 同方向のベクトルの配列 | |
#vector ベクトル | |
for move in movable | |
for vector in move | |
#駒の現在位置を基本に | |
#ベクトルを足した後の座標を盤でチェック | |
## check_posi チェックする座標 | |
check_posi = addArray(this.getKomaPosition(koma_id), vector) | |
c_x = check_posi[0] | |
c_y = check_posi[1] | |
# 盤の外に出たらストップ | |
if c_x == 0 or c_x == 10 or c_y == 0 or c_y == 10 | |
break | |
check_posi_id = @ban.get(c_x, c_y) | |
# 駒があり、味方駒なら動けないにチェックしてその方向は終了 break | |
if check_posi_id != 0 | |
check_posi_owner = this.getKomaOwner(check_posi_id) | |
if check_posi_owner == this.getKomaOwner(koma_id) | |
movable_ban.set(0, c_x, c_y) | |
break | |
# 駒があり、敵駒なら動けるにチェックしてその方向は終了 break | |
else | |
movable_ban.set(1, c_x, c_y) | |
break | |
# 駒がなければ動ける1にチェックして次 | |
else | |
movable_ban.set(1, c_x, c_y) | |
movable_ban | |
setKomaMovableClass: (koma_id) -> | |
movable_ban = this.getKomaMovable(koma_id) | |
movable_ban.setClass("movable") | |
removeKomaMovableClass: () -> | |
$(".movable").removeClass("movable") | |
doNariConfirm: (dragged_id, dragged_y) -> | |
dragged_koma = @koma[dragged_id] | |
## 成ることができる種類の駒で | |
## まだ成っていない場合(逆を排除) | |
if dragged_koma.canNari == false or dragged_koma.isNari == "Nari" | |
return false | |
## 動いた先が敵の陣地の場合 | |
if (@teban == "sente" && dragged_y <= 3) or (@teban == "gote" && dragged_y >= 7) | |
#銀の場合と桂馬と香が3段目のときだけ選択できるようにした | |
if (dragged_koma.shurui == "Gin") or ((dragged_koma.shurui == "Keima" or dragged_koma.shurui == "Kyo") and (dragged_y == 3 or dragged_y == 7)) | |
return true if confirm("成りますか?") == true | |
else | |
#その他は自動的に成り | |
return true | |
##もともとの位置が成れる位置から移動した場合 | |
else | |
if (@teban == 'sente' && dragged_koma.y <= 3) or (@teban == 'gote' && dragged_koma.y >= 7) | |
if dragged_koma.shurui == "Gin" | |
return true if confirm("成りますか?") == true | |
else | |
return true | |
else | |
return false | |
doNari: (dragged_id) -> | |
this.koma[dragged_id].setNari() | |
getKomaSettable: () -> | |
settable_ban = new Ban() | |
for i in [1..9] | |
for j in [1..9] | |
if @ban.get(i, j) != 0 | |
settable_ban.set(0, i, j) | |
else | |
settable_ban.set(1, i, j) | |
settable_ban | |
setSettableClass: () -> | |
settable_ban = this.getKomaSettable() | |
settable_ban.setClass('movable') | |
setTegoma: (id) -> | |
koma = @koma[id] | |
koma.setOwner(@teban) | |
koma.isTegoma = true | |
koma.isNari = "Narazu" | |
#画像変更 | |
koma.setImage() | |
#盤から消去 | |
@ban.set(0, koma.x, koma.y) | |
setTegomaToBan: (dragged_id, dropped_kifu) -> | |
koma = @koma[dragged_id] | |
koma.isTegoma = false | |
@koma[dragged_id].setPosi(dropped_kifu[0], dropped_kifu[1]) | |
@ban.set(dragged_id, dropped_kifu[0], dropped_kifu[1]) | |
checkNifu: (dragged_id, dropped_kifu) -> | |
koma = @koma[dragged_id] | |
return false if koma.shurui != 'Fu' | |
for i in [1..9] | |
check_id = @ban.get(dropped_kifu[0],i) | |
console.log dropped_kifu | |
#駒があるかどうか | |
if check_id != 0 | |
#駒があるなら歩かどうか 所有者が同一か 成ってるかどうか | |
if @koma[check_id].shurui == 'Fu' and @koma[check_id].owner == @teban and @koma[check_id].isNari == 'Narazu' | |
return true | |
return false | |
checkSetKeimaAndKyo: (dragged_id, dropped_kifu) -> | |
koma = @koma[dragged_id] | |
switch koma.shurui | |
when 'Kyo', 'Fu' | |
if (@teban == 'sente' and dropped_kifu[1] == '1') or (@teban == 'gote' and dropped_kifu[1] == '9') | |
return true | |
when 'Keima' | |
if (@teban == 'sente' and dropped_kifu[1] <= 2) or (@teban == 'gote' and dropped_kifu[1] >= 8) | |
return true | |
else | |
return false | |
false | |
checkOute: () -> | |
this.flashKomaEffectClass(@teban) | |
effect_ban = this.getKomaEffect(@teban) | |
aite = this.aite() | |
#王のid sente 1 gote 21 | |
if aite == "sente" | |
aite_ou_id = 1 | |
else | |
aite_ou_id = 21 | |
aite_ou = @koma[aite_ou_id] | |
if effect_ban.get(aite_ou.x, aite_ou.y) != 0 | |
return true | |
else | |
return false | |
flashKomaEffectClass: (teban) -> | |
effect_ban = this.getKomaEffect(teban) | |
effect_ban.setClass('effect') | |
$('.effect').effect("highlight", {color: "beige"}, 800) | |
.removeClass('effect') | |
getKomaEffect: (teban) -> | |
koma = this.getAllKoma(teban) | |
effect_ban = new Ban() | |
for k in koma | |
continue if k is undefined | |
effect_ban.setOrArray(this.getKomaMovable(k.id)) if k.isTegoma == false | |
effect_ban | |
getAllKoma: (owner) -> | |
ks = [] | |
for k, i in @koma | |
continue if k is undefined | |
ks[i] = k if k.owner == owner | |
ks | |
aite: () -> | |
if @teban == "sente" | |
return "gote" | |
else | |
return "sente" | |
logKifu: (id, action, x = 0, y = 0) -> | |
koma = @koma[id] | |
if action == "move" | |
msg = "(#{x},#{y}) -> (#{koma.x}, #{koma.y})" | |
else | |
msg = "(#{koma.x}, #{koma.y})" | |
log = "[Kifu]#{@kifu.max_count}:#{@teban} #{msg}, #{@koma[id].shurui} #{action} " | |
yomiage = "" | |
if @teban == "sente" | |
yomiage += "▲" | |
else | |
yomiage += "△" | |
yomiage += koma.x | |
@KANSUJI = '一二三四五六七八九' | |
yomiage += @KANSUJI.charAt(koma.y) | |
@SHURUI = [] | |
@SHURUI['Narazu'] = | |
Ou : "玉" | |
Hisha: "飛" | |
Kaku: "角" | |
Kin: "金" | |
Gin: "銀" | |
Keima: "桂" | |
Kyo: "香" | |
Fu: "歩" | |
@SHURUI['Nari'] = | |
Hisha: "龍" | |
Kaku: "馬" | |
Gin: "成銀" | |
Keima: "成桂" | |
Kyo: "成香" | |
Fu: "と金" | |
if action.match(/nari/) | |
yomiage += @SHURUI['Narazu'][koma.shurui] | |
yomiage += "成" | |
else | |
if action.match(/set/) | |
yomiage += @SHURUI[koma.isNari][koma.shurui] | |
yomiage += "打" | |
else | |
yomiage += @SHURUI[koma.isNari][koma.shurui] | |
@kifu.setKifu(this, yomiage) | |
if @teban == "sente" | |
next = "後手" | |
else | |
next = "先手" | |
$("#teban-msg").html("#{@kifu.max_count}: #{yomiage} 次は#{next}の番です") | |
class Main | |
constructor:() -> | |
@dm = new DataManager() | |
$('#message').hide() | |
$("button").button() | |
$("#memo").hide() | |
$("#control").hide() | |
$("#reset button").click -> | |
location.reload() | |
$('#kifu-select').jQselectable() | |
start: -> | |
setDraggableInitial(@dm) | |
setDroppable(@dm) | |
doTeban(@dm) | |
#draggable初期設定 | |
##メソッドにしていない理由はdrag、stopなどのイベントハンドラ関数のなかでプロパティをよむ方法が分からなかったから | |
setDraggableInitial = (dm) -> | |
$(".koma").draggable | |
snap : ".masu", | |
snapMode : "inner", | |
grid : [50, 50], | |
containment : "#ban", | |
start : (event, ui) -> | |
#ドラッグされてる駒のid | |
@dragged_id = $(".ui-draggable-dragging").attr("id").replace('koma_','') | |
# new Banの座標のmasuにmovableクラスを追加して色付け | |
dm.setKomaMovableClass(@dragged_id) | |
# できればdraggableの対象に限定 | |
# できない.ドラッグ開始後に動的にcontainmentの対象クラスを変えても機能しない | |
stop : (event, ui) -> | |
# movableクラスの除去 | |
dm.removeKomaMovableClass() | |
#ドラッグされた先の駒の棋譜座標 | |
dragged_kifu = get_kifu(ui.position.left, ui.position.top) | |
#手番変更していいかチェック | |
checkTebanChange(dm, @dragged_id, dragged_kifu) | |
false | |
.draggable('disable') | |
#手駒から盤上に駒を置くときの処理 | |
setDraggable = (dm, id) -> | |
$("#koma_" + id).appendTo("#main").effect("highlight", {color: "beige"}, 1000) | |
$("#koma_" + id) | |
.draggable('destroy') | |
.removeClass("is_tegoma") | |
.addClass("koma") | |
$("#koma_" + id).draggable | |
snap : ".masu", | |
snapMode : "inner", | |
grid : [50, 50], | |
containment : "#ban", | |
start : (event, ui) -> | |
#ドラッグされてる駒のid | |
@dragged_id = $(".ui-draggable-dragging").attr("id").replace('koma_','') | |
# new Banの座標のmasuにmovableクラスを追加して色付け | |
dm.setKomaMovableClass(@dragged_id) | |
# できればdraggableの対象に限定 | |
# できない.ドラッグ開始後に動的にcontainmentの対象クラスを変えても機能しない | |
stop : (event, ui) -> | |
# movableクラスの除去 | |
dm.removeKomaMovableClass() | |
#ドラッグされた先の駒の棋譜座標 | |
dragged_kifu = get_kifu(ui.position.left, ui.position.top) | |
#手番変更していいかチェック | |
checkTebanChange(dm, @dragged_id, dragged_kifu) | |
#手駒用のdraggableを設定 | |
setTegomaDraggable = (dm, id) -> | |
teban = dm.teban | |
aite = dm.aite() | |
$("#koma_" + id) | |
.draggable('destroy') | |
.css({"position": "static", "top": 0, "left": 0}) | |
.addClass("is_tegoma") | |
.removeClass("koma") | |
.removeClass(aite) | |
.addClass(teban) | |
.draggable | |
revert: 'invalid' | |
start: () -> | |
dm.setSettableClass() | |
stop: () -> | |
dm.removeKomaMovableClass() | |
$("#koma_" + id).appendTo("#" + teban + "_tegoma").effect("bounce", {}, 1000) | |
setDroppable = (dm) -> | |
$(".masu").droppable | |
accept: ".is_tegoma" | |
drop: (event, ui) -> | |
dragged_id = ui.draggable.context["id"].replace('koma_', '') | |
dropped_kifu = $(this).attr("id").replace('ban', '').split('-') | |
checkKomaSettable(dm, dragged_id, dropped_kifu) | |
#以下、ループ処理 | |
#doTeban -> checkTebanChange -> (盤上の駒をドラッグした場合) -> tebanShori -> tebanChange -> doTeban | |
# -> (手駒をドラッグした場合) -> tebanChange -> doTeban | |
##doTeban | |
## 手番のdraggable.enableを設定 | |
##checkTebanChange | |
## draggableがstop後の処理 | |
##tebanShori | |
## 手番変更してよい場合,駒を取るとか成りとかの処理 | |
##tebanChange | |
## draggableをdisableにして先手後手を入れ替える | |
# | |
doTeban = (dm) -> | |
teban = dm.teban | |
dm.kifu.getAllKifuLabel() | |
$("." + teban).draggable('enable') | |
checkTebanChange = (dm, dragged_id, dragged_kifu) -> | |
#同じ場所にドラッグして戻ってきたらもう1回 | |
##元の棋譜座標 | |
kifu_posi = dm.koma[dragged_id].getKifuPosition() | |
if kifu_posi["x"] == dragged_kifu["x"] && kifu_posi["y"] == dragged_kifu["y"] | |
return doTeban(dm) | |
#movableの範囲外に移動したらもう1回 | |
if dm.koma[dragged_id].isTegoma == false | |
movable_ban = dm.getKomaMovable(dragged_id) | |
if movable_ban.get(dragged_kifu["x"], dragged_kifu["y"]) == 0 | |
#駒をもとの場所()に移動 | |
dm.moveKomaPosition(dragged_id, kifu_posi["x"], kifu_posi["y"]) | |
return doTeban(dm) | |
##王は王手のかかる場所に移動できない | |
##王手がかかっている場合を解消しなければならない できなければ詰みで終了 | |
##千日手 | |
#手番交代していいなら駒を取る処理と成りの処理 | |
tebanShori(dm, dragged_id, dragged_kifu) | |
checkKomaSettable = (dm, dragged_id, dropped_kifu) -> | |
##駒がある場所にはおけない | |
settable_ban = dm.getKomaSettable() | |
d_x = parseInt(dropped_kifu[0]) | |
d_y = parseInt(dropped_kifu[1]) | |
save_id = dm.ban.get(d_x, d_y) | |
if settable_ban.get(d_x, d_y) == 0 | |
showMessage '置けません!' | |
## 状態を手駒にする | |
dm.setTegoma(dragged_id) | |
#手駒用のdraggableを設定 | |
setTegomaDraggable(dm, dragged_id) | |
#盤上の駒idが消されるので書き直す | |
dm.ban.set(save_id, d_x, d_y) | |
dm.koma[save_id].setPosi(d_x, d_y) | |
return false | |
##桂馬は下2段と香車下1段に置けない | |
if dm.checkSetKeimaAndKyo(dragged_id, dropped_kifu) == true | |
showMessage '置けません' | |
## 状態を手駒にする | |
dm.setTegoma(dragged_id) | |
#手駒用のdraggableを設定 | |
setTegomaDraggable(dm, dragged_id) | |
return false | |
##二歩のときはやり直し | |
if dm.checkNifu(dragged_id, dropped_kifu) == true | |
showMessage '二歩です' | |
## 状態を手駒にする | |
dm.setTegoma(dragged_id) | |
#手駒用のdraggableを設定 | |
setTegomaDraggable(dm, dragged_id) | |
#盤上の駒idが消されるので書き直す | |
# dm.ban.set(save_id, d_x, d_y) | |
# dm.koma[save_id].setPosi(d_x, d_y) | |
return false | |
#駒を置ける場合の処理 | |
#盤上に駒を登録 | |
dm.setTegomaToBan(dragged_id, dropped_kifu) | |
#盤上の駒用のdraggableを再設定 | |
setDraggable(dm, dragged_id) | |
#棋譜記録 | |
dm.logKifu(dragged_id, "set") | |
tebanChange(dm) | |
#駒を移動できる場合の処理(駒を取る、成り) | |
tebanShori = (dm, dragged_id, dragged_kifu) -> | |
teban = dm.teban | |
#駒を取る | |
## 動いた先に敵の駒がある場合 | |
saki_id = dm.ban.get(dragged_kifu["x"], dragged_kifu["y"]) | |
if saki_id != 0 | |
saki_koma = dm.koma[saki_id] | |
if saki_koma.owner != teban | |
## 取った駒の種類が王ならばゲーム終了! | |
if saki_koma.shurui == "Ou" | |
if teban == "sente" | |
showMessage "先手の勝ち!" | |
$('#teban').text('先手の勝ち') | |
else | |
showMessage "後手の勝ち!" | |
$('#teban').text('後手の勝ち') | |
return gameEnd() | |
else | |
## 状態を手駒にする | |
dm.setTegoma(saki_id) | |
#手駒用のdraggableを設定 | |
setTegomaDraggable(dm, saki_id) | |
#成り | |
if dm.doNariConfirm(dragged_id, dragged_kifu["y"]) == true | |
dm.doNari(dragged_id) | |
showMessage '成り!' | |
doNari = true | |
#前の座標も記録 | |
b_x = dm.koma[dragged_id].x | |
b_y = dm.koma[dragged_id].y | |
##盤の駒座標も移動 | |
dm.moveKomaPosition(dragged_id, dragged_kifu["x"], dragged_kifu["y"]) | |
#棋譜記録 | |
move = "move" | |
move += " nari" if doNari == true | |
dm.logKifu(dragged_id, move, b_x, b_y) | |
tebanChange(dm) | |
tebanChange = (dm) -> | |
#王手 | |
## 動いたあとの味方の駒の影響範囲に敵の王がいれば王手 | |
if dm.checkOute() == true | |
showMessage '王手!' | |
$("."+ dm.teban ).draggable("disable") | |
dm.setTeban dm.aite() | |
doTeban(dm) | |
showMessage = (msg) -> | |
$('#message h3').text(msg) | |
$('#message').show() | |
setTimeout -> | |
$('#message').hide().effect('clip','fast') | |
, 600 | |
gameEnd = () -> | |
$('.sente').draggable('disable') | |
$('.gote').draggable('disable') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment