• 10
• 11
• 12
• 14
• 15

# Baseball game

## Recommended Posts

I'd like to make a baseball game of my own in C++, and in compliance with the rules of the game, with three different difficulty settings (Easy, Normal, and Hard) and varieties of teams, players, stadia, and other options. As part of this desire, I'm researching the various probabilities involved; after all, baseball, like Mario Party, is a game of chance. I'd like to know how to trigger varieties of conditions; for example, an uncaught third strike (around 3-4% probability), which is invoked if either first base is unoccupied, or first base is occupied with two outs. I'd also like to know how to program each zone (e.g. fair/foul territory bounds, the stands, the batter's box, etc.).

##### Share on other sites

Anything related to chance or as many call "RNG", is extremely easy to do. You can simply generate a random number from 1 to 10 as an example, and if the number hits 7 do (action) to give a 10% chance. You would have to figure out if you want to do this using rand() or another library.

Dealing with zones is also easy. You would just generate a formula that takes into account the attributes of the Environment (wind), Pitcher, and Hitter that would generate different conditions and percentages in which fouls would occur, missed hits, out of the park, and other scenarios.

You can use IF Statements or Switches to cycle through different possibilities. You can make a class for each base to keep track of who is on the plate, and if it's free.

if (firstBase.playerOn())
{

Do Something
}

In terms of making each zone, just create a zone class that manage all the zones and keeps track of who is where, and doing what. There are many ways to do what you're asking, but as an example I would just have a class that lists all the players and their status, as well as zones and who is on what zone doing a certain role.

Edited by Rutin

##### Share on other sites
3 hours ago, Fotomac said:

I'd like to know how to trigger varieties of conditions; for example, an uncaught third strike (around 3-4% probability), which is invoked if

As @Rutin pointed out, you use a random number generator, often shortened to RNG.

There are many statistical functions in games that are commonly implemented, and several distribution curves.

Sometimes a number generator will have a family of functions:

class Random { ...

float Float01(); // return a floating point value from 0 to 1
float Float0N( float n ); // return a floating point value from 0 to n
float FloatNM( float low, float high); // return a floating point value from n inclusive to m inclusive
bool CoinFlip(); // return either true or false at random

... etc ...

}

Then you can do something like this:

if( odds >= random.Float01() ) { /* better than odds */ }

else { /* worse than odds */ }

You can also build more helper functions as you see fit.

You can also build adapter functions to turn a flat distribution into a Gausian curve (a bell shape), or a Poisson curve (different bell), or a sigmoid curve (s-shaped), to name a few.

Modern C++ has a bunch of random number generation tools that can help so you don't need to write the libraries from scratch.

Edited by frob
Was editing and it suddenly saved, must have hit Save on accident. Editing to fill rest of message.