//Main logic module public Move Misirlou_v3(Map vmap) { timer.Start(); simap map = new CDC.simap(); map = map.createDeepClone(vmap); int now_sim = 0; //现在的模拟数 nodes rootnode = new nodes(); //创造根节点 rootnode.board = map; //复制地图进入根节点 List <Move> moveList = AI_tools.getallmove(rootnode.board); //根节点时的可行动列表 List <Move> flipList = AI_tools.getallfliplist(rootnode.board); //根节点时的翻棋列表 for (int a = 1; a <= max_sim; a++) { if (timer.ElapsedMilliseconds <= Lim_time) { UCT(rootnode, map.currentside, map.firstplayer); now_sim++; } else { break; } } Move bestmove = best_move(rootnode); timer.Stop(); timer.Reset(); return(bestmove); }
private void atk_expand(nodes node) { simap board2 = new CDC.simap(); board2 = node.board.createDeepClone(); List <Move> atkandmoveList = new List <Move>();//移动指令序列 List <Move> flippingList = new List <Move>(); node.visit = 1; //节点已经被访问过 atkandmoveList = AI_tools.getallmove(node.board); //移动指令变为对方可移动指令 flippingList = AI_tools.getallfliplist(node.board); for (int i = 0; i < atkandmoveList.Count; i++) //对攻击指令进行遍历 { node.child.Add(new nodes()); //添加子节点 node.child[i].movtion = atkandmoveList[i].deepclone(); //添加攻击移动指令进指令集 node.child[i].board = board2.createDeepClone(); //复制地图信息进入子节点 node.child[i].board.executemove(node.child[i].board, atkandmoveList[i]); node.child[i].depth = (node.depth) + 1; node.child[i].parent = node; } for (int i = 0; i < flippingList.Count; i++) { node.child.Add(new nodes()); //添加子节点 node.child[i + atkandmoveList.Count].movtion = flippingList[i].deepclone(); //添加翻棋移动指令进指令集 node.child[i + atkandmoveList.Count].board = board2.createDeepClone(); //复制地图信息进入子节点 node.child[i + atkandmoveList.Count].depth = (node.depth) + 1; node.child[i + atkandmoveList.Count].parent = node; node.child[i + atkandmoveList.Count].visit = -3;//翻棋节点不会有子节点,表示为翻棋节点 } }
//扩展结点 private void expand(nodes node) { simap board2 = new CDC.simap(); board2 = node.board.createDeepClone(); List <Move> atkandmoveList; //移动指令序列 List <Move> flipList; //攻击指令序列 atkandmoveList = AI_tools.getallmove(node.board); //移动指令变为对方可移动指令 flipList = AI_tools.getallfliplist(node.board); //攻击指令变为对方可攻击指令 for (int i = 0; i < atkandmoveList.Count; i++) //对攻击指令进行遍历 { node.child.Add(new nodes()); //添加子节点 node.child[i].movtion = atkandmoveList[i]; //添加攻击移动指令进指令集 node.child[i].board = board2.createDeepClone(); //复制地图信息进入子节点 node.child[i].board.executemove(node.child[i].board, atkandmoveList[i]); } for (int i = 0; i < flipList.Count; i++) //对翻棋指令进行遍历 { node.child.Add(new nodes()); //添加子节点 node.child[i + atkandmoveList.Count].movtion = flipList[i]; //添加翻棋指令进指令集 node.child[i + atkandmoveList.Count].board = board2.createDeepClone(); //复制地图信息进入子节点 node.child[i + atkandmoveList.Count].board.executemove(node.child[i + atkandmoveList.Count].board, flipList[i]); //在子节点地图上执行指令 } }
public Move Misirlou_v3(block[][] matrix, int currentside, int firstplayer, int turn)//Monte-Carlo { timer.Start(); int now_sim = 0; //现在的模拟数 nodes rootnode = new nodes(); //创造根节点 rootnode.board = matrix; //复制地图进入根节点 rootnode.side = currentside; //根节点的玩家参数 List <Move> moveList = AI_tools.getallmove(rootnode.board, currentside); //根节点时的可行动列表 List <Move> flipList = AI_tools.getallfliplist(rootnode.board); //根节点时的翻棋列表 for (int a = 1; a <= max_sim; a++) { UCT(rootnode, currentside, firstplayer); now_sim++; } Move bestmove = best_move(rootnode); timer.Stop(); Lef_time -= timer.ElapsedMilliseconds; timer.Reset(); return(bestmove); }
private bool domoveandatk(simap board, int currentside) { List <Move> atkandmovelist = AI_tools.getallmove(board); if (atkandmovelist.Count == 0) { return(false); } int tmp = rand.Next(atkandmovelist.Count); board.executemove(board, atkandmovelist[tmp]); return(true); }
private Move getnear(simap map, int currentside) { double distance = 0; double mindistance = 100; List <Move> movtionlist = new List <Move>(); Move nearmove = new Move(); movtionlist = AI_tools.getallmove(map); foreach (Move movtion in movtionlist) { distance = (Math.Sqrt((Math.Pow(Math.Abs(movtion.desY - movtion.froY), 2) + Math.Pow(Math.Abs(movtion.desX - movtion.froX), 2)))); if (distance < mindistance) { nearmove = movtion; } } if (mindistance == 100) { nearmove = movtionlist[rand.Next(movtionlist.Count)]; } return(nearmove); }
public nodes subAI_Evaluate(Map map) { simap map2 = new CDC.simap(); map2 = map2.createDeepClone(map); //复制一下地图 List <Move> movtionlist = new List <Move>(); //移动指令表 List <Move> fliplist = new List <Move>(); //翻棋指令表 List <Move> Alllist = new List <Move>(); //翻棋指令表 Move bestmove = new CDC.Move(); //最佳动作 Move bestflip = new Move(); //最佳翻棋 Move startflip = new Move(); //起始翻棋 nodes bestnode = new nodes(); if (map2.turncount == 1) //一局开始 { fliplist = AI_tools.getallfliplist(map2); //可翻行动列表 startflip = fliplist[rand.Next(fliplist.Count)]; //起始翻 map2.firstplayer = map.Matrix[startflip.froX, startflip.froY].item.side; //第一个棋子即为第一玩家的颜色 map2.secondplayer = (map.Matrix[startflip.froX, startflip.froY].item.side + 1) % 2; //第二玩家为另一颜色 map2.currentside = map2.firstplayer; bestnode.movtion.froX = startflip.froX; bestnode.movtion.froY = startflip.froY; bestnode.movtion.desX = startflip.desX; bestnode.movtion.desY = startflip.desY; return(bestnode); } else//非开局 { int point; int Maxpoint = 0; movtionlist = AI_tools.getallmove(map2); //获取所有可行指令 foreach (Move movtion in movtionlist) //遍历进攻选项 { point = AI_tools.point_Evaluate(map2, movtion); //计算进攻选项得点 if (point > Maxpoint) //判断得点最大 { Maxpoint = point; bestmove = movtion; bestnode.movtion.froX = bestmove.froX; bestnode.movtion.froY = bestmove.froY; bestnode.movtion.desX = bestmove.desX; bestnode.movtion.desY = bestmove.desY; return(bestnode); } } if (Maxpoint == 0)//为移动指令时 { if (movtionlist.Count != 0) { bestmove = getnear(map2, map2.currentside); } Alllist = AI_tools.getmovflip(map2); if (Alllist.Count == 0) { checkboard(map2); } bestmove = Alllist[rand.Next(Alllist.Count)];//无有效步的时候生成随机步数 } } bestnode.movtion.froX = bestmove.froX; bestnode.movtion.froY = bestmove.froY; bestnode.movtion.desX = bestmove.desX; bestnode.movtion.desY = bestmove.desY; return(bestnode); }
public nodes alpha_Beta(Map map) { //start rootnode; nodes rootnode = new nodes(); simap board = new simap(); nodes bestnode = new nodes(); List <Move> atkmovtion = new List <Move>(); List <Move> allmovtion = new List <Move>(); double maxscore = -1000; board = board.createDeepClone(map); rootnode.board = board; rootnode.depth = 0;//根节点的深度为零 currentside = rootnode.board.currentside; atkmovtion = AI_tools.getallatk(board); allmovtion = AI_tools.getallmove(board); alphabeta(rootnode); Console.WriteLine("CUT:" + cut + "\r\n\r\n\r\n"); foreach (nodes child in rootnode.child) { if (child.visit == 0) { continue; } else if (maxscore <= child.beta) { maxscore = child.beta; bestnode = child; } } if (deloop(bestnode, map) == true)//6步里2步以上重复 且为bestnode 则跳出 { rootnode.child.Remove(bestnode); if (map.unflipped > 0) { bestnode = smartflipping(rootnode, map); } else if (map.unflipped == 0 && rootnode.child.Count != 0) { foreach (nodes achild in rootnode.child) { if (maxscore <= achild.beta) { maxscore = achild.beta; bestnode = achild; } } } else if (map.unflipped == 0 && rootnode.child.Count == 0) { bestnode = subAI_Evaluate(map); } } if (bestnode.movtion.desX == -1 && bestnode.movtion.desY == -1) { bestnode = smartflipping(rootnode, map); } return(bestnode); }
private void atkandmove(Map map, nodes node) { #region Branching Factor Part List <Move> flipList = new List <Move>(); List <Move> movelist = new List <Move>(); simap map2 = new CDC.simap(); map2 = map2.createDeepClone(vmap); int branchingF = 0; int flipList_num = 0; int e_flip = AI_tools.getallfliplist(map2).Count; if ((map.currentside == 0 && map.firstplayer == 0) || (map.currentside == 1 && map.firstplayer == 1))//Red { e_flip = (e_flip - map.unflipp_blue); } else if ((map.currentside == 0 && map.firstplayer == 1) || (map.currentside == 1 && map.firstplayer == 0))//Blue { e_flip = (e_flip - map.unflipp_red); } flipList_num = e_flip;//从缩减后改为毫无缩减的状况 movelist = AI_tools.getallmove(map2); flipList = AI_tools.getallfliplist(map2); branchingF = movelist.Count + flipList.Count; allB.Add(branchingF); #endregion Move use = new CDC.Move(); use.froX = node.movtion.froX; use.froY = node.movtion.froY; use.desX = node.movtion.desX; use.desY = node.movtion.desY; //避免将原数据消除 if (use.desX == -1 && use.desY == -1 && use.froX != -1 && use.froY != -1) //AI给出的行动为翻棋 { flipchess(map, use.froX, use.froY); } if (use.froX != -1 && use.froY != -1 && use.desX != -1 && use.desY != -1)//都选定的情况 { if (GameController.checkmove(map, use) == true) { setmove(map, use); } else { use.froX = -1; use.froY = -1; use.desX = -1; use.desY = -1; } } if (map.currentside == 0) { label1.Text = "Current Player: First Player"; } if (map.currentside == 1) { label1.Text = "Current Player: Second Player"; } if ((vmap.currentside + 1) % 2 == 0) //已经实行的那一步的实施方(因为再flpchess和setmove中改变了currentside的值)属于先手或者后手 { vmap.Plyr1move.Add(node); //movtion存入先手List vmap.Plyr_move.Add(node); } else if (((vmap.currentside + 1) % 2 == 1)) { vmap.Plyr2move.Add(node);//movtion存入后手List vmap.Plyr_move.Add(node); } double displayturn = map.turncount - 1; use.froX = -1; use.froY = -1; use.desX = -1; use.desY = -1; }