本文整理汇总了C#中Board.GetValidMoves方法的典型用法代码示例。如果您正苦于以下问题:C# Board.GetValidMoves方法的具体用法?C# Board.GetValidMoves怎么用?C# Board.GetValidMoves使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Board
的用法示例。
在下文中一共展示了Board.GetValidMoves方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OpponentMove
// perform opponents next move on the board
private static bool OpponentMove(Board board, bool preComputeMyMove = true)
{
// if opponent can't move, i win!
if (!board.GetOpponentValidMoves().Any())
{
return false;
}
// start evaluation for my next move on a bunch of new threads
if (preComputeMyMove)
{
Searcher.I.PreComputeNextMove(board);
}
// ask for move
var move = GetOpponentMove();
// ensure it is valid
if (!board.GetValidMoves().Any(x => x.Equals(move)))
{
Console.WriteLine("Not a valid opponent move! Was it entered correctly? (y/n):");
// if opponent enters invalid move, they forfeit
if ("y".Equals(Console.ReadLine(), StringComparison.OrdinalIgnoreCase))
{
return false;
}
// otherwise it was a mistake, so let's try again
return OpponentMove(board, false);
}
// confirm move is correct, which is equivalent to rollback functionality
// a rollback would simply mean asking this question after performing the move instead of before
Console.WriteLine("Are you sure? 'undo' to rollback, anything else to continue.");
if ("undo".Equals(Console.ReadLine(), StringComparison.OrdinalIgnoreCase))
{
return OpponentMove(board, false);
}
// now that we know it's valid, and we've asked if they want to undo, perform move
board.Move(move);
return true;
}
示例2: BestMoveInternal
// recursive alpha beta
private BestMoveResultWithStats BestMoveInternal(Board board, int depth, int alpha, int beta, CancellationToken cancelToken)
{
// if we reached the bottom, return
if (depth == 0)
{
_numNodesAtDepthLimit++;
return new BestMoveResultWithStats(_config.Heuristic.Evaluate(board), null);
}
var isMaxTurn = board.MyPlayer == board.PlayerToMove;
var validMoves = board.GetValidMoves();
// if we hit game over before the depth limit, return infinity/-infinity if it's our/their turn
if (!validMoves.Any())
{
return new BestMoveResultWithStats(isMaxTurn ? int.MinValue : int.MaxValue, null);
}
BoardSpace bestMove = null;
// generate new boards for each move and evaluate them so we can sort
var validMovesWithBoard = validMoves.Select(x =>
{
var newBoard = board.Copy().Move(x);
_nodesGeneratedByDepth[depth]++;
var score = _config.Heuristic.Evaluate(newBoard);
return new {move = x, newBoard, score};
});
// if we're maxing, sort with largest first, otherwise sort with smallest first
if (isMaxTurn)
{
validMovesWithBoard = validMovesWithBoard.OrderByDescending(x => x.score);
}
else
{
validMovesWithBoard = validMovesWithBoard.OrderBy(x => x.score);
}
// evaluate this board because we'll need to for quiessence search
var boardScore = _config.Heuristic.Evaluate(board);
foreach (var move in validMovesWithBoard)
{
BestMoveResultWithStats childResult;
// if we're doing a quiessence search, check to see if heuristic score change is interesting
if (IsInterestingMove(boardScore, move.score))
{
// extend search depth because this move looks interesting
_numNodesQuiessenceSearched++;
childResult = BestMoveInternal(move.newBoard, depth, alpha, beta, cancelToken);
}
else
{
// normal evaluation
childResult = BestMoveInternal(move.newBoard, depth - 1, alpha, beta, cancelToken);
}
// if we're near timeout or asked to cancel, just bail :(
if (_timer.Timeout() || cancelToken.IsCancellationRequested)
{
_timedOut = true;
break;
}
if (isMaxTurn) // if it's a max turn, we want to check alpha
{
if (childResult.Score > alpha)
{
alpha = childResult.Score;
bestMove = move.move;
}
}
else // else it's a min turn, so we want to check beta
{
if (childResult.Score < beta)
{
beta = childResult.Score;
bestMove = move.move;
}
}
// alpha-beta trim
if (alpha >= beta)
{
break;
}
}
// if we didn't find anything good, just return the first one
if (bestMove == null)
{
bestMove = validMoves.First();
}
return new BestMoveResultWithStats(isMaxTurn ? alpha : beta, bestMove);
}
示例3: PreComputeNextMove
public void PreComputeNextMove(Board board)
{
// cancel any tasks that are running from last round
foreach (var task in _tasksByOpponentMove.Select(x => x.Value).Where(x => !x.BestMoveTask.IsCompleted))
{
task.CancelSource.Cancel();
}
// remove all saved tasks
_tasksByOpponentMove.Clear();
// for every possible opponent move, pretend like the opponent picks it and start computing my response
foreach (var move in board.GetValidMoves())
{
var newBoard = board.Copy().Move(move);
var task = new AsyncSearchTask(TimeSpan.MaxValue);
task.BestMoveTask = Task.Factory.StartNew(() =>
{
task.Timer.StartTimer();
var result = MoveGetter().BestMove(newBoard, _config, MoveTimer.I, task.CancelSource.Token);
task.Timer.StopTimer();
return result;
}, task.CancelSource.Token);
_tasksByOpponentMove[move] = task;
}
}