player 2 is disproportionally winning

bigmike2238

Member
Joined
Dec 24, 2013
Messages
7
Programming Experience
Beginner
Hello,

I have taken on a challenge set forth by a popular .Net training site. I think my math is right but the problem is player 2 is disproportionally winning in the simulated darts game. Here is the class where the calculations are being performed:

C#:
public class Dart
{
    Random random = new Random();
    public int Score { get; set; }
    private int bonusValue { get; set; }

    public int throwMethod()
    {
        int _throwResult = random.Next(0, 20);
        
        if (_throwResult == 0)
        {
            checkForBullsEye();
            return this.Score;
        }
        else
        {
            calcBonus();
            this.Score += _throwResult * (this.bonusValue);
            return this.Score;
        }
    }

    void checkForBullsEye()
    {
        double _bullCheck = random.NextDouble();
        if (_bullCheck <= 0.5)
            this.Score = 25;
        else
            this.Score = 50;

    }

    void calcBonus()
    {
        double _check = random.NextDouble();
        if (_check < (1 / 3))
            this.bonusValue = 2;
        else if (_check >= (1 / 3) && _check < (2 / 3))
            this.bonusValue = 3;
        else
            this.bonusValue = 1;
    }
     
}

...And the method that tallys up the score:

C#:
        static public void OneRun()
        {
            Dart dart = new Dart();
            Player1Score += dart.throwMethod();
            Player2Score += dart.throwMethod();
        }

I would appreciate any assistance with this issue. Thank you to all that reply.

Mike :witless:
 
The Fix...

The problem how the code was written. Apparently loops do not work well with static methods. This and the fact that some display variables were not properly assigned led to a world of problems. :highly_amused:

Thanks to all that looked.

Mike

Working code:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void okButton_Click(object sender, EventArgs e)
    {
        resultLabel.Text = null;
        Game game = new Game();
        do
        {
            for (int i = 0; i < 3; i++)
            {
                game.Player1Score += game.TallyRound();
                game.Player2Score += game.TallyRound();
            }
        } while (game.Player1Score <= 300 && game.Player2Score <= 300);

        game.AssignPlayerName();
        resultLabel.Text = game.DisplayResults();
    }
    
    public class Game
    {
        static public int Score { get; set; }
        public int Player1Score { get; set; }
        public int Player2Score { get; set; }
        static string playerName { get; set; }
        
        public int TallyRound()
        {
            int Tally = CalcThrow();
            return Tally;
        }
        
        static public int CalcThrow()
        {
            Dart dart = new Dart();
            int _throwResult = dart.throwMethod();
            if (_throwResult == 0)
            {
                Score = dart.checkForBullsEye();
            }
            else
            {
               dart.calcBonus();
                Score = _throwResult * (dart.calcBonus());
            }
            return Score;
        }

       public void AssignPlayerName()
       {
            if ((Player1Score > Player2Score) && Player1Score >= 300)
                playerName = "Player 1 Wins!";
            else if ((Player2Score > Player1Score) && Player2Score >= 300)
                playerName = "Player 2 Wins!"; 
           else if (Player1Score >= 300)
                playerName = "Player 1 Wins!";
            else if (Player2Score >= 300)
                playerName = "Player 2 Wins!";
        }

        public string DisplayResults()
        {
            string result = String.Format("Player 1 Score: {0} <br /> Player 2 Score: {1} <br /> {2}", Player1Score,
                Player2Score, playerName);
            Player1Score = 0;
            Player2Score = 0;
            return result;
        }
        
    }
    
}

...and the dart class:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;

public class Dart
{
    Random random = new Random();
    private int score { get; set; }
    private int bonusValue { get; set; }

    public int throwMethod()
    {
        Thread.Sleep(5);
        int _throwResult = random.Next(0, 20);
        return _throwResult;
    }

    public int checkForBullsEye()
    {
        double _bullCheck = random.NextDouble();
        if (_bullCheck <= 0.5)
            this.score = 25;
        else
            this.score = 50;

        return this.score;

    }

     public int calcBonus()
    {
        double _check = random.NextDouble();
        if (_check < (1 / 3))
            this.bonusValue = 2;
        else if (_check >= (1 / 3) && _check < (2 / 3))
            this.bonusValue = 3;
        else
            this.bonusValue = 1;

        return this.bonusValue;
    }
     
}
 
Apparently loops do not work well with static methods.

As you have resolved the issue, I haven't actually read either of your posts in detail but I can tell you that there is no truth whatsoever to that statement. There is nothing about static methods that make loops any more or less useful. It really just comes down to whether you write good code or not.
 
Thanks for the critique. To retort, I can say after stepping through my original code multiple times, I found that iterative variables weren't updating correctly, causing my program to enter loop hell... which led me to believe otherwise. I've been using C# for a total of less than a year, and I don't even consider myself at an intermediate level...so any constructive feedback is always welcome.

-Mike
 
Back
Top Bottom