Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Gambler"/> class.
        /// </summary>
        public Gambler()
        {
            Console.WriteLine("enter stakes:");
            int stake = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("enter goal:");
            int goal = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Number of turns:");
            int noOfturns            = Convert.ToInt32(Console.ReadLine());
            UtilityAlgorithm utility = new UtilityAlgorithm();

            utility.Gamble(stake, goal, noOfturns);
        }
Esempio n. 2
0
        /// <summary>
        /// this is PlayGame
        /// </summary>
        public void PlayGame()
        {
            //// initialize the variable
            int  count = 0, intChoice;
            char choice, player1Marker, player2Marker;
            bool isWin = false;

            //// create 3*3 char multidimension array
            char[,] board = new char[3, 3];
            //// create random object
            Random random = new Random();
            //// create game object
            UtilityAlgorithm game = new UtilityAlgorithm();

            Console.WriteLine("Enter player 1 name: ");
            string player1 = Console.ReadLine();

            Console.WriteLine("player 2 is Computer.");

validation1:
            try
            {
                //// take input choice in char type
                Console.WriteLine(player1 + " enter your choice marker for the game 'x' or '$':");
                player1Marker = Convert.ToChar(Console.ReadLine());
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid Choice");
                goto validation1;
            }
            //// assign player's marker to player
            if (player1Marker == 'X' || player1Marker == 'x')
            {
                player2Marker = '$';
            }
            else
            {
                player2Marker = 'X';
            }
            //// declare the array
            game.Initialize(board);
            game.Display(board);
            //// do while loop for executing 1st iteration without any condition
            do
            {
                ////Block-1
validation2:
                try
                {
                    Console.WriteLine("Now, select your choice:");
                    intChoice = Convert.ToChar(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid Choice");
                    goto validation2;
                }

                if (intChoice >= 48 && intChoice <= 56)
                {
                    choice = Convert.ToChar(intChoice);
                }
                else
                {
                    Console.WriteLine("Invalid Choice");
                    goto validation2;
                }

                bool valid = game.Replace(board, choice, player1Marker);
                if (!valid)
                {
                    Console.WriteLine("Invalid Choice");
                    goto validation2;
                }

                game.Display(board);

                isWin = game.WinCheck(board);
                if (isWin)
                {
                    Console.Write("we have winner: " + player1);
                    break;
                }

                count++;
                if (count == 5)
                {
                    Console.WriteLine("****Draw****");
                    break;
                }

                ////Block-2
validation3:
                Console.WriteLine("Computer's turn:");
                int computerChoice = (int)random.Next(9);
                Console.WriteLine(computerChoice);
                char computerChoose = (char)(computerChoice + '0');
                bool computerValid  = game.Replace(board, computerChoose, player2Marker);
                if (!computerValid)
                {
                    // Console.WriteLine("Invalid Choice");
                    goto validation3;
                }

                game.Display(board);
                isWin = game.WinCheck(board);
                if (isWin)
                {
                    Console.Write("we have winner: computer");
                    break;
                }
            }while (isWin == false || count < 5);
            Console.ReadKey();
        }
Esempio n. 3
0
        /// <summary>
        /// checks the win.
        /// </summary>
        /// <param name="board">The board.</param>
        /// <returns>boolean value </returns>
        public bool WinCheck(char[,] board)
        {
            UtilityAlgorithm game = new UtilityAlgorithm();

            return(game.CheckRow(board) || game.CheckColumn(board) || game.CheckDiagonal(board));
        }