public void TestP1IsWinAndNextStateSeqWithFullStateTokenConstructor() { var g = new TicTacToeGame(16352); Assert.IsTrue(g.IsWin); var t = new List<int>() { 1635, 16, 0 }; CollectionAssert.AreEqual(t, g.GetLastPlayersStateSequence().Select(p => p.StateToken).ToList()); t = new List<int>() { 16352, 163, 1 }; CollectionAssert.AreEqual(t, g.GetLastPlayersStateSequence().Select(p => p.NextStateToken).ToList()); }
public void TestIsSecondBlockOtherPlayer() { var g = new TicTacToeGame(1532, 8); Assert.IsTrue(g.IsBlock); }
public void TestIsBlock() { var g = new TicTacToeGame(163, 2); Assert.IsTrue(g.IsBlock); }
public void TestNextPlayerAs2() { var g = new TicTacToeGame(123, 5); Assert.AreEqual(g.NextPlayer, 2); }
public void TestP2WinnerNextStatesSeq() { var g = new TicTacToeGame(1632957, 8); Assert.IsTrue(g.IsWin); var pastStates = new List<int>() { {16329578}, {163295}, {1632}, {16} }; CollectionAssert.AreEqual(pastStates, g.GetLastPlayersStateSequence().Select(p => p.NextStateToken).ToList()); }
private async Task TicTacToeTurnAsync(int x = -1, int y = -1) { await Context.Message.DeleteAsync(); if (!GameHandler.DoesGameExist(Context.Guild.Id, GameType.TICTACTOE)) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "TicTacToe Error", "No game could be found here..."); return; } TicTacToeGame game = (TicTacToeGame)GameHandler.GetGame(Context.Guild.Id, GameType.TICTACTOE); if (game == null) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "TicTacToe Error", "No game could be found here..."); return; } if (game.Players[0] != Context.User.Id && game.Players[1] != Context.User.Id) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "TicTacToe Error", "You are not part of this game..."); return; } if (game.Players[game.Turn] != Context.User.Id) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "TicTacToe Error", "It is not your turn..."); return; } if (x <= 0 || y <= 0 || x > 3 || y > 3) { await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "TicTacToe Error", "You need to choose and x and y between of 1, 2 or 3..."); return; } GameHandler.TakeTurn(Context.Guild.Id, GameType.TICTACTOE, Context.User.Id, x, y); ulong winner = GameHandler.CheckForWinner(Context.Guild.Id, GameType.TICTACTOE); if (winner == 0L) { var oldMsg = await Context.Channel.GetMessageAsync(game.RenderId); await oldMsg.DeleteAsync(); var nextUp = await Context.Guild.GetUserAsync(game.Players[game.Turn]); string render = game.RenderGame(); var msg = await Context.Channel.SendFileAsync(render, $"**TicTacToe**\n" + $"Next Up: {nextUp.Mention}\n" + $"`{CommandHandler.GetPrefix(Context.Guild.Id)}t <x> <y>` to take your turn\n`{CommandHandler.GetPrefix(Context.Guild.Id)}t end` to end the game"); game.RenderId = msg.Id; } else { IMessage msg = await Context.Channel.GetMessageAsync(game.RenderId); await msg.DeleteAsync(); string render = game.RenderGame(); await Context.Channel.SendFileAsync(render, $"**TicTacToe**\n" + $"Game Won by " + (await Context.Guild.GetUserAsync(winner)).Mention); var winwin = ProfileDatabase.GetUser(winner); if (winwin != null) { ProfileDatabase.AddCurrency(winner, 100); await LeaderboardDatabase.CheckAsync(winner, winwin.Name, "Tic Tac Toe"); await LeaderboardDatabase.AddScoreAsync(winner, "Tic Tac Toe"); } GameHandler.EndGame(game); } }
/// <summary> /// Evaluate the provided IBlackBox against the random tic-tac-toe player and return its fitness score. /// Each network plays 10 games against the random player and two games against the expert player. /// Half of the games are played as circle and half are played as x. /// /// A win is worth 10 points, a draw is worth 1 point, and a loss is worth 0 points. /// </summary> public FitnessInfo Evaluate(IBlackBox box) { double fitness = 0; SquareTypes winner; OptimalPlayer optimalPlayer = new OptimalPlayer(SquareTypes.O); RandomPlayer randomPlayer = new RandomPlayer(); NeatPlayer neatPlayer = new NeatPlayer(box, SquareTypes.X); // Play 50 games as X against a random player for (int i = 0; i < 50; i++) { // Compete the two players against each other. winner = TicTacToeGame.PlayGameToEnd(neatPlayer, randomPlayer); // Update the fitness score of the network fitness += getScore(winner, neatPlayer.SquareType); } // Play 50 games as O against a random player neatPlayer.SquareType = SquareTypes.O; for (int i = 0; i < 50; i++) { // Compete the two players against each other. winner = TicTacToeGame.PlayGameToEnd(randomPlayer, neatPlayer); // Update the fitness score of the network fitness += getScore(winner, neatPlayer.SquareType); } // Play 1 game as X against an optimal player neatPlayer.SquareType = SquareTypes.X; optimalPlayer.SquareType = SquareTypes.O; // Compete the two players against each other. winner = TicTacToeGame.PlayGameToEnd(neatPlayer, optimalPlayer); // Update the fitness score of the network fitness += getScore(winner, neatPlayer.SquareType); // Play 1 game as O against an optimal player neatPlayer.SquareType = SquareTypes.O; optimalPlayer.SquareType = SquareTypes.X; // Compete the two players against each other. winner = TicTacToeGame.PlayGameToEnd(optimalPlayer, neatPlayer); // Update the fitness score of the network fitness += getScore(winner, neatPlayer.SquareType); // Update the evaluation counter. _evalCount++; // If the network plays perfectly, it will beat the random player // and draw the optimal player. if (fitness >= 1002) { _stopConditionSatisfied = true; } // Return the fitness score return(new FitnessInfo(fitness, fitness)); }
public void TestInit() { game = new TicTacToeGame(); }
public abstract void MakeMove(TicTacToeGame currentGame);
private async void btnGenerateGames_Click(object sender, RoutedEventArgs e) { try { DisableControls(_buttons); ShowLoader(gridDataContainer); var loadingMode = (LoadingMode)ddlLoadingMode.SelectedDdlItem().Index; var merge = loadingMode == LoadingMode.Merge; var games = merge ? gvData.Items.Cast <TicTacToeGame>().ToList() : new List <TicTacToeGame>(); var maxID = games.Any() ? games.Select(g => g.Id).Max() : 0; var nGames = numGames.Value ?? 0; var gamesType = (GeneratedGamesType)ddlGeneratedGamesType.SelectedDdlItem().Index; var tempGames = new List <TicTacToeGame>(); await Task.Run(() => { if (gamesType == GeneratedGamesType.VsRandomAi) { for (var i = 0; i < nGames; i++) { Dispatcher.Invoke(() => { _gameBoard.GameState = TicTacToeGame.Empty(); while (_gameBoard.GameState.Result() == GameResult.Unfinished) { _gameBoard.MakeRandomMove(); } tempGames.Add(new TicTacToeGame(++maxID, _gameBoard.GameState.State)); }); } games.AddRange(tempGames); } else if (gamesType == GeneratedGamesType.VsRandomAiWithUnfinished) { for (var i = 0; i < nGames; i++) { Dispatcher.Invoke(() => { var length = _gameBoard.GameState.State.Length *_gameBoard.GameState.State[0].Length; var maxMoves = _rng.Next(1, length); var moveNum = 0; _gameBoard.GameState = TicTacToeGame.Empty(); while (_gameBoard.GameState.Result() == GameResult.Unfinished && ++moveNum <= maxMoves) { _gameBoard.MakeRandomMove(); } tempGames.Add(new TicTacToeGame(++maxID, _gameBoard.GameState.State)); }); } games.AddRange(tempGames); } else if (gamesType == GeneratedGamesType.VsNNAi) { if (_nnHowToPlayX == null) { Dispatcher.Invoke(() => this.ShowMessageAsync("Error occured", "You need to train Neural Network how to play first")); return; } var prevStartGameSym = _rng.Next(0, 2).ToEnum <S>(); for (var i = 0; i < nGames; i++) { Dispatcher.Invoke(() => { _gameBoard.GameState = TicTacToeGame.Empty(); var prevSymbol = _gameBoard.MakeRandomMove(prevStartGameSym); while (_gameBoard.GameState.Result() == GameResult.Unfinished) { var currSym = TicTacToeGame.Opposite(prevSymbol); if (currSym == S.O) { _gameBoard.MakeRandomMove(S.O); } else if (currSym == S.X) { _gameBoard.MakeNNMove(S.X, _nnHowToPlayX); } else { throw new Exception("Valid move is either 'O' or 'X'"); } prevSymbol = currSym; } tempGames.Add(new TicTacToeGame(++maxID, _gameBoard.GameState.State)); prevStartGameSym = TicTacToeGame.Opposite(prevStartGameSym); }); } games.AddRange(tempGames); } var ntmp = tempGames.Count; var ntmpOWon = tempGames.Count(g => g.Result() == GameResult.OWon); var ntmpXWon = tempGames.Count(g => g.Result() == GameResult.XWon); var ntmpDraws = tempGames.Count(g => g.Result() == GameResult.Draw); var ptmpOWon = Math.Round(ntmpOWon.ToDouble() / ntmp * 100); var ptmpXWon = Math.Round(ntmpXWon.ToDouble() / ntmp * 100); var ptmpDraws = Math.Round(ntmpDraws.ToDouble() / ntmp * 100); var nall = games.Count; var nallOWon = games.Count(g => g.Result() == GameResult.OWon); var nallXWon = games.Count(g => g.Result() == GameResult.XWon); var nallDraws = games.Count(g => g.Result() == GameResult.Draw); var pallOWOn = Math.Round(nallOWon.ToDouble() / nall * 100); var pallXWOn = Math.Round(nallXWon.ToDouble() / nall * 100); var pallDraws = Math.Round(nallDraws.ToDouble() / nall * 100); Dispatcher.Invoke(() => { lblInfo.SetWrappedText( $"Generated {nGames} (Total: {games.Count})\n" + $"New - O: {ptmpOWon}%, X: {ptmpXWon}%, Draws: {ptmpDraws}%\n" + $"Total - O: {pallOWOn}%, X: {pallXWOn}%, Draws: {pallDraws}%\n" + "(Neural Network Plays with 'X')"); gvData.RefreshWith(games); gvData.SelectedItems.Clear(); gvData.Focus(); _gameBoard.GameState = TicTacToeGame.Empty(); }); }); } catch (Exception ex) { File.WriteAllText(ErrorLogPath, ex.StackTrace); await this.ShowMessageAsync("Error occured", ex.Message); } finally { HideLoader(gridDataContainer); EnableControls(_buttons.Except(new[] { btnSave, btnDelete })); } }
private static void Main() { var game = new TicTacToeGame(); game.Start(); }
private static void CloseGame(TicTacToeGame game) { Storage.Delete(Storage.CurrentContext, "o-" + game.Id); SaveGame(game); }
private static void SaveGame(TicTacToeGame game) { Storage.Put(Storage.CurrentContext, game.Id, game.Serialize()); }
public void SetUp() { _ticTacToe = null; _ticTacToe = new TicTacToeGame(); }
public void TestIsBlockAndSequenceForReward() { var g = new TicTacToeGame(124, 7); Assert.IsTrue(g.IsBlock); var pastStates = new List<int>() { {124}, {1} }; CollectionAssert.AreEqual(pastStates, g.GetLastPlayersStateSequence().Select(p => p.StateToken).ToList()); pastStates = new List<int>() { {1247}, {12} }; CollectionAssert.AreEqual(pastStates, g.GetLastPlayersStateSequence().Select(p => p.NextStateToken).ToList()); }
public void GetWinnerTest() { SquareTypes[,] board = TicTacToeGame.GetBoardFromString(@" | | | | | | "); SquareTypes expected = SquareTypes.N; SquareTypes actual; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Diagnol left to right board = TicTacToeGame.GetBoardFromString(@"X|O|O O|X|O O|O|X"); expected = SquareTypes.X; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Diagnol right to left board = TicTacToeGame.GetBoardFromString(@"O|O|X O|X| X|O|X"); expected = SquareTypes.X; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Horizontal bottom board = TicTacToeGame.GetBoardFromString(@"X| |X X|X|O O|O|O"); expected = SquareTypes.O; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Horizontal middle board = TicTacToeGame.GetBoardFromString(@"X|O|O X|X|X O|O| "); expected = SquareTypes.X; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Horizontal top board = TicTacToeGame.GetBoardFromString(@"O|O|O X|X|O |O|X"); expected = SquareTypes.O; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Vertical right board = TicTacToeGame.GetBoardFromString(@"X|O|O O|X|O X|O|O"); expected = SquareTypes.O; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Vertical middle board = TicTacToeGame.GetBoardFromString(@"X|O| |O|X X|O| "); expected = SquareTypes.O; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //Vertical left board = TicTacToeGame.GetBoardFromString(@"X|O| X| |O X|O| "); expected = SquareTypes.X; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //No winner board = TicTacToeGame.GetBoardFromString(@"X|O| | |O X|O| "); expected = SquareTypes.N; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //No winner board = TicTacToeGame.GetBoardFromString(@"X|O|O |X|O X|O| "); expected = SquareTypes.N; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //No winner board = TicTacToeGame.GetBoardFromString(@"X|X| O|X|O X|O|O "); expected = SquareTypes.N; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); //No winner board = TicTacToeGame.GetBoardFromString(@"X|X|O O|X|X X|O|O"); expected = SquareTypes.N; actual = TicTacToeGame.GetWinner(board); Assert.AreEqual(expected, actual); }
public static void APlayerWinsByTakingDiagonalFieldsStartingFromThirdToSixthPosition(this TicTacToeGame game) { game.Play("X", 3); game.Play("O", 2); game.Play("X", 5); game.Play("O", 4); game.Play("X", 6); }
public void testCurrentPlayerX() { TicTacToeGame game = new TicTacToeGame(); Assert.AreEqual("x", game.currentPlayer()); }
public TicTacToeGameSession(string ID) { game = new TicTacToeGame(); this.ID = ID; GameState = GameState.InProgress; }
public void setUp() { game = new TicTacToeGame(); state = game.GetInitialState(); }
public void TestIsBlockTest() { var g = new TicTacToeGame(124, 7); Assert.IsTrue(g.IsBlock); }
static void Main(string[] args) { bool exit = false; var tccSearchDepth = 9; var taSearchDepth = 15; var c4SearchDepth = 6; var taCards = 15; var taRemoveAmount = 3; var c4minimax = false; var tccStarter = true; var taStarter = true; var c4Starter = true; while (!exit) { Console.Clear(); Console.WriteLine("" + " _____ ____ __ __ _____ ______ __ ______ \n" + " / ____/ __ \\| \\/ | __ \\____ /_ |____ |\n" + " | | | | | | \\ / | |__) | / / | | / / \n" + " | | | | | | |\\/| | ___/ / / | | / / \n" + " | |___| |__| | | | | | / / | | / / \n" + " \\_____\\____/|_| |_|_| /_/ |_|/_/ v1.1"); Console.WriteLine("\n -- Assignment One - Game Playing Agents --"); Console.WriteLine(" -- Matt Tribble - 19076935 --\n"); Console.WriteLine("1) Tic Tac Toe"); Console.WriteLine("2) Take Away"); Console.WriteLine("3) Connect 4"); Console.WriteLine("4) Settings"); Console.WriteLine("x) Exit\n"); Console.Write("> "); string input = Console.ReadLine(); switch (input.ToLower()) { case "1": Console.Clear(); TicTacToeGame tcc = new TicTacToeGame(tccStarter, tccSearchDepth); break; case "2": Console.Clear(); TakeAwayGame ta = new TakeAwayGame(taCards, taRemoveAmount, taSearchDepth, true, taStarter); break; case "3": Console.Clear(); ConnectFourGame c4 = new ConnectFourGame(c4SearchDepth, c4Starter, c4minimax); break; case "4": input = ""; while (input != "x") { Console.Clear(); Console.WriteLine("" + " ___ ___ _____ _____ ___ _ _ ___ ___ \n" + " / __| __|_ _|_ _|_ _| \\| |/ __/ __| \n" + " \\__ \\ _| | | | | | || .` | (_ \\__ \\ \n" + " |___/___| |_| |_| |___|_|\\_|\\___|___/\n\n"); Console.WriteLine("1) Tic Tac Toe"); Console.WriteLine("2) Take Away"); Console.WriteLine("3) Connect 4"); Console.WriteLine("x) Exit\n"); Console.Write("> "); input = Console.ReadLine(); if (input == "x") { break; } switch (input.ToLower()) { /* Settings for Tic Tac Toe */ case "1": input = ""; while (input != "x") { Console.Clear(); Console.WriteLine("Tic Tac Toe Settings\n"); Console.WriteLine("1) Search Depth (" + tccSearchDepth + ")"); Console.WriteLine("2) Starting Player (" + (tccStarter ? "Player" : "Agent") + ")"); Console.WriteLine("x) Exit\n"); Console.Write("> "); input = Console.ReadLine(); if (input == "x") { break; } if (input == "1") { var set = false; while (!set) { try { Console.Write("\nSearch Depth> "); input = Console.ReadLine(); if (int.Parse(input) <= 0 || int.Parse(input) > 9) { Console.WriteLine("\nSearch depth must be between 1 and 9\n"); continue; } tccSearchDepth = int.Parse(input); input = ""; set = true; } catch { Console.WriteLine("\nNot a number!\n"); } } } if (input == "2") { var set = false; while (!set) { Console.Write("\nStarting Player (1: Player, 2: Agent)> "); input = Console.ReadLine(); if (input != "1" && input != "2") { Console.WriteLine("\nMust be 1 or 2!\n"); continue; } tccStarter = input == "1" ? true : false; input = ""; set = true; } } } break; /* Settings for Take Away */ case "2": input = ""; while (input != "x") { Console.Clear(); Console.WriteLine("Take Away Settings\n"); Console.WriteLine("1) Search Depth (" + taSearchDepth + ")"); Console.WriteLine("2) Number of Chips (" + taCards + ")"); Console.WriteLine("3) Chip Removal Limit (" + taRemoveAmount + ")"); Console.WriteLine("4) Starting Player (" + (taStarter ? "Player" : "Agent") + ")"); Console.WriteLine("x) Exit\n"); Console.Write("> "); input = Console.ReadLine(); if (input == "x") { break; } if (input == "1") { var set = false; while (!set) { try { Console.Write("\nSearch Depth> "); input = Console.ReadLine(); if (int.Parse(input) <= 0 || int.Parse(input) > taCards) { Console.WriteLine("\nSearch depth must be between 1 and " + taCards + "\n"); continue; } taSearchDepth = int.Parse(input); input = ""; set = true; } catch { Console.WriteLine("\nNot a number!\n"); } } } if (input == "2") { var set = false; while (!set) { try { Console.Write("\nChips> "); input = Console.ReadLine(); if (int.Parse(input) <= 0) { Console.WriteLine("\nNumber must be greater than 0\n"); continue; } taCards = int.Parse(input); input = ""; set = true; } catch { Console.WriteLine("\nNot a number!\n"); } } } if (input == "3") { var set = false; while (!set) { try { Console.Write("\nRemoval Limit> "); input = Console.ReadLine(); if (int.Parse(input) <= 0) { Console.WriteLine("\nNumber must be greater than 0\n"); continue; } taRemoveAmount = int.Parse(input); input = ""; set = true; } catch { Console.WriteLine("\nNot a number!\n"); } } } if (input == "4") { var set = false; while (!set) { Console.Write("\nStarting Player (1: Player, 2: Agent)> "); input = Console.ReadLine(); if (input != "1" && input != "2") { Console.WriteLine("\nMust be 1 or 2!\n"); continue; } taStarter = input == "1" ? true : false; input = ""; set = true; } } } break; /* Settings for Connect 4 */ case "3": input = ""; while (input != "x") { Console.Clear(); Console.WriteLine("Connect 4 Settings\n"); Console.WriteLine("1) Search Depth (" + c4SearchDepth + ")"); Console.WriteLine("2) Search Algoritm (" + (c4minimax ? "Minimax" : "Alpha Beta Pruning") + ")"); Console.WriteLine("3) Starting Player (" + (c4Starter ? "Player" : "Agent") + ")"); Console.WriteLine("x) Exit\n"); Console.Write("> "); input = Console.ReadLine(); if (input == "x") { break; } if (input == "1") { var set = false; while (!set) { try { Console.Write("\nSearch Depth> "); input = Console.ReadLine(); if (int.Parse(input) <= 0 || int.Parse(input) > 42) { Console.WriteLine("\nSearch depth must be between 1 and 42\n"); continue; } c4SearchDepth = int.Parse(input); input = ""; set = true; } catch { Console.WriteLine("\nNot a number!\n"); } } } if (input == "2") { var set = false; while (!set) { Console.Write("\nSearch Algorithm (1: Minimax, 2: Alpha Beta Pruning)> "); input = Console.ReadLine(); if (input != "1" && input != "2") { Console.WriteLine("\nMust be 1 or 2!\n"); continue; } c4minimax = input == "1" ? true : false; input = ""; set = true; } } if (input == "3") { var set = false; while (!set) { Console.Write("\nStarting Player (1: Player, 2: Agent)> "); input = Console.ReadLine(); if (input != "1" && input != "2") { Console.WriteLine("\nMust be 1 or 2!\n"); continue; } c4Starter = input == "1" ? true : false; set = true; } } } break; } } break; case "x": exit = true; break; } } }
public void SetUp() { _gameBoardMock = new Mock<IGameBoard>(); _displayHelperMock = new Mock<IDisplayHelper>(); _randomNumberGenerator = new Mock<IRandomNumberGenerator>(); _game = new TicTacToeGame(_gameBoardMock.Object, _displayHelperMock.Object, _randomNumberGenerator.Object); }
public void Init() { ticTacToe = new TicTacToeGame(gameDimensions); }
public void TestTieSequence() { var g = new TicTacToeGame(17325986, 4); Assert.IsTrue(g.IsTie); var pastStates = new List<int>() { {173259864}, {17325986}, {1732598}, {173259}, {17325}, {1732}, {173}, {17}, {1}, }; CollectionAssert.AreEqual(pastStates, g.GetAllStateSequence().Select(p => p.NextStateToken).ToList()); g = new TicTacToeGame(17325984, 6); Assert.IsTrue(g.IsTie); pastStates = new List<int>() { {173259846}, {17325984}, {1732598}, {173259}, {17325}, {1732}, {173}, {17}, {1}, }; CollectionAssert.AreEqual(pastStates, g.GetAllStateSequence().Select(p => p.NextStateToken).ToList()); }
private void Move_GameFinished_NoMovePossible() { // arrange #region Moves of a finished Game var movesForFinishedGame = new List <Move> { new Move { Player = Player.Cross, BoardPosition = new Position(0, 0), TilePosition = new Position(0, 0), MoveNumber = 1 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 0), TilePosition = new Position(2, 0), MoveNumber = 2 }, new Move { Player = Player.Cross, BoardPosition = new Position(2, 0), TilePosition = new Position(0, 2), MoveNumber = 3 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 2), TilePosition = new Position(2, 0), MoveNumber = 4 }, new Move { Player = Player.Cross, BoardPosition = new Position(2, 0), TilePosition = new Position(0, 1), MoveNumber = 5 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 1), TilePosition = new Position(2, 0), MoveNumber = 6 }, new Move { Player = Player.Cross, BoardPosition = new Position(2, 0), TilePosition = new Position(0, 0), MoveNumber = 7 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 0), TilePosition = new Position(1, 0), MoveNumber = 8 }, new Move { Player = Player.Cross, BoardPosition = new Position(1, 0), TilePosition = new Position(2, 2), MoveNumber = 9 }, new Move { Player = Player.Circle, BoardPosition = new Position(2, 2), TilePosition = new Position(0, 0), MoveNumber = 10 }, new Move { Player = Player.Cross, BoardPosition = new Position(0, 0), TilePosition = new Position(0, 1), MoveNumber = 11 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 1), TilePosition = new Position(0, 0), MoveNumber = 12 }, new Move { Player = Player.Cross, BoardPosition = new Position(0, 0), TilePosition = new Position(0, 2), MoveNumber = 13 }, new Move { Player = Player.Circle, BoardPosition = new Position(0, 2), TilePosition = new Position(1, 0), MoveNumber = 14 }, new Move { Player = Player.Cross, BoardPosition = new Position(1, 0), TilePosition = new Position(2, 0), MoveNumber = 15 }, new Move { Player = Player.Circle, BoardPosition = new Position(1, 1), TilePosition = new Position(1, 0), MoveNumber = 16 }, new Move { Player = Player.Cross, BoardPosition = new Position(1, 0), TilePosition = new Position(2, 1), MoveNumber = 17 } }; #endregion var game = new TicTacToeGame(movesForFinishedGame); // act MoveResult result = game.Move(new Move { Player = Player.Circle, BoardPosition = new Position(2, 1), TilePosition = new Position(2, 1), MoveNumber = 18 }); // assert result.IsValid.Should().BeFalse(); result.InvalidReason.Should().Be(ExceptionMessages.GameFinished); }
public void TestP2IsWin() { var g = new TicTacToeGame(16359, 4); Assert.IsTrue(g.IsWin); }
public TicTacToeTest() { t = new TicTacToeGame(); }
public void TestIsSecondBlock() { var g = new TicTacToeGame(16324, 7); Assert.IsTrue(g.IsBlock); }
private static void RunExperiment(StreamWriter saveFileWriter) { int inputCount = 9; int outputCount = 9; int depth = 3; int hiddenNeuronsPerLayer = 5; PopulationManager pop = GeneratePopulation(inputCount, outputCount, depth, hiddenNeuronsPerLayer); Dictionary <int, List <double> > fitnesses = new Dictionary <int, List <double> >(); DNA bestIndividual = new DNA(bestDNA); const int genCount = 2000; using (ProgressBar progress = new ProgressBar()) { Console.WriteLine("Running genetics..."); for (int gen = 0; gen < genCount; gen++) { progress.SetLabel($"gen {gen+GenToLoad}/{genCount+GenToLoad}"); progress.Report((double)gen / genCount); foreach (DNA individual in pop.Population) { NeuralPlayer p1 = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, individual); NeuralPlayer p2 = MakeNeuralPlayer(inputCount, outputCount, depth, hiddenNeuronsPerLayer, bestIndividual); p1.SquareType = SquareTypes.X; p2.SquareType = SquareTypes.O; for (int g = 0; g < 50; g++) { (SquareTypes winner, int moveNum) = TicTacToeGame.PlayGameToEnd(p1, p2); if (winner == SquareTypes.X) { individual.Fitness += 50f / moveNum; } else if (winner == SquareTypes.N) { individual.Fitness += 10f / moveNum; } } p2.SquareType = SquareTypes.X; p1.SquareType = SquareTypes.O; for (int g = 0; g < 50; g++) { (SquareTypes winner, int moveNum) = TicTacToeGame.PlayGameToEnd(p2, p1); if (winner == SquareTypes.O) { individual.Fitness += 50f / moveNum; } else if (winner == SquareTypes.N) { individual.Fitness += 10f / moveNum; } } } if (gen % 50 == 0) { SaveGeneration(gen + 1 + GenToLoad, pop); } int dnaCounter = 0; pop.Population.OrderByDescending(i => i.Fitness).ToList().ForEach(dna => { if (!fitnesses.ContainsKey(dnaCounter)) { fitnesses[dnaCounter] = new List <double>(); } fitnesses[dnaCounter].Add(dna.Fitness); dnaCounter++; }); var thisBest = pop.Population.OrderByDescending(i => i.Fitness).First(); if (thisBest.Fitness > bestIndividual.Fitness) { bestIndividual = thisBest; } pop.NextGeneration(); } } Console.WriteLine(" Done."); SaveGeneration(genCount + GenToLoad, pop); foreach (KeyValuePair <int, List <double> > fitnessList in fitnesses) { saveFileWriter.WriteLine($"{fitnessList.Key},{String.Join(',', fitnessList.Value)}"); } saveFileWriter.WriteLine(""); saveFileWriter.WriteLine("Best final dna:"); saveFileWriter.WriteLine(String.Join("||", pop.Population.OrderByDescending(i => i.Fitness).First().Genes)); }
public void TestIsBlockTest() { var g = new TicTacToeGame(124, 7); Assert.IsTrue(g.IsBlock); }
internal override IEnumerable <int> GetTransitions(int stateToken) { var game = new TicTacToeGame(stateToken); return(game.GetPossiblePlays()); }
public static void APlayerWinsByTakingDiagonalFieldsStartingFromFirstToNinthPosition(this TicTacToeGame game) { game.Play("X", 1); game.Play("O", 2); game.Play("X", 5); game.Play("O", 4); game.Play("X", 9); }
public void Setup() { _console = Substitute.For <IConsole>(); _board = Substitute.For <IBoard>(); _game = new TicTacToeGame(_console, _board); }
public ServiceDependents(JsonConverter converter, TicTacToeGame game) { Game = game; Converter = converter; }
public static ContractResult NewContractResult(bool operationResult, string error, TicTacToeGame game) { return(new ContractResult { OperationResult = operationResult, Error = error, Game = game }); }
public static void Main(string[] args) { var game = new TicTacToeGame(); game.Play(); }
public void TestNextPossiblePlays() { var g = new TicTacToeGame(null, 1); var p = new List<int>() { 2, 3, 4, 5, 6, 7, 8, 9 }; CollectionAssert.AreEqual(g.GetPossiblePlays(), p); g = new TicTacToeGame(1, 2); p = new List<int>() { 3, 4, 5, 6, 7, 8, 9 }; CollectionAssert.AreEqual(g.GetPossiblePlays(), p); g = new TicTacToeGame(12, 3); p = new List<int>() { 4, 5, 6, 7, 8, 9 }; CollectionAssert.AreEqual(g.GetPossiblePlays(), p); }
public void NewGame() { TheGame = new TicTacToeGame(); }
public void TestIsTie() { var g = new TicTacToeGame(16324795, 8); Assert.IsTrue(g.IsTie); }
public void TestIsSecondBlockOtherPlayer() { var g = new TicTacToeGame(1532, 8); Assert.IsTrue(g.IsBlock); }
public void TestNextPlayerAs2() { var g = new TicTacToeGame(123, 5); Assert.AreEqual(g.NextPlayer, 2); }
public void TestIsTie() { var g = new TicTacToeGame(16324795, 8); Assert.IsTrue(g.IsTie); }
public void TestP2WinnerSequences3WithFullStateTokenConstructor() { var g = new TicTacToeGame(16329578); Assert.IsTrue(g.IsWin); var pastStates = new List<int>() { {1632957}, {16329}, {163}, {1} }; CollectionAssert.AreEqual(pastStates, g.GetLastPlayersStateSequence().Select(p => p.StateToken).ToList()); }
public void TestP2IsWin() { var g = new TicTacToeGame(16359, 4); Assert.IsTrue(g.IsWin); }
public VNetworkBasedMCTreeSearchExpander(TicTacToeGame game) : base(game) { }