IEnumerator doAI() { yield return(0); if (Tool.getInstance().AILevel == 0) { yield return(new WaitForSeconds(0.1f)); } Tool.getInstance().getBestScore(gameBoard, 2, Tool.getInstance().AILevel); MoveDate md = Tool.getInstance().bestMove; isChecked = true; selectY = md.fromY; selectX = md.fromX; dealChessClick(md.toY, md.toX); yield return(0); if (Tool.getInstance().effectVolume != 0) { if (aiEffect == 0) { AudioSource.PlayClipAtPoint(moveAudio, Vector3.zero, Tool.getInstance().effectVolume); } else if (aiEffect == 1) { AudioSource.PlayClipAtPoint(eatAudio, Vector3.zero, Tool.getInstance().effectVolume); } else { AudioSource.PlayClipAtPoint(deadAudio, Vector3.zero, Tool.getInstance().effectVolume); } } }
public int Find() { ClassLibrary.JDataBase DB = new ClassLibrary.JDataBase(); try { DB.setQuery("select Code from AUTBazRasService a where a.BusNumber=" + BusNumber + " and a.MoveDate='" + MoveDate.ToString("yyyy-MM-dd HH:mm:ss") + "'"); DataTable DT = DB.Query_DataTable(); if (DT != null && DT.Rows.Count == 1) { return(int.Parse(DT.Rows[0]["Code"].ToString())); } } finally { DB.Dispose(); } return(0); }
private List <MoveDate> getAbleMoveDataList(int[,] gameBoard, int type) { List <MoveDate> moveList = new List <MoveDate> (); for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (gameBoard [y, x] != type) { continue; } if (x - 1 >= 0 && gameBoard [y, x - 1] == 0) //棋子是否可以往左走 { MoveDate tp = new MoveDate(y, x, y, x - 1); moveList.Add(tp); } if (x + 1 < 4 && gameBoard [y, x + 1] == 0) //往右走 { MoveDate tp = new MoveDate(y, x, y, x + 1); moveList.Add(tp); } if (y - 1 >= 0 && gameBoard [y - 1, x] == 0) //往下走 { MoveDate tp = new MoveDate(y, x, y - 1, x); moveList.Add(tp); } if (y + 1 < 4 && gameBoard [y + 1, x] == 0) //往上走 { MoveDate tp = new MoveDate(y, x, y + 1, x); moveList.Add(tp); } } } return(moveList); }
private int tryMoveChess(int[,] gameBoard, MoveDate md, int type) { int enemy = 1; if (type == 1) { enemy = 2; } int score = 0; gameBoard [md.fromY, md.fromX] = 0; gameBoard [md.toY, md.toX] = type; int [] tmp = new int[4]; int judgeIndex = 0; if (type == 2) { judgeIndex = 4; } int eat = 0; for (int i = 0; i < 4; i++) { tmp [i] = gameBoard [md.toY, i]; } if (judgeEqual(tmp, judgeIndex + 0) || judgeEqual(tmp, judgeIndex + 1) || judgeEqual(tmp, judgeIndex + 2) || judgeEqual(tmp, judgeIndex + 3)) { eat++; for (int i = 0; i < 4; i++) { if (gameBoard[md.toY, i] == enemy) { gameBoard[md.toY, i] = 0; } } } for (int i = 0; i < 4; i++) { tmp [i] = gameBoard [i, md.toX]; } if (judgeEqual(tmp, judgeIndex + 0) || judgeEqual(tmp, judgeIndex + 1) || judgeEqual(tmp, judgeIndex + 2) || judgeEqual(tmp, judgeIndex + 3)) { eat++; for (int i = 0; i < 4; i++) { if (gameBoard[i, md.toX] == enemy) { gameBoard[i, md.toX] = 0; } } } if (eat == 2) { score += eatDoubleChess; } else if (eat == 1) { score += eatChess; } if (!isAbleToMove(gameBoard, enemy)) { score += victory; } if (!isChessEnough(gameBoard, enemy)) { score += victory; } return(score); }
public int getBestScore(int [,] gameBoard, int type, int level) //参数分别对应棋盘数组,谁走棋,博弈树的深度*2,通俗的可以理解为AI的等级 { int bestScore = -1000000; //初始分数 int bestIndex = -1; //初始下标 int enemy = 1; //用户转换敌人 if (type == 1) { enemy = 2; } List <MoveDate> moveList1 = getAbleMoveDataList(gameBoard, type); //获取所有能走的步法 do { if (moveList1.Count == 0) //没有则无棋可走,直接输了 { bestScore = -victory; break; } for (int i = 0; i < moveList1.Count; i++) { MoveDate md1 = moveList1[i]; int [,] tmpGame1 = (int [, ])gameBoard.Clone(); int ownScore = tryMoveChess(tmpGame1, md1, type); //对AI的步法进行基础评分 List <MoveDate> moveList2 = getAbleMoveDataList(tmpGame1, enemy); //获取对手所有可走的步法 if (moveList2.Count == 0) //对手无棋可走,直接获胜 { bestScore = victory; moveList1[i].score = bestScore; bestIndex = i; break; } int bestEnemyScore = -1000000; //对手的初始分 int bestEnemyIndex = -1; for (int j = 0; j < moveList2.Count; j++) //寻找对手最优的得分,即对自己最不利的步法 { MoveDate md2 = moveList2[j]; int [,] tmpGame2 = (int [, ])tmpGame1.Clone(); int enemyScore = tryMoveChess(tmpGame2, md2, enemy); if (enemyScore >= victory) { bestEnemyScore = victory; break; } if (level > 0) //递归获取更深节点博弈树的分数 { int tmpBestScore = getBestScore(tmpGame2, type, level - 1); enemyScore -= tmpBestScore; } if (enemyScore > bestEnemyScore) { bestEnemyScore = enemyScore; bestEnemyIndex = j; } } moveList1[i].score = ownScore - bestEnemyScore; if (bestEnemyScore >= victory) { continue; } else if (ownScore - bestEnemyScore > bestScore) { bestScore = ownScore - bestEnemyScore; bestIndex = i; } } }while(false); if (level == AILevel) { if (bestIndex != -1) { bestMove = getRandBestMove(moveList1, bestIndex); } else { bestMove = moveList1[0]; } } return(bestScore - 1); }