Brand new C# coder here, I made a simple "casino" game (probably with horrible syntax). I am trying to play this script over and over again while keeping track of the "money" variable instead of the script just ending when one game is played. Any tips?
// See https://aka.ms/new-console-template for more information
using System;
using Microsoft.VisualBasic;
Console.WriteLine(" ___ _ ");
Console.WriteLine(" / __|__ _ __(_)_ _ ___ ");
Console.WriteLine("| (__/ _` (_-< | ' |/ _ | ");
Console.WriteLine("|_______,_/__/_|_||_|___/");
Random rnd = new Random();
int money = 10; // starts the user with 10 dollars
int random = rnd.Next(1, 4); // choosing random ass number for the random variable
Console.WriteLine("Welcome");
Console.WriteLine("Please enter age");
string input1 = Console.ReadLine();
int ass = Int32.Parse(input1); // making "ass" equal to "input1"
if (ass >= 18)
{
Console.WriteLine("Please choose an integer 1-3 ");
string input2 = Console.ReadLine();
Console.WriteLine("You have " + money + " dollars."); // stating the current balance
Console.WriteLine("How much would you like to bet?");
string inputmoney = Console.ReadLine();
int bet = Int32.Parse(inputmoney); // making the string "input" equal to the int variable "shit"
Console.WriteLine("Spinning");
int shit = Int32.Parse(input2); // making the string "input" equal to the int variable "shit"
if (shit == random) // if the user's guess was correct
{
int sum = (bet * 3) + money; // generates user's current sum
Console.WriteLine("You Win!");
Console.WriteLine("Your current balance is " + sum); // displays user's current sum
Console.Write("The integer was " + shit);
}
else // if the user's guess was incorrect
{
int sum = money - bet; // generates user's current sum
Console.WriteLine("You Lose");
Console.WriteLine("Your current balance is " + sum); // displays user's current sum
Console.Write("The integer was " + random);
}
}
else
Console.WriteLine("Please come back when you are 18.");
I tried just copy and pasting this over and over again but that is a sin against god
Welcome to C#! In it's simplest form, what you are probably looking for is a loop construct.
In this case just wrap your logic in a do
- while
loop:
...
bool continueLoop = true;
do
{
// BEGIN your repetitive logic
...
...
// END your repetitive logic
// ask the user if they want to continue
Console.WriteLine();
Console.WriteLine("Pres Enter to play again, any other character will quit");
continueLoop = String.IsNullOrEmpty(Console.ReadLine());
} while(continueLoop)
...
// end script logic after the loop
But you will also need to update the money
variable in the two cases where you have updated the sum
. So for the case where the guess was wrong
int sum = money - bet;
money = sum; // assign back to the bank/wallet
This can be completed in a single operation:
money = money - bet;
you could also shorten this using the Unary plus and minus operator to decrement the money
:
money -= bet;
So put that together and you get this:
I've prefixed my comments with NOTE:
or TODO:
// See https://aka.ms/new-console-template for more information
using System;
using Microsoft.VisualBasic;
Console.WriteLine(" ___ _ ");
Console.WriteLine(" / __|__ _ __(_)_ _ ___ ");
Console.WriteLine("| (__/ _` (_-< | ' |/ _ | ");
Console.WriteLine("|_______,_/__/_|_||_|___/");
Random rnd = new Random();
int money = 10; // starts the user with 10 dollars
Console.WriteLine("Welcome");
Console.WriteLine("Please enter age");
string input1 = Console.ReadLine();
int ass = Int32.Parse(input1); // making "ass" equal to "input1"
if (ass >= 18)
{
// NOTE: keeping track of the number of rounds
int round = 1;
bool continueLoop = true;
do
{
// BEGIN your repetitive logic
int random = rnd.Next(1, 4); // choosing random ass number for the random variable
Console.WriteLine();
Console.WriteLine("Playing Round #{0}, you have {1:c} in the bank", round, money);
Console.WriteLine();
Console.WriteLine("Please choose an integer 1-3 ");
string input2 = Console.ReadLine();
Console.WriteLine("You have " + money + " dollars."); // stating the current balance
Console.WriteLine("How much would you like to bet?");
string inputmoney = Console.ReadLine();
int bet = Int32.Parse(inputmoney); // making the string "input" equal to the int variable "selection"
Console.WriteLine("Spinning");
// TODO: add a wait here, maybe an ascii animation ;)
// NOTE: let's call this the 'selection'...
int selection = Int32.Parse(input2);
if (selection == random) // if the user's guess was correct
{
// NOTE: calculate the winnings, put it back in the bank ;)
int winnings = (bet * 3);
money += winnings;
Console.WriteLine("You Win!");
Console.WriteLine("Your current balance is " + money);
Console.Write("The integer was " + selection);
}
else // if the user's guess was incorrect
{
// NOTE: take the money out of the bank variable
money -= bet;
Console.WriteLine("You Lose");
Console.WriteLine("Your new balance is " + money);
Console.Write("The integer was " + random);
// NOTE: if the balance is zero or less you can forcibly exist the loop
if (money <= 0)
{
Console.WriteLine("You have run out of money... Security, take this player away!");
break;
}
}
// END your repetitive logic
// ask the user if they want to continue
Console.WriteLine();
Console.WriteLine("Press Enter to play again, any other character will quit");
continueLoop = String.IsNullOrEmpty(Console.ReadLine());
} while (continueLoop);
}
else
Console.WriteLine("Please come back when you are 18.");
// TODO: write "GAME OVER" in ascii script
Console.WriteLine("GAME OVER");
As fun as this is, take it to the next level by adding these features
int.TryParse(string, out int)
A note about comments... make sure they provide more meaning or context that doesn't come across from your code, in this example, the comment is redundant. Even as you learn the code and what it means, try not to retain comments that simply explain the code in natural language
Console.WriteLine("Your new balance is " + money); // displays user's current sum
It will be better to immerse yourself, leave this comment out to force you to try to understand what the code is doing. Comments like those are training wheels, you're ready to take them off now.