[SOLVED] 代写 C game math graph Lab Session 4 Conditions and Loops

30 $

File Name: 代写_C_game_math_graph_Lab_Session_4__Conditions_and_Loops.zip
File Size: 565.2 KB

SKU: 5010244856 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


Lab Session 4Conditions and Loops
Exercise 6: if conditional Background
The logic of an if statement is fairly simple. If the statement is true, the code block indicated by a statement or the contents of a … block is run. A closely related command is the if … else, which acts precisely as you would expect.
Here are two cautions to avoid common problems.
1. In C, x1 and x1 do completely different things:
o The singleis an assignment operator: it changes to the value of x to be 1.
o The doubleis the comparison operator: it returns true if x is equal to 1.
2. If you forget to use the…code block, then it will be very easy to make a mistake later on. The computer will happily execute statements which you did not think would be running. This is illustrated at the bottom of the conditional example.
Technical details
Conditional:
include stdio.h
int main
int x3;
if x4
this line will not be printed
printfx is equal to 4.n;

one of these two lines will be printed
if x101 ! 0
printfExpression is true.n;
printfx is greater than 10.n;
else
printfExpression is false.n;
printfx is not greater than 10.n;

something weird happens here!
if x10
printfExpression is true.n;
printfx is greater than 10.n;
getchar;
The logical operators not ! andorare particularly useful with if. Occasionally the exclusive oris useful. Parenthesesare highly encouraged when using logical operators.
UESTC1005 2019 Page 1 of 6

Your task…
Create a guessing game program. Your program should:
Make the computer pick a random number to be the answer. It should be an int between 1 and 32 inclusive.
Use the getRand function, but do not modify it. Instead, you should take the value it returns a float between 0 and 0.999… and do some math to transform that into an int between 1 and 32.
Write a function for the users guess. This function must:
o have one integer argument int correctanswer,
o read an int from the keyboard,
o check the users int against the correct answer,
o output the appropriate message correcttoo hightoo low,
o returns a 1 if the users guess was correct, and 0 if the users guess was wrong.
Give the user 5 chances to guess the right answer.
hint: you have already written a function that checks a guess. What happens if you copypaste that function call 5 times?
End the game if the user is correct, or if hes used up all 5 chances. Print either a you win or you lose, as appropriate.
You may not use any loops for this program.
optional: instead of guessing an int, ask players to guess a float. Then, instead of trying to guess exactly the right number, the players win if they guess a number within0.1 of the correct value.
Note: Show your work to a demonstratorGTA. Exercise 7: While loops
Background
The simplest loops in C are while loops. These loops are very similar to if statements. The difference is that there is not else statement included in a while loop, and the…code block will be repeated as long as the statement is true.
There is no builtin true statement in C, so we use 1 for true and 0 for false. To stop an infinite loop, use ctrlc
A closelyrelated form is the do … while loop. Examine the code presentedis there any difference in the output? Try setting int i10;
UESTC1005 2019 Page 2 of 6

Technical details
Infinite loops are fun:
include stdio.h
int main
while 1
printfInfinite loop is infinite.n;
getchar;

Noninfinite loops are more useful:
include stdio.h
int main
int i0;
while i10
printfThe loop has repeated ;
printfi times.n, i;
i;

getchar;
Another form of loop:
include stdio.h
int main
int i0; do
printfThe loop has repeated ;
printfi times.n, i;
i;
while i10;
getchar;
Your task…
Save your guessing game from the previous exercise if conditionals with a new file name; we will be modifying it, but you dont want to lose all your hard work.
Modify your guessing game:
Instead of giving the user X chances, keep on offering guesses until the user is correct.
Keep track of the number of guesses, and tell the player how many guesses it took. UESTC1005 2019 Page 3 of 6

After the first game is finished, start a new game with a different answer to guess. Keep track of the number of guesses the player needed to win.
After the second and later games are finished, tell the player their score number of guesses and the best score the fewest number of guesses. If the players score is better, store it as the best score.
Keep on repeating until the player wins with 4 or fewer guesses. or until the player hits ctrlc
optional: randomly change the rulesinstead of always going from 1 to 32, let the computer randomly decide to do 100 to 131, 97 to 66, etc. If you change the size of the range, you cant compare high scores for different games. However, you could program the computer to randomly select all numbers divisible by 4 between 32 and 156 and the like.
Note: Show your work to a demonstratorGTA. Exercise 8: for Loops
Background
A loop that counts up or down is the most common type of loop, so most programming languages have a shortcut: the for loop.
This loop squishes the initial setting, conditional do we continue, and ending how to change the variable into a single line. Given a statement of the form:
for init; runloop?; afterloop…

Technical details
The most common form of loop is this:
include stdio.h
int main
int i;
for i0; i10; i
printfThe loop has repeated ;
init: This is executed once, at the beginning of the entire loop. Generally will be something like i0.
runloop?: The computer tests this before each loop. Generally will be something like i10.
afterloop: the computer executes this statement after each loop. Generally will be something like i.
UESTC1005 2019
Page 4 of 6

printfi times.n, i;

getchar;
return 0;
Heres a weird loop:
include stdio.h
int main

int i;
for i2000; i0; ii23
printfWhat is this loop ;
printfdoing?i is in,i;

getchar;
return 0;
Your task…
Back in the good old days, there were no fancy graphics in computer games; we used text to represent everything. Your task is to draw a room from a family of games called Roguelikesthe player represented by thesymbol must explore a dungeon.
2×2 room, player at 0,1:

..
.

5×3 room, player at 1,2:

…..
…..
….

14×8 room, player at 8,5:

…………..
…………..
…………..
…………..
…………..
………….
…………..
…………..

Your task is to write a function with extra helper functions to draw such a room.
How big are the rooms? What is the coordinate system?
UESTC1005 2019 Page 5 of 6

Your int main must contain only:
int main
drawRoom2,2,0,1;
drawRoom5,3,1,2;
drawRoom14,8,8,5;
getchar;
return 0;
In addition to drawRoom, write three extra functions. The first draw a horizontalline, the second draws a line without the player …., and the third draws the line with the player …. The drawRoom function should call those other helper functions when appropriate.
examples of the three extra functions are not drawn to scale
Use for loops. You may not use if or while in this exercise.
optional: combine this exercise with keyboard inputlet the player move around in the room, bump into walls, etc. Ask the user to turn on the numlock key and to press enter after every move, then you can read his moves by reading int from the keyboard. Use a while loop for this movement.
Note: Show your work to a demonstratorGTA.
UESTC1005 2019 Page 6 of 6

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] 代写 C game math graph Lab Session 4 Conditions and Loops
30 $