private int maxMove(CheckersBoard board, int depth, int alpha, int beta) { if (cutOffTest(board, depth)) { return(eval(board)); } List sucessors; List move; CheckersBoard nextBoard; int value; sucessors = board.legalMoves(); while (mayPlay(sucessors)) { move = (List)sucessors.pop_front(); nextBoard = (CheckersBoard)board.clone(); nextBoard.move(move); value = minMove(nextBoard, depth + 1, alpha, beta); if (value > alpha) { alpha = value; } if (alpha > beta) { return(beta); } } return(alpha); }
private List minimax(CheckersBoard board) { List sucessors; List move, bestMove = null; CheckersBoard nextBoard; int value, maxValue = Int32.MinValue; sucessors = board.legalMoves(); while (mayPlay(sucessors)) { move = (List)sucessors.pop_front(); nextBoard = (CheckersBoard)board.clone(); nextBoard.move(move); value = minMove(nextBoard, 1, maxValue, Int32.MaxValue); if (value > maxValue) { maxValue = value; bestMove = move; } } return(bestMove); }
// Клик мышки по полю public void panel_MouseClick(MouseEventArgs e) { int pos; pos = board.getСellPos(e.X, e.Y); if (pos != -1) { try { int cell = board.getCell(pos); if (cell != CheckersBoard.EMPTY && // Если выбрана фишка текущего игрока (((cell == CheckersBoard.WHITE || cell == CheckersBoard.WHITE_KING) && board.getCurrentPlayer() == CheckersBoard.WHITE) || ((cell == CheckersBoard.BLACK || cell == CheckersBoard.BLACK_KING) && board.getCurrentPlayer() == CheckersBoard.BLACK))) { if (board.selected.isEmpty()) { board.selected.push_back(pos); } else { int temp = (int)board.selected.peek_tail(); if (temp == pos) { board.selected.pop_back(); } else { MessageBox.Show("не правильный ход", "Ошибка"); } } peraent.Invalidate(); return; } else // Если выбрана пустая клетка или фишка другого игрока { bool good = false; CheckersBoard tempBoard; if (!board.selected.isEmpty()) // Если фишка выбрана { if (boards.Count == 0) { tempBoard = (CheckersBoard)board.clone(); boards.Push(tempBoard); } else { tempBoard = (CheckersBoard)boards.Peek(); } int from = (int)board.selected.peek_tail(); if (tempBoard.isValidMove(from, pos)) { tempBoard = (CheckersBoard)tempBoard.clone(); bool isAttacking = tempBoard.mustAttack(); tempBoard.move(from, pos); if (isAttacking && tempBoard.mayAttack(pos)) { board.selected.push_back(pos); boards.Push(tempBoard); } else { board.selected.push_back(pos); makeMoves(board.selected, board); peraent.Invalidate(); board.selected.clear(); boards = new Stack(); if (!gameEnded()) { Thread.Sleep(1000); computer.play(); peraent.Invalidate(); } //boards = new Stack(); } good = true; } else if (from == pos) { board.selected.pop_back(); boards.Pop(); good = true; } } if (!good) { MessageBox.Show("не правильный ход", "Ошибка"); } else { peraent.Invalidate(); } } } catch { MessageBox.Show("не правильный ход", "Ошибка"); } } }