/// <summary> /// 评价一个棋面上的一方 /// </summary> /// <param name="state">状态</param> /// <param name="type">评价方</param> /// <returns></returns> public int evaluateState(ChessBoard state, int type) { int value = 0; // 线状态 int[][] line = new int[6][]; line[0] = new int[17]; line[1] = new int[17]; line[2] = new int[17]; line[3] = new int[17]; line[4] = new int[17]; line[5] = new int[17]; int lineP; // 方便检查边界 for (int p = 0; p < 6; ++p) { line[p][0] = line[p][16] = Globe.EVA_OP; } // 从四个方向产生 for (int i = 0; i < Globe.BOARD_SIZE; ++i) { // 产生线状态 lineP = 1; for (int j = 0; j < Globe.BOARD_SIZE; ++j) { line[0][lineP] = Globe.getPieceType(state.chessBoard, i, j, type); // | line[1][lineP] = Globe.getPieceType(state.chessBoard, j, i, type); // - line[2][lineP] = Globe.getPieceType(state.chessBoard, i + j, j, type); // \ line[3][lineP] = Globe.getPieceType(state.chessBoard, i - j, j, type); // / line[4][lineP] = Globe.getPieceType(state.chessBoard, j, i + j, type); // \ line[5][lineP] = Globe.getPieceType(state.chessBoard, Globe.BOARD_SIZE - j - 1, i + j, type); // / ++lineP; } // 评估线状态 int special = i == 0 ? 4 : 6; for (int p = 0; p < special; ++p) { value += evaluateLine(line[p], true); } } return(value); }
/// <summary> /// 对一个状态的一个位置 /// 放置一种类型的棋子的优劣进行估价 /// </summary> /// <param name="state">状态</param> /// <param name="x">位置的横坐标</param> /// <param name="y">位置的纵坐标</param> /// <param name="type">棋子的类型</param> /// <returns>评估值</returns> private int evaluatePiece(ChessBoard state, int x, int y, int type) { int value = 0; // 估价值 int[] line = new int[17]; // 线状态 bool[] flagX = new bool[8]; // 横向边界标志 flagX[0] = x - 4 < 0; flagX[1] = x - 3 < 0; flagX[2] = x - 2 < 0; flagX[3] = x - 1 < 0; flagX[4] = x + 1 > 14; flagX[5] = x + 2 > 14; flagX[6] = x + 3 > 14; flagX[7] = x + 4 > 14; bool[] flagY = new bool[8]; // 纵向边界标志 flagY[0] = y - 4 < 0; flagY[1] = y - 3 < 0; flagY[2] = y - 2 < 0; flagY[3] = y - 1 < 0; flagY[4] = y + 1 > 14; flagY[5] = y + 2 > 14; flagY[6] = y + 3 > 14; flagY[7] = y + 4 > 14; line[4] = Globe.EVA_MY; // 设置中心棋子 // 横 line[0] = flagX[0] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x - 4, y].type, type)); line[1] = flagX[1] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x - 3, y].type, type)); line[2] = flagX[2] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x - 2, y].type, type)); line[3] = flagX[3] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x - 1, y].type, type)); line[5] = flagX[4] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x + 1, y].type, type)); line[6] = flagX[5] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x + 2, y].type, type)); line[7] = flagX[6] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x + 3, y].type, type)); line[8] = flagX[7] ? Globe.EVA_OP : (Globe.getPieceType(state.chessBoard[x + 4, y].type, type)); value += evaluateLine(line); // 纵 line[0] = flagY[0] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y - 4].type, type); line[1] = flagY[1] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y - 3].type, type); line[2] = flagY[2] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y - 2].type, type); line[3] = flagY[3] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y - 1].type, type); line[5] = flagY[4] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y + 1].type, type); line[6] = flagY[5] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y + 2].type, type); line[7] = flagY[6] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y + 3].type, type); line[8] = flagY[7] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x, y + 4].type, type); value += evaluateLine(line); // 左上-右下 line[0] = flagX[0] || flagY[0] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 4, y - 4].type, type); line[1] = flagX[1] || flagY[1] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 3, y - 3].type, type); line[2] = flagX[2] || flagY[2] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 2, y - 2].type, type); line[3] = flagX[3] || flagY[3] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 1, y - 1].type, type); line[5] = flagX[4] || flagY[4] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 1, y + 1].type, type); line[6] = flagX[5] || flagY[5] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 2, y + 2].type, type); line[7] = flagX[6] || flagY[6] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 3, y + 3].type, type); line[8] = flagX[7] || flagY[7] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 4, y + 4].type, type); value += evaluateLine(line); // 右上-左下 line[0] = flagX[7] || flagY[0] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 4, y - 4].type, type); line[1] = flagX[6] || flagY[1] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 3, y - 3].type, type); line[2] = flagX[5] || flagY[2] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 2, y - 2].type, type); line[3] = flagX[4] || flagY[3] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x + 1, y - 1].type, type); line[5] = flagX[3] || flagY[4] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 1, y + 1].type, type); line[6] = flagX[2] || flagY[5] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 2, y + 2].type, type); line[7] = flagX[1] || flagY[6] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 3, y + 3].type, type); line[8] = flagX[0] || flagY[7] ? Globe.EVA_OP : Globe.getPieceType(state.chessBoard[x - 4, y + 4].type, type); value += evaluateLine(line); return(value); }