none
getting a class to return a value from another class

    Question

  • hi all, im still learning to code so this will be a basic question for an experienced coder.

    im stuck on a program.

    1. Create an updateScore() method in CrapsGame that keeps a count of games won and games lost (you will need to get the play() method in Craps to return something).

    i still dont fully understand the class system and ive been going at this now for a few hours. can some look at my code and point me in the right direction. 

    using System;
    using System.Threading;
    using System.Threading.Tasks.Dataflow;
    
    namespace CrapGame
    {
        class CrapGame
        {
            Craps myDice = new Craps(new Random());
            static CrapGame myGame = new CrapGame();
    
            public int gamesPlayed = 0;
            public int howManyGames = 0;
    
            static void Main(string[] args)
            {
                
                myGame.Rules();
                myGame.ManyGames(0,0,0);
                Console.ReadLine();
            }
            
            public void ManyGames(int won,int lose,int diceRolls)
            {
                Console.WriteLine("How many games would you like to play?");
                int input = Convert.ToInt32(Console.ReadLine());
    
                for (int i = 0; i < input; i ++)
                {
                    myDice.Play(won,lose,diceRolls);
                }
            }
    
            public void UpdatedScore()
            {
                
            }
    
            public void Rules()
            {
                Console.WriteLine("Welcome player to my dice game");
                Console.WriteLine("Here are the rules");
                Console.WriteLine("2, 3 or 12  is Craps -- You lose! End of game!");
                Console.WriteLine("7 or 11 -- You win! End of game!");
                Console.WriteLine("Roll a 4, 5, 6, 8, 9 or 10 the game will carry on til you win or lose");
                Console.WriteLine("---------------------------------------------------------------------");
            }
    
            public void FinalScore()
            {
    
            }
    
        }
    }
    
    using System;
    using System.ComponentModel;
    
    namespace CrapGame
    {
        class Craps
        {
            private Random rand;
    
            public Craps(Random rand)
            {
                this.rand = rand;
                this.rand = new Random();
            }
    
            public int Play(int won,int lose,int diceRolls)
            {
                var diceOne = 0;
                var diceTwo = 0;
                var diceTotal = 0;
                won = 0;
                lose = 0;
                diceRolls = 0;
    
                do
                {
                    diceOne = OneThrow();
                    diceTwo = TwoThrow();
                    diceTotal = diceTwo + diceOne;
                    Console.WriteLine("I have thrown {0} and {1} = {2}", diceOne, diceTwo, diceTotal);
                    diceRolls ++;
    
                 
                } while (diceTotal == 4 || diceTotal == 5 || diceTotal == 6 || diceTotal == 8 || diceTotal == 9 || diceTotal == 10);
    
                if (diceTotal == 2 || diceTotal == 3 || diceTotal == 12)
                {
                    Console.WriteLine("You lose! End of game!");
                    lose ++;
                    return lose;
                }
                else if (diceTotal == 7 || diceTotal == 11)
                {
                    Console.WriteLine("You win! End of game!");
                    won ++;
                    return won;
                }
    
                return diceRolls;
            }
    
    
            public int OneThrow()
            {
                return rand.Next(6) + 1;
            }
    
            public int TwoThrow()
            {
                return OneThrow();
            }
    
        }
    }
    


    Wednesday, January 3, 2018 7:27 PM

Answers

  • Hello 5216perry,

    >>Create an updateScore() method in CrapsGame that keeps a count of games won and games lost (you will need to get the play() method in Craps to return something).

    You could regard the score as object and create a new instance to store the score information.

    Create a class

        class ScoreInfo {
           public int won = 0;
           public int lose = 0;
           public int diceRolls = 0;
        }

    Then use as below

     class CrapGame
        {
                  ...
            static ScoreInfo scoreInfo = new ScoreInfo();
                  ...     
            static void Main(string[] args)
            {
                  ...
                myGame.ManyGames(scoreInfo);
                  ...
            }
    
            public void ManyGames(ScoreInfo scoreInfo)
            {
                   ...
                    myDice.Play(scoreInfo);
                    UpdatedScore();
                }       
            }
            public void UpdatedScore()
            {
                Console.WriteLine($"The score: Win {scoreInfo.won} Lose: {scoreInfo.lose} Roll : {scoreInfo.diceRolls}");
            }
     public void Play(ScoreInfo scoreInfo)
            {
                ...
               scoreInfo.diceRolls++;
                ...
               scoreInfo.lose++;
                ...
               scoreInfo.won++;
                ...
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by 5216perry Thursday, January 4, 2018 10:51 AM
    Thursday, January 4, 2018 2:30 AM
    Moderator

All replies

  • Hello 5216perry,

    >>Create an updateScore() method in CrapsGame that keeps a count of games won and games lost (you will need to get the play() method in Craps to return something).

    You could regard the score as object and create a new instance to store the score information.

    Create a class

        class ScoreInfo {
           public int won = 0;
           public int lose = 0;
           public int diceRolls = 0;
        }

    Then use as below

     class CrapGame
        {
                  ...
            static ScoreInfo scoreInfo = new ScoreInfo();
                  ...     
            static void Main(string[] args)
            {
                  ...
                myGame.ManyGames(scoreInfo);
                  ...
            }
    
            public void ManyGames(ScoreInfo scoreInfo)
            {
                   ...
                    myDice.Play(scoreInfo);
                    UpdatedScore();
                }       
            }
            public void UpdatedScore()
            {
                Console.WriteLine($"The score: Win {scoreInfo.won} Lose: {scoreInfo.lose} Roll : {scoreInfo.diceRolls}");
            }
     public void Play(ScoreInfo scoreInfo)
            {
                ...
               scoreInfo.diceRolls++;
                ...
               scoreInfo.lose++;
                ...
               scoreInfo.won++;
                ...
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by 5216perry Thursday, January 4, 2018 10:51 AM
    Thursday, January 4, 2018 2:30 AM
    Moderator
  • Thanks Neil that really helped me out!
    Thursday, January 4, 2018 10:52 AM