2019-07-07 07:33:41 UNSELECTED

Unknown type code

Copy Copied! Full
--駒を置けるか探す処理 function PosibleSearch(min, ang) --ここから横縦斜めの、駒置けるか判断する処理 local ama = (min - 1) % 8 local num = 0 local 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 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 then for i = 1, num do BRD_ANG[min - i] = ang end BRD_FLG[min] = TARG_NUM end local 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 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 then for i = 1, num do BRD_ANG[min - i * 8] = ang end BRD_FLG[min] = TARG_NUM end --斜 num = 0 FLG = false local data = 0 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 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 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 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 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 --自動化部分 local FullSearchCount = 10 local MinScore = -10000 local MaxScore = 10000 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) local 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 break end end --駒置けるところを探す PosibleSearch(min, ang) end if ang == 0 then --今回はここのみ ang = 180 else ang = 0 end end function Estimate(board) --残り駒12以下なら if (64 - board.stones <= FullSearchCount) then return fullSearch(TableBoardCreate(board)) end return iterativeDeepning(TableBoardCreate(board)) end function fullSearch(board) local scores = -10000000000 local num = 0 --スコア保存 if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end --移動可能場所求める local movables = MovePossible(TableBoardCreate(board)) 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 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 move(board, move) --駒置けるところを探す if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end 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 board.stones = board.stones + 1 break end end return board end function alphaBetaFull(board, passes, a, b) if (board.stones == 64) then return stones(board) end if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end local movables = MovePossible(TableBoardCreate(board)) if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end if (#movables == 0) then return stones(board) 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 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 white - black end function iterativeDeepning(board) if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end --移動可能場所求める 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, 10 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 local k = 3 function evaluate(board, __board) movablesScore = 50 * (#MovePossible(board) - #MovePossible(TableBoardCreate(__board))) lineScore = k * (calcLineScore(board) - 0 * calcLineScore(TableBoardCreate(__board))) local white = 0 local black = 0 for i = 1, #board.brd_ang do if board.brd_ang[i] == board.now_ang then black = black + 1 else white = white + 1 end end return movablesScore + lineScore + (white - black) * 30 end function calcLineScore(board) local score = 0 for i = 1, #board.brd_ang do if board.brd_ang[i] == board.now_ang then if (i == 1) or (i == 8) or (i == 64) or (i == 57) then score = score + 60 end if (i == 2) or (i == 7) or (i == 63) or (i == 58) then score = score - 105 end if (i == 3) or (i == 5) or (i == 62) or (i == 59) then score = score + 26 end if (i == 10) or (i == 15) or (i == 50) or (i == 55) then score = score - 105 end if (i == 19) or (i == 22) or (i == 46) or (i == 43) then score = score + 26 end end end return score end --サイトのを変換して持ってくる function alphaBetaEval(board, depth, a, b, __board) if depth == nil then depth = 0 end if depth <= 0 then return -evaluate(TableBoardCreate(board), __board) end if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end local movables = MovePossible(TableBoardCreate(board)) if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end if #movables == 0 then if board.now_ang == 180 then board.now_ang = 0 else board.now_ang = 180 end return -alphaBetaEval(TableBoardCreate(board), depth - 1, -b, -a, __board) end for key, move_point in pairs(movables) do local aaa = -alphaBetaEval(move(TableBoardCreate(board), move_point), passes, -b, -a, board) if a < aaa then a = aaa end end return a end function MovePossible(board) local move_table = {} for i = 1, #board.brd_flg do if board.brd_flg[i] == 0 then if true == PosibleSearch22(i, board.now_ang, board.brd_ang) then table.insert(move_table, i) end end end return move_table end --駒を置けるか探す処理 function PosibleSearch22(min, now_ang, brd_ang) --ここから横縦斜めの、駒置けるか判断する処理 local ama = (min - 1) % 8 local num = 0 local FLG = false for i = 1, 7 - ama do if not (brd_ang[min + i] == now_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 then return true end num = 0 FLG = false for i = 1, ama do if not (brd_ang[min - i] == now_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 then return true end local sho = math.floor((min - 1) / 8) num = 0 FLG = false for i = 1, 7 - sho do if not (brd_ang[min + i * 8] == now_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 then return true end num = 0 FLG = false for i = 1, sho do if not (brd_ang[min - i * 8] == now_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 then return true end --斜 num = 0 FLG = false local data = 0 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] == now_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 then return true 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] == now_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 then return true 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] == now_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 then return true 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] == now_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 then return true end return false end