-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (150 loc) · 4.56 KB
/
Copy pathProgram.cs
File metadata and controls
160 lines (150 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public class Dice
{
private int diceSides;
private int topSide;
private static Random rand = new Random();
public int TopSide { get { return topSide; } }
public int DiceSides { get { return diceSides; } }//Made public just so we know how many unique sides were working with
public Dice()
{
diceSides = 6;
}
public Dice(int numOfSides)
{
if (numOfSides < 1)
{
diceSides = 6;
}
else
{
diceSides = numOfSides;
}
}
public void Roll()
{
topSide = rand.Next(1, diceSides + 1);
}
}
public class DiceGame
{
//Method Function for 3
public static bool ContainsNumber(int[] rolls, int number)
{
foreach (int roll in rolls)
{
if (roll == number)
{
return true;
}
}
return false;
}
//Method Function for 4
public static int[] GetUniqueRolls(Dice dice)
{
List<int> uniqueRolls = new List<int>();
while (uniqueRolls.Count < dice.DiceSides)
{
dice.Roll();
if (!uniqueRolls.Contains(dice.TopSide))
{
uniqueRolls.Add(dice.TopSide);
}
}
return uniqueRolls.ToArray();
}
public static void Main(String[] args)
{
//1. Create a dice object using default constructor
Dice dice1 = new Dice();
dice1.Roll();
Console.WriteLine($"\nDefault Dice rolled: {dice1.TopSide}");
//2. Create a dice object using with positive number of side
Dice dice2 = new Dice(10);
dice2.Roll();
Console.WriteLine($"\nPositive Number Dice rolled: {dice2.TopSide}");
//3. Create a dice object using with non-positive number of side
Dice dice3 = new Dice(-2);
dice3.Roll();
Console.WriteLine($"\nNegative number Dice rolled: {dice3.TopSide}");
Dice dice4 = new Dice();
dice4.Roll();
Console.WriteLine($"\nThe second Default Dice rolled: {dice4.TopSide}");
//4. Check the topSide after rolling the dice, ensure it is within[1, dicesides]
for (int i = 0; i < 5; i++)
{
dice1.Roll();
Console.WriteLine($"Roll #{i + 1}: {dice1.TopSide}");
}
//-----Testing the Dice Class-----
//1)
dice1.Roll();
dice4.Roll();
if (dice1.TopSide > dice4.TopSide)
{
Console.WriteLine($"\nDice 1 Roll: ({dice1.TopSide}) is greater than Dice 2 Roll:({dice4.TopSide}).");
}
else if (dice1.TopSide < dice4.TopSide)
{
Console.WriteLine($"\nDice 2 Roll: ({dice4.TopSide}) is greater than Dice 1 Roll:({dice1.TopSide}).");
}
else
{
Console.WriteLine($"\nDice 1 Roll: ({dice1.TopSide}) is equal to Dice 2 Roll:({dice4.TopSide}).");
}
//2)
int Dice1Wins = 0, Dice2Wins = 0;
for (int i = 0; i < 5; i++)
{
dice1.Roll();
dice4.Roll();
if (dice1.TopSide > dice4.TopSide)
{
Dice1Wins++;
}
else if (dice1.TopSide < dice4.TopSide)
{
Dice2Wins++;
}
}
if (Dice1Wins > Dice2Wins)
{
Console.WriteLine($"\nDice 1 wins with ({Dice1Wins}) counts compared to Dice 2's ({Dice2Wins}) counts..");
}
else if (Dice2Wins > Dice1Wins)
{
Console.WriteLine($"\nDice 2 wins with ({Dice2Wins}) counts compared to Dice 1's ({Dice1Wins}) counts..");
}
else
{
Console.WriteLine($"\nBoth dices tied with the count: {Dice1Wins}...");
}
//3)
int[] rolls = new int[5];
for (int i = 0; i < rolls.Length; i++)
{
dice1.Roll();
rolls[i] = dice1.TopSide;
}
Console.WriteLine("Enter a number to check (1-6): ");
string? input = Console.ReadLine();
if (int.TryParse(input, out int num))
{
if (ContainsNumber(rolls, num))
{
Console.WriteLine($"\nYes, the array contains {num}.");
}
else
{
Console.WriteLine($"\nNo, the array does not contain {num}.");
}
}
else
{
Console.WriteLine("\nInvalid input. Please enter a valid number.");
}
//4)
int[] unique = GetUniqueRolls(dice1);
Console.WriteLine("\nAll Unique rolls: " + string.Join(", ", unique));
}
}