最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Replaying a Script in C#? - Stack Overflow

programmeradmin2浏览0评论

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  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

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

Share Improve this question asked Feb 15 at 4:41 Anderson GleasonAnderson Gleason 112 bronze badges 3
  • 2 Pro tip, try to use appropriate and meaningful variable names... I think guess might be more appropriate for storing input2... – Chris Schaller Commented Feb 15 at 5:17
  • "there has to be a better way than copying this block" - good instinct! – Hans Kesting Commented Feb 15 at 8:05
  • Chris, I implemented the code you gave me and it works great! I have included a feature to adjust the odds and a pause when spinning. Working on making an actual spinning animation. Thanks so much for your advice! – Anderson Gleason Commented Feb 15 at 8:41
Add a comment  | 

1 Answer 1

Reset to default 1

Welcome to C#! In it's simplest form, what you are probably looking for is a loop construct.

  • C# While Loop
  • Iteration statements - for, foreach, do, and while

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

  1. Sanitse the inputs and display error info, look at int.TryParse(string, out int)
  2. Validate the inputs, don't allow the user to wager more than they have in the bank
  3. Add a pause while the spinner is spinning
  • Bonus points for an ascii animation of a single character spinner ;)
  1. Use String Interpolation or the String Format args to format the console messages that need to inject variable values, do not concatenate integers into strings, it is a lazy habit that you do not want to learn.
  2. Let the user choose the length of possible values and adjust the odds to match it
  3. Clear the screen at each round but rewrite the header, so the user can always see the logo and their current bank balance.
  4. Generate some ascii for game over, why not right ;)

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.

发布评论

评论列表(0)

  1. 暂无评论