'F' → Fullscreen
Today you build a shell game in a new program named 1-23-shell-game: a ball under one of three cups, a shuffle you have to follow, and — in the second session — a dealer who cheats.
This activity runs over two sessions. Tasks 1 to 3 are the first; Tasks 4 to 6 are the second. The second session begins at the deck's “Now the Strange Part” slide.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
cups[ballAt] uses a variable as the index. Random.Shared.Next(0, 3) gives 0, 1, or 2 — exactly the three indexes this array has.cups[0]. The player counts from 1, the array counts from 0, and the display line is where the program translates.Console.WriteLine(cups); as a last line, run it, and write down what it prints. Then delete it.string[] cups = { "empty", "empty", "empty" };
int ballAt = Random.Shared.Next(0, 3);cups[ballAt] = "BALL";
Console.WriteLine("Watch the ball.");Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");guess - 1 turns the player's cup number into an index. Cup 1 is cups[0], so the program always looks one lower than the number typed.== compares the element with "BALL", the same way Activity 1.11 compared any two values.string[] cups = { "empty", "empty", "empty" };
int ballAt = Random.Shared.Next(0, 3);cups[ballAt] = "BALL";
Console.WriteLine("Watch the ball.");Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");Console.WriteLine();Console.Write("Which cup has the ball? (1, 2, or 3) ");int guess = int.Parse(Console.ReadLine());
if (cups[guess - 1] == "BALL"){ Console.WriteLine("You found it!");}else{ Console.WriteLine("Wrong cup.");}
Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");while re-rolls second until it is a different cup from first, so every swap really moves something.held are the deck's swap, with first and second as the indexes.first + 1 is the translation in the other direction: the program swaps indexes, and tells the player cup numbers.string[] cups = { "empty", "empty", "empty" };
int ballAt = Random.Shared.Next(0, 3);cups[ballAt] = "BALL";
Console.WriteLine("Watch the ball.");Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");Console.WriteLine();
for (int swap = 1; swap <= 3; swap++){ int first = Random.Shared.Next(0, 3); int second = Random.Shared.Next(0, 3);
while (second == first) { second = Random.Shared.Next(0, 3); }
string held = cups[first]; cups[first] = cups[second]; cups[second] = held;
Console.WriteLine($"Swap {swap}: cup {first + 1} and cup {second + 1}");}
Console.WriteLine();Console.Write("Which cup has the ball? (1, 2, or 3) ");int guess = int.Parse(Console.ReadLine());
if (cups[guess - 1] == "BALL"){ Console.WriteLine("You found it!");}else{ Console.WriteLine("Wrong cup.");}
Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");The second session starts here. Three changes, one at a time. Write down exactly what happens, and put each one back before the next.
cups[guess - 1] to cups[guess]. Play a game and pick the cup you know is right. Then play again and pick 3. Record what happens each time, including any line number.Random.Shared.Next(0, 3) to Random.Shared.Next(1, 4) — “a cup from 1 to 3,” the way a person would say it. Run it ten times. Record where the ball starts, and what happens on some of the runs.string held line and change cups[second] = held; to cups[second] = cups[first];. Play several games and count the balls in the final line each time.Numbers 1 and 2 are the same mistake made in opposite directions. Say what the mistake is. For number 3, say why the ball can multiply as well as vanish.
There is no code block for this task. When it is over, your program is exactly what it was at the end of Task 3.
seen saves the table right after the last swap, and the if at the bottom compares it with the table at the end. If any cup changed, the dealer cheated.cheatChance to 100 so the dealer cheats every game. Follow the ball carefully so you know he cheated.cheatChance at 100 for Task 6.string[] cups = { "empty", "empty", "empty" };
int ballAt = Random.Shared.Next(0, 3);cups[ballAt] = "BALL";
Console.WriteLine("Watch the ball.");Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");Console.WriteLine();
for (int swap = 1; swap <= 3; swap++){ int first = Random.Shared.Next(0, 3); int second = Random.Shared.Next(0, 3);
while (second == first) { second = Random.Shared.Next(0, 3); }
string held = cups[first]; cups[first] = cups[second]; cups[second] = held;
Console.WriteLine($"Swap {swap}: cup {first + 1} and cup {second + 1}");}
string[] seen = cups;
int cheatChance = 50;
if (Random.Shared.Next(1, 101) <= cheatChance){ cups[0] = "empty"; cups[1] = "empty"; cups[2] = "empty";}
Console.WriteLine();Console.Write("Which cup has the ball? (1, 2, or 3) ");int guess = int.Parse(Console.ReadLine());
if (cups[guess - 1] == "BALL"){ Console.WriteLine("You found it!");}else{ Console.WriteLine("Wrong cup.");}
Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");
if (seen[0] != cups[0] || seen[1] != cups[1] || seen[2] != cups[2]){ Console.WriteLine("The cups changed after the last swap. The dealer cheated!");}seen as a new array from the three values, the way the deck's last code slide did.cheatChance at 100 — the detector should speak every time. Then set it to 0 and play again — it should never accuse an honest dealer. Then put it back to 50.seen line explaining why the Task 5 version could never catch him.string[] cups = { "empty", "empty", "empty" };
int ballAt = Random.Shared.Next(0, 3);cups[ballAt] = "BALL";
Console.WriteLine("Watch the ball.");Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");Console.WriteLine();
for (int swap = 1; swap <= 3; swap++){ int first = Random.Shared.Next(0, 3); int second = Random.Shared.Next(0, 3);
while (second == first) { second = Random.Shared.Next(0, 3); }
string held = cups[first]; cups[first] = cups[second]; cups[second] = held;
Console.WriteLine($"Swap {swap}: cup {first + 1} and cup {second + 1}");}
string[] seen = { cups[0], cups[1], cups[2] };
int cheatChance = 50;
if (Random.Shared.Next(1, 101) <= cheatChance){ cups[0] = "empty"; cups[1] = "empty"; cups[2] = "empty";}
Console.WriteLine();Console.Write("Which cup has the ball? (1, 2, or 3) ");int guess = int.Parse(Console.ReadLine());
if (cups[guess - 1] == "BALL"){ Console.WriteLine("You found it!");}else{ Console.WriteLine("Wrong cup.");}
Console.WriteLine($"Cup 1: {cups[0]} Cup 2: {cups[1]} Cup 3: {cups[2]}");
if (seen[0] != cups[0] || seen[1] != cups[1] || seen[2] != cups[2]){ Console.WriteLine("The cups changed after the last swap. The dealer cheated!");}for loop that plays five rounds, and keep a score of how many times the player found the ball. Print the score at the end.cups is declared — inside the rounds loop or above it. Try the other one too, and say what happens to the table by round 5.Two things are different here, and both will cost you points if you read past them.
string[] cups = { "empty", "BALL", "empty" };string middle = cups[1];string[] seen = cups;cups ← ["empty", "BALL", "empty"]middle ← cups[2]seen ← cupsLists are written with square brackets, [ ], rather than C#'s curly braces, and they have no type, because nothing on the reference sheet does.
cups[1] on the exam is the first element. In C#, cups[1] is the second. That is why the middle cup is cups[1] on the left and cups[2] on the right.
| First element | Last element of a 3-item list | |
|---|---|---|
| C# | cups[0] | cups[2] |
| Exam notation | cups[1] | cups[3] |
The exam counts the way your player does. Your game needed guess - 1 because C# does not; in exam notation, cups[guess] would have been right. When you trace one of these, write the positions out as 1, 2, 3, and do not let your C# habit answer for you.
seen ← cups gives you an independent copy. The reference sheet says so outright. Change cups and seen keeps its values.
C# does the opposite, as Task 5 proved. Same-looking line, opposite behavior:
seen = cups, then change cups | |
|---|---|
| C# | seen changes too — one array, two names |
| Exam notation | seen is unaffected — two separate lists |
Written in exam notation, Task 5's detector would have worked. This is the subtlest difference in the whole course, because nothing looks wrong. If an exam question copies a list and then changes one of them, the other keeps its values. Answering from C# habit gets it backwards.
Two real games from the finished program. Your cups and swaps will be different. Follow the ball through the swaps in each one before reading the result.
Watch the ball.Cup 1: BALL Cup 2: empty Cup 3: empty
Swap 1: cup 2 and cup 1Swap 2: cup 2 and cup 3Swap 3: cup 2 and cup 3
Which cup has the ball? (1, 2, or 3) 2 [Enter]You found it!Cup 1: empty Cup 2: BALL Cup 3: emptyWatch the ball.Cup 1: BALL Cup 2: empty Cup 3: empty
Swap 1: cup 1 and cup 3Swap 2: cup 2 and cup 1Swap 3: cup 2 and cup 1
Which cup has the ball? (1, 2, or 3) 3 [Enter]Wrong cup.Cup 1: empty Cup 2: empty Cup 3: emptyThe cups changed after the last swap. The dealer cheated!cheatChance at 100 the detector catches the dealer every game, and at 0 it never accuses himConsole.WriteLine(cups); printed, your five-game score, all three Task 4 results, and what the Task 5 detector saidAnswer the following questions before submitting your work.
cups[0] in your program. Explain why your guess needed - 1, and what the last index of an array of a hundred elements would be.string[] seen = cups; actually did, and why building seen from cups[0], cups[1], and cups[2] fixed it.seen ← cups makes a real copy. Describe a question you could get wrong by answering from C# habit.Submit four things to the dropbox:
Console.WriteLine(cups); printed, and your five-game score from Task 3.