Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PROGRAMMING

Options
  • 08-12-2011 5:48pm
    #1
    Registered Users Posts: 43


    Hi GUYS I NEED HELP WITH THIS PROJECT PLEASE, IM NEW IN PROGRAMMING I DONT KNOW WHERE TO START FROM.

    Develop an application that simulates a lottery draw. The lottery will randomly draw 4
    distinct numbers from the numbers 1 - 10. The player must choose 4 distinct
    numbers for each set of numbers (i.e., a line of numbers) they want to play. The
    player starts out with a credit of M €. The value for M is an input to the program, and
    you charge 50 cents per each line of numbers. For each play, the player can choose 1
    to 4 lines of numbers. If the player enters 0 as the number of lines to choose, then the
    program stops playing. The player must have enough credit to pay for the number of
    lines chosen. At the end of the game, the program displays the amount of € left and
    how much the player has won or lost in €s. Pay-out should be calculated in
    accordance with the following table:

    If the player matc 4 numbers he gets 100€
    If the player matc 3 numbers he gets €4
    If the player matc 2 numbers he gets €0.50



Comments

  • Registered Users Posts: 2,089 ✭✭✭henryporter


    Might be a help if you indicated which language you need to develop the application in...


  • Registered Users Posts: 43 kouffaley


    I THINK THE LANGUAGE IS JAVA C++ ,IM NOT SURE


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    kouffaley wrote: »
    I THINK THE LANGUAGE IS JAVA C++ ,IM NOT SURE
    Java and C++ are two different languages not the same language.

    Could you please stop writing in capitals too?

    This seems like a very advanced problem to start somebody who is new to programming off on. When you say help what do you mean?


  • Registered Users Posts: 43 kouffaley


    sorry about the caps, i need help to create the codes


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    For what exactly, are you saying you haven't any idea what the first few lines you need are?

    Do you even know which language you are to use?

    What is this for?


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    are you studying maths or programming?


  • Registered Users Posts: 300 ✭✭nickcave


    You're probably doing an undergraduate maths course and this is part of a programming module you're doing?

    Figure out what language it is and what environment you're supposed to be using as a start...


  • Closed Accounts Posts: 159 ✭✭yenoah


    I shouldn't be doing your homework for you, but I have done the question in c# and refactored the code into helper methods so hopefully it'll leave you soe work to figure it out. I have commented it all over the place so you can see what's going on.

    There is no error checking in place whatsoever, so this code assumes that the user will always input correctly etc

    For someone, who judging by your OP has not got a clue about programming, I have to say this is a very complex question and I would question if you are in the right class, or have you missed lots of lectures?
    using System;
    using System.Collections.Generic;
    
    namespace LotteryGame
    {
        class Program
        {
            private static Random rnd = new Random();
            private static int drawSize = 4;
            private static int lotterySize = 10;
            private static int[] lotteryNumbers;
    
    
    
            static void Main(string[] args)
            {
                double credit = 0;
                do
                {
                    //Menu system in a loop, it will keep reappearing until the user presses 0
                    Console.WriteLine("Lottery Game\r\n============\r\n");
                    Console.WriteLine("Enter Option");
                    Console.WriteLine("  [1] Enter Credit");
                    Console.WriteLine("  [2] Play Lottery");
                    Console.WriteLine("  [0] Quit");
    
                    //get user input
                    string inp = Console.ReadLine(); 
    
                    //decide what to do with that input
                    switch (inp)
                    {
                        //if it's 0, end the game
                        case "0": 
                            Console.WriteLine("Game Over");
                            return;
    
                        //if it's 1, enter some credit    
                        case "1":                                            
                            Console.WriteLine("Enter credit amount? ");
                            credit += double.Parse(Console.ReadLine());
                            Console.WriteLine("Total Credit now equals {0}", credit);
                            break;
    
                        //if it's 2, play the lottery
                        case "2":
                            Console.WriteLine("New Game");
    
                            //lottery draws a new set of numbers
                            lotteryNumbers = pickNumbers();
    
                            //ask user how many lines they want to play
                            Console.WriteLine("How many lines would you like to play?");
                            int gos = int.Parse (Console.ReadLine());
                            
                            //check the user has enough credit for the amount of lines they want to play (50c a line)
                            if (credit < gos * .5)
                            {
                                Console.WriteLine("Sorry, not enough credit.\r\nEither enter more credit or play less lines");
                            }
                            else
                            {
                                //everything is ok, play the lottery
                                PlayLottery(gos);                            
                            }
                            break;
                    }
    
                } while (true);
    
            }
    
    
            //This method draws the lottery numbers into an array if integers and returns that.
            private static int[] pickNumbers()
            {
                //create a new line
                int[] line = new int[drawSize];
    
                //need to fill the line with numbers
                for (int i = 0; i < line.Length ; i++)
                {
                    int pick = rnd.Next(lotterySize);
    
                    //make sure each CheckNumbers is unique, so if the number already exists in the line, pick again
                    while (Array.Exists(line, o => o == pick))
                        pick = rnd.Next(lotterySize);
    
                    //we have a new unique number, so add it to our line
                    line[i] = pick;
                }
    
                //we have the line full, return it
                return line;
            }
    
    
            //when the user plays a line, they are asked to enter their numbers seperated by a space.
            //this method takes that entire string and picks the numbers out of it returning an array of integers as a user line
            private static int[] ParseLine(string line)
            {
                //user enters say 2 4 7 9, this is actually a string "2 4 7 9". we need to convert it to numbers
                
                //split the string into smaller strings each one seperated by a space
                string[] items = line.Split(' ');
                int num = 0;
    
                //create a new array of integers to hold the line
                int[] ret = new int[items.Length];
    
                //go through each place on the line ans assign the user numbers
                for (int i = 0; i < items.Length; i++)
                {
                    //convert string item to number
                    num = int.Parse(items[i]); 
    
                    //assign that number to the array
                    ret[i] = num;
                }
    
                //return the array
                return ret;
            }
    
    
    
            //Play the game
            private static void PlayLottery(int gos)
            {
                //Create a list of lines (gos)
                List<int []> lines = new List<int[]> ();
    
                //for each line in the list, ask user to enter their choice of numbers
                for (int i  = 0; i  < gos; i ++)
                {
                    Console.WriteLine("Enter {0} numbers seperated by a space.", drawSize);               
                    lines.Add(ParseLine (Console.ReadLine ()));
                }
    
    
                //here we check the numbers to see if player won anything
                CheckNumbers (lines);
            }
    
    
    
            //check players gos (lines) against lotteryNumbers
            private static void CheckNumbers(List<int[]> lines)
            {
                int matchCount = 0;
                int lineCount = 0;
    
                //go through each line
                foreach (int[] line in lines)
                {
                    lineCount++;
                    //for each line, go through each number to check for a match against lottery numbers
                    for (int i = 0; i < line.Length; i++)
                    {
                        if (Array.Exists(lotteryNumbers, o => o == line[i]))
                            matchCount++; //if theres a match, increment a counter
                    } 
                   
                    //for each line, output a simple report
                    Console.WriteLine("Line {0}, You matched {1} numbers; You win {2}",
                        lineCount, matchCount, CalculatePrizeMoney(matchCount));
    
                    matchCount = 0;
                }
            }
    
    
            //simple prize money calculator 
            private static double CalculatePrizeMoney(int matchCount)
            {
                double prize = 0.00;
                if (matchCount == 4)
                    prize = 100;
                else if (matchCount == 3)
                    prize = 3.00;
                else if (matchCount == 2)
                    prize = 0.50;
                else
                    prize = 0.00;
    
                return prize;
            }
    
            
    
        }
    }
    
    


  • Registered Users Posts: 43 kouffaley


    Thanks a lot guys for your help, especially to Yenoah


Advertisement