Need help with %s

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Need help with %s

Post by MikeD »

I'm working on a baseball sim, and have hit a wall right now.

What I'm trying to accomplish is getting the Contact% to real life standards. Which is 87% For a Pitch in the Strike Zone and 68% outside the strike zone.

I could easily do this, but in a way that player's attributes would have no effect, and therefore everybody is the same. The problem is though, the way I have this set up right now is normal for the best batters matched with the best pitchers. However once I start mixing up the batters and pitchers, these percentages go WAY off.

So basically what I'm asking for is some help getting these percentages back to normal, even with mismatches in the batters/pitchers. (Hope that made sense)

Here's the function, $pitch is just the type of pitch (Fastball, Curve Etc), $strike is the variable that indicates if the ball is in the strike zone (1) or not (0), $conpow and $confin are player variables for their contact attributes with around 45 being the highest and 20 being the lowest, $pitch_rating is the rating based on Speed/Movement of a pitch. the highest being around 75-80 and the lowest being around 50-55 at these player levels.

Any help is much much appreciated.

Code: Select all

function Contact($pitch,$strike,$conpow,$confin,$pitch_rating){
            switch($pitch){
                case '1':
                case '2':
                case '3':
                case '4':
                    $contact = $conpow;
                    break;
                case '5':
                    $contact = $confin;
                    break;
            }
            $batter_roll = rand(25,40);
            $contact = $contact + $batter_roll;

            if ($strike == 1){ //Swung at Strike
                if ( $contact >= $pitch_rating ){
                    $contact = 1;
                }else{
                    $contact = 0;
                }
            }else{//Swung at Ball
                if ( $contact >= $pitch_rating ){
                    $contact = 1;
                }else{
                    $contact = 0;
                }
            }

        return array($contact);
    }
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Need help with %s

Post by Xaleph »

So good for you to be so busy working on a baseball Sim! I like it :)

First of, when hitting the 'wall' ( to keep close to your words) allways to try exagerate values. Meaning, you have 20 as the lowest, 45 as the max right? Try blowing it up to 2000 and 50000, see what effects it generate. If you blow things out of proportion, you will start to see where your calculations are going wrong.

Second, I see you use integers as strings, that`s not neccesary, just use integers in your switch. Same goes for the 1,0 values, make it a boolean.

Third, try to stick to the most primitive types of variables, this comes back to point two, but generally speaking, using the primitives gives you a better understanding.

Now, what I notice for just looking at data types ( ints etc) I see your pitch is an int. What if a float would be a better value? so instead of 20 - 45, why not use 1.20 to 1.45? These values are marginally better and probably yield better results.

Coming to my last point, the whole if - else branch is doing nothing particularly good, both branch at the same result. $contact = 1 when $contact is >= $pitch_rating. So maybe you are missing some logic there?
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need help with %s

Post by Jackolantern »

MikeD wrote:What I'm trying to accomplish is getting the Contact% to real life standards. Which is 87% For a Pitch in the Strike Zone and 68% outside the strike zone.

I could easily do this, but in a way that player's attributes would have no effect, and therefore everybody is the same.
Could you explain how those percentages work in real-life baseball (that is, I am assuming this is something in real-life baseball you are trying to model into your game)? How does someone go above 87%, between 68% and 87%, and below 68%? If you take that mechanic, and try to convert that into your game, you may be able to get more varied stats for the players.

I know really nothing about baseball, but does a pitch further inside the strike zone get a higher percent, and a more wild pitch outside the strikezone get a lower percent? If so, then you may need to create more of a spectrum for where the pitch is going. Maybe 0 - 59 is outside the strike zone and 60 - 100 is inside, with closer to 0 being a catastrophic pitch that could allow runners to steal bases, closer to 59 being right outside the strike zone, and then 61 being right inside the strike zone and perhaps still difficult to hit, and closer to 100 is right down the middle.

Again, my lack of knowledge about baseball and how its stats work is probably hampering me from coming up with a suitable solution. But I guess my best advice is to try to break these stats down, add in mechanics to help the stats work realistically, and then use those created mechanics to make more varied play in the game.
The indelible lord of tl;dr
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Need help with %s

Post by MikeD »

Xaleph wrote:
Second, I see you use integers as strings, that`s not neccesary, just use integers in your switch. Same goes for the 1,0 values, make it a boolean.

Coming to my last point, the whole if - else branch is doing nothing particularly good, both branch at the same result. $contact = 1 when $contact is >= $pitch_rating. So maybe you are missing some logic there?
Ha, I didn't even know you could use a case without the quotes, thank for that :).

And the if else is the same so I can see exactly what the Contact% is, I will change it once I get it figured out.

Working on a different method to do this right now, I'll let you guys know how it goes. Once I get this hammered out, I could probably have an alpha sim done by the weekend if things go right.
Jackolantern wrote: Could you explain how those percentages work in real-life baseball (that is, I am assuming this is something in real-life baseball you are trying to model into your game)? How does someone go above 87%, between 68% and 87%, and below 68%? If you take that mechanic, and try to convert that into your game, you may be able to get more varied stats for the players.

I know really nothing about baseball, but does a pitch further inside the strike zone get a higher percent, and a more wild pitch outside the strikezone get a lower percent? If so, then you may need to create more of a spectrum for where the pitch is going. Maybe 0 - 59 is outside the strike zone and 60 - 100 is inside, with closer to 0 being a catastrophic pitch that could allow runners to steal bases, closer to 59 being right outside the strike zone, and then 61 being right inside the strike zone and perhaps still difficult to hit, and closer to 100 is right down the middle.

Again, my lack of knowledge about baseball and how its stats work is probably hampering me from coming up with a suitable solution. But I guess my best advice is to try to break these stats down, add in mechanics to help the stats work realistically, and then use those created mechanics to make more varied play in the game.
I was going to have Squares, but it's too much of a pain to do, and not really necessary. I'm not really a baseball fan at all either. (Football and Hockey for me), I went into this thinking it would be easier to do than a Football sim, and there isn't really a 2D Sim that I've seen. (I'll be converting all this code into Impact later on)

I've been using these stats to model my sim.

http://www.fangraphs.com/leaders.aspx?p ... &players=0
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need help with %s

Post by Jackolantern »

MikeD wrote:I've been using these stats to model my sim.

http://www.fangraphs.com/leaders.aspx?p ... &players=0
:shock: :shock:

You may want to see if you can enlist a Sabermetrics fan to help you out on this one lol. The actual objectives of baseball are simple, but it is actually an extraordinarily complex game once you get into stats. As to a clue on the depth, some hardcore fans have several spreadsheets open while watching :?
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”