public TicTacToeGame(TicTacToeObjects ticTacToeObjects)
 {
     _inputReader = ticTacToeObjects.InputReader;
     _boardState = ticTacToeObjects.BoardState;
     _humanPlayer = ticTacToeObjects.HumanPlayer;
     _computerPlayer = ticTacToeObjects.ComputerPlayer;
     _pieceManager = ticTacToeObjects.PieceManager;
 }
 public void GameClassDisplaysBoardAtStartOfGame()
 {
     Board board = new Board(3);
     IPlayer player1 = new ComputerPlayer(BoardCharacter.X);
     IPlayer player2 = new ComputerPlayer(BoardCharacter.Zero);
     Game game = new Game(board, player1, player2, gameSubscriber.Object, 0);
     game.Run();
     gameSubscriber.Verify(g => g.GameBegin(It.Is<string>(s => IsEmptyBoard(s))), Times.Once());
 }
Exemple #3
0
        public void PlayerDoesNotMoveToOccupiedStopOnBoard()
        {
            ComputerPlayer computerPlayer = new ComputerPlayer(BoardCharacter.Zero);

            Board board = BoardTestHelper.Get3DimensionBoardWithFourOccupiedPositions();
            for (int i = 0; i < 100; i++)
            {
                var move = computerPlayer.MakeMove(board);
                Assert.IsTrue(board.GetPosition(move.Item1, move.Item2) == BoardCharacter.Empty);
            }
        }
 public void WhenComputerPlayerHasPossibilityToWinInNextMove_ThenDoMoveMustMustSelectWinningMove()
 {
     ComputerPlayer target = new ComputerPlayer();
     var board = new Piece[3, 3];
     board[0, 0] = Piece.X; board[1, 0] = Piece.O; board[2, 0] = Piece.X;
     board[0, 1] = Piece.X; board[1, 1] = Piece.O; board[2, 1] = Piece.Empty;
     board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty;
     var boardState = new BoardState(null);
     boardState.Board = board;
     target.DoMove(Piece.O, boardState);
     Assert.AreEqual(Piece.O, boardState.Board[1, 2]);
 }
 public void WhenAllButOnePositionsAreFull_ThenDoMoveUsesLastPosition()
 {
     ComputerPlayer target = new ComputerPlayer();
     var board = new Piece[3, 3];
     board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O;
     board[0, 1] = Piece.O; board[1, 1] = Piece.O; board[2, 1] = Piece.X;
     board[0, 2] = Piece.X; board[1, 2] = Piece.X; board[2, 2] = Piece.Empty;
     var boardState = new BoardState(null);
     boardState.Board = board;
     target.DoMove(Piece.O, boardState);
     Assert.AreEqual(Piece.O, boardState.Board[2, 2]);
 }
 public void WhenFirstPlayerStartsInCorner_ThenDoMoveMustPutPieceInCenter()
 {
     ComputerPlayer target = new ComputerPlayer();
     var board = new Piece[3, 3];
     board[0, 0] = Piece.X; board[1, 0] = Piece.Empty; board[2, 0] = Piece.Empty;
     board[0, 1] = Piece.Empty; board[1, 1] = Piece.Empty; board[2, 1] = Piece.Empty;
     board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty;
     var boardState = new BoardState(null);
     boardState.Board = board;
     target.DoMove(Piece.O, boardState);
     Assert.AreEqual(Piece.O, boardState.Board[1, 1]);
 }
Exemple #7
0
        public MainForm()
        {
            InitializeComponent();

            _chooseBestMove = invencibleDifficultyRadioButton.Checked;

            // i save in an array the references to the 9 panels
            foreach (Panel panel in backgroundPanel.Controls)
            {
                int panelNumber = int.Parse(panel.Name[panel.Name.Length - 1].ToString());
                _panels[panelNumber] = panel;
            }

            _computerPlayer = new ComputerPlayer(_board);
        }
