示例#1
0
 static void Main(string[] args)
 {
     IUI ui = new ConsoleUI();
     GameLogic Game = new GameLogic(ui);
     Game.StartGame();
     Console.WriteLine("Thank you for playing hope to see again ");
     Console.ReadKey();
 }
示例#2
0
        public void TestingnewBoard_moveCountToZero()
        {
            //Arrange
            IUI ui = new MockUI();
            GameLogic GLnewgame = new GameLogic(ui);
            GLnewgame.CreatePlayers();
            int expectedMoveCount = 0;

            //Act
            int actualMoveCount;
            actualMoveCount = GLnewgame.GetMoveCount();

            //Assert
            Assert.AreEqual(expectedMoveCount, actualMoveCount);
        }
示例#3
0
        public void TestingNewBoard()
        {
            //Arrange

            IUI ui = new MockUI();
            GameLogic GLnewgame = new GameLogic(ui);
            GLnewgame.NewBoard();

            //Act
            Board ActualBoard;
            ActualBoard = GLnewgame.GetGameBoard();

            //Assert
            Assert.IsInstanceOf<Board>(ActualBoard);
        }
示例#4
0
        public void TestingAnotherGame()
        {
            //Arange
            int[,] anotherGame = new int[9, 2] { { 2, 2 }, { 2, 3 }, { 3, 2 }, { 1, 2 }, { 1, 3 }, { 3, 1 }, { 3, 3 }, { 1, 1 }, { 2, 1 } };
            MockUI ui = new MockUI(anotherGame, 9);
            bool expected = true;

            //Act
            GameLogic Game = new GameLogic(ui);
            Game.StartGame();
            bool actual = ui.GetAnnounceDraw();

            //Assert
            Assert.AreEqual(expected, actual);
        }
示例#5
0
        public void TestingWinIn9Moves()
        {
            //Arange
            int[,] WinIn9 = new int[9, 2] { { 2, 2 }, { 2, 3 }, { 3, 2 }, { 1, 2 }, { 1, 3 }, { 3, 1 }, { 3, 3 }, { 2, 1 }, { 1, 1 } };
            MockUI ui = new MockUI(WinIn9, 9);
            bool expected = true;

            //Act
            GameLogic Game = new GameLogic(ui);
            Game.StartGame();
            bool actual = ui.GetAnnounceWinner();

            //Assert
            Assert.AreEqual(expected, actual);
        }
示例#6
0
        public void TestingCreatePlayers()
        {
            //Arrange
            IUI ui = new MockUI();
            GameLogic GLnewgame = new GameLogic(ui);
            GLnewgame.CreatePlayers();

            //Act
            Player ActualPlayer1, ActualPlayer2;
            ActualPlayer1 = GLnewgame.GetPlayer1();
            ActualPlayer2 = GLnewgame.GetPlayer2();

            //Assert
            Assert.IsInstanceOf<Player>(ActualPlayer1);
            Assert.IsInstanceOf<Player>(ActualPlayer2);
        }
示例#7
0
        protected void Click_Command(object sender, CommandEventArgs e)
        {
            if (ViewState["Board"] == null)
            {
                ViewState["Board"] = "---------";
            }

            var board = ViewState["Board"].ToString().ToCharArray();

            var index = int.Parse(e.CommandArgument.ToString());
            if (board[index] != '-')
            {
                this.Result.Text = "Invalid move";
                return;
            }

            board[index] = 'X';
            ViewState["Board"] = new string(board);

            var logic = new GameLogic();
            var resultX = logic.GetResult(board);
            if (resultX == GameResult.WonByX)
            {
                this.Result.Text = "You win!";
                this.RestartGame();
                return;
            }
            else if (resultX == GameResult.Draw)
            {
                this.Result.Text = "Draw";
                this.RestartGame();
                return;
            }

            var ai = new GameNPCLogic();

            for (int i = 0; i < board.Length; i++)
            {
                if (board[i] == '-')
                {
                    board[i] = 'O';
                    break;
                }
            }

            ViewState["Board"] = new string(board);

            var resultY = logic.GetResult(board);
            if (resultY == GameResult.WonByO)
            {
                this.Result.Text = "You loose!";
                this.RestartGame();
                return;
            }
            else if (resultY == GameResult.Draw)
            {
                this.Result.Text = "Draw";
                this.RestartGame();
                return;
            }
        }
