Exemplo n.º 1
0
 public void TTboardClone()
 {
     TTTBoard board = new TTTBoard(3);
     TTTBoard newBoard = board.Clone();
     board.Move(0, 0, Player.PLAYERX);
     board.Move(0, 1, Player.PLAYERO);
     int emptyBoard = board.GetEmptySquares().Count;
     int emptyNewBoard = newBoard.GetEmptySquares().Count;
     Assert.AreNotEqual(emptyNewBoard, emptyBoard, "Bad clone");
 }
Exemplo n.º 2
0
 public void TTboardCheckWin()
 {
     TTTBoard board = new TTTBoard(3);
     TTTBoard newBoard = board.Clone();
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 1");
     board.Move(0, 0, Player.PLAYERO);
     board.Move(0, 1, Player.PLAYERX);
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 2");
     board.Move(1, 0, Player.PLAYERO);
     board.Move(0, 2, Player.PLAYERX);
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 3");
     board.Move(2, 0, Player.PLAYERO);
     Assert.AreEqual(Player.PLAYERO, board.CheckWin(), "Check Win should be game winner  4");
 }
Exemplo n.º 3
0
        /// <summary>
        ///  Make a move on the board
        /// </summary>
        /// <param name="board"></param>
        /// <param name="player"></param>
        /// <returns></returns>
        private Tuple <int, int[]> mmMove(TTTBoard board, Player player)
        {
            if ((int)board.CheckWin() == 1)
            {
                return(new Tuple <int, int[]>(Score(board.CheckWin()), new int[] { -1, -1 }));
            }
            else if ((board.GetEmptySquares()).Count == (board.Dim * board.Dim))
            {
                return(new Tuple <int, int[]>(0, new int[] { rnd.Next(board.Dim), rnd.Next(board.Dim) }));
            }
            else if (board.GetEmptySquares().Count == 0)
            {
                return(new Tuple <int, int[]>(Score(board.CheckWin()), new int[] { -1, -1 }));
            }

            List <int>   listScore = new List <int>();
            List <int[]> listMove  = new List <int[]>();
            Player       newPlayer = player == Player.PLAYERO ? Player.PLAYERX : Player.PLAYERO;

            List <int[]> moves = board.GetEmptySquares();

            foreach (int[] move in moves)
            {
                TTTBoard newBoard = board.Clone();
                newBoard.Move(move[0], move[1], player);

                int newScore = mmMove(newBoard, newPlayer).Item1;
                listScore.Add(newScore);
                listMove.Add(move);

                if (player == Player.PLAYERX && newScore == 1)
                {
                    break;
                }
                else if (player == Player.PLAYERO && newScore == -1)
                {
                    break;
                }
            }
            int[] moveNew;
            int   newScoreNew;

            if (player == Player.PLAYERX)
            {
                newScoreNew = listScore.Max();
                moveNew     = listMove[listScore.IndexOf(newScoreNew)];
            }
            else
            {
                newScoreNew = listScore.Min();
                moveNew     = listMove[listScore.IndexOf(newScoreNew)];
            }
            return(new Tuple <int, int[]>(newScoreNew, moveNew));
        }
Exemplo n.º 4
0
 public void TTboardCheckWin2()
 {
     TTTBoard board = new TTTBoard(3);
     TTTBoard newBoard = board.Clone();
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 1");
     board.Move(0, 0, Player.PLAYERO);
     board.Move(0, 1, Player.PLAYERO);
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 2");
     board.Move(1, 0, Player.PLAYERX);
     board.Move(1, 1, Player.PLAYERX);
     Assert.AreEqual(Player.NONE, board.CheckWin(), "Check Win game in progres 3");
     board.Move(1, 2, Player.PLAYERO);
     board.Move(0, 2, Player.PLAYERX);
     Assert.AreEqual(Player.NONE, board.CheckWin(), "heck Win game in progres 4");
     board.Move(2, 0, Player.PLAYERO);
     board.Move(2, 1, Player.PLAYERO);
     board.Move(2, 2, Player.PLAYERX);
     Assert.AreEqual(Player.DRAW, board.CheckWin(), "heck Win game DRAW");
 }
Exemplo n.º 5
0
        /// <summary>
        /// Put X to correct place in board from view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayerMakeMove(object sender, RoutedEventArgs e)
        {
            string rowString = (sender as Button).Name[3].ToString();
            string colString = (sender as Button).Name[4].ToString();
            int    row       = Convert.ToInt32(rowString);
            int    col       = Convert.ToInt32(colString);

            if (this.inProgress &&
                this.turn == this.humanPlayer &&
                this.board.Square(row, col) == (int)Player.EMPTY
                )
            {
                board.Move(row, col, humanPlayer);
                this.turn = this.aiPlayer;

                Player winner = this.board.CheckWin();
                if (winner != Player.NONE)
                {
                    GameOver(winner);
                }
                this.wait = true;
            }

            //    def click(self, position):
            //        """
            //        Make human move.
            //        """
            //        if self._inprogress and (self._turn == self._humanplayer):
            //            row, col = self.get_grid_from_coords(position)
            //            if self._board.square(row, col) == provided.EMPTY:
            //                self._board.move(row, col, self._humanplayer)
            //                self._turn = self._aiplayer
            //                winner = self._board.check_win()
            //                if winner is not None:
            //                    self.game_over(winner)
            //                self._wait = True
        }