Exemple #8
0
        /// <summary>
        /// Starts game
        /// </summary>
        private void StartGame()
        {
            _ticTacToe?.Reset();
            _view.BtnRefresh.Enabled = false;
            _board.Clean();
            RefreshButtons();

            var opponentComboBox = _view.ComboBoxOpponent;

            Enum.TryParse(opponentComboBox.SelectedItem.ToString(), out PlayerType opponentType);

            var difficultyComboBox = _view.ComboBoxDifficulty;

            Enum.TryParse(difficultyComboBox.SelectedItem.ToString(), out DifficultyType difficultyType);

            Player player1 = new HumanPlayer();
            Player player2 = null;

            if (opponentType == PlayerType.Human)
            {
                player2 = new HumanPlayer();
            }
            else
            {
                player2 = new ComputerPlayer(difficultyType);
            }

            player1.PlayersTurn = TicTacToe.Turn.PlayerX;
            player2.PlayersTurn = TicTacToe.Turn.PlayerO;

            _ticTacToe = new TicTacToe(_board, player1, player2, difficultyType);
            _ticTacToe.GameFinishedWin += _ticTacToe_GameFinishedWin;
            _ticTacToe.GameFinishedTie += _ticTacToe_GameFinishedTie;
            _ticTacToe.UIBoardChanged  += _ticTacToe_UIBoardChanged;
            _ticTacToe.TurnChanged     += _ticTacToe_TurnChanged;

            if (!_playerUseX)
            {
                player2.PlayersTurn = TicTacToe.Turn.PlayerX;
                player1.PlayersTurn = TicTacToe.Turn.PlayerO;

                _ticTacToe.PlayerX = player2;
                _ticTacToe.PlayerO = player1;
            }

            _ticTacToe.Start();
        }
Exemple #9
0
        static void Main(string[] args)
        {
            var board   = new Board();
            var playe1  = new HumanPlayer(board, "George", GameLetter.X);
            var player2 = new ComputerPlayer(board, "Computer", GameLetter.O);
            var game    = new Game(board, playe1, player2);

            while (!game.GameIsOver)
            {
                game.RenderGame();
                game.NextPlayer();
                game.Play();
            }

            game.RenderGame();

            Console.ReadKey();
        }
        private void gameSPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSingleplayer frmSP = new frmSingleplayer();

            if (frmSP.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                diffToolStripMenuItem.Enabled = true;
                Player human    = new HumanPlayer(frmSP.getPlayerName(), frmSP.getPlayerTile());
                Player computer = new ComputerPlayer(-frmSP.getPlayerTile(), frmSP.getDiffLevel());
                controller.changePlayers(human, computer);
                updateP1Label(human);
                updateP2Label(computer);
                controller.resetGame();
                enableButtons();
                restartToolStripMenuItem.Enabled = true;
                diffToolStripMenuItem.Enabled    = true;
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Super-Awesome TicTacToe!!");
            Console.WriteLine();
            Console.Write("Any key to start");
            Console.ReadKey();

            // Create these objects and inject them into the game object, such
            // that the game has no idea about where it's drawing to, or what kind of players
            // are playing (Human / Computer etc)
            GenericGameState  gameState = new GenericGameState();
            ConsoleGameDrawer drawer    = new ConsoleGameDrawer();
            ComputerPlayer    player1   = new ComputerPlayer("Jim", SpaceState.X);
            ComputerPlayer    player2   = new ComputerPlayer("Susan", SpaceState.O);

            Game3x3 game = new Game3x3(gameState, drawer, player1, player2);

            RunGame(game);
        }
Exemple #12
0
        private void CreatePlayers()
        {
            var rand             = new Random((int)DateTime.Now.Ticks);
            var player1PieceType = (PieceType)(1 + (rand.Next(100) % 2));
            var player2PieceType = player1PieceType == PieceType.O ? PieceType.X : PieceType.O;

            if ((rand.Next(100) % 2) == 0)
            {
                // computer begins.
                _player1 = new ComputerPlayer(player1PieceType);
                _player2 = new HumanPlayer(player2PieceType);
            }
            else
            {
                // human begins
                _player1 = new HumanPlayer(player1PieceType);
                _player2 = new ComputerPlayer(player2PieceType);
            }
        }
