mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 15:37:51 -08:00
Created C# slots.csx
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/)
|
||||
|
||||
This C# implementation of slots was done using a [C# script](https://github.com/filipw/dotnet-script).
|
||||
|
||||
# Required
|
||||
[.NET Core SDK (i.e., .NET 6.0)](https://dotnet.microsoft.com/en-us/download)
|
||||
|
||||
Install dotnet-script. On the command line run:
|
||||
```
|
||||
dotnet tool install -g dotnet-script
|
||||
```
|
||||
|
||||
# Run
|
||||
```
|
||||
dotnet script .\slots.csx
|
||||
```
|
||||
190
80_Slots/csharp/slots.csx
Normal file
190
80_Slots/csharp/slots.csx
Normal file
@@ -0,0 +1,190 @@
|
||||
Print("SLOTS");
|
||||
Print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
Print(); Print(); Print();
|
||||
Print("YOU ARE IN THE H&M CASINO,IN FRONT OF ONE OF OUR");
|
||||
Print("ONE-ARM BANDITS. BET FROM $1 TO $100.");
|
||||
Print("TO PULL THE ARM, PUNCH THE RETURN KEY AFTER MAKING YOUR BET.");
|
||||
|
||||
var _standings = 0;
|
||||
|
||||
var play = true;
|
||||
while(play)
|
||||
{
|
||||
Play();
|
||||
play = PlayAgain();
|
||||
}
|
||||
|
||||
Done();
|
||||
|
||||
public void Play()
|
||||
{
|
||||
var bet = GetBet();
|
||||
Print();
|
||||
Ring();
|
||||
|
||||
var random = new Random();
|
||||
var x = GetSlot();
|
||||
var y = GetSlot();
|
||||
var z = GetSlot();
|
||||
|
||||
Print();
|
||||
Print($"{x.ToString()} {y.ToString()} {z.ToString()}");
|
||||
|
||||
if(x == y && x == z)
|
||||
{
|
||||
if(z == Slot.BAR)
|
||||
{
|
||||
// BAR BAR BAR
|
||||
Print();
|
||||
Print("***JACKPOT***");
|
||||
Print("YOU WON!");
|
||||
_standings = (100*bet) + bet + _standings;
|
||||
}
|
||||
else
|
||||
{
|
||||
Print();
|
||||
Print("**TOP DOLLAR**");
|
||||
Print("YOU WON!");
|
||||
_standings = (10*bet) + bet + _standings;
|
||||
}
|
||||
}
|
||||
else if(x == y)
|
||||
{
|
||||
if(y == Slot.BAR)
|
||||
{
|
||||
DoubleBar(bet);
|
||||
}
|
||||
else
|
||||
{
|
||||
Double(bet);
|
||||
}
|
||||
}
|
||||
else if(x == z)
|
||||
{
|
||||
if(z == Slot.BAR)
|
||||
{
|
||||
DoubleBar(bet);
|
||||
}
|
||||
else
|
||||
{
|
||||
Lost(bet);
|
||||
}
|
||||
}
|
||||
else if(y == z)
|
||||
{
|
||||
if(z == Slot.BAR)
|
||||
{
|
||||
DoubleBar(bet);
|
||||
}
|
||||
else
|
||||
{
|
||||
Double(bet);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Lost(bet);
|
||||
}
|
||||
|
||||
Print($"YOUR STANDINGS ARE ${_standings}");
|
||||
}
|
||||
|
||||
public bool PlayAgain()
|
||||
{
|
||||
Console.Write("AGAIN? (Y) ");
|
||||
var playAgain = Console.ReadKey(true);
|
||||
Print();
|
||||
return playAgain.Key == ConsoleKey.Y || playAgain.Key == ConsoleKey.Enter;
|
||||
}
|
||||
|
||||
public void Done()
|
||||
{
|
||||
Print();
|
||||
if(_standings < 0)
|
||||
{
|
||||
Print("PAY UP! PLEASE LEAVE YOUR MONEY ON THE TERMINAL.");
|
||||
}
|
||||
else if (_standings == 0)
|
||||
{
|
||||
Print("HEY, YOU BROKE EVEN.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Print("COLLECT YOUR WINNINGS FROM THE H&M CASHIER");
|
||||
}
|
||||
}
|
||||
|
||||
// Prints the text provided. Default is a blank line
|
||||
public void Print(string line = "")
|
||||
{
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
|
||||
public int GetBet()
|
||||
{
|
||||
Print();
|
||||
Console.Write("YOUR BET ");
|
||||
var betInput = ReadLine();
|
||||
int bet;
|
||||
var inputValid = int.TryParse(betInput, out bet);
|
||||
if (!inputValid)
|
||||
{
|
||||
Print("NUMBER EXPECTED - RETRY");
|
||||
return GetBet();
|
||||
}
|
||||
|
||||
if(bet > 100)
|
||||
{
|
||||
Print("HOUSE LIMITS ARE $100");
|
||||
inputValid = false;
|
||||
}
|
||||
else if(bet < 1)
|
||||
{
|
||||
Print("MINIMUM BET IS $1");
|
||||
inputValid = false;
|
||||
}
|
||||
|
||||
return inputValid ? bet : GetBet();
|
||||
}
|
||||
|
||||
public enum Slot { BAR, BELL, ORANGE, LEMON, PLUM, CHERRY };
|
||||
|
||||
public Slot GetSlot()
|
||||
{
|
||||
var rand = new Random();
|
||||
var num = rand.Next(0, 5);
|
||||
return (Slot)num;
|
||||
}
|
||||
|
||||
public void DoubleBar(int bet)
|
||||
{
|
||||
Print();
|
||||
Print("*DOUBLE BAR*");
|
||||
Print("YOU WON!");
|
||||
_standings = (5*bet) + bet + _standings;
|
||||
}
|
||||
|
||||
public void Double(int bet)
|
||||
{
|
||||
Print();
|
||||
Print("DOUBLE!!");
|
||||
Print("YOU WON!");
|
||||
_standings = (2*bet) + bet + _standings;
|
||||
}
|
||||
|
||||
public void Lost(int bet)
|
||||
{
|
||||
Print();
|
||||
Print("YOU LOST.");
|
||||
_standings = _standings - bet;
|
||||
}
|
||||
|
||||
public void Ring()
|
||||
{
|
||||
for(int i = 1; i <= 10; i++)
|
||||
{
|
||||
// https://stackoverflow.com/a/321148/1497
|
||||
Console.Beep();
|
||||
// Console.Beep(800, 501 - (i * 50)); // Uncomment for a fancier bell
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user