Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Visual Studio* Purpose: The purpose of this assignment is for students to becom

ID: 3913124 • Letter: #

Question

*Visual Studio*

Purpose: The purpose of this assignment is for students to become familiar with the Visual Studio IDE, compiling and executing a C# app, C# input and output commands and C# control structures. Since we have not yet studied the C# object-oriented concepts and syntax, you may code this app in the main class. All future assignments will be object-oriented.

Assignment Description:
Write a complete C# app to simulate a game of Craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is over. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then that sum is known as the player’s “point”. To win, he must keep throwing the dice until he/she “makes his point”, that is, the sum of the dice is equal to his point. The player loses if he throws a 7 before making his point. In either case, the game is over.
The player will be assigned 100 chips before beginning to play the game. Each time the game is played, the user will be asked to make a wager. If the player wins the game, s(he) receives double his wager. If s(he) loses, then the wager is lost. The game is played until the player no longer wishes to play or until the chips are all used. At this time, display the total number of chips remaining.

Requirements:
• After the first roll of the pair of dice, display the outcome as to whether the player won, lost, or the value of the player’s “point”.
• If the player continues to roll to make his/her point, display a message as to whether he/she won or lost after he/she has either made is “point” or rolled a 7.
• When a game is finished, the program should ask if the player wants to play another. It should either play another game or terminate, based on the user response.
• When the player does not wish to play anymore or all the chips are used, display the number of chips remaining.
• Be sure to use clear instructions to the user. You may design your output as you wish as long at it is clear to the user.
• Include comments to explain the purpose of the code.
• Include comments defining the purpose of the code.
• Include your name as a comment at the beginning of the program.
• Use all C# naming conventions as well as indentation for if statements and loops.
• Save your program as Craps

NOTE: Remember that all programs will be checked for duplicates by computer software. Be sure to adhere to the University’s Policy on Academic Integrity as discussed in class. Programming assignments are to be written individually.

Explanation / Answer

Below is the solution:

code:

using System;
using System.Collections.Generic;

namespace gameofCrap
{
    class Program
    {
        //class variable to ensure random numbers appear when finding random dice values
        static Random randomNumbers = new Random();

        //declaring class variables for end game statistics
      
        static int highestNoOfRolls = 1;
        static int lowestNoOfRolls = 0;
        static int mostCommonRoll = 0;
        static int noOfWins = 0;
        static int noOfLosses = 0;

        static void Main(string[] args)
        {
            DisplayIntro();
        }

        private static void DisplayIntro()
        {
            Console.WriteLine(" The Game Craps ");

            Console.WriteLine("How to play Craps: ");

            Console.WriteLine("You enter the number of games you want to play.");
            Console.WriteLine("In each game, the shooter rolls two dice.");
            Console.WriteLine("If the numbers on the dice add up to 2, 3 or 12, the shooter looses.");
            Console.WriteLine("If the numbers on the dice add up to 7 or 11, the shooter wins.");
            Console.WriteLine("If the numbers on the dice add up to 4, 5, 6, 8, 9 or 10, that sets the points.");
            Console.WriteLine("In the latter case, the shooter continues to roll until the numbers match the points, then the shooter wins.");
            Console.WriteLine("Or, if a 7 is rolled, the shooter looses. ");

            //call on method to commence asking the number of games a player would like to play
            ProcessNumberofGames();
        }

        private static void ProcessNumberofGames()
        {
            int numberOfGames = 0;
            string continueGame = "";
            Console.WriteLine("How many games of Craps do you want to play?");

            try
            {
                numberOfGames = Int32.Parse(Console.ReadLine());

                if (numberOfGames <= 0)
                {
                    NumberofGamesEntryError();
                }
                else
                {
                    PlayGame(numberOfGames);
                    Console.WriteLine();
                    Console.WriteLine("Press 'y' to continue playing");
                    continueGame = Console.ReadLine();
                    if (continueGame == "y" || continueGame == "Y")
                    {
                        ClearVariableValuesForNewGame();
                        DisplayIntro();
                    }
                    else
                    {
                        Console.WriteLine("Exiting.");
                        Console.ReadKey();
                    }
                }
            }
            catch
            {
                NumberofGamesEntryError();
            }
        }

