Beispiel #1
0
        private void btnEndTurn_Click(object sender, RoutedEventArgs e)
        {
            // Add current score to the players score and update both displays
            currentPlayer.Score = currentPlayer.Score + currentScore;
            txtP1Score.Text = player1.Score.ToString();
            txtP2Score.Text = player2.Score.ToString();

            // Reset current score ready for the next players turn
            currentScore = 0;
            txtCurrentScore.Text = currentScore.ToString();

            IsGameOver();

            // Swap the players around ready for the next turn
            if (currentPlayer.Equals(player1))
            {
                currentPlayer = player2;
            }
            else
            {
                currentPlayer = player1;
            }

            txtPlayerName.Text = currentPlayer.Name;
        }
Beispiel #2
0
        public MainWindow()
        {
            InitializeComponent();

            currentPlayer = player1;
            txtPlayerName.Text = currentPlayer.Name;
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Let's play Pig!");

            int die = 0;
            Player playerOne = new Player();
            Player playerTwo = new Player();

            Console.WriteLine("Player One, what is your name?");
            playerOne.Name = Console.ReadLine().ToUpper(); //assigns name

            Console.WriteLine("Hello, {0:S}! Player Two, what is your name?", playerOne.Name);
            playerTwo.Name = Console.ReadLine().ToUpper(); //assigns name

            Console.WriteLine("Hello, {0:S}! We are playing to 100. {1:S} goes first. Let's start!", playerTwo.Name, playerOne.Name);

            //the game itself
            playerOne.playing = true;
            string userChoice;

            //looping until both scores are 100 or greater
            while (playerOne.Total < 100 && playerTwo.Total < 100)
            {

                //player one's turn
                if (playerOne.playing)
                {
                    Console.WriteLine("\n{0:S} is playing.", playerOne.Name);

                    playerOne.printTotal();

                    Console.WriteLine("Rolling die...");
                    die = 1;
                    Console.WriteLine("You rolled a {0:D}!\n", die);

                    //adding die number to round total
                    playerOne.Round += die;

                    if (die == 1)
                    {
                        Console.WriteLine("Skip a turn!\n");
                    }
                    else do
                    {
                        HoldOrRoll:
                            Console.WriteLine("Hold or roll?");
                            userChoice = Console.ReadLine().ToLower();

                        switch (userChoice)
                        {
                            case "hold":
                                Console.WriteLine("You hold your turn. You add {0:D} to your total.", playerOne.Round);
                                playerOne.Total += playerOne.Round;
                                playerOne.printTotal();
                                break;

                            case "roll":
                                die = playerOne.rollDie();

                                if (die == 1)
                                {
                                    Console.WriteLine("You rolled a one. Skip a turn!");
                                    playerOne.playing = false;
                                }
                                else
                                {
                                    Console.WriteLine("You rolled a {0:D}.", die);
                                    playerOne.Round += die;
                                    playerOne.printRound();
                                    goto HoldOrRoll;
                                }
                                break;

                            default:
                                Console.WriteLine("I do not understand.");
                                goto HoldOrRoll;
                        }

                        playerOne.Round = 0;
                        playerOne.playing = false;
                        playerTwo.playing = true;
                    } while (playerOne.playing);

                }

                //player two's turn
                else
                {
                    Console.WriteLine("\n{0:S} is playing.", playerTwo.Name);

                    playerTwo.printTotal();

                    Console.WriteLine("Rolling die...");
                    die = playerTwo.rollDie();
                    Console.WriteLine("You rolled a {0:D}!\n", die);

                    playerTwo.Round += die;

                    if (die == 1)
                    {
                        Console.WriteLine("Skip a turn!\n");
                    }
                    else do
                        {
                        HoldOrRoll:
                            Console.WriteLine("Hold or roll?");
                            userChoice = Console.ReadLine().ToLower();

                            switch (userChoice)
                            {
                                case "hold":
                                    Console.WriteLine("You hold your turn. You add {0:D} to your total.", playerTwo.Round);
                                    playerTwo.Total += playerTwo.Round;
                                    playerTwo.printTotal();
                                    break;

                                case "roll":
                                    die = playerTwo.rollDie();

                                    if (die == 1)
                                    {
                                        Console.WriteLine("You rolled a one. Skip a turn!");
                                        playerTwo.playing = false;
                                    }
                                    else
                                    {
                                        Console.WriteLine("You rolled a {0:D}.", die);
                                        playerTwo.Round += die;
                                        playerTwo.printRound();
                                        goto HoldOrRoll;
                                    }
                                    break;

                                default:
                                    Console.WriteLine("I do not understand.");
                                    goto HoldOrRoll;
                            }

                            playerTwo.Round = 0;
                            playerTwo.playing = false;
                            playerOne.playing = true;
                        } while (playerTwo.playing);
                }
            }

            Console.ReadLine();
        }
Beispiel #4
0
        private void IsGameOver()
        {
            if (currentPlayer.Score >= 10)
            {
                MessageBox.Show("Congratulations " + currentPlayer.Name + "! You have won!", "Game Over", MessageBoxButton.OK);
                currentScore = 0;
                txtCurrentScore.Text = currentScore.ToString();
                currentPlayer = player1;
                txtPlayerName.Text = currentPlayer.Name;
                player1.Score = 0;
                player2.Score = 0;
                txtP1Score.Text = player1.Score.ToString();
                txtP2Score.Text = player2.Score.ToString();

            }
        }
Beispiel #5
0
        private void CheckResults()
        {
            // Check if either dice rolled a 1. Total up and display scores if they didn't
            // but end game if they did.

            if (dice1.CurrentFace == 1 || dice2.CurrentFace == 1)
            {
                MessageBox.Show("Unlucky!", "Turn Over", MessageBoxButton.OK);
                currentScore = 0;
                txtCurrentScore.Text = currentScore.ToString();

                // Change player for the next turn
                if (currentPlayer.Equals(player1))
                {
                    currentPlayer = player2;
                }
                else
                {
                    currentPlayer = player1;
                }

                txtPlayerName.Text = currentPlayer.Name;
            }
            else
            {
                // Add dice values to current score and update display
                currentScore = currentScore + (dice1.CurrentFace + dice2.CurrentFace);
                txtCurrentScore.Text = currentScore.ToString();
            }
        }