Esempio n. 1
0
        void test2()
        {
            BoardNumber[,] board = new BoardNumber[2, 2];

            maxit.PopulateBoard(board);

            if (board[0, 0] == null)
            {
                Console.WriteLine("test2 failed at node [0,0] == null.");
                return;
            }
            else if (board[0, 1] == null)
            {
                Console.WriteLine("test2 failed at node [0,0] == null.");
                return;
            }
            else if (board[1, 0] == null)
            {
                Console.WriteLine("test2 failed at node [0,0] == null.");
                return;
            }
            else if (board[1, 1] == null)
            {
                Console.WriteLine("test2 failed at node [0,0] == null.");
                return;
            }
            else
            {
                Console.WriteLine("test2 finished successfully.");
            }
        }
Esempio n. 2
0
        public BoardNumber FindLargestAvailable(BoardNumber[,] board)
        {
            BoardNumber LargestNodeAvailable = board[selectedNode.X, 0];

            for (int i = 0; i < BoardDimension; i++)
            {
                BoardNumber temp = board[selectedNode.X, i];
                if (!temp.selected && !temp.consumed && (temp.Value > LargestNodeAvailable.Value || LargestNodeAvailable.consumed))
                {
                    LargestNodeAvailable = temp;
                    Console.WriteLine("setting largestnodeavail in if1 to: [{0}, {1}] = {2}", temp.X, temp.Y, temp.Value);
                }

                temp = board[i, selectedNode.Y];
                if (!temp.selected && !temp.consumed && (temp.Value > LargestNodeAvailable.Value || LargestNodeAvailable.consumed))
                {
                    Console.WriteLine("setting largestnodeavail in if2 to: [{0}, {1}] = {2}", temp.X, temp.Y, temp.Value);
                    LargestNodeAvailable = temp;
                }
            }

            if (!LargestNodeAvailable.consumed && !LargestNodeAvailable.selected)
            {
                return(LargestNodeAvailable);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 3
0
        void test4()
        {
            BoardNumber[,] board = new BoardNumber[2, 2];
            maxit.BoardDimension = 2;
            board[0, 0]          = new BoardNumber(0, 0);
            board[0, 1]          = new BoardNumber(0, 1);
            board[1, 0]          = new BoardNumber(1, 0);
            board[1, 1]          = new BoardNumber(1, 1);

            maxit.selectedNode   = board[0, 0];
            board[0, 0].selected = true;

            board[0, 1].consumed = true;
            board[1, 0].consumed = true;

            maxit.PrintBoard(board);

            if (!maxit.isGameOver(board))
            {
                Console.WriteLine("test4 failed, isGameOver returned false with no moves left.");
            }

            board[0, 1].consumed = false;

            maxit.PrintBoard(board);

            if (maxit.isGameOver(board))
            {
                Console.WriteLine("test4 failed, isGameOver returned true with one move left.");
            }

            Console.WriteLine("test4 finished successfully.");
        }
Esempio n. 4
0
 public void PopulateBoard(BoardNumber[,] board)
 {
     for (int x = 0; x < board.GetLength(0); x += 1)
     {
         for (int y = 0; y < board.GetLength(1); y += 1)
         {
             board[x, y] = new BoardNumber(x, y);
         }
     }
 }
Esempio n. 5
0
        void test1()
        {
            BoardNumber[,] board = new BoardNumber[2, 2];
            board[0, 0]          = new BoardNumber(0, 0);
            board[0, 1]          = new BoardNumber(0, 1);
            board[1, 0]          = new BoardNumber(0, 0);
            board[1, 1]          = new BoardNumber(0, 0);

            board[0, 1].selected = true;
            board[0, 0].consumed = true;

            maxit.PrintBoard(board);

            Console.WriteLine("test1 finished successfully.");
        }
Esempio n. 6
0
        void test3()
        {
            BoardNumber[,] board = new BoardNumber[2, 2];
            maxit.BoardDimension = 2;
            board[0, 0]          = new BoardNumber(0, 0);
            board[0, 1]          = new BoardNumber(0, 1);
            board[1, 0]          = new BoardNumber(1, 0);
            board[1, 1]          = new BoardNumber(1, 1);

            maxit.selectedNode   = board[0, 0];
            board[0, 0].selected = true;

            board[0, 0].Value = 0;
            board[0, 1].Value = 1;
            board[1, 0].Value = 2;
            board[1, 1].Value = 0;

            maxit.PrintBoard(board);

            BoardNumber b = maxit.FindLargestAvailable(board);

            if (b.Value != 2)
            {
                Console.WriteLine("test3 failed finding node.value = 2.");
            }

            board[0, 0].consumed = true;
            board[0, 1].consumed = true;
            board[1, 0].consumed = true;
            board[1, 1].consumed = true;

            b = maxit.FindLargestAvailable(board);

            if (b != null)
            {
                Console.WriteLine("test3 failed. FindLargestAvailable should have returned null.");
            }

            Console.WriteLine("test3 finished successfully.");
        }
Esempio n. 7
0
        public void Run()
        {
            string cDim = string.Empty;

            Random randomNumber = new Random();

            Console.WriteLine("Welcome to MAXIT\n");
            Console.Write("Please enter board dimension: ");

            while (!int.TryParse(cDim, out BoardDimension))
            {
                cDim = Console.ReadLine();
            }
            board = new BoardNumber[BoardDimension, BoardDimension];
            PopulateBoard(board);

            selectedNode          = board[randomNumber.Next(0, BoardDimension - 1), randomNumber.Next(0, BoardDimension - 1)];
            selectedNode.selected = true;

            while (!isGameOver(board))
            {
                Console.Clear();

                PrintBoard(board);
                Console.WriteLine("Player: {0}    Computer: {1}\n", PlayerScore, ComputerScore);

                Console.Write("Traverse X or Y? ");
                string direction = string.Empty;
                direction = Console.ReadLine();
                direction = direction.ToLower();
                if (direction != "x" && direction != "y")
                {
                    Console.WriteLine("\nPlease enter X or Y.");
                    Thread.Sleep(1500);
                    continue;
                }

                Console.Write("\nHow far? ");
                int    distance       = 0;
                string distanceString = string.Empty;
                distanceString = Console.ReadLine();
                if (!int.TryParse(distanceString, out distance))
                {
                    Console.WriteLine("\nPlease enter a valid number.");
                    Thread.Sleep(1500);
                    continue;
                }
                else if (direction == "x" && (selectedNode.X + distance > BoardDimension - 1 || selectedNode.X + distance < 0))
                {
                    Console.WriteLine("\nDistance out of bounds.");
                    Thread.Sleep(1500);
                    continue;
                }
                else if (direction == "y" && (selectedNode.Y + distance > BoardDimension - 1 || selectedNode.Y + distance < 0))
                {
                    Console.WriteLine("\nDistance out of bounds.");
                    Thread.Sleep(1500);
                    continue;
                }
                ////////


                BoardNumber temp;
                if (direction == "x")
                {
                    temp = board[selectedNode.X + distance, selectedNode.Y];
                }
                else
                {
                    temp = board[selectedNode.X, selectedNode.Y + distance];
                }

                if (temp.selected == true)
                {
                    Console.WriteLine("\nThis node is already selected.");
                    Thread.Sleep(1500);
                    continue;
                }
                else if (temp.consumed == true)
                {
                    Console.WriteLine("\nThis node is already consumed.");
                    Thread.Sleep(1500);
                    continue;
                }


                selectedNode.selected = false;
                selectedNode          = temp;
                selectedNode.selected = true;
                selectedNode.consumed = true;
                PlayerScore          += selectedNode.Value;

                BoardNumber nextMove = FindLargestAvailable(board);
                if (nextMove != null)
                {
                    Console.WriteLine("Computer Player's turn...");
                    selectedNode.selected = false;
                    selectedNode          = nextMove;
                    selectedNode.selected = true;
                    selectedNode.consumed = true;
                    ComputerScore        += selectedNode.Value;
                }
            }

            Console.WriteLine("****GAME OVER****");
            if (ComputerScore > PlayerScore)
            {
                Console.WriteLine("Computer player wins with {0} points!", ComputerScore);
            }

            else if (PlayerScore > ComputerScore)
            {
                Console.WriteLine("You with {0} points!", PlayerScore);
            }

            else
            {
                Console.WriteLine("Tie game at {0} points!", PlayerScore);
            }

            Console.Write("\n\nPress any key to continue...");
            Console.ReadKey();
        }