Exemple #13
0
        private void SetPlayers()
        {
            var numberOfHumanPlayers    = GetNumberOfPlayers("human");
            var numberOfComputerPlayers = GetNumberOfPlayers("computer");

            if (numberOfHumanPlayers != 0)
            {
                for (var i = 1; i <= numberOfHumanPlayers; i++)
                {
                    var humanPlayer = new HumanPlayer(i, new PlayerConsoleRenderer());
                    try
                    {
                        Game.AddPlayerToGame(humanPlayer);
                    }
                    catch (Exception ex)
                    {
                        Renderer.RenderMessage(ex.Message);
                        i--;
                    }
                }
            }

            if (numberOfComputerPlayers != 0)
            {
                numberOfHumanPlayers = numberOfHumanPlayers > 0?  numberOfHumanPlayers : numberOfHumanPlayers + 1;
                for (var i = numberOfHumanPlayers; i <= numberOfComputerPlayers; i++)
                {
                    var computerPlayer = new ComputerPlayer(i + 1, new PlayerConsoleRenderer());
                    try
                    {
                        Game.AddPlayerToGame(computerPlayer);
                    }
                    catch (Exception ex)
                    {
                        Renderer.RenderMessage(ex.Message);
                        i--;
                    }
                }
            }

            Game.SetCurrentPlayer(Game.GetGamePlayers().First());
        }
Exemple #14
0
        private void PlayerMovesRandomlyOnBoardWithNEmptySpaces(int possibleMoves, Board board)
        {
            Dictionary<Tuple<int, int>, int> generatedMoves = new Dictionary<Tuple<int, int>, int>();
            ComputerPlayer computerPlayer = new ComputerPlayer(BoardCharacter.Zero);

            for (int i = 0; i < possibleMoves * 1000; i++)
            {
                var move = computerPlayer.MakeMove(board);
                int count;
                if (!generatedMoves.TryGetValue(move, out count))
                    count = 0;
                count++;
                generatedMoves[move] = count;
            }

            foreach (var generatedMove in generatedMoves)
            {
                Assert.IsTrue(generatedMove.Value > 500);
                Assert.IsTrue(generatedMove.Value < 1500);
            }
        }
