November 1, 2018
This lab will focus on getting you started with your third project. You can find the instructions for this project at http://spots.augusta.edu/caubert/teaching/2018/fall/csci1301/shared/project3.pdf. The hints below are relative to each problem, but I strongly recommend you go over them all no matter what problem you end up choosing.
This problem is about computing a value that partially depends on the user, and displaying it nicely.
Observe the following code:
Adapt it so that
a
would be multiplied by 2
insted of being incremented by 2
every time the loop execute?b
would be set by the user?a
is computed and stored in a variable?Once you completed all of those steps, your loop will be pretty close to what you actually need for this problem!
The other aspect of this problem is to display the values nicely. One way of doing this is by using the String.Format
method. Observe the following:
Console.Write("*");
Console.Write(String.Format("{0, 10}", "Hello"));
Console.Write("*\n");
Console.Write("*");
Console.Write(String.Format("{0, -10}", "Hello"));
Console.Write("*\n");
Adapt it so that
\t
is displayed before the *\n
in both statements.This program have two aspects: you need to loop (“As long as the user did not gave me enough money…”), and to convert a character or string input (Q
, for instance) into a number (0.25 in that case).
Write a program that asks the user for an integer, add that integer to all the previous integers entered so far, and repeat as long as the sum of all the integer entered so far is less than 100. A do while
loop will be more appropriate for this task, something like:
Now, in the problem, we don’t want to ask the user for actual integers. We want to give the user the possibility to enter a character, and then to map that character to a value. Write a small program that
For this problem, you need first to simulate how a computer could “play”, and then to implement the logic of the game.
The most surprising part in this problem is probably the use of random numbers. This is the way we will simulate some really primitive artificial intelligence: the computer will play rock-paper-scissors by just randomly taking one of the possibilities.
The code given in the problem (without the comments) is the following:
Random myRandomObject = new Random();
int a, i = 0;
while (i <= 100)
{
a = myRandomObject.Next(1, 11);
Console.Write(a + " ");
i++;
}
Complete the following table with Win, Loose or get a Tie:
If you play | and the computer plays | then you |
---|---|---|
Rock | Rock, | _______ |
Rock | Paper, | _______ |
Rock | Scissors, | _______ |
Paper | Rock, | _______ |
Paper | Paper, | _______ |
Paper | Scissors, | _______ |
Scissors | Rock, | _______ |
Scissors | Paper, | _______ |
Scissors | Scissors, | _______ |
Now, in our program, the user will enter a string made of a single character (“R”, “S”, “P”) and the computer will randomly generate an integer (1, 2, 3), so we can’t “directly” use this table in our program.
But, exactly as you associate a string made of only one character to a word (R ⇆ Rock, P ⇆ Paper, S ⇆ Scissors), you can associate an integer to a word, can’t you? Then, how to decide who won or if it is a tie should be easy!
This problem involves a bit of Mathematics that can be tricky to get correctly, and involve cleverly getting the information from the user.
The first thing you need to do for this problem is to understand the equation needed to compute the grade. Start by trying to reproduce the example given in the project description on paper. Can you get the same result? Can you find a way that seems simple and flexible to compute the current grade so far in all generality?
Once you have the equation figured out, you need to understand how you can obtain the data from the user. You have two possible ways of doing it:
Try to implement one or the other, without worrying about the computation at this point (just add, for instance, the value entered).