示例#8
0
文件: UI.cs 项目: mdrozda46/cohort
        static void Main(string[] args)
        {
            string player1Name;
            string player1Marker = "X";
            string player2Name;
            string player2Marker = "O";
            string[] gameBoardMarkers = new string[] {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
            string currentPlayer;
            string playerInput;
            int squareSelected;
            int movesPlayed = 0;
            bool isThereAWinner = false;
            GameLogic gameBoard = new GameLogic();

            Console.WriteLine("Player 1 enter your name:");
            player1Name = Console.ReadLine();

            Console.WriteLine("\nPlayer 2 enter your name:");
            player2Name = Console.ReadLine();

            do
            {
                Console.WriteLine("\nLet's play Tic Tac Toe!!!");
                Console.WriteLine("\n  *{0} your marker is {1}", player1Name, player1Marker);
                Console.WriteLine("  *{0} your marker is {1}", player2Name, player2Marker);

                currentPlayer = player1Name;

                while (!isThereAWinner)
                {
                    GameLogic.DrawBoard(gameBoardMarkers);
                    Console.WriteLine("\n {0} , pick an open square by entering a number:", currentPlayer);
                    playerInput = Console.ReadLine();

                    //Check for valid integer input between 1 and 9
                    if (int.TryParse(playerInput, out squareSelected) && (int.Parse(playerInput) >= 1) && (int.Parse(playerInput) <= 9))
                    {
                        //Check if square has already been selected
                        if (gameBoardMarkers[squareSelected - 1] == squareSelected.ToString())
                        {
                            // Mark spot and swith players
                            if (currentPlayer == player1Name)
                            {
                                gameBoardMarkers[squareSelected - 1] = player1Marker;
                                currentPlayer = player2Name;
                            }

                            // Mark spot and swith players
                            else
                            {
                                gameBoardMarkers[squareSelected - 1] = player2Marker;
                                currentPlayer = player1Name;
                            }

                            // Count number of moves made
                            movesPlayed++;

                            //Check for winner
                            isThereAWinner = GameLogic.CheckForWinner(gameBoardMarkers);

                            //If not winner after 9 play game is a tie
                            if (movesPlayed == 9)
                            {
                                break;
                            }
                        }

                        else
                        {
                            Console.WriteLine("{0}, that square is already selected. Please pick another square.", currentPlayer);
                        }

                    }

                    else
                    {
                        Console.WriteLine("\n{0}, {1} IS NOT A VALID INPUT! ", currentPlayer, playerInput);
                    }

                }

                GameLogic.DrawBoard(gameBoardMarkers);

                // Announce winner or tie
                if (isThereAWinner)
                {
                    if (currentPlayer == player1Name)
                    {
                        Console.WriteLine("\n{0} IS THE WINNNER!!!", player2Name);
                    }

                    else
                    {
                        Console.WriteLine("\n{0} IS THE WINNNER!!!", player1Name);
                    }
                }

                else
                {
                    Console.WriteLine("\nThe game was a tie.");
                }

                Console.WriteLine("Press Enter to play again or (Q) to quit.");
                Console.ReadLine();
            } while (playerInput.ToUpper() != "Q");
        }
示例#9
0
        public Movement GetBestMove(char[] board, int boardSize, int CurrentPlayer, int alpha, int beta, GameLogic logic)
        {
            Movement BestMove = null;
            int iPossibleMoves = board.Count(b => b == '-');

            Random rand = new Random();
            int i = rand.Next(boardSize);
            int j = rand.Next(boardSize);

            while (iPossibleMoves > 0)
            {
                do
                {
                    if (i < boardSize - 1)
                    {
                        i++;
                    }
                    else if (j < boardSize - 1)
                    {
                        i = 0; j++;
                    }
                    else
                    {
                        i = 0; j = 0;
                    }
                }
                while (board[FromMatrixToIndex(i, j)] != '-');

                Movement NewMove = new Movement(i, j);
                iPossibleMoves--;

                char[] NewBoard = (new string(board)).ToCharArray();

                NewBoard[FromMatrixToIndex(NewMove.iRow, NewMove.iCol)] = 'O';
                var result = logic.GetResult(NewBoard);

                if (result == GameResult.NotFinished)
                {
                    Movement tempMove = GetBestMove(NewBoard, 3, -CurrentPlayer, alpha, beta, logic);
                    NewMove.iRank = tempMove.iRank;
                }
                else
                {
                    if (result == GameResult.NotFinished)
                    {
                        NewMove.iRank = 0;
                    }
                    else
                    {
                        if (result == GameResult.WonByO)
                        {
                            NewMove.iRank = -1;
                        }
                        else
                        {
                            if (result == GameResult.WonByX)
                            {
                                NewMove.iRank = 1;
                            }
                        }
                    }
                }

                if (BestMove == null ||
                    (CurrentPlayer == 1 && NewMove.iRank < BestMove.iRank) ||
                    (CurrentPlayer == -1 && NewMove.iRank > BestMove.iRank))
                {
                    BestMove = NewMove;
                }

                if (CurrentPlayer == 1 && BestMove.iRank < beta)
                {
                    beta = BestMove.iRank;
                }

                if (CurrentPlayer == -1 && BestMove.iRank > alpha)
                {
                    alpha = BestMove.iRank;
                }

                if (alpha > beta)
                {
                    iPossibleMoves = 0;
                }
            }

            return BestMove;
        }
示例#10
0
 static void Main(string[] args)
 {
     GameLogic test = new GameLogic();
 }
 static void Main(string[] args)
 {
     GameLogic.GetAppInfo();
     GameLogic.GamePlay();
 }
 public MinMaxAlgorithm(GameLogic logic, Player player)
 {
     this.Logic     = logic;
     this.BotPlayer = player;
 }
示例#13
0
        protected void Click_Command(object sender, CommandEventArgs e)
        {
            if (ViewState["Board"] == null)
            {
                ViewState["Board"] = "---------";
            }

            var board = ViewState["Board"].ToString().ToCharArray();

            var index = int.Parse(e.CommandArgument.ToString());

            if (board[index] != '-')
            {
                this.Result.Text = "Invalid move - try again !";
                return;
            }

            board[index]       = 'X';
            ViewState["Board"] = new string(board);

            var logic   = new GameLogic();
            var resultX = logic.GetResult(board);

            if (resultX == GameResult.WonByX)
            {
                this.Result.Text = "Incredible ! You win ! ";
                this.RestartGame();
                return;
            }
            else if (resultX == GameResult.Draw)
            {
                this.Result.Text = "Draw .. what a surprise .. ";
                this.RestartGame();
                return;
            }

            var ai = new TicTacToeAI();

            for (int i = 0; i < board.Length; i++)
            {
                if (board[i] == '-')
                {
                    board[i] = 'O';
                    break;
                }
            }

            ViewState["Board"] = new string(board);

            var resultY = logic.GetResult(board);

            if (resultY == GameResult.WonByO)
            {
                this.Result.Text = "Damn ! You loose ! ";
                this.RestartGame();
                return;
            }
            else if (resultY == GameResult.Draw)
            {
                this.Result.Text = "Draw .. what a surprise .. ";
                this.RestartGame();
                return;
            }
        }
示例#14
0
 public MainWindow()
 {
     InitializeComponent();
     gameGrid.Loaded += GameGrid_Loaded;
     gameLogic        = new GameLogic();
 }