Exemple #15
0
        static void Main(string[] args)
        {
            HumanPlayer gA = new HumanPlayer();
            ComputerPlayer gB = new ComputerPlayer();

            gA.Name = "User";
            gB.Name = "Computer";
            gA.Sign = "x";
            gB.Sign = "o";

       
            char[,] board = new char[3, 3] {
                { '1', '2', '3' },
                { '4', '5', '6' },
                { '7', '8', '9' }
            };
            char[,] boardCopy = board.Clone() as char[,];

            bool endGame = false;
            bool playerMoveA = ture;
            for (int round = 0; round < board.Length; ++round)
            {
                Console.Clear();
                DrawBoard(board);
                    if (playerMoveA)
                    {
                        Console.WriteLine("Ruch wykonuje: " + gA.Name);
                        endGame = gA.MakeMove(board, boardCopy);
                        playerMoveA = false;
                    }  
                    else
                    {
                        Console.WriteLine("Ruch wykonuje: " + gB.Name);
                        endGame = gB.MakeMove(board, boardCopy);
                        playerMoveA = true;
                    }
            Console.ReadKey();
            }
        }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            TicTacToeForm f = new TicTacToeForm();

            // Create the game players
            // Players can be either human or computer players
            // It does not matter which piece 'X' or 'O' player 1 or two have
            // but they must be different

            // Create a human player
            Player p1 = new HumanPlayer("PP", Board.Pieces.X, f);


            // Create a computer player
            // You can create varying degrees of difficulty by creating computer
            // players that build bigger game trees
            // uncomment desired player and comment all other player 2s


            // create a computer player with the default game tree search depth
            Player p2 = new ComputerPlayer("nT", Board.Pieces.O);

            // Create a computer player that only looks ahead 1 move
            // i.e only considers their immediate move and not any subsequent moves
            // by their opponent.
            // this is a very poor player
            // Player p2 = new ComputerPlayer(Board.Pieces.X, f, 1);

            // Creates an advanced computer player that looks ahead 5 moves
            // Player p2 = new ComputerPlayer("Advanced HAL", Board.Pieces.X, 5);

            f.AddPlayer(p1);
            f.AddPlayer(p2);

            Application.Run(f);
        }
        /// <summary>
        /// Start Tic Tac Toe Game
        /// </summary>
        public static void StartTheGame()
        {
            Board          board    = new Board(new Tuple <char, char>('x', 'o'));
            HumanPlayer    p1       = new HumanPlayer();
            ComputerPlayer p2       = new ComputerPlayer();
            var            noOneWon = true;

            while (noOneWon)
            {
                // play random
                noOneWon = p1.MakeRandomMove();
                if (noOneWon)
                {
                    noOneWon = p2.MakeRandomMove();
                }

                noOneWon = false;// just to end this loop
                //// OR play smart
                //noOneWon = p1.MakeSmartMove();
                //if (noOneWon)
                //    noOneWon = p2.MakeSmartMove();
            }
        }
 public void GetEmptyFieldsTest_WhenTwoFieldsAreEmpty_ThenReturnThoseEmptyFields()
 {
     ComputerPlayer target = new ComputerPlayer();
     var board = new Piece[3, 3];
     board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O;
     board[0, 1] = Piece.O; board[1, 1] = Piece.O; board[2, 1] = Piece.Empty;
     board[0, 2] = Piece.X; board[1, 2] = Piece.X; board[2, 2] = Piece.Empty;
     var boardState = new BoardState(null);
     boardState.Board = board;
     List<Position> emptyFields = target.GetEmptyFields(boardState);
     List<Position> expectedEmptys = new List<Position>()
     {
         new Position {X = 2, Y = 1},
         new Position {X = 2, Y = 2}
     };
     Assert.AreEqual(emptyFields.Count == expectedEmptys.Count, true);
     int i = 0;
     foreach (var position in expectedEmptys)
     {
         Assert.AreEqual(true, position.IsEqual(emptyFields[i]));
         i++;
     }
 }
Exemple #19
0
 public App()
 {
     _computerPlayer = new ComputerPlayer(_winDetector, new ScenarioCalculator(_winDetector));
 }
Exemple #20
0
 public Game(string playerName)
 {
     this.board  = new Board();
     this.npc    = new ComputerPlayer("Bernie", this.board);
     this.player = new HumanPlayer(playerName);
 }
Exemple #21
0
 public void ComputerPlayerReturnsCorrectCharacter()
 {
     ComputerPlayer player = new ComputerPlayer(BoardCharacter.X);
     Assert.IsTrue(player.GetCharacterType() == BoardCharacter.X);
 }
		void SetupGame() {
			Player p1 = new HumanPlayer("Joe", Board.Pieces.X);

			// Create a computer player
			// You can create varying degrees of difficulty by creating computer
			// players that build bigger game trees
			// uncomment desired player and comment all other player 2s


			// create a computer player with the default game tree search depth
			Player p2 = new ComputerPlayer("HAL", Board.Pieces.O);

			// Create a computer player that only looks ahead 1 move
			// i.e only considers their immediate move and not any subsequent moves
			// by their opponent. 
			// this is a very poor player
			// Player p2 = new ComputerPlayer(Board.Pieces.X, f, 1);

			// Creates an advanced computer player that looks ahead 5 moves
			// Player p2 = new ComputerPlayer("Advanced HAL", Board.Pieces.X, 5);

			AddPlayer(p1);
			AddPlayer(p2);
		}
Exemple #23
0
 // Constructor with 2 ComputerPlayers
 public Hud(ComputerPlayer computerPlayer1, ComputerPlayer computerPlayer2)
 {
     this.computerPlayer2 = computerPlayer1;
     this.computerPlayer1 = computerPlayer2;
 }
Exemple #24
0
 // Constructor with 1 Player 1 ComputerPlayer
 public Hud(Player player1, ComputerPlayer computerPlayer)
 {
     this.player1         = player1;
     this.computerPlayer1 = computerPlayer;
 }