예제 #1
0
        static void Main(string[] args)
        {
            ///STARTUP

            bool   running = true;
            string input;
            char   selection = '*';

            //load an existing profile? Enter player names?

            while (running)
            {
                Console.Clear();
                //continue an existing game?
                if (File.Exists(saveFileName))
                {
                    Console.Write("Do you want to resume an old game? (Y/N)");
                    input = Console.ReadLine();
                    if (input[0] == 'y' || input[0] == 'Y')
                    {
                        Stream        saveFile     = File.OpenRead(saveFileName);
                        SoapFormatter deserializer = new SoapFormatter();
                        board = (Board)(deserializer.Deserialize(saveFile));
                        saveFile.Close();
                    }
                    //remove file regardless of restore or not since a new game would be started.
                    File.Delete(saveFileName);
                }

                //game not restored so start a new one
                if (board == null)
                {
                    board = new Board(boardHeight, boardWidth);
                }

                //get user action
                Console.WriteLine("Welcome to Connect Four! \n" +
                                  "Enter 'P' to play. \n" +
                                  /*  "Enter 'S' to see player stats. \n" + */
                                  "Enter 'Q' to quit.");

                input = Console.ReadLine().ToUpper();
                try
                {
                    selection = input[0];
                }
                catch (IndexOutOfRangeException) // no input
                {
                    continue;
                }

                /*  if (selection == 'S') User.GetStats();  */
                if (selection == 'Q')
                {
                    running = false;
                }



                ///PLAY CONNECT FOUR
                bool play       = running; //only play game if user has chosen not to exit app
                int  gameResult = 1;
                while (play)
                {
                    play = true;
                    Console.Clear();
                    Console.WriteLine("Enter the column number (1-7) where you would like to place your peice.\n" +
                                      "Enter 'S' to save and exit.\n" +
                                      "Enter 'Q' to quit game.\n");

                    ///DRAW THE BOARD
                    board.Draw();
                    ///GET NEXT ACTION
                    input = Console.ReadLine().ToUpper();
                    try
                    {
                        selection = input[0];
                    }
                    catch (IndexOutOfRangeException) // no input
                    {
                        continue;
                    }
                    ///HANDLE USER SELECTION
                    if (selection == 'S')
                    //save game
                    {
                        Stream        saveFile   = File.Create(saveFileName);
                        SoapFormatter serializer = new SoapFormatter();
                        serializer.Serialize(saveFile, board);
                        saveFile.Close();
                        return;//exit program directly
                    }
                    //quit game
                    else if (selection == 'Q')
                    {
                        play = false;
                    }

                    else if ((int)(selection - charOffset) < 7)
                    //valid column number entered
                    {
                        try
                        {
                            //perform move - returns game status
                            gameResult = board.Drop((int)(selection - charOffset));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e.Message + " Please try again.");
                            Console.ReadLine();
                            continue;
                        }
                    }
                    ///EVALUATE RESULT OF MOVE
                    if (gameResult == 0)
                    {
                        endGame("You win!");
                        play = false;
                    }
                    else if (gameResult == 2)
                    {
                        endGame("The game is a draw.");
                        play = false;
                    }
                }
                //game over
                Console.Clear();
            }
            //end program
            Console.WriteLine("Goodbye!");
        }
예제 #2
0
        static void Main(string[] args)
        {
            ///STARTUP
            Board  board;
            bool   running = true;
            string input;
            char   selection = '*';

            //continue an existing game?
            //load an existing profile?
            //enter player one name
            //enter player two name

            while (running)
            {
                Console.WriteLine("Welcome to Connect Four! \n" +
                                  "Enter 'P' to play. \n" +
                                  /*  "Enter 'S' to see player stats. \n" + */
                                  "Enter 'Q' to quit.");

                input = Console.ReadLine().ToUpper();
                try
                {
                    selection = input[0];
                }
                catch (IndexOutOfRangeException) // no input
                {
                    continue;
                }

                /*  if (selection == 'S') User.GetStats();  */
                if (selection == 'Q')
                {
                    running = false;
                }

                //initaialize game board & variables
                board = new Board();
                char piece = '+'; // '+' ==> player 1
                                  // 'o' ==> player 2

                ///PLAY CONNECT FOUR
                int gameResult = 1; //
                while (selection != 'Q')
                {
                    Console.Clear();
                    Console.WriteLine("Enter the row number (1-7) where you would like to place your peice.\n" +
                                      "Enter 'Q' to quit game.\n");

                    ///DRAW THE BOARD
                    board.Draw();
                    ///GET NEXT ACTION
                    input = Console.ReadLine().ToUpper(); // FIXME: simply pressing enter will crash program
                    try
                    {
                        selection = input[0];
                    }
                    catch (IndexOutOfRangeException) // no input
                    {
                        continue;
                    }

                    //if (input == null) continue;
                    if ((int)(selection - 48) <= 7)
                    {
                        if (board.ColumnIsFull((int)(selection - 49)))
                        {
                            Console.WriteLine("That column is full. Please try again.");
                            continue; // go get a new input
                        }
                        //perform move - returns game status
                        gameResult = board.Drop((int)(selection - 49), piece);
                        //change player peice
                        if (piece == '+')
                        {
                            piece = 'o';
                        }
                        else
                        {
                            piece = '+';
                        }
                    }

                    if (gameResult == 0)
                    {
                        Console.Clear();
                        Console.WriteLine("You win!");
                        board.Draw();
                        Console.ReadLine();
                        break;
                    }
                    else if (gameResult == 2)
                    {
                        Console.WriteLine("The game is a draw.");
                    }
                }


                Console.Clear();
            }

            Console.WriteLine("Goodbye!");
        }