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 Move deepclone() { Move movtion = new CDC.Move(); movtion.froX = this.froX; movtion.froY = this.froY; movtion.desX = this.desX; movtion.desY = this.desY; movtion.risk = this.risk; return(movtion); }
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 nodes(nodes parent)//创造节点的方法 { this.parent = parent; ucb = 10; //初始化UCB值为10 sim_Num = 0; //初始化节点模拟数为零 win_Num = 0; //初始化结点胜利数为零 child = new List <nodes>(); //创造出子节点序列 Move movtion = new CDC.Move(); win_ratio = 0;//胜率 turn = 0; visit = 0; alpha = -1000; beta = 1000; remain_flip_number = 0; resign = -1; }
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); }
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); }
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; }