Ejemplo n.º 1
0
 public void InvariantTests0()
 {
     TTTBoard board = new TTTBoard(3);
     List<int[]> squares = board.GetEmptySquares();
     int count = squares.Count;
     Assert.AreEqual(9, count, "Empty squares");
 }
Ejemplo n.º 2
0
 /// <summary>
 /// New game button click
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnNewGame_Click(object sender, RoutedEventArgs e)
 {
     board            = new TTTBoard(3);
     this.turn        = this.initialTurn == Player.PLAYERO ? Player.PLAYERX : Player.PLAYERO;
     this.initialTurn = this.initialTurn == Player.PLAYERO ? Player.PLAYERX : Player.PLAYERO;
     this.wait        = false;
     this.inProgress  = true;
 }
Ejemplo n.º 3
0
 private int[] MoveWrapper(TTTBoard board, Player player)
 {
     int[] move = mmMove(board, player).Item2;
     if (move[0] == -1 && move[1] == -1)
     {
         throw new Exception("ilegal move -1,-1");
     }
     return(move);
 }
Ejemplo n.º 4
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");
 }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Return a copy of the board.
        /// </summary>
        /// <returns></returns>
        public TTTBoard Clone()
        {
            //    def clone(self):
            //        """
            //        Return a copy of the board.
            //        """
            //using object extensions.cs to new copy
            TTTBoard newBoard = this.Copy();

            return(newBoard);
        }
Ejemplo n.º 7
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");
 }
Ejemplo n.º 8
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");
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Redraws gamefield
 /// </summary>
 private void DrawBoard(TTTBoard board)
 {
     for (int row = 0; row < board.Dim; row++)
     {
         for (int col = 0; col < board.Dim; col++)
         {
             if (board.Square(row, col) == (int)Player.PLAYERO)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/kolko.png");
             }
             else if (board.Square(row, col) == (int)Player.PLAYERX)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/krzyzyk.png");
             }
             else if (board.Square(row, col) == (int)Player.EMPTY)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/nic.png");
             }
         }
     }
 }
 /// <summary>
 /// New game button click
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnNewGame_Click(object sender, RoutedEventArgs e)
 {
     board = new TTTBoard(3);
     this.turn = this.initialTurn == Player.PLAYERO ? Player.PLAYERX : Player.PLAYERO;
     this.initialTurn = this.initialTurn == Player.PLAYERO ? Player.PLAYERX : Player.PLAYERO;
     this.wait = false;
     this.inProgress = true;
 }
 private int[] MoveWrapper(TTTBoard board, Player player)
 {
     int[] move = mmMove(board, player).Item2;
     if (move[0] == -1 && move[1] == -1) throw new Exception("ilegal move -1,-1");
     return move;
 }
        /// <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);
        }
 /// <summary>
 /// Redraws gamefield
 /// </summary>
 private void DrawBoard(TTTBoard board)
 {
     for (int row = 0; row < board.Dim; row++)
     {
         for (int col = 0; col < board.Dim; col++)
         {
             if (board.Square(row, col) == (int)Player.PLAYERO)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/kolko.png");
             }
             else if (board.Square(row, col) == (int)Player.PLAYERX)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/krzyzyk.png");
             }
             else if (board.Square(row, col) == (int)Player.EMPTY)
             {
                 ChangeButtonImage("btn" + row.ToString() + col.ToString(), "Images/nic.png");
             }
         }
     }
 }