patterncModerate
Simple Lotto Game
Viewed 0 times
gamesimplelotto
Problem
I have developed a Lotto Game in C for my Assignment, it allows a user to enter one of the 6 menu options.
From the menu the user would be able to view the numbers they have chosen, another option would sort the numbers in ascending order, in this case I used bubble sort.
What it does in the 1st menu is read the 6 numbers that the user has entered from the keyboard. The number must range from 1-42 and if the user enters any number out of bounds it will give an error message. It also gives an error message if the user enters a number that they has entered already.
Another option will compare the users numbers with the winning numbers and see if they match.
It will also display how many times a number has been entered, for example if the user entered 5 twice from 4 games it will display Number 5 has been selected 2 times. without exiting the program. This can be selected from the menu too.
Last but not least, the last option from the menu exits the program.
I added key features such as displaying the menu after finishing with the current option from the menu and options 2,3,4 and 5 only to be selected if option 1 has been selected from the menu.
Please take a look and let me know how this code could be improved to make this a better Lotto Game.
Here is the code:
```
#include
#include
#define SIZE 6 // Size of the array as only 6 numbers are entered.
#define MAXOUT 43 // For Frequency use when displaying the numbers.
#define ZERO 0 // Replacing 0 with Zero.
#define ARRAYSIZE 500 // Storing the frequency, 500 should be enough.
/Prototypes/
//For Option 1
//
void menu_1(int, int);
//For Option 2
//
void menu_2(int*);
//For Option 3
//
void menu_3(int*);
//For Option 4
//
void menu_4(int, int);
//For Option 5
//
void menu_5(int, int);
//For Option 6
//
void menu_6();
//Ninja fix for entering the number twice.
//
int error_dubhandle(int *user_lottonumber);
//Out of bound error fix
//
void error_handle1();
//Main
//
int
From the menu the user would be able to view the numbers they have chosen, another option would sort the numbers in ascending order, in this case I used bubble sort.
What it does in the 1st menu is read the 6 numbers that the user has entered from the keyboard. The number must range from 1-42 and if the user enters any number out of bounds it will give an error message. It also gives an error message if the user enters a number that they has entered already.
Another option will compare the users numbers with the winning numbers and see if they match.
It will also display how many times a number has been entered, for example if the user entered 5 twice from 4 games it will display Number 5 has been selected 2 times. without exiting the program. This can be selected from the menu too.
Last but not least, the last option from the menu exits the program.
I added key features such as displaying the menu after finishing with the current option from the menu and options 2,3,4 and 5 only to be selected if option 1 has been selected from the menu.
Please take a look and let me know how this code could be improved to make this a better Lotto Game.
Here is the code:
```
#include
#include
#define SIZE 6 // Size of the array as only 6 numbers are entered.
#define MAXOUT 43 // For Frequency use when displaying the numbers.
#define ZERO 0 // Replacing 0 with Zero.
#define ARRAYSIZE 500 // Storing the frequency, 500 should be enough.
/Prototypes/
//For Option 1
//
void menu_1(int, int);
//For Option 2
//
void menu_2(int*);
//For Option 3
//
void menu_3(int*);
//For Option 4
//
void menu_4(int, int);
//For Option 5
//
void menu_5(int, int);
//For Option 6
//
void menu_6();
//Ninja fix for entering the number twice.
//
int error_dubhandle(int *user_lottonumber);
//Out of bound error fix
//
void error_handle1();
//Main
//
int
Solution
Comments
Comments should tell the reader why you're doing something, not what you're doing. Comments like this are redundant:
Method Naming
Consider giving your functions more meaningful names that describe what they do. Using the example above,
Magic Numbers Vs Defines
Replacing magic numbers with meaningful constants is a good thing, however simply replacing a magic number with it's word value adds little value. Consider:
What should ZERO be used for? You're using it as a direct replacement for 0 in your code, but not always:
Another example is
Unused variables
If you change your plan for your program, don't forget to clean up after yourself. For example in
Expressive names
Consider how much easier this would be to read with more expressive names and meaningful constants:
becomes:
Be Consistent
Your using different conventions for naming your variables. You've got both
Bug when matching 3 numbers and 1 bonus
You're not returning from your matches and you're not using ifs, so if
You could either update your code flow, or change the final if to something like:
Comments should tell the reader why you're doing something, not what you're doing. Comments like this are redundant:
//For Option 6
//
void menu_6();Method Naming
Consider giving your functions more meaningful names that describe what they do. Using the example above,
menu_6. Unless you already have the menu in your head the name is meaningless. A better name might be menu_exit_game.Magic Numbers Vs Defines
Replacing magic numbers with meaningful constants is a good thing, however simply replacing a magic number with it's word value adds little value. Consider:
#define ZERO 0What should ZERO be used for? You're using it as a direct replacement for 0 in your code, but not always:
int selectOnefirst = 0;Another example is
SIZE, SIZE of what? NUM_LOTTERY_NUMBERS perhaps? Naming is hard, however if you get it right it makes your programs a lot more expressive.Unused variables
If you change your plan for your program, don't forget to clean up after yourself. For example in
menu_5, you're declaring two variables that you're not using (j and adder).Expressive names
Consider how much easier this would be to read with more expressive names and meaningful constants:
//If 6 numbers match the user wins this.
//
if(compareNum == 6)
{
printf("Congratulations, You have won the Jackpot!");
}
//If 5 numbers and the bonus matches, the user wins this.
//
if(compareNum == 5 && bonusCount == 1)
{
printf("Congratulations, You have won a Car!");
}becomes:
if(matched_numbers == NUM_LOTTERY_NUMBERS)
{
printf("Congratulations, You have won the Jackpot!");
}
if(matched_numbers == (NUM_LOTTERY_NUMBERS - 1) && matched_bonus)
{
printf("Congratulations, You have won a Car!");
}Be Consistent
Your using different conventions for naming your variables. You've got both
selectOnefirst and user_lottonumber. Pick a naming strategy and be consistent throughout the program.Bug when matching 3 numbers and 1 bonus
You're not returning from your matches and you're not using ifs, so if
compareNum == 3, both of these if clauses are matched:if(compareNum == 3 && bonusCount == 1)
{
printf("Congratulations, You have won a Cinema Ticket!");
}
if(compareNum == ZERO || compareNum == 1 || compareNum == 2 || compareNum == 3)
{
printf("Hard luck, you have not won anything.");
}You could either update your code flow, or change the final if to something like:
if(compareNum <= 2 || (compareNum == 3 && !bonusCount))Code Snippets
//For Option 6
//
void menu_6();#define ZERO 0int selectOnefirst = 0;//If 6 numbers match the user wins this.
//
if(compareNum == 6)
{
printf("Congratulations, You have won the Jackpot!");
}
//If 5 numbers and the bonus matches, the user wins this.
//
if(compareNum == 5 && bonusCount == 1)
{
printf("Congratulations, You have won a Car!");
}if(matched_numbers == NUM_LOTTERY_NUMBERS)
{
printf("Congratulations, You have won the Jackpot!");
}
if(matched_numbers == (NUM_LOTTERY_NUMBERS - 1) && matched_bonus)
{
printf("Congratulations, You have won a Car!");
}Context
StackExchange Code Review Q#134223, answer score: 10
Revisions (0)
No revisions yet.