CSE240 – Assignment 2
Input/Output, File IO and more
Topics:
Create and use variables
Use printf and scanf/cout and cin to work with input and output
Use simple File I/O
Usedcontrol structures to control the flow of logic
Work with math and random numbers
Functions
Macros
Create C/C++ code from scratch
Description
The aim of this assignment is to make sure you understand Input and Output and basic control structures in C/C++.
This assignment has two major parts. Both will be in the same code file.
Use the following Guidelines:
Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
Keep identifiers to a reasonably short length.
Adopt a coding standard and be consistent. Suggestion:
o Use upper case for constants. Use title case (first letter is upper case) for classes. Use camelCase for all other identifiers (variables, methods, objects).
Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
Use white space to make your program more readable.
Important Note:
All submitted assignments must begin with the descriptive comment block. To avoid losing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assignment to assignment.
/*
Author:
Date:
Description:
*/
February 2020
Programming Assignment:
Instructions:
This assignment has two major parts: Part 1 – A mini-games collection, Part 2 – Macros vs. Functions
This assignment is going to be a collection of simple input driven “mini-games.” Each game will consistent of a simple mechanic pitting the user against a simulated opponent. The opponent will just use randomized moves.
You will keep score for the player vs the opponent.
This assignment has multiple parts. Create the code from scratch and create all required functions.
You may use C or C++ for this assignment. Your code file should be either:
You should not use any techniques we haven’t covered yet. With the exception of creating character pointers and/or character arrays for reading the name from your file and the FILE* for C File I/O. Beyond that do not use arrays, pointers, structures or object orientation in this assignment. You must use functions.
Specifications:
Part 0 – Main Menu (5% of specification)
Show this menu and make it function properly. If the user inputs something incorrect, provide an error message to the user and loop the menu and prompt.
Create a function to run each item in the menu.
Welcome to the CSE240 Assignment 2!
Menu:
1 – CSE240 Mini-Game Collection
2 – Macros vs Functions Throw-down
0 – Exit
Choose an option:
Part 1 – Mini-Games (80% of specifications)
Set Up (12% of specifications)
The player will play against a simulated opponent. The opponent will be named by reading a name from a provided text file. You will pick the name by generating a random number between 0 and the number of names in the file and reading that number name in the file. The sample file provided will have 100 names.
IE: If the code randomly generates 23 it would Delicia and store that as the opponent’s name.
Note: the longest name in the provided file is simply create a character array size 10 to fit
read and skip 22 names then read 9 characters long, so you can
this you can set your array size to 30 instead of 10 to cover your bases.
The user will be presented with a welcome and be prompted to choose how many rounds they want to play. The input MUST be odd so a winner can be decided. Present an error message and loop the question if the user enters a negative number or even number
Welcome to CSE240 Mini-Game Collection!
I am your opponent Delicia
How many rounds should we play? (Choose an odd number)
>
… after game 1 …
The score is:
Player: 0
Delicia: 1
If you want, you can continue right to the next game, or get player input to continue/quit.
any name: char name[10];
Take the filename and number of names as command line arguments. If you do
Extra Credit Opportunity (+3 points):
After choosing the number of rounds the player will play a random set of mini- games. Between each game show the player’s score and the opponents score. After each game you should randomly pick which game is next. Note: yes, this means the player will play duplicate games and sometimes the same game back to back.
If the player or the opponent gain the majority of victories, end the game. For example:2out3,4outof7,etc. victory=(#ofrounds+1)/2
Required Functions:
Random numbers in a range are going to be used frequently. Write a function to get a random number between a range.
Function signature: int RandomInRange(int min, int max) Mini-Games (Each mini-game is worth 17% of specifications)
A – Evens or Odds
Write a function to drive the Even Odds game
The player will choose either even or odd. Then then enter a number. The computer will give a random number. Add the two numbers. If the total’s evenness/oddness matches the player’s guess – then the player wins! You can set the number limit. In real life the game is played with fingers – so feel free the restrict number choices between 0 and 5
Sample output:
Even/Odd game!
Guess even or odd! (0 for even, 1 for odd): 1
Enter a number (between 0 and 100): 23
Delicia picked 57
23+57 = 80 – EVEN!
Delicia wins the round!
Other Required Function:
Write a function to check if a number is even. It should return a 1 if true or 0 if false. If coding in C++, you can use a Boolean.
int IsEven(int number)
–or–
bool IsEven(int number)
B – Rock, Paper, Scissors
Write a function to drive a Rock, Paper, Scissors game. The player will choose Rock, Paper or Scissors. The opponent will pick a random choice. You can use a simple integer driven menu, make sure to loop the menu on bad input
Paper beats Rock
Rock beats Scissors
Scissors beasts Paper
Announce the winner.
Sample Output:
Rock, Paper, Scissors game! Choose a throw!
1. Rock
2. Paper
3. Scissors >>
C – I’m thinking of a number between …
Write a function to run the number guessing game. Have the opponent “think of a number” between two values. The two values should be at least 30 apart, but no greater than 100. Use your random between function from earlier.
Prompt the user to guess a number. Give feedback if the number the opponent is “thinking of” is higher or lower.
The player gets a maximum of 5 guesses. If the player gets the number within 5 guesses the win otherwise the opponent wins.
Sample Output:
Number Guessing game!
Player I’m thinking of a number between 350 and 450!
You have 5 guesses!
What’s guess #1? 400
My number is lower!
What’s guess #2?
…
D – Dice Roll Showdown
Create a function for a simple high number wins game. Ask the user how many and what size dice they want to roll. Then have the opponent roll the same number of dice. Total the player’s dice and the opponent’s dice. High value wins. Report each roll value and the total and announce who won.
Other required function:
Write a function called RollDie which takes the number of sides as a parameter and returns the random result.
int RollDie(int sides)
Use it appropriately in your algorithm.
Sample Output:
Dice Roll Showdown!
How many sides do the dice have (input a positive number): 6 How many dice will each player roll? 2
Player 1 Rolled:
3
6
Total: 9
Delicia Rolled
2
5
Total: 7
Player Wins!
Part 2 – Macro’s vs. Functions Throw-down (15% spec)
Review C_part1.pptx and/or text section 1.4.2
Reminder:
Macros are available in most high-level programming languages. The body of a macro is simply used to replace a macro-call during the preprocessing stage. A macro introduces a “true inline” function that is normally more efficient than an “out-line” function. However, macros suffer from the side-effect, unwanted, or unexpected modifications to variables.
Macros should be used cautiously. The main purpose of the following programs are to demonstrate the differences between a function and a macro.
Other purposes include demonstrating the differences between different programming environments, and learning different ways of writing comments, formatted input and output, variable declaration and initialization, unary operation ++, macro definition/call, function definition/call, if-then-else and loop structures, etc.
Create these functions in your code
Write four macros that re-implement the above four functions and name them: sub_macro, cube_macro, min_macro and odd_macro
Write a function (that will be called from your earlier menu) to test and compare the results of running the functions vs the macro.
Reset num1 to 10 and num2 to 17 between each function and macro call.
Order to call things in:
int num1 = 10, num2 = 17;
subf(num1, num2);
sub_macro(num1, num2);
subf(num1++, num2–);
sub_macro(num1++, num2–);
cubef(num1);
cube_macro(num1);
cubef(–num1);
cube_macro(–num1);
minf(num1, num2);
min_macro(num1, num2);
minf(–num1, –num2);
min_macro(–num1, –num2);
oddf(num1);
odd_macro(num1);
oddf(num1++);
odd_macro(num1++);
Output the result with clear labels.
In the comment block for this function explain why the results differ.
Important Notes:
Your game functions should return either a Boolean or integer to indicate whether or not the player won. Do not use global variables to solve this problem
o Certain logic should be in your main scope …
o Often the opponent’s name is used in the games
o Think about how to accomplish this … hint: look at the
You should not be using global variables
You should work within reasonable I/O standards. If the user gives you a
number outside the parameters you should correct the user and loop the input prompt again.
o I will not hold you responsible for bizarre input like text into an integer at this point.
I have listed required functions … you might find it beneficial to make more functions than the required ones this is acceptable.
Sample Output:
Welcome to the CSE240 Assignment 2!
Menu:
1 – CSE240 Mini-Game Collection
2 – Macros vs Functions Throw-down
0 – Exit
Choose an option: 1
Welcome to CSE240 Mini-Game Collection!
I am your opponent Delicia
How many rounds should we play? (Choose an odd number) >3
The score is:
Player – 0
Delicia – 0
Game #1
Dice Roll Showdown!
How many sides do the dice have (input a positive number): 6 How many dice will each player roll? 2
Player 1 Rolled:
3
6
Total: 9
Delicia Rolled
2
5
Total: 7
Player wins the round!
The score is:
Player – 1
Delicia – 0
Game #2
Even/Odd game!
Guess even or odd! (0 for even, 1 for odd): 1 Enter a number (between 0 and 100): 23 Delicia picked 57
23+57 = 80 – EVEN!
Delicia wins the round!
The score is:
Player – 1
Delicia – 1
…
When the player or opponent gets the required wins (in this case 2 out of 3) end the game. Optionally, you can take them back to the main menu.
Grading of Programming Assignment
The TA will grade your program following these steps:
(1) Compile the code. If it does not compile a U or F will be given in the
Specifications section. This will probably also affect the
Efficiency/Stability section.
(2) The TA will read your program and give points based on the points allocated
to each component, the readability of your code (organization of the code and comments), logic, inclusion of the required functions, and correctness of the implementations of each function.
Rubric:
What to Submit?
You are required to submit your solutions in a compressed format (.zip). Zip all files into a single zip file. Make sure your compressed file is labeled correctly – lastname_firstname2.zip
The compressed file MUST contain the following:
— or –
No other files should be in the compressed folder.
If multiple submissions are made, the most recent submission will be graded, even if the assignment is submitted late.
Where to Submit?
All submissions must be electronically submitted to the respected homework link in the course shell (Canvas) where you downloaded the assignment.
Academic Integrity and Honor Code.
You are encouraged to cooperate in study group on learning the course materials. However, you may not cooperate on preparing the individual assignments. Anything that you turn in must be your own work: You must write up your own solution with your own understanding. If you use an idea that is found in a book or from other sources, or that was developed by someone else or jointly with some group, make sure you acknowledge the source and/or the names of the persons in the write-up for each problem. When you help your peers, you should never show your work to them. All assignment questions must be asked in the course discussion board. Asking assignment questions or making your assignment available in the public websites before the assignment due will be considered cheating.
The instructor and the TA will CAREFULLY check any possible proliferation or plagiarism. We will use the document/program comparison tools like MOSS (Measure Of Software Similarity: http://moss.stanford.edu/) to check any assignment that you submitted for grading. The Ira A. Fulton Schools of Engineering expect all students to adhere to ASU’s policy on Academic Dishonesty. These policies can be found in the Code of Student Conduct:
http://www.asu.edu/studentaffairs/studentlife/judicial/academic_integrity.h tm
ALL cases of cheating or plagiarism will be handed to the Dean’s office. Penalties include a failing grade in the class, a note on your official transcript that shows you were punished for cheating, suspension, expulsion and revocation of already awarded degrees.