Click here to Skip to main content
15,889,795 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HighLow
{
    class Program
    {
        static void Main(string[] args)
        {
            int numPoints;
            printRules();
            do
            {
                numPoints = 1000;
                getWager(numPoints);
                int Prediction = getPrediction();
                if (isWin(Prediction) == true)
                {
                    AddPoints(numPoints);
                    Console.WriteLine("You Win!!!!!");
                }
                else
                {
                    Console.WriteLine("You Lose!");
                }
            } while (numPoints >= 0);
            Console.ReadLine();
        }
        /* Precondition: None
        * This method prints out the welcome message and rules of the game.
        * Postcondition: The message has been printed to the screen.
        */
        static void printRules()
        {
            Console.WriteLine("High Low Game!");
            Console.WriteLine("Welcome to the Game!");
            Console.WriteLine("RULES:");
            Console.WriteLine("Numbers 1 through 6 are low.\nNumbers 8 through 13 are high.\nNumber 7 is neither high nor low.");
            Console.WriteLine("You have 1000 points to wager. BEGIN!");
        }

        /* Precondition: numPoints is a valid integer
          * getWager asks the player to enter their wager.  It checks the validity of
          * the wager (a valid wager cannot be less than zero nor can it be more
          * points than the player currently has).  The method then returns the valid
        * wager.
        * Postcondition: A valid wager is returned.
        */
        static int getWager(int numPoints)
        {
            int wager;
            int timesPlayed = 0;
            numPoints = 1000;
            Console.Write("Please place you're wager");
            wager = int.Parse(Console.ReadLine());
            do
            {
                if (wager > numPoints)
                {
                    Console.WriteLine("Not enough funds.");
                    Console.Write("Enter a smaller wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else if (wager == 0)
                {
                    Console.WriteLine("You cannot bet 0 points.");
                    Console.Write("Please place a higher wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else if (wager < 0)
                {
                    Console.WriteLine("You cannot wager a negative amount of points");
                    Console.Write("Enter a higher wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else
                {
                    numPoints = numPoints - wager;
                    Console.WriteLine("You currently have " + numPoints + " points left.");
                    timesPlayed--;
                }
            } while (timesPlayed == 0);
            return numPoints;
        }

        /* Precondition: None.
          * getPrediction gets the player's prediction for high or low.  The
          * prediction must be either high (1) or low (0).  Validity checks are done
          * to ensure the prediction is within that range.  Hence, a 0 or 1 is
        * returned from the method.
        * Postcondition: 0 or 1 is returned.
        */
        static int getPrediction()
        {
        retry:
            int prediction = -1;
            Console.Write("enter your prediction for a high (1) or low (0) number: ");
            try
            {
                prediction = int.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Please input numbers only.");
            }
            if (prediction != 0 && prediction != 1)
            {
                goto retry;
            }
            return prediction;

        }
        static int AddPoints(int NumPoints)
        {
            return NumPoints * 2;
        }

        /* Precondition: thePrediction and num are valid integers
          * isWin will return a boolean as to whether or not the player has won their
        * bet. A win is defined as the following:

          * if the player bet on low (0) and the number is less than 7, win is true
          * if the player bet on high (1) and the number is greater than 7, win is
        * true
        * otherwise, win is false
        *
          * Postcondition: True is returned if the player has won; false is returned
        * otherwise.
        */
        static Boolean isWin(int thePrediction)
        {
            Random Rand = new Random();
            int Guess = Rand.Next(1, 14);
            if (Guess > 7 && thePrediction == 0 || Guess < 7 && thePrediction == 1)
            {
                return false;
            }
            else if (Guess > 7 && thePrediction == 1 || Guess < 7 && thePrediction == 0)
            {
                return true;
            }
            return false;
        }

        /* Precondition: The game is over.
          * finalMessage prints out 1 of two messages.  One is if the player has no
          * points left. A message indicating so is printed.  Otherwise, the amount
        * of points is printed to the screen, with a farewell message.
          * Postcondition: A message indicating no points OR points remaining is
        * printed to the screen.
        */
        static void finalMessage(int thePoints)
        {

            {
                if (thePoints <= 0)
                {
                    Console.WriteLine("Not enough funds.");
                }
            }
        }
    }
}


The only problem for some reason it doesn't deduct points if you play more than once and Im not sure what to write for the last method.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 23:42pm    
No, no, this cannot help. Please explain what this code is supposed to do, what it does correctly and what is not, what you fail to achieve.
--SA
Member 8349150 21-Nov-11 23:43pm    
High-Low Game
In the High-Low game, a player begins with a score of 1000 which can be used to wager on the results of a random number selection. The computer generates a random number between 1 and 13, and the player is asked to guess if the number is low (1-6) or high (8-13). Seven is considered neither high nor low, and is an automatic loss for the player. If the player guesses correctly, then the number of points wagered is doubled and added to the person’s original amount. The player is then asked if they want to play again. The game continues until either the player decides to quit, in which case a farewell message is printed with the player’s final points, or until the player has no points left. Your task is to create a HighLow Console application which implements this game. Output should reflect the following:
High Low Game!
Welcome to the Game!

RULES:
Numbers 1 through 6 are low.
Numbers 8 through 13 are high.
Number 7 is neither high nor low.

You have 1000 points.

Enter points to risk: 500

Predict (1=High, 0=Low): 0
The computer generated a 4.
You win!
Play again? (y/n) y

You have 2000 points.

The first step you need to take is to open HighLowTemplate.cs from the shared drive (s:\CSharp) and resave it into your directory with the name HighLow.cs. It has all of the methods that you will need to define. Here is a paste from the file:

/* Precondition: None
* This method prints out the welcome message and rules of the game.
* Postcondition: The message has been printed to the screen.
*/
static void printRules()
{
//Type code.
}

/* Precondition: numPoints is a valid integer
* getWager asks the player to enter their wager. It checks the validity of
* the wager (a valid wager cannot be less than zero nor can it be more
* points than the player currently has). The method then returns the valid
* wager.
* Postcondition: A valid wager is returned.
*/
static int getWager(int numPoints) {
//Type code
}

/* Precondition: None.
* getPrediction gets the player's prediction for high or low. The
* prediction must be either high (1) or low (0). Validity checks are done
* to ensure the prediction is within that range. Hence, a 0 or 1 is
* returned from the method.
* Postcondition: 0 or 1 is returned.
*/
static int getPrediction() {
//Type code
}

/* Precondition: thePrediction and num are valid integers
* isWin will return a boolean as to whether or not the player has won their
* bet. A win is defined as the following:

* if the player bet on low (0) and the number is less than 7, win is true
* if the player bet on high (1) and the number is greater than 7, win is
* true
* otherwise, win is false
*
* Postcondition: True is returned if the player has won; false is returned
* otherwise.
*/
static Boolean isWin(int thePrediction, int num) {
//Type code
}

/* Precondition: The game is over.
* finalMessage prints out 1 of two messages. One is if the player has no
* points left. A message indicating so is printed. Otherwise, the amount
* of points is printed to the screen, with a farewell message.
* Postcondition: A message indicating no points OR points remaining is
* printed to the screen.
*/
static void finalMessage(int thePoints)
{
//Type code
}
Sergey Alexandrovich Kryukov 22-Nov-11 0:09am    
Please move this comment to the main question as you can use code formatting, and because it belongs there. It will help you to get answers.
--SA
LanFanNinja 22-Nov-11 0:50am    
Check my solution.
Anuja Pawar Indore 22-Nov-11 1:14am    
I don't understand, why you are posting same question twice, and in none you are trying to explain what is wrong. You have just pasted the code in the comment section. Please be short and specific if you need right guidance.

1 solution

Ok as I said before I am very tired but I decided to look over your code any way. Here is your code with my modifications. If you have additional question post them and I will try to answer them tonight or in a few hours when I wake back up.

Edit: Fixed AddPoints method.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HighLow
{
    class Program
    {
        static void Main(string[] args)
        {
            int numPoints = 1000;
            printRules();
            do
            {
                //numPoints = 1000;
                int wager = getWager(numPoints);
                int Prediction = getPrediction();
                if (isWin(Prediction) == true)
                {
                    //AddPoints(numPoints);
                    numPoints = AddPoints(wager, numPoints);
                    Console.WriteLine("You Win!!!!!");
                }
                else
                {
                    numPoints = RemovePoints(wager, numPoints);
                    Console.WriteLine("You Lose!");
                }
            } while (numPoints >= 0);
            Console.ReadLine();
        }
        /* Precondition: None
        * This method prints out the welcome message and rules of the game.
        * Postcondition: The message has been printed to the screen.
        */
        static void printRules()
        {
            Console.WriteLine("High Low Game!");
            Console.WriteLine("Welcome to the Game!");
            Console.WriteLine("RULES:");
            Console.WriteLine("Numbers 1 through 6 are low.\nNumbers 8 through 13 are high.\nNumber 7 is neither high nor low.");
            Console.WriteLine("You have 1000 points to wager. BEGIN!");
        }

        /* Precondition: numPoints is a valid integer
          * getWager asks the player to enter their wager.  It checks the validity of
          * the wager (a valid wager cannot be less than zero nor can it be more
          * points than the player currently has).  The method then returns the valid
        * wager.
        * Postcondition: A valid wager is returned.
        */
        static int getWager(int numPoints)
        {
            int wager;
            int timesPlayed = 0;
            //numPoints = 1000;
            Console.Write("Please place you're wager");
            wager = int.Parse(Console.ReadLine());
            do
            {
                if (wager > numPoints)
                {
                    Console.WriteLine("Not enough funds.");
                    Console.Write("Enter a smaller wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else if (wager == 0)
                {
                    Console.WriteLine("You cannot bet 0 points.");
                    Console.Write("Please place a higher wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else if (wager < 0)
                {
                    Console.WriteLine("You cannot wager a negative amount of points");
                    Console.Write("Enter a higher wager: ");
                    wager = int.Parse(Console.ReadLine());
                }
                else
                {
                    //numPoints = numPoints - wager;
                    Console.WriteLine("You currently have " + numPoints + " points left.");
                    timesPlayed--;
                }
            } while (timesPlayed == 0);
            //return numPoints;
            return wager;
        }

        /* Precondition: None.
          * getPrediction gets the player's prediction for high or low.  The
          * prediction must be either high (1) or low (0).  Validity checks are done
          * to ensure the prediction is within that range.  Hence, a 0 or 1 is
        * returned from the method.
        * Postcondition: 0 or 1 is returned.
        */
        static int getPrediction()
        {
        retry:
            int prediction = -1;
            Console.Write("enter your prediction for a high (1) or low (0) number: ");
            try
            {
                prediction = int.Parse(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Please input numbers only.");
            }
            if (prediction != 0 && prediction != 1)
            {
                goto retry;
            }
            return prediction;

        }
        static int AddPoints(int wager, int NumPoints)
        {
            //return NumPoints * 2;
            return NumPoints += wager * 2;
        }

        static int RemovePoints(int wager, int numPoints)
        {
            return numPoints -= wager;
        }

        /* Precondition: thePrediction and num are valid integers
          * isWin will return a boolean as to whether or not the player has won their
        * bet. A win is defined as the following:

          * if the player bet on low (0) and the number is less than 7, win is true
          * if the player bet on high (1) and the number is greater than 7, win is
        * true
        * otherwise, win is false
        *
          * Postcondition: True is returned if the player has won; false is returned
        * otherwise.
        */
        static Boolean isWin(int thePrediction)
        {
            Random Rand = new Random();
            int Guess = Rand.Next(1, 14);
            if (Guess > 7 && thePrediction == 0 || Guess < 7 && thePrediction == 1)
            {
                return false;
            }
            else if (Guess > 7 && thePrediction == 1 || Guess < 7 && thePrediction == 0)
            {
                return true;
            }
            return false;
        }

        /* Precondition: The game is over.
          * finalMessage prints out 1 of two messages.  One is if the player has no
          * points left. A message indicating so is printed.  Otherwise, the amount
        * of points is printed to the screen, with a farewell message.
          * Postcondition: A message indicating no points OR points remaining is
        * printed to the screen.
        */
        static void finalMessage(int thePoints)
        {

            {
                if (thePoints <= 0)
                {
                    Console.WriteLine("Not enough funds.");
                }
            }
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900