        private static void NumberofGamesEntryError()
        {
            string agreeToContinue = "";
            Console.WriteLine("That entry was not recognized. Press 'y' if you wish to continue, or press any other key to exit.");
            agreeToContinue = Console.ReadLine();
            if (agreeToContinue == "y" || agreeToContinue == "Y")
                DisplayIntro();
            else
            {
                Console.WriteLine("Exiting");
                Console.ReadKey();
            }
        }

        //commence the actual game
        private static void PlayGame(int numberOfGames)
        {
            int dice1 = 0;
            int dice2 = 0;
            int sum = 0;

            for (int i = 0; i < numberOfGames; i++)
            {
                //a blank line for display purposes
                Console.WriteLine();

                dice1 = RollDice();
                dice2 = RollDice();

                //sum of dice1 and dice2
                sum = dice1 + dice2;
              
                Console.WriteLine("For round " + (i + 1) + ", the first dice has the value: " + dice1 + " + " + dice2 + " : "+sum);

                //process the dice results
                ProcessDiceResults(sum);

                //display newline
                Console.WriteLine();
            }
            DisplayEndGameStatistics();
        }

        //process dice results by calling on relevant method using a switch
        private static void ProcessDiceResults(int sum)
        {
            switch (sum)
            {
                //if the sum of the dice1 and dice2 is 2,3,12 shooter looses
                case 2:
                case 3:
                case 12:
                    ShooterLoses();
                    break;
                //if the sum of the dice1 and dice2 is 7,11 shooter wins
                case 7:
                case 11:
                    ShooterWins();
                    break;
                //if the sum of the dice1 and dice2 is 4,5,6,8,9,10 pointround
                case 4:
                case 5:
                case 6:
                case 8:
                case 9:
                case 10:
                    PointsRound(sum);
                    break;
                default:
                    break;
            }
        }

        //shooter loses method
        private static void ShooterLoses()
        {
            Console.WriteLine("Sorry, shooter looses this round.");
            //calculation for end of game statistic
            NoOfLosses();
        }
        //shooter wins method
        private static void ShooterWins()
        {
            Console.WriteLine("Shooter wins this round!");
            //calculation for end of game statistic
            NoOfWins();
        }

        //point round method
        private static void PointsRound(int sum)
        {
            //set the pointsum to zero dice1 and dice is zero, round counter to zero
            int pointSum = 0;
            int dice1 = 0;
            int dice2 = 0;
            int roundsCounter = 1;
            Boolean matchfound = false;

            Console.WriteLine("Commencing points round: Rolling again Shooter wins the round if the dice sum matches the points value " + sum + " before a 7 is rolled. Otherwise, the shooter loses.");
            Console.WriteLine("");

            //match found loop untill matchfound set to true
            while (matchfound == false)
            {
                Console.WriteLine();
                dice1 = RollDice(); //set the dice1 value
                dice2 = RollDice(); //set the dice2 value
                Console.WriteLine("Points round:");
                Console.WriteLine("The first dice has the value: " + dice1 + " + " + dice2 + ".");

                //adding the dice1 and dice2
                pointSum = dice1 + dice2;
                //chek if the point is 7 shooter loses
                if (pointSum == 7)
                {
                    Console.WriteLine("This gives the sum of: " + pointSum);
                    ShooterLoses();
                    matchfound = true;
                }
                //check if the point is sum value shooter wins
                else if (pointSum == sum)
                {
                    Console.WriteLine("This gives the sum of: " + pointSum);
                    ShooterWins();
                    matchfound = true;
                }
                else
                    Console.WriteLine("This gives the sum of: " + pointSum + ". This does not match the points value " + sum + " or a 7. Rolling again.");
                Console.ReadLine();

                //collect and manipulate data for end of game statistics
                roundsCounter++;
            }

          
        }

        //set the all varaible to 0 or default value initialize to the game
        public static void ClearVariableValuesForNewGame()
        {
            highestNoOfRolls = 1;
            lowestNoOfRolls = 0;
            mostCommonRoll = 0;
            noOfWins = 0;
            noOfLosses = 0;

        }
        //roll dice
        static int RollDice()
        {
            int dice = 0;
            dice = randomNumbers.Next(1, 7); //random number from 1 to 7
            return dice;
        }

        //no of wins method
        static void NoOfWins()
        {
            //count the number of wins
            noOfWins = noOfWins + 1;
        }

        //no of losses
        static void NoOfLosses()
        {
            //counts the no of loses
            noOfLosses = noOfLosses + 1;
        }

        //game statisrics method
        static void DisplayEndGameStatistics()
        {
            //prints the game statistics
            Console.WriteLine("The highest number of rolls was: " + highestNoOfRolls);
            Console.WriteLine("The lowest number of rolls was: " + lowestNoOfRolls);
            Console.WriteLine("The most common roll was: " + mostCommonRoll);
            Console.WriteLine("Total games you won: " + noOfWins);
            Console.WriteLine("Total games you lost: " + noOfLosses);
        }
    }
}

sample output:


The Game Craps

How to play Craps:

You enter the number of games you want to play.
In each game, the shooter rolls two dice.
If the numbers on the dice add up to 2, 3 or 12, the shooter looses.
If the numbers on the dice add up to 7 or 11, the shooter wins.
If the numbers on the dice add up to 4, 5, 6, 8, 9 or 10, that sets the points.
In the latter case, the shooter continues to roll until the numbers match the points, then the shooter wins.
Or, if a 7 is rolled, the shooter looses.

How many games of Craps do you want to play?
2

For round 1, the first dice has the value: 5 + 3 : 8
Commencing points round: Rolling again Shooter wins the round if the dice sum matches the points value 8 before a 7 is rolled. Otherwise, the shooter loses.


Points round:
The first dice has the value: 3 + 2.
This gives the sum of: 5. This does not match the points value 8 or a 7. Rolling again.


Points round:
The first dice has the value: 4 + 6.
This gives the sum of: 10. This does not match the points value 8 or a 7. Rolling again.


Points round:
The first dice has the value: 5 + 2.
This gives the sum of: 7
Sorry, shooter looses this round.

For round 2, the first dice has the value: 5 + 1 : 6
Commencing points round: Rolling again Shooter wins the round if the dice sum matches the points value 6 before a 7 is rolled. Otherwise, the shooter loses.


Points round:
The first dice has the value: 2 + 6.
This gives the sum of: 8. This does not match the points value 6 or a 7. Rolling again.


Points round:
The first dice has the value: 1 + 5.
This gives the sum of: 6
Shooter wins this round!


The average number of rolls per game was: 0
The highest number of rolls was: 1
The lowest number of rolls was: 0
The most common roll was: 0
Total games you won: 1
Total games you lost: 1

Press 'y' to continue playing
y

The Game Craps

How to play Craps:

You enter the number of games you want to play.
In each game, the shooter rolls two dice.
If the numbers on the dice add up to 2, 3 or 12, the shooter looses.
If the numbers on the dice add up to 7 or 11, the shooter wins.
If the numbers on the dice add up to 4, 5, 6, 8, 9 or 10, that sets the points.
In the latter case, the shooter continues to roll until the numbers match the points, then the shooter wins.
Or, if a 7 is rolled, the shooter looses.

How many games of Craps do you want to play?
3

For round 1, the first dice has the value: 3 + 3 : 6
Commencing points round: Rolling again Shooter wins the round if the dice sum matches the points value 6 before a 7 is rolled. Otherwise, the shooter loses.


Points round:
The first dice has the value: 3 + 2.
This gives the sum of: 5. This does not match the points value 6 or a 7. Rolling again.


Points round:
The first dice has the value: 5 + 2.
This gives the sum of: 7
Sorry, shooter looses this round.

For round 2, the first dice has the value: 3 + 4 : 7
Shooter wins this round!


For round 3, the first dice has the value: 3 + 4 : 7
Shooter wins this round!

The average number of rolls per game was: 0
The highest number of rolls was: 1
The lowest number of rolls was: 0
The most common roll was: 0
Total games you won: 2
Total games you lost: 1

Press 'y' to continue playing

Exiting.