March 15, 2018
Read the Part II of the Homework #5, available at http://spots.augusta.edu/caubert/teaching/2018/spring/csci1301/hw/05.pdf.
As you may notice, your program should essentially be one big loop. Let us try to make a program that would have a similar behaviour, shall me?
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
The program implementing problem 1 is basically taking the two previous hints, and putting them together!
Make sure you don’t overlook the other aspects of the problem: to return the change, and once this is working, to implement the additional features.
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!
Writing the program for this game is pretty much putting those two hints together, and enclosing the whole in a while
loop. For this part, looking at the first hint of Problem 1 could be of some help.