コード例 #1
0
ファイル: Program.cs プロジェクト: landon/Playground
        static void PlayGame(Player p1, Player p2)
        {
            var b = new Board();

            b.Draw();
            var color  = -1;
            var p      = new[] { p1, p2 };
            var column = -1;

            while (true)
            {
                column = p[(color + 1) / 2].SelectMove(b, column, color);
                var m   = new Move(column, color);
                var win = b.IsWinningMove(m);
                Console.WriteLine("playing " + m.Column);
                Console.WriteLine();
                b.DoMove(m);
                b.Draw();

                if (win)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("good game");
                    break;
                }

                color = -color;
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: HollowLotus/ConnectFour
        static void Main(string[] args)
        {
            Board board = new Board();

            board.Draw();
            Console.ReadLine();
        }
コード例 #3
0
ファイル: GameState.cs プロジェクト: sma-it/game-02-connect4
 public void Draw(GameTime gameTime)
 {
     if (controller.Winner == Winner.None)
     {
         currentChip.Draw();
         board.Draw();
     }
     else if (controller.Winner == Winner.Player1)
     {
         Support.Font.PrintAt(new Vector2(-0.4f, 0), "Player 1 has won this round.", Color.White);
         Support.Font.PrintAt(new Vector2(-0.37f, -0.2f), "Press N for another game.", Color.White);
     }
     else
     {
         Support.Font.PrintAt(new Vector2(-0.4f, 0), "Player 2 has won this round.", Color.White);
         Support.Font.PrintAt(new Vector2(-0.37f, -0.2f), "Press N for another game.", Color.White);
     }
 }
コード例 #4
0
        static void Main(string[] args)
        {
            bool       turn = false;
            int        column;
            TokenState state;

            do
            {
                turn = turn ? false : true;
                Console.Clear();
                Board.Draw();

                Console.WriteLine("Player " + (turn ? 1 : 2));

                do
                {
                    column = Prompt.GetMove(1, 7);
                    state  = turn ? TokenState.X : TokenState.O;
                } while (!Board.DropToken(state, column) && !Board.IsFull());
            } while (!Board.IsWin() && !Board.IsFull());

            Console.Clear();
            Board.Draw();

            if (Board.IsFull())
            {
                Console.WriteLine("Tie!");
            }
            else
            {
                Console.WriteLine("Player " + (turn ? 1 : 2) + " wins!");
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
コード例 #5
0
ファイル: ConnectFour.cs プロジェクト: dsme1/Connect-Four
 //draws the gamepanel in the window
 private void gamePanel_Paint(object sender, PaintEventArgs e)
 {
     board.Draw(e.Graphics);
 }
コード例 #6
0
ファイル: Winner.cs プロジェクト: HollowLotus/ConnectFour
 public void ChickenDinner(Board masterboard)
 {
     //bool result = false;
     //Vertical Check
     for (int i = 35; i >= 0; i -= 7)
     {
         if ((masterboard.test[i] == "O" && masterboard.test[i + 1] == "O" && masterboard.test[i + 2] == "O" && masterboard.test[i + 3] == "O") ||
             (masterboard.test[i + 1] == "O" && masterboard.test[i + 2] == "O" && masterboard.test[i + 3] == "O" && masterboard.test[i + 4] == "O") ||
             (masterboard.test[i + 2] == "O" && masterboard.test[i + 3] == "O" && masterboard.test[i + 4] == "O" && masterboard.test[i + 5] == "O") ||
             (masterboard.test[i + 3] == "O" && masterboard.test[i + 4] == "O" && masterboard.test[i + 5] == "O" && masterboard.test[i + 6] == "O")
             )
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("play 2 wins");
             break;
         }
         else if ((masterboard.test[i] == "X" && masterboard.test[i + 1] == "X" && masterboard.test[i + 2] == "X" && masterboard.test[i + 3] == "X") ||
                  (masterboard.test[i + 1] == "X" && masterboard.test[i + 2] == "X" && masterboard.test[i + 3] == "X" && masterboard.test[i + 4] == "X") ||
                  (masterboard.test[i + 2] == "X" && masterboard.test[i + 3] == "X" && masterboard.test[i + 4] == "X" && masterboard.test[i + 5] == "X") ||
                  (masterboard.test[i + 3] == "X" && masterboard.test[i + 4] == "X" && masterboard.test[i + 5] == "X" && masterboard.test[i + 6] == "X"))
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("Play 1 wins");
             break;
         }
     }
     //Horizontal Check
     for (int i = 0; i <= 6; ++i)
     {
         if ((masterboard.test[i] == "O" && masterboard.test[i + 7] == "O" && masterboard.test[i + 14] == "O" && masterboard.test[i + 21] == "O") ||
             (masterboard.test[i + 7] == "O" && masterboard.test[i + 14] == "O" && masterboard.test[i + 21] == "O" && masterboard.test[i + 28] == "O") ||
             (masterboard.test[i + 14] == "O" && masterboard.test[i + 21] == "O" && masterboard.test[i + 28] == "O" && masterboard.test[i + 35] == "O"))
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("play 2 wins");
             break;
         }
         else if ((masterboard.test[i] == "X" && masterboard.test[i + 7] == "X" && masterboard.test[i + 14] == "X" && masterboard.test[i + 21] == "X") ||
                  (masterboard.test[i + 7] == "X" && masterboard.test[i + 14] == "X" && masterboard.test[i + 21] == "X" && masterboard.test[i + 28] == "X") ||
                  (masterboard.test[i + 14] == "X" && masterboard.test[i + 21] == "X" && masterboard.test[i + 28] == "X" && masterboard.test[i + 35] == "X"))
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("play 1 wins");
             break;
         }
     }
     //Diagonal Check
     for (int i = 0; i <= 3; ++i)
     {
         if ((masterboard.test[i] == "O" && masterboard.test[i + 8] == "O" && masterboard.test[i + 16] == "O" && masterboard.test[i + 24] == "O") ||
             (masterboard.test[i + 7] == "O" && masterboard.test[i + 15] == "O" && masterboard.test[i + 23] == "O" && masterboard.test[i + 31] == "O") ||
             (masterboard.test[i + 14] == "O" && masterboard.test[i + 22] == "O" && masterboard.test[i + 30] == "O" && masterboard.test[i + 38] == "O") ||
             (masterboard.test[i + 3] == "O" && masterboard.test[i + 9] == "O" && masterboard.test[i + 15] == "O" && masterboard.test[i + 21] == "O") ||
             (masterboard.test[i + 10] == "O" && masterboard.test[i + 16] == "O" && masterboard.test[i + 22] == "O" && masterboard.test[i + 28] == "O") ||
             (masterboard.test[i + 17] == "O" && masterboard.test[i + 23] == "O" && masterboard.test[i + 29] == "O" && masterboard.test[i + 35] == "O"))
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("play 2 wins");
             break;
         }
         else if ((masterboard.test[i] == "X" && masterboard.test[i + 8] == "X" && masterboard.test[i + 16] == "X" && masterboard.test[i + 24] == "X") ||
                  (masterboard.test[i + 7] == "X" && masterboard.test[i + 15] == "X" && masterboard.test[i + 23] == "X" && masterboard.test[i + 31] == "X") ||
                  (masterboard.test[i + 14] == "X" && masterboard.test[i + 22] == "X" && masterboard.test[i + 30] == "X" && masterboard.test[i + 38] == "X") ||
                  (masterboard.test[i + 3] == "X" && masterboard.test[i + 9] == "X" && masterboard.test[i + 15] == "X" && masterboard.test[i + 21] == "X") ||
                  (masterboard.test[i + 10] == "X" && masterboard.test[i + 16] == "X" && masterboard.test[i + 22] == "X" && masterboard.test[i + 28] == "X") ||
                  (masterboard.test[i + 17] == "X" && masterboard.test[i + 23] == "X" && masterboard.test[i + 29] == "X" && masterboard.test[i + 35] == "X"))
         {
             masterboard.p++;
             masterboard.Restart();
             masterboard.Draw();
             Console.WriteLine("play 1 wins");
             break;
         }
     }
 }
コード例 #7
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!");
        }
コード例 #8
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!");
        }