private void doRandomMove(simap board) { List <Move> alllist = AI_tools.getmovflip(board); int tmp = rand.Next(alllist.Count); board.executemove(board, alllist[tmp]); }
public static bool getcheckone(simap map, Move move) { List <Move> movtionlist = new List <Move>(); bool value = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++)//遍历棋盘 { if ((map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.firstplayer && map.currentside == 0) || (map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.secondplayer && map.currentside == 1))//遍历已翻开的棋子里面的己方棋子 { Move movtion = new CDC.Move(); movtion.froX = i; movtion.froY = j; movtion.desX = move.desX; movtion.desY = move.desY; if (GameController.checkmove(map, movtion) == true)//合法步的话加入List { movtionlist.Add(movtion); } } } } if (movtionlist.Count != 0) { value = true; } return(value); }
public static List <Move> getmovflip(simap map) { List <Move> fliplist = new List <Move>(); List <Move> movtionlist = new List <Move>(); fliplist = getallfliplist(map); movtionlist = getallmove(map); movtionlist.AddRange(fliplist); return(movtionlist); }
private bool doonlyMove(simap board, int currentside) { List <Move> movelist = AI_tools.getonlymove(board); if (movelist.Count == 0) { return(false); } int tmp = rand.Next(movelist.Count); board.executemove(board, movelist[tmp]); return(true); }
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 void domove(simap board, int currentside) { int side = currentside; while (doRandomflip(board, side)) { side = (side + 1) % 2; } while (domoveandatk(board, side)) { side = (side + 1) % 2; } }
private bool doRandomflip(simap board, int currentside) { List <Move> fliplist = AI_tools.getallfliplist(board); if (fliplist.Count == 0) { return(false); } int tmp = rand.Next(fliplist.Count); board.executemove(board, fliplist[tmp]); return(true); }
private double checkboard(simap map) { int redCount = 0; int blueCount = 0; List <Move> alllist = AI_tools.getmovflip(map); if ((map.turncount >= 300) || (alllist.Count == 0))//Limit number of Draw { return(0.5); } for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if (map.Matrix[i, j].item.side == 0) //为红方 { redCount++; //红加1 } if (map.Matrix[i, j].item.side == 1) //为蓝方 { blueCount++; //蓝加1 } } } if (redCount == 0) //Blue win! { if ((map.firstplayer == 0 && map.currentside == 0) || (map.firstplayer == 1 && map.currentside == 1)) //map.currentside is red { return(0); } if ((map.firstplayer == 1 && map.currentside == 0) || (map.firstplayer == 1 && map.currentside == 0))//map.currentside is blue { return(1); } } if (blueCount == 0) //Red win ! { if ((map.firstplayer == 1 && map.currentside == 0) || (map.firstplayer == 1 && map.currentside == 0)) //map.currentside is blue { return(0); } if ((map.firstplayer == 0 && map.currentside == 0) || (map.firstplayer == 1 && map.currentside == 1))//map.currentside is red { return(1); } } return(-1);//游戏未结束则返回-1 }
//到达末端结点则进行一次终盘模拟 private void simulate(nodes node, int currentside, int firstplayer) { //叶子结点为最终节点 if (checkboard(node.board) != -1)//即游戏已结束 { if (checkboard(node.board) == 1) { node.sim_Num++; node.win_Num++; } else if (checkboard(node.board) == 0) { node.sim_Num++;//模拟数+1 } else { node.sim_Num++; node.win_Num += 0.5; } } else//游戏没结束则扩展节点 { expand(node);//扩展末端节点,被扩展的子节点内只有Move int temp = rand.Next(node.child.Count); //随机选择一个末端子节点 simap board2 = node.child[temp].board.createDeepClone(); //复制末端子节点的地图 while (checkboard(board2) == -1) //循环进行模拟直到终局 { doRandomMove(board2); } //更新末端节点胜利数与模拟数 if (checkboard(board2) == 1) //终局模拟返回为胜 { node.win_Num++; //末端子节点胜利数+1 node.sim_Num++; //末端子节点模拟数+1 } else if (checkboard(board2) == 0.5) { node.win_Num += 0.5; node.sim_Num++; } else { node.sim_Num++; } } }
public static bool checktac_op(simap map)//是否可以攻击 { bool isit = false; List <Move> movtionlist = new List <Move>(); int currentside = (map.currentside + 1) % 2; for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++)//遍历棋盘 { if ((map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.firstplayer && currentside == 0) || (map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.secondplayer && currentside == 1))//遍历已翻开的棋子里面的己方棋子 { for (int a = 0; a < 4; a++) { for (int b = 0; b < 8; b++) { if ((map.Matrix[a, b].flip == 1 && map.Matrix[a, b].item.side == map.firstplayer && currentside == 1) || (map.Matrix[a, b].flip == 1 && map.Matrix[a, b].item.side == map.secondplayer && currentside == 0))//遍历已翻开的棋子里面的非己方棋子 { Move movtion = new CDC.Move(); movtion.froX = i; movtion.froY = j; movtion.desX = a; movtion.desY = b; if (GameController.checkmove(map, movtion) == true)//合法步的话加入List { movtionlist.Add(movtion); } } } } } } } if (movtionlist.Count != 0) { isit = true; } else if (movtionlist.Count == 0) { isit = false; } return(isit); }
public static bool checkmove(simap map, Move move)//检查步数是否合法 { chesstype my_item_type = map.Matrix[move.froX, move.froY].item.type; chesstype target_type = map.Matrix[move.desX, move.desY].item.type; if (map.Matrix[move.froX, move.froY].flip != 0)//选中区域不为不为背面的状态。 { if ((Math.Abs(move.desY - move.froY) <= 1 && Math.Abs(move.desX - move.froX) == 0) || (Math.Abs(move.desY - move.froY) == 0 && Math.Abs(move.desX - move.froX) <= 1)) //只能移动一格 { if (map.Matrix[move.desX, move.desY].item.type == chesstype.blank) //移动棋子 { return(true); } else if (my_item_type > target_type)//我的棋子rank更小则返回错误除非为卒和将 { if (my_item_type == chesstype.zu && target_type == chesstype.jiang) { return(true); } return(false); } else if (my_item_type == chesstype.jiang && target_type == chesstype.zu)//将不能吃卒 { return(false); } else if (my_item_type == chesstype.pao && target_type == chesstype.zu) { return(false); } else if (my_item_type == chesstype.pao && target_type == chesstype.pao) { return(false); } return(true); } else if (my_item_type == chesstype.pao) { if (judge_pao(map, move) == true) { return(true); } } } return(false); }
public static List <Move> getallfliplist(simap map) { List <Move> fliplist = new List <Move>(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if (map.Matrix[i, j].flip == 0)//未翻开的状态的话 { Move flip = new Move(); flip.froX = i; flip.froY = j; fliplist.Add(flip); } } } return(fliplist); }
public static int point_Evaluate(simap map, Move move) { int point = -1; chesstype target_type = map.Matrix[move.desX, move.desY].item.type; switch (target_type) { case chesstype.jiang: point = 10; break; case chesstype.shi: point = 7; break; case chesstype.xiang: point = 5; break; case chesstype.che: point = 4; break; case chesstype.ma: point = 3; break; case chesstype.zu: point = 2; break; case chesstype.pao: point = 6; break; case chesstype.blank: //移动 point = 0; break; } return(point); }
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 static List <Move> getallmove(simap map) { List <Move> movtionlist = new List <Move>(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++)//遍历棋盘 { if ((map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.firstplayer && map.currentside == 0) || (map.Matrix[i, j].flip == 1 && map.Matrix[i, j].item.side == map.secondplayer && map.currentside == 1))//遍历已翻开的棋子里面的己方棋子 { for (int a = 0; a < 4; a++) { for (int b = 0; b < 8; b++) { if ((map.Matrix[a, b].flip == 1 && map.Matrix[a, b].item.side == map.firstplayer && map.currentside == 1) || (map.Matrix[a, b].flip == 1 && map.Matrix[a, b].item.side == map.secondplayer && map.currentside == 0) || (map.Matrix[a, b].flip == 1 && map.Matrix[a, b].item.side == 2)) //遍历已翻开的棋子里面的非己方棋子 { Move movtion = new CDC.Move(); movtion.froX = i; movtion.froY = j; movtion.desX = a; movtion.desY = b; if (GameController.checkmove(map, movtion) == true)//合法步的话加入List { movtionlist.Add(movtion); } } } } } } } return(movtionlist); }
public static List <Move> getallatk(simap map) { List <Move> atklist = new List <Move>(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) //遍历棋盘 { if (map.Matrix[i, j].flip != 0 && map.Matrix[i, j].item.side == map.currentside) //遍历已翻开的棋子里面的己方棋子 { for (int a = 0; a < 4; a++) { for (int b = 0; b < 8; b++) { if (map.Matrix[a, b].flip != 0 && map.Matrix[a, b].item.side != map.currentside)//遍历已翻开的棋子里面的非己方棋子 { Move movtion = new CDC.Move(); movtion.froX = i; movtion.froY = j; movtion.desX = a; movtion.desY = b; if (map.Matrix[a, b].item.side == (map.currentside + 1) % 2) //目的地为对方棋子 { if (GameController.checkmove(map, movtion) == true) //合法步的话加入List { atklist.Add(movtion); } } } } } } } } return(atklist); }
private void cal_Proba(Map map) { int unflip_blue_che = map.blueche; int unflip_blue_ma = map.bluema; int unflip_blue_jiang = map.bluejiang; int unflip_blue_shi = map.blueshi; int unflip_blue_xiang = map.bluexiang; int unflip_blue_zu = map.bluezu; int unflip_blue_pao = map.bluepao; int unflip_red_che = map.redche; int unflip_red_ma = map.redma; int unflip_red_jiang = map.redjiang; int unflip_red_shi = map.redshi; int unflip_red_xiang = map.redxiang; int unflip_red_zu = map.redzu; int unflip_red_pao = map.redpao; simap sim_map = new simap(); sim_map = sim_map.createDeepClone(map); List <Move> flipping = new List <Move>(); flipping = AI_tools.getallfliplist(sim_map); int all_unflip = flipping.Count; for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if ((map.Matrix[i, j].item.side == 1) && (map.Matrix[i, j].flip == 1))//蓝方 { switch (map.Matrix[i, j].item.type) { case chesstype.che: unflip_blue_che -= 1; break; case chesstype.ma: unflip_blue_ma -= 1; break; case chesstype.jiang: unflip_blue_jiang -= 1; break; case chesstype.pao: unflip_blue_pao -= 1; break; case chesstype.zu: unflip_blue_zu -= 1; break; case chesstype.shi: unflip_blue_shi -= 1; break; case chesstype.xiang: unflip_blue_xiang -= 1; break; } } if (map.Matrix[i, j].item.side == 0)//红方 { switch (map.Matrix[i, j].item.type) { case chesstype.che: unflip_red_che -= 1; break; case chesstype.ma: unflip_red_ma -= 1; break; case chesstype.jiang: unflip_red_jiang -= 1; break; case chesstype.pao: unflip_red_pao -= 1; break; case chesstype.zu: unflip_red_zu -= 1; break; case chesstype.shi: unflip_red_shi -= 1; break; case chesstype.xiang: unflip_red_xiang -= 1; break; } } } } map.Pbche = unflip_blue_che / all_unflip; map.Pbjiang = unflip_blue_jiang / all_unflip; map.Pbma = unflip_blue_ma / all_unflip; map.Pbxiang = unflip_blue_xiang / all_unflip; map.Pbshi = unflip_blue_shi / all_unflip; map.Pbzu = unflip_blue_zu / all_unflip; map.Pbpao = unflip_blue_pao / all_unflip; map.Prche = unflip_red_che / all_unflip; map.Prjiang = unflip_red_jiang / all_unflip; map.Prma = unflip_red_ma / all_unflip; map.Prxiang = unflip_red_xiang / all_unflip; map.Prshi = unflip_red_shi / all_unflip; map.Przu = unflip_red_zu / all_unflip; map.Prpao = unflip_red_pao / all_unflip; }
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 static bool judge_pao(simap map, Move move) { int count = 0; if ((map.Matrix[move.desX, move.desY].item.side == 2)) { return(false); } if (move.froX != move.desX && move.froY != move.desY)//炮的目标不在同一行或者同一列时 { return(false); } else//在同一行或者同一列 { if (move.desX > move.froX && move.froY == move.desY)//同列不同行且在下 { for (int i = move.froX + 1; i < move.desX; i++) { if (map.Matrix[i, move.froY].item.type != chesstype.blank) { count++; } } } if (move.desX < move.froX && move.froY == move.desY)//同列不同行且在上 { for (int i = move.desX + 1; i < move.froX; i++) { if (map.Matrix[i, move.froY].item.type != chesstype.blank) { count++; } } } if (move.desY < move.froY && move.froX == move.desX)//同行不同列且在左 { for (int j = move.desY + 1; j < move.froY; j++) { if (map.Matrix[move.froX, j].item.type != chesstype.blank) { count++; } } } if (move.desY > move.froY && move.froX == move.desX)//同行不同列且在右 { for (int j = move.froY + 1; j < move.desY; j++) { if (map.Matrix[move.froX, j].item.type != chesstype.blank) { count++; } } } if (count == 1) { return(true); } } return(false); }