2019-05-05 09:25:31 UNSELECTED

Unknown type code

Copy Copied! Full
--簡略化 ASET = vci.assets STAT = vci.state VEC001 = Vector3.__new(0, 0, 1) --Unity駒の名称 FRM_STR = { "Frame1", "Frame2", "Frame3", "Frame4", "Frame5", "Frame6", "Frame7", "Frame8", "Frame9", "Frame10", "Frame11", "Frame12", "Frame13", "Frame14", "Frame15", "Frame16", "Frame17", "Frame18", "Frame19", "Frame20", "Frame21", "Frame22", "Frame23", "Frame24", "Frame25", "Frame26", "Frame27", "Frame28", "Frame29", "Frame30", "Frame31", "Frame32", "Frame33", "Frame34", "Frame35", "Frame36", "Frame37", "Frame38", "Frame39", "Frame40", "Frame41", "Frame42", "Frame43", "Frame44", "Frame45", "Frame46", "Frame47", "Frame48", "Frame49", "Frame50", "Frame51", "Frame52", "Frame53", "Frame54", "Frame55", "Frame56", "Frame57", "Frame58", "Frame59", "Frame60", "Frame61", "Frame62", "Frame63", "Frame64" } --FRM_STR使用されているかどうか FRM_FLG = { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } TARG_NUM = 0 --STAT.Set("TARG_NUM", TARG_NUM) --FRM_STRの掴んだ番号 --盤面に置かれているFRM_STRの番号 BRD_FLG = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } --どの方向を向いているか(0:黒、90:駒なし、180:白) BRD_ANG = { 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 180, 0, 90, 90, 90, 90, 90, 90, 0, 180, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90 } GRIP_FLG = false --グリップ判定 --STAT.Set("GRIP_FLG", GRIP_FLG) USE_FLG = false --使用判定 --STAT.Set("USE_FLG", USE_FLG) BLACK_FLG = false --黒カプセルが押された --STAT.Set("BLACK_FLG", BLACK_FLG) WHITE_FLG = false --白カプセルが押された --STAT.Set("WHITE_FLG", WHITE_FLG) ang = 0 --現在駒方向 ANG = 0 --現在の手番を表す Mine = false function update() Mine = true TARG_NUM = STAT.Get("TARG_NUM") --ゲーム初期化 if (WHITE_FLG == true) or (STAT.Get("WHITE_FLG")) then WHITE_FLG = false BLACK_FLG = true --下も起動 ang = 0 for i = 1, #FRM_FLG do if i > 4 then FRM_FLG[i] = 0 end if i == 28 then BRD_FLG[i] = 1 BRD_ANG[i] = 180 elseif i == 37 then BRD_FLG[i] = 2 BRD_ANG[i] = 180 elseif i == 29 then BRD_FLG[i] = 3 BRD_ANG[i] = 0 elseif i == 36 then BRD_FLG[i] = 4 BRD_ANG[i] = 0 else BRD_FLG[i] = 0 BRD_ANG[i] = 90 end end end local stones = 0 for i = 1, #FRM_FLG do if FRM_FLG[i] == 1 then stones = stones + 1 end end if #FRM_FLG == stones then brd_flg = TableCreate(BRD_FLG) brd_ang = TableCreate(BRD_ANG) black_count = 1 for i2 = 1, 2 do for i = 1, #brd_ang do if i2 == 1 then if brd_ang[i] == 0 then BRD_FLG[black_count] = black_count BRD_ANG[black_count] = 0 black_count = black_count + 1 end else if brd_ang[i] == 180 then BRD_FLG[black_count] = black_count BRD_ANG[black_count] = 180 black_count = black_count + 1 end end end end end if ang == 0 then --グリップされていたならば if ((GRIP_FLG == true) or (STAT.Get("GRIP_FLG") == true)) and (TARG_NUM > 0) then min = 0 --近傍マス目 min_loc = 0.06 --最近傍距離 --現在持っている駒の位置と盤面のマス目の最近傍を求める for i = 1, #BRD_FLG do if BRD_FLG[i] == 0 then OBJ = ASET.GetSubItem("Board") POS = OBJ.GetPosition() ANG = Vector3.Angle(VEC001, OBJ.GetForward()) if OBJ.GetForward().x < 0 then ANG = -1 * ANG end sho = (-3.5 + math.floor((i - 1) / 8)) * 0.08 ama = (-3.5 + (i - 1) % 8) * 0.08 OBJ = ASET.GetSubItem(FRM_STR[TARG_NUM]) print(TARG_NUM .. ";" .. FRM_STR[TARG_NUM]) ANG2 = Vector3.Angle(VEC001, Vector3.__new(sho, 0, ama).normalized) if sho < 0 then ANG2 = -1 * ANG2 end pow1 = sho * sho pow2 = ama * ama POS2 = POS POS2.Set( POS.x + math.sqrt(pow1 + pow2) * math.sin(math.pi * (ANG2 - ANG) / 180), POS.y + 0.1, POS.z - math.sqrt(pow1 + pow2) * math.cos(math.pi * (ANG2 - ANG) / 180) ) POS = OBJ.GetPosition() loc = math.sqrt((POS.x - POS2.x) * (POS.x - POS2.x) + (POS.z - POS2.z) * (POS.z - POS2.z)) if min_loc > loc then min_loc = loc min = i end end end --luaでは配列[0]エラーになるから、それ以外の時補助の駒を動かす OBJ = ASET.GetSubItem("Board") POS = OBJ.GetPosition() if min > 0 then ANG = Vector3.Angle(VEC001, OBJ.GetForward()) if OBJ.GetForward().x < 0 then ANG = -1 * ANG end sho = (-3.5 + math.floor((min - 1) / 8)) * 0.08 ama = (-3.5 + (min - 1) % 8) * 0.08 OBJ = ASET.GetSubItem("Frame100") ANG2 = Vector3.Angle(VEC001, Vector3.__new(sho, 0, ama).normalized) if sho < 0 then ANG2 = -1 * ANG2 end pow1 = sho * sho pow2 = ama * ama OBJ.SetPosition( Vector3.__new( POS.x + math.sqrt(pow1 + pow2) * math.sin(math.pi * (ANG2 - ANG) / 180), POS.y + 0.1, POS.z - math.sqrt(pow1 + pow2) * math.cos(math.pi * (ANG2 - ANG) / 180) ) ) OBJ.SetRotation(Quaternion.Euler(ang, 0, 0)) else OBJ = ASET.GetSubItem("Board") POS = OBJ.GetPosition() OBJ = ASET.GetSubItem("Frame100") pow1 = sho * sho pow2 = ama * ama OBJ.SetPosition( Vector3.__new( POS.x + math.sqrt(pow1 + pow2) * math.sin(math.pi * (ANG2 - ANG) / 180), 0, POS.z - math.sqrt(pow1 + pow2) * math.cos(math.pi * (ANG2 - ANG) / 180) ) ) end --使用されたならば if (USE_FLG == true) or (STAT.Get("USE_FLG") == true) then if min > 0 then GRIP_FLG = false STAT.Set("GRIP_FLG", GRIP_FLG)--駒置けるところを探す PosibleSearch() --駒をとれた(置けた)ならば次の人 if BRD_FLG[min] > 0 then BRD_ANG[min] = ang FRM_FLG[TARG_NUM] = 1 if ang == 0 then --今回はここのみ ang = 180 else ang = 0 end end USE_FLG = false end end else OBJ = ASET.GetSubItem("Board") POS = OBJ.GetPosition() OBJ = ASET.GetSubItem("Frame100") OBJ.SetPosition(Vector3.__new(POS.x, -10, POS.z)) end if #FRM_FLG > stones then local move_table = {} for i = 1, #BRD_FLG do if BRD_FLG[i] == 0 then if true == PosibleSearch22(i, 0, BRD_ANG) then table.insert(move_table, i) end end end if #move_table == 0 then ang = 180 end end elseif ang == 180 then local board = Board board.stones = 0 for i = 1, #FRM_FLG do if FRM_FLG[i] == 1 then board.stones = board.stones + 1 end end board.frm_flg = TableCreate(FRM_FLG) board.brd_flg = TableCreate(BRD_FLG) board.brd_ang = TableCreate(BRD_ANG) board.now_ang = 180 AI_Estimate(TableBoardCreate(board)) end STAT.Set("BLACK_FLG", BLACK_FLG) STAT.Set("WHITE_FLG", WHITE_FLG) STAT.Set("USE_FLG", USE_FLG) for i = 1, #FRM_FLG do STAT.Set(tostring(i), FRM_FLG[i]) end for i = 1, #BRD_FLG do STAT.Set(tostring(i + 100), BRD_FLG[i]) end for i = 1, #BRD_ANG do STAT.Set(tostring(i + 200), BRD_ANG[i]) end end function updateAll() if Mine==true then else for i = 1, #FRM_FLG do if not (STAT.Get(tostring(i)) == nil) then FRM_FLG[i] = STAT.Get(tostring(i)) end end for i = 1, #BRD_FLG do if not (STAT.Get(tostring(i)) == nil) then BRD_FLG[i] = STAT.Get(tostring(i + 100)) end end for i = 1, #BRD_ANG do if not (STAT.Get(tostring(i)) == nil) then BRD_ANG[i] = STAT.Get(tostring(i + 200)) end end end --駒を黒カプセル周辺に集める if (BLACK_FLG == true) or (STAT.Get("BLACK_FLG") == true) then OBJ = ASET.GetSubItem("Black") POS = OBJ.GetPosition() for i = 1, #FRM_FLG do if FRM_FLG[i] == 0 then OBJ = ASET.GetSubItem(FRM_STR[i]) if OBJ.IsMine == true then OBJ.SetPosition( Vector3.__new( POS.x + 0.55 * math.sin(2 * math.pi * i / 54), POS.y, POS.z + 0.55 * math.cos(2 * math.pi * i / 54) ) ) end end end BLACK_FLG = false STAT.Set("BLACK_FLG", BLACK_FLG) GRIP_FLG = false STAT.Set("GRIP_FLG", GRIP_FLG) end Move_STROBJ() end function onUse(use) --駒を使用したならば for i = 1, #FRM_STR do if FRM_FLG[i] == 0 then if use == FRM_STR[i] then USE_FLG = true STAT.Set("USE_FLG", USE_FLG) end end end if use == "White" then WHITE_FLG = true STAT.Set("WHITE_FLG", WHITE_FLG) BLACK_FLG = true STAT.Set("BLACK_FLG", BLACK_FLG) end if use == "Black" then BLACK_FLG = true STAT.Set("BLACK_FLG", BLACK_FLG) end end function onTriggerExit(it, h) Velo_Ang_Zero(it) end function onCollisionExit(it, h) Velo_Ang_Zero(it) end function onGrab(tar) --駒を握ったならば for i = 1, #FRM_STR do if FRM_FLG[i] == 0 then if tar == FRM_STR[i] then TARG_NUM = i GRIP_FLG = true STAT.Set("TARG_NUM", TARG_NUM) STAT.Set("GRIP_FLG", GRIP_FLG) end end end end function onUngrab(tar) --駒を離したならば for i = 1, #FRM_STR do if FRM_FLG[i] == 0 then if tar == FRM_STR[i] then TARG_NUM = 0 GRIP_FLG = false STAT.Set("TARG_NUM", TARG_NUM) STAT.Set("GRIP_FLG", GRIP_FLG) end end end Velo_Ang_Zero(tar) end function Velo_Ang_Zero(it) OBJ = ASET.GetSubItem(it) OBJ.SetVelocity(Vector3.__new(0, 0, 0)) OBJ.SetAngularVelocity(Vector3.__new(0, 0, 0)) if it == "Board" then OBJ.SetRotation(Quaternion.Euler(0, ANG, 0)) end FIRST_FLG = true STAT.Set("FIRST_FLG", FIRST_FLG) end --駒をBRD_FLGに従い配置 function Move_STROBJ() --駒をBRD_FLGに従い配置 for i = 1, #BRD_FLG do if BRD_FLG[i] > 0 then OBJ = ASET.GetSubItem("Board") POS = OBJ.GetPosition() ANG = Vector3.Angle(VEC001, OBJ.GetForward()) if OBJ.GetForward().x < 0 then ANG = -1 * ANG end sho = (-3.5 + math.floor((i - 1) / 8)) * 0.08 ama = (-3.5 + (i - 1) % 8) * 0.08 OBJ = ASET.GetSubItem(FRM_STR[BRD_FLG[i]]) if OBJ.IsMine == true then ANG2 = Vector3.Angle(VEC001, Vector3.__new(sho, 0, ama).normalized) if sho < 0 then ANG2 = -1 * ANG2 end pow1 = sho * sho pow2 = ama * ama OBJ.SetPosition( Vector3.__new( POS.x + math.sqrt(pow1 + pow2) * math.sin(math.pi * (ANG2 - ANG) / 180), POS.y + 0.1, POS.z - math.sqrt(pow1 + pow2) * math.cos(math.pi * (ANG2 - ANG) / 180) ) ) OBJ.SetRotation(Quaternion.Euler(BRD_ANG[i], 0, 0)) end end end end --駒を置けるか探す処理 function PosibleSearch() --ここから横縦斜めの、駒置けるか判断する処理 ama = (min - 1) % 8 num = 0 FLG = false for i = 1, 7 - ama do if not (BRD_ANG[min + i] == ang) then if BRD_ANG[min + i] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end end if FLG == true then for i = 1, num do BRD_ANG[min + i] = ang end BRD_FLG[min] = TARG_NUM end num = 0 FLG = false for i = 1, ama do if not (BRD_ANG[min - i] == ang) then if BRD_ANG[min - i] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end end if FLG == true then for i = 1, num do BRD_ANG[min - i] = ang end BRD_FLG[min] = TARG_NUM end sho = math.floor((min - 1) / 8) num = 0 FLG = false for i = 1, 7 - sho do if not (BRD_ANG[min + i * 8] == ang) then if BRD_ANG[min + i * 8] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end end if FLG == true then for i = 1, num do BRD_ANG[min + i * 8] = ang end BRD_FLG[min] = TARG_NUM end num = 0 FLG = false for i = 1, sho do if not (BRD_ANG[min - i * 8] == ang) then if BRD_ANG[min - i * 8] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end end if FLG == true then for i = 1, num do BRD_ANG[min - i * 8] = ang end BRD_FLG[min] = TARG_NUM end --斜 num = 0 FLG = false if (7 - sho) < (7-ama) then data = 7 - sho else data = 7-ama end for i = 1, data do if min + i * 9 < 65 then if not (BRD_ANG[min + i * 9] == ang) then if BRD_ANG[min + i * 9] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end else break end end if FLG == true then for i = 1, num do BRD_ANG[min + i * 9] = ang end BRD_FLG[min] = TARG_NUM end num = 0 FLG = false if sho < ama then data = sho else data = ama end for i = 1, data do if min - i * 9 > 0 then if not (BRD_ANG[min - i * 9] == ang) then if BRD_ANG[min - i * 9] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end else break end end if FLG == true then for i = 1, num do BRD_ANG[min - i * 9] = ang end BRD_FLG[min] = TARG_NUM end num = 0 FLG = false if sho < (7 - ama) then data = sho else data = 7 - ama end for i = 1, data do if min - i * 7 > 0 then if not (BRD_ANG[min - i * 7] == ang) then if BRD_ANG[min - i * 7] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end else break end end if FLG == true then for i = 1, num do BRD_ANG[min - i * 7] = ang end BRD_FLG[min] = TARG_NUM end num = 0 FLG = false if (7 - sho) < ama then data = 7 - sho else data = ama end for i = 1, data do if min + i * 7 < 65 then if not (BRD_ANG[min + i * 7] == ang) then if BRD_ANG[min + i * 7] == 90 then break else num = num + 1 end else if num > 0 then FLG = true end break end else break end end if FLG == true then for i = 1, num do BRD_ANG[min + i * 7] = ang end BRD_FLG[min] = TARG_NUM end --駒をとれた(置けた)ならば次の人 if BRD_FLG[min] > 0 then flg = true end end --自動化部分 FullSearchCount = 16 MinScore = -10000 MaxScore = 10000 Board = { frm_flg = {}, brd_flg = {}, brd_ang = {}, now_ang = 0, stones = 0 } ScoreBase = { score = 0, brd_num = 0 } function TableCreate(table_data) local new = {} for k, v in ipairs(table_data) do new[k] = v end return new end function TableBoardCreate(table_data) local lists = Board lists.frm_flg = TableCreate(table_data.frm_flg) lists.brd_flg = TableCreate(table_data.brd_flg) lists.brd_ang = TableCreate(table_data.brd_ang) lists.now_ang = table_data.now_ang lists.stones = table_data.stones return lists end function AI_Estimate(board) min = Estimate(TableBoardCreate(board)) if min > 0 then --駒をとれた(置けた)ならば次の人 BRD_ANG[min] = ang for i = 1, #FRM_FLG do if FRM_FLG[i] == 0 then FRM_FLG[i] = 1 BRD_FLG[min] = i TARG_NUM = i print("i" .. i .. "min" .. min) break end end --駒置けるところを探す PosibleSearch() end if ang == 0 then --今回はここのみ ang = 180 else ang = 0 end end function Estimate(board) --残り駒12以下なら print("num:" .. board.stones .. "targ:" .. TARG_NUM) if (64 - board.stones <= FullSearchCount) then print("full") return fullSearch(TableBoardCreate(board)) end print("deep") return iterativeDeepning(TableBoardCreate(board)) end function move(board, move) --駒置けるところを探す PosibleSearch() for i = 1, #board.frm_flg do if board.frm_flg[i] == 0 then board.brd_flg[move] = i board.brd_ang[move] = board.now_ang board.frm_flg[i] = 1 break end end return board end function fullSearch(board) local scores = -100000000 local num = 0 --スコア保存 --移動可能場所求める local movables = MovePossible(TableBoardCreate(board)) for key, move_point in pairs(movables) do local data = -alphaBetaFull(move(TableBoardCreate(board), move_point), 0, -MaxScore, -MinScore) if scores < data then scores = data num = move_point end end return num end function stones(board) local white = 0 local black = 0 for i = 1, #board.brd_ang do if board.brd_ang[i] == 0 then black = black + 1 elseif board.brd_ang[i] == 180 then white = white + 1 end end return black - white end function alphaBetaFull(board, passes, a, b) if (board.stones == 64) then return stones(board) end local movables = MovePossible(TableBoardCreate(board)) if ((#movables == 0) and (passes > 0)) then return stones(board) end if (#movables == 0) then return -alphaBetaFull(TableBoardCreate(board), passes + 1, -b, -a) end if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end for key, move_point in pairs(movables) do local aaa = -alphaBetaFull(move(TableBoardCreate(board), move_point), passes, -b, -a) if a < aaa then a = aaa end if a >= b then return a end end return a end function iterativeDeepning(board) --移動可能場所求める local movables = MovePossible(TableBoardCreate(board)) if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end local scores = -10000000000000000 local num = 0 for depth = 3, 7 do for key, move_point in pairs(movables) do local data = -alphaBetaEval(move(TableBoardCreate(board), move_point), depth - 1, -MaxScore, -MinScore, board) if scores < data then scores = data num = move_point end end end return num end
RECOMMEND