Exemplo n.º 1
0
        public void DoGame(GameBoard.Player player,
                           Func <GameBoard, GameBoard.Player, GameHistory.GameMove> player1Move,
                           Func <GameBoard, GameBoard.Player, GameHistory.GameMove> player2Move)
        {
            currentBoard = GameBoard.StartGame;
            ConsoleWriteLine("Start New Game");
            currentBoard.DisplayBoard();
            while (!currentBoard.Winner(player) && !currentBoard.IsTie())
            {
                //reverse player
                player = (player == GameBoard.Player.O) ? GameBoard.Player.X : GameBoard.Player.O;

                GameHistory.GameMove move = player1Move(currentBoard, player);
                ConsoleWriteLine(String.Format("The First player places {0}.", move));
                gameHistory.MakeMove(currentBoard, move);
                currentBoard.DoMove(move);
                currentBoard.DisplayBoard();
                if (currentBoard.Winner(player))
                {
                    break;
                }

                player = (player == GameBoard.Player.O) ? GameBoard.Player.X : GameBoard.Player.O;
                //query game stats to get next move
                move = player2Move(currentBoard, player);
                ConsoleWriteLine(String.Format("The Second player places {0}.", move));
                //register move in game hidtory
                gameHistory.MakeMove(currentBoard, move);
                //update current board
                currentBoard.DoMove(move);
                currentBoard.DisplayBoard();
            }
            //by default all games start with X. Game win is relative to player X
            gameHistory.RegisterGameResults(currentBoard.Winner(GameBoard.Player.X));
        }
Exemplo n.º 2
0
        internal Move[] DoMove(GameHistory.GameMove mv)
        {
            //Debug.Assert(row < 3 && row >= 0);
            //Debug.Assert(col < 3 && col >= 0);
            //Debug.Assert(mv != Move.Sp);
            //int index = row * 3 + col;
            if (Board[mv.Position] == Move.Sp)
            {
                Board[mv.Position] = mv.Player;
                return(Board);
            }

            throw new InvalidOperationException("Cannot place a move on an occupied board position");
        }
Exemplo n.º 3
0
        public bool GetNextMove(GameBoard.Player player, out GameHistory.GameMove mv, out string prompt)
        {
            prompt = String.Empty;

            //keep trying until the user quits or enters a valid input
            //while (true)
            //{
            //prompt user for imnput
            ConsoleWriteLine("Enter row and column for your move: row , column. \nor enter 'Q' to quit the game: ", false);
            string input = Console.ReadLine();

            //if player is quiting throw quit exception
            if (input.Trim().ToUpper() == "Q")
            {
                throw new QuitGameException("Quiting TicTacToe Game");
            }

            //split inputs into row and col numbers
            var inputs = input.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int        digit = -1;
            List <int> move  = new List <int>();

            foreach (string i in inputs)
            {
                if (Int32.TryParse(i, out digit))
                {
                    move.Add(digit);
                }
            }

            //check move input for valid input
            if (move.Count == 2)
            {
                if (currentBoard.ValidMove(move[0] - 1, move[1] - 1, player == GameBoard.Player.O ? GameBoard.Move.O : GameBoard.Move.X))
                {
                    var row = move[0] - 1;
                    var col = move[1] - 1;
                    mv = new GameHistory.GameMove(MoveFromPlayer(player), GameBoard.IndexFromRowCol(row, col));
                    return(true);
                }

                prompt = "\nInvalid Input read. Must enter an integer between 1 and 3 a comma and an integer between 1 and 3\n";
            }
            else if (move.Count > 2)
            {
                prompt =
                    "\nToo many inputs entered" + "\nUser input was " + input +
                    "\nMust enter an integer between 0 and 2 a comma and an integer between 0 and 2\n";
            }
            else if (move.Count < 2)
            {
                prompt =
                    "\nToo few inputs entered" + "\nUser input was " + input +
                    "Must enter an integer between 0 and 2 a comma and an integer between 0 and 2\n";
            }

            mv = null;
            return(false);
            //}
        }
Exemplo n.º 4
0
 public bool ValidMove(GameHistory.GameMove mv) //int index, Move mv)
 {
     return((mv.Position < 9 && mv.Position >= 0) &&
            //(GameBoard.MoveFromPlayer(mv.Player) != Move.Sp) &&
            Board[mv.Position] == Move.Sp);
 }