public DraughtsBoard() { InitializeComponent(); foreach (var width in Enumerable.Range(0, BoardWidth)) { foreach (var height in Enumerable.Range(0, BoardHeight)) { int convertedY = (BoardHeight - 1) - height; var cell = new GameSquare(width, convertedY); if (width % 2 == 0 && height % 2 == 0) { cell.SquareColour = LightColourBrush; } else if (width % 2 != 0 && height % 2 != 0) { cell.SquareColour = LightColourBrush; } else { cell.SquareColour = DarkColourBrush; } cell.MouseLeftButtonDown += PieceLeftButtonDown; Grid.SetColumn(cell, width); Grid.SetRow(cell, height); Board.Children.Add(cell); squareList.Add(cell); } } SetupFromGameState(GameStateFactory.StandardStartGameState()); }
public IActionResult CreateGame() { var playerId = Guid.NewGuid().ToString(); var humanPlayer = new HumanPlayer(); IGamePlayer white; IGamePlayer black; if (random.Next() % 2 == 0) { white = humanPlayer; black = Program.AiOpponent; humanPlayer.PieceColour = Service.PieceColour.White; } else { white = Program.AiOpponent; black = humanPlayer; humanPlayer.PieceColour = Service.PieceColour.Black; } var match = new GameMatch(GameStateFactory.StandardStartGameState(), white, black); humanPlayer.GameMatch = match; humanPlayers.TryAdd(playerId, humanPlayer); return(Ok(playerId)); }
public void OpponentMovesAvailableTest() { var move = new GameMove( new GamePiece(PieceColour.White, PieceRank.Minion, 0, 2), new GamePiece(PieceColour.White, PieceRank.Minion, 1, 3), new List <GamePiece>(), GameStateFactory.StandardStartGameState() ); var metrics = move.CalculateGameMoveMetrics(PieceColour.White); Assert.AreEqual(7, metrics.OpponentMovesAvailable); }
public void FriendlyMinionsAwayTest() { var move = new GameMove( new GamePiece(PieceColour.White, PieceRank.Minion, 0, 2), new GamePiece(PieceColour.White, PieceRank.Minion, 1, 3), new List <GamePiece>(), GameStateFactory.StandardStartGameState() ); var metrics = move.CalculateGameMoveMetrics(PieceColour.White); Assert.AreEqual(0, metrics.FriendlyMinionsAway); }
public MainWindow() { InitializeComponent(); aiLoader = ((App)App.Current).AiLoader; Board.WhitePlayer = SetupService.FetchWhitePlayer(); Board.BlackPlayer = SetupService.FetchBlackPlayer(); gameMatch = new GameMatch(GameStateFactory.StandardStartGameState(), Board.WhitePlayer, Board.BlackPlayer); Info.UpdateFromGameMatch(gameMatch); dispatcherTimer.Tick += DispatcherTimer_Tick; dispatcherTimer.Interval = new TimeSpan(0, 0, 1); dispatcherTimer.Start(); }
private static int PlayGames(IGamePlayer contestant, IGamePlayer opponent, int gameCount) { int wins = 0; foreach (var randomGameI in Enumerable.Range(0, gameCount).Where(g => !shouldClose)) { var gameMatch = new GameMatch( GameStateFactory.StandardStartGameState(), contestant, opponent); var outcome = gameMatch.CompleteMatch(); if (outcome == GameMatchOutcome.WhiteWin) { wins++; } } return(wins); }
public int EvaluateNet(IReadOnlyList <Net> competingNets, Net evaluatingNet, TrainingStatus trainingStatus) { var currentPlayer = NetPlayerLookup[evaluatingNet]; foreach (var opponentNet in competingNets) { var opponent = NetPlayerLookup[opponentNet]; var gameMatch = new GameMatch( GameStateFactory.StandardStartGameState(), currentPlayer.GamePlayer, opponent.GamePlayer ); var matchResult = gameMatch.CompleteMatch(); Interlocked.Increment(ref _gamesPlayed); currentPlayer.IncrementMatch(); opponent.IncrementMatch(); int uniqueGameStateCount = gameMatch.GameStateList.Distinct().Count(); currentPlayer.AddUniqueGameStates(uniqueGameStateCount); if (matchResult == GameMatchOutcome.WhiteWin) { currentPlayer.IncrementWin(); Interlocked.Increment(ref _whiteWins); } else if (matchResult == GameMatchOutcome.BlackWin) { opponent.IncrementWin(); Interlocked.Increment(ref _blackWins); } else if (matchResult == GameMatchOutcome.Draw) { currentPlayer.IncrementDraw(); opponent.IncrementDraw(); Interlocked.Increment(ref _gamesDrawn); } } int winScore = currentPlayer.Wins * WinWeight; int drawScore = currentPlayer.Draws * DrawWeight; var randomGamePlayer = new RandomGamePlayer(_random); int randomWins = 0; foreach (var i in Enumerable.Range(0, RandomGamesToPlay)) { GameMatch gameMatch; GameMatchOutcome winOutcome; if (i % 2 == 0) { gameMatch = new GameMatch( GameStateFactory.StandardStartGameState(), currentPlayer.GamePlayer, randomGamePlayer); winOutcome = GameMatchOutcome.WhiteWin; } else { gameMatch = new GameMatch( GameStateFactory.StandardStartGameState(), randomGamePlayer, currentPlayer.GamePlayer); winOutcome = GameMatchOutcome.BlackWin; } var outcome = gameMatch.CompleteMatch(); if (outcome == winOutcome) { randomWins++; } } int randomScore = randomWins * RandomWinWeight; int score = winScore + drawScore + randomScore; return(score); }
static void Main(string[] args) { IGamePlayer ai = new RandomGamePlayer(); var gameState = GameStateFactory.StandardStartGameState(); while (true) { var board = new GamePiece[gameState.XLength, gameState.YLength]; foreach (var piece in gameState.GamePieceList) { board[piece.Xcoord, piece.Ycoord] = piece; } var rows = new List <string>(); foreach (var y in Enumerable.Range(0, gameState.YLength)) { List <string> row = new List <string>(); foreach (var x in Enumerable.Range(0, gameState.XLength)) { char pieceSymbol; var piece = board[x, y]; if (null == piece) { pieceSymbol = ' '; } else if (PieceColour.Black == piece.PieceColour && PieceRank.King == piece.PieceRank) { pieceSymbol = 'X'; } else if (PieceColour.White == piece.PieceColour && PieceRank.King == piece.PieceRank) { pieceSymbol = 'O'; } else if (PieceColour.Black == piece.PieceColour && PieceRank.Minion == piece.PieceRank) { pieceSymbol = 'x'; } else if (PieceColour.White == piece.PieceColour && PieceRank.Minion == piece.PieceRank) { pieceSymbol = 'o'; } else { throw new GameException("Unknown piece colour."); } row.Add(pieceSymbol.ToString()); } rows.Add($"{y}|{string.Join("|", row.ToArray())}"); } var rowSeperator = new string(Enumerable.Range(0, ((gameState.XLength + 1) * 2) - 1).Select(i => i % 2 == 0 ? '-' : '+').ToArray()); var output = string.Join(Environment.NewLine + rowSeperator + Environment.NewLine, rows.Reverse <string>()); output += Environment.NewLine + " |" + string.Join(string.Empty, Enumerable.Range(0, (gameState.XLength * 2) - 1).Select(i => i % 2 == 0 ? (i / 2).ToString() : "|").ToArray()); var moveOutputLines = new List <string>(); var moves = new List <GameMove>(); var availableMoves = gameState.CalculateAvailableMoves().Where(m => m.StartGamePiece.PieceColour == PieceColour.White).ToList(); if (availableMoves.Count == 0) { System.Console.WriteLine("No available moves. You have lost."); System.Console.ReadKey(); return; } foreach (var move in availableMoves) { int index = moves.Count; var s = move.StartGamePiece; var e = move.EndGamePiece; string moveOutputLine = $"{index} - Start: '{s.Xcoord}, {s.Ycoord}' End: '{e.Xcoord}, {e.Ycoord}'"; moveOutputLines.Add(moveOutputLine); moves.Add(move); } string moveOutput = string.Join(Environment.NewLine, moveOutputLines.ToArray()); System.Console.Write(output); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine("Moves:"); System.Console.Write(moveOutput); System.Console.WriteLine(); System.Console.WriteLine("Choose a move:"); int selection; while (true) { var readLine = System.Console.ReadLine(); if (!int.TryParse(readLine, out selection)) { System.Console.WriteLine("Type a number."); } else if (selection > moves.Count || selection < 0) { System.Console.WriteLine("Number not in range."); } else { break; } } var selectedMove = moves[selection]; gameState = selectedMove.PerformMove(); // perform ai move var aiResult = ai.MakeMove(PieceColour.Black, gameState); if (aiResult.MoveStatus == MoveStatus.NoLegalMoves) { System.Console.WriteLine("Opponent cannot move. You have won."); System.Console.ReadKey(); return; } else if (aiResult.MoveStatus == MoveStatus.SuccessfulMove) { gameState = aiResult.GameState; } else { throw new GameException("Unknown AI result state."); } System.Console.Clear(); } }