c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.227 seguidores
Pré-visualização50 páginas
programming and the C# language 
65 
Arrays
Cast(beaker);
Show(beaker);
++count;
}
while (!Equals(beaker));
Console.WriteLine("You've got yatzy after {0} attempts", count);
}
static void Cast(int[] beaker)
{
for (int i = 0; i < beaker.Length; ++i) beaker[i] = rand.Next(1, 7);
}
static void Show(int[] beaker)
{
Console.Write(&quot;[&quot;);
for (int i = 0; i < beaker.Length; ++i) Console.Write(&quot; {0}&quot;, beaker[i]);
Console.WriteLine(&quot; ]&quot;);
}
static bool Equals(int[] beaker)
{
for (int i = 1; i < beaker.Length; ++i)
if (beaker[i] != beaker[0]) return false;
return true;
}
}
Explanation
In order to simulate the game is a need for a random number generator. .NET has a class Random to 
that purpose and the program starts therefore, to create a random number generator called rand. Note 
that it is created outside of all methods. This means that there is access to it anywhere in the program, 
and hence that all methods know it.
The program will simulate a dice cup with 5 dice (cubes). In Main() is defined an array of 5 int elements 
to simulate the cup. There is also defined a variable count, which will be used to count how many times 
you have to toss the cup until you get yatzy.
In addition to Main() there are three methods. The first simulates that you toss with the cup. Here especially 
note how the random number generator is used: rand.Next (1, 7) means that one will have a random 
number which is greater than or equal to 1 and less than 7, and thus a random of the numbers 1, 2 , 3, 4, 
5 and 6. The method Show() does nothing more than to the print the contents of the cup on the screen.
The method Equals() is used to test whether the 5 dice in the cup are the same. Here you should note two 
things: The algorithm and the return value. Regarding the latter is the methods return value something 
discussed in a later section, but a method that has a value, can for example be used in a condition as 
here in the Main() method. Note also how the method\u2019s value is assigned with a return statement. The 
algorithm consists in comparing all the cubes from index 1 and up to the first cube \u2013 cube with index 
0. If you find one that differs from the first, all the cubes are not identical, and the method returns false. 
If in contrast you go throughout the loop, all the dice are the same, and the method returns true.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
66 
Arrays
Exam21
Craps
This example show a program that simulates a simple dice game called craps:
A player throws two dice, then the sum of the eyes are noted. If the sum is 7 or 11, the player has won. 
If the sum is 2, 3 or 12, the player has lost (the house has won). If the sum in contrast is 4, 5, 6, 8, 9 or 
10, you record the sum as the player\u2019s points. The player then proceeds to throw the dice until the sum 
of the eyes either is the player points or until the sum is 7. Is the sum are the player\u2019s points the player 
has won. Is the sum is 7, the player has lost, and the house wins.
The program will operate in the way that the user must first enter how many games you want to play. 
Then the program simulates the desired number of games and finally prints, how many times a player 
has won and how many times the house has won.
How to
This time, the solution is not quite simple, and there are several options. I will try with a kind of 
decomposition of the task into smaller parts, and the Main() method can be written as follows:
EXPERIENCE THE POWER OF 
FULL ENGAGEMENT\u2026
 RUN FASTER.
 RUN LONGER..
 RUN EASIER\u2026
READ MORE & PRE-ORDER TODAY 
WWW.GAITEYE.COM
Challenge the way we run
1349906_A6_4+0.indd 1 22-08-2014 12:56:57
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
67 
Arrays
Main()
{
let count = the number of games
repeat count times
{
Play
if player has won then add 1 to the variable won
print who is the winder
}
print the result
}
From this sketch of the Main() method, it is clear that the most important is Play(), which is a method 
that simulates a single game, and thus the method to implement the rules for craps. The algorithm can 
be written as follows:
Play()
{
bg is a cup with two dice
toss with bg and notes the sum of the dice eyes as points
if points is 7 or points is 11 then the player has won
else if points is 2 or points is 3 or points is 12 then the house has won
else
{
toss with bg and note the sum of the eyes as sum
as long as sum is different from the points and the sum varies from 7 repeat
{
toss with bg and note the sum of the eyes as sum
}
}
if sum is equal to points then the player has won elllers the house has won
}
The algorithm for Play() basically consists of simple operations, except perhaps the toss with the cup 
that can be described as follows:
1. Roll the dice
2. Print the content of the cup
3. Determine the sum of the eyes of the dice
With everything in place, there essentially only remains to translate the above algorithms to C#, and I 
will start with the latter and thus to simulate how you toss with the cup:
static int Throw(int[] beaker)
{
Cast(beaker);
Show(beaker);
return Sum(beaker);
}
static void Cast(int[] beaker)
{
for (int i = 0; i < beaker.Length; ++i) beaker[i] = rand.Next(1, 7);
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
68 
Arrays
static void Show(int[] beaker)
{
Console.Write(&quot;[&quot;);
for (int i = 0; i < beaker.Length; ++i) Console.Write(&quot; {0}&quot;, beaker[i]);
Console.WriteLine(&quot; ]&quot;);
}
static int Sum(int[] beaker)
{
int sum = 0;
for (int i = 0; i < beaker.Length; ++i) sum += beaker[i];
return sum;
}
where rand is a random number generator. As shown above, it consists of three operations:
1. Roll the dice is implemented as the method Cast()
2. Print the content of the cup is implemented as the method Show()
3. Determine the sum of the eyes of the dice and return the value is implemented as the 
method Sum()
Note that the three methods are relatively simple and all are written so they are independent of the 
number of cubes in the cup.
Using this method Throw(), you can write the algorithm for Play() in C#:
static bool Play()
{
int[] beaker = new int[2];
int point = Throw(beaker);
if (point == 7 || point == 11) return true;
if (point == 2 || point == 3 || point == 12) return false;
int sum = Throw(beaker);
while (sum != point && sum != 7) sum = Throw(beaker);
return sum == point;
}
Note that the method is essentially a direct copy of the above algorithm.
Next the Main() method:
static void Main()
{
int count = Enter();
int won = 0;
for (int i = 0; i < count; ++i)
if (Play())
{
Console.WriteLine(&quot;Player has won...&quot;);
++won;
}
else
Console.WriteLine(&quot;The house has won...&quot;);
Result(count, won);
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
69 
Arrays
static int Enter()
{
Console.Write(&quot;Enter the numbers of games: &quot;);
string text = Console.ReadLine();
return Convert.ToInt32(text);
}
Explanation
I will not systematically review the entire code, but merely pointing out things that you should special to 
be aware of. The program does not contain anything new compared to what is shown in earlier examples.
Note therefore that several methods have a return value . Note for example the Enter() method that returns 
an int. Notice how the method\u2019s return value in Main() is stored in the variable count.
The method Play() performs a game. It creates a cup as an array with two cubes (a cube is again 
represented as an int). You throw the dice the first time and note what the dice show (variable