Posts

Showing posts from November, 2020

Baseball game dev diary V0.5.1.202011302300 cleaning up after pitches

Image
Enabled/disabled buttons according to situation. Not very confident that it’s efficient code, but it works. If already in top of last inning, sim to top of last button is now disabled.  Button also shows which inning is the top of the last on the button text. Changed startgame procedure to call resetgame and resetinterface instead of duplicating that same code. Wondered why extra base hits stopped happening. Discovered that on a hit the checks for a number between 0 and 1 checked correctly for a homer (>0.85) but all others checked integers 80, 50, 10 etc rather than 0.8, 0.5, 0.1 etc. This session was really just about tidying up after adding the individual pitches and the issues that created. Interface evolved a little too (note that Winner at the bottom is a static label, when the game ends the winning team number appears after it):

Baseball game dev diary V0.5.0.202011300120 pitches pitches pitches!

Added a lastInning variable so that end of game check is in the ‘last inning or later’, rather than 9th inning or later. Some games could be 7, or even 3 innings. Inspired by a review of my own dev diary, 0.1.1. Tested 9, 2, 1, and 90 inning games to ensure reliability, by simming to the top of the last inning. (Incidentally, the 90 inning game was a 153-175 scoreline).  At last – pitches! Presently it simply randomises whether it’s a ball or a strike and whether a swing is taken, and whether the swing connects (connection = hit, miss = strike), but it’s a start (all are 50/50). If this seems simple, it’s not. Presently called strike and swinging strike are the same value. This will need to be changed.  Had to move ballCount, strikeCount and resultText scope to global.  Had to implement ball 4 and strike 3 logic. As a result created Strikeout procedure.  Put in basic hit logic so that if connection is made, determine whether single or extra base hits. Presently set as a single for code

Baseball game dev diary version 0.4.2.202011252200: 10,000 games in less than a second

Image
I wonder how accurate it is to timestamp before and after code to determine how much time elapses during. Oh well, that's how I'm going to see how long things take. Changed txtOutput to display who’s up before plate appearance begins, and play result after. Rearranged beginPlateAppearance so check for winner and switching batter is after output of data. Public variables startmilliseconds and finishmilliseconds can be reused anywhere for checking time taken for various loops such as simming game. Sim Entire Game button added to sim everything from setting up teams to final pitch. Added time stamp before and after simming loop. Turns out it takes less than 1 millisecond to sim a game at present. 10,000 games simulated in 0.3 seconds! However, would need to also consider individual pitches, substitutions (presently no subs, no pitcher changes, and pitch counts are randomised). Would probably get away with less than 5 milliseconds per game with these included.  Winner variable move

Baseball game dev diary version 0.4.1.202011251825: improving output info

Image
Added to end of inning data output procedure to display ‘Middle of’ and ‘End of’ if 3 outs. Added simming Boolean variable so that checks can be made whether to do certain things if simming, such as outputting to the display. Moved inning suffix (st, nd, rd, th) calculation to its own procedure to avoid duplication because of point #1 above. Going to do some testing of how long simming games will take with intention of simming 2500+ games for a season - Majors and Minors. This is going to be very interesting. Mindful of the fact that the times will be faster than expected as there is presently no AI or individual pitches. Here's what it looks like so far - note the runners on 2nd and 3rd after a double and no runner on 1st (-1 indicates base is empty).

Baseball game dev diary version 0.4.0.202011242245: hits, extra base hits, walks and runs

Image
 Moved code for plate appearance and set up batter from within event handlers to their own procedures and had the event handlers call them. This is particularly so that the plate appearances can be simmed without having to call the event handlers which gives errors as parameters  are not specified for parameters for calling object and event arguments. Makes sense to do this anyway – ie have event handlers with minimal code. The idea to move code out of event procedures is probably good practice anyway, and definitely ensures that simming games quickly will now be possible quite easily. Quite proud of this idea. Changed Skip to top of 9th procedure so that it actually sims up to the 9th rather than skipping the innings completely.  Ensured the Next batter button was disabled at end of game by moving button toggles before the check for winner and disabling next batter button in postgame procedure. Added a new procedure to checkBases at the end of each pitch. This counts runs scored (play

Baseball game dev diary version 0.3.0.202011232200: adding the bases

 Added subs for hitSingle, hitDouble, hitTriple and hitHomer. Only hitSingle to be utilised initially. Sidenote: turns out you can’t use single and double as sub names. Added buttons for bases, and code to display playerIDs in button text for batter and last three batters on bases. Added output all data before pitch procedure to Next batter button to have a separation between data shown at end of play and start of next play. Needed to invoke two clicks from user per play – one to prepare for the next plate appearance (nextBatter) and one to execute the next plate appearance (BeginPA). Added a procedure for postPlayDataUpdate for this same reason – end of play display would be lost by the next before pitch display if there isn’t a user interruption between them.  Broke the check end of inning down into two separate procedures instead of being in the begin plate appearance procedure. Checkendofinning() simply calls switchTeams if outs is at 3, and SwitchTeams does the switch and resets o

Baseball game dev diary: UI preview

Image
 As you can see from the screenshot, my priority is content first, presentation later. So presently it's just a case of get the data onto the screen and make sure it's correct.

Baseball game dev diary version 0.2.1.202011192214: rewriting 60 lines of code as 4

Image
 Changed the temporary setLineUp and setSquad procedures to use loops instead of repetitive code. The 300 + (teamtemp*100) + playeridtemp means that for team 0 the squad IDs are set as 300+ and for team 1, set as 400+.  Doing this was a no-brainer, not sure why I previously had 60+ lines in setSquad and 20+ lines in setLineUp. Probably a combination of it being late when I thought of it and figuring it was only temporary anyway so not thinking too much about it. Identifying when to use loops is usually what I'm good at. I'm so embarrassed. You may have worked out by now I'm doing this in Visual Basic. This is simply because it's the language that I can most easily code in off the top of my head, creating a simple user interface is super easy (rather than having to create a web page which is much more time consuming) and I can then turn them into algorithms later which can be converted to whichever language I want (most likely Python or PHP). It was at this point that it

Baseball game dev diary version 0.2.0.202011112223: multiple players

 Added array squad to include all available players for the game, including those in lineup, bench and all pitchers. Should this be an array of records, the one that will include attributes, name, positions etc? It should include a value for whether the player is currently in the game, has been taken out or still yet to play. Because any player could theoretically pinch, will need to identify players further as Position, Starter, Long relief, Middle relief, Setup or Closer. Should add an identifier for an Opener too as it could become more common. Possibly use P, S, O, L, M, U, C. This will be important when selecting players for relief but also making prioritising bullpen when subbing in a pitcher, or bench when pinch hitting. Pinch runner on the other hand is another story (eg Matz).   Added start game button to populate the squads and lineups and reset all game data to starting points.  Added code so that plate appearances cycle through lineups (if last batting player was > #8 th

Baseball game dev diary version 0.1.2.202011102300: Adding lineups

Image
 Changed runs array data type to UShort (previously Integer) as it will never have negative values and goes up to 65000 approx. Added public declarations for lineUp multidimensional array: teams 0-1 and batting order positions 1-9 (position 0 is ignored).  Added (in hindsight, very inefficient) procedure to populate the lineUps. Presently data is fixed for testing purposes. PlayerIDs will be stored in the Lineups, not jersey numbers. The jersey numbers can then be retrieved from database along with name and attributes. Note that PlayerIDs are set as Integer to take into account hundreds of millions of players. Left as signed data type because values could be -1 (eg when there is nobody in the lineup spot or base is empty). Added resetBases procedure to run at end of inning. Hopefully I'll remember its there when I work on the 'moving around the bases' part. Not sure why I did it so early, obviously I was planning ahead, judging from these notes:

Baseball game dev diary version 0.1.1.202011092030: minor edits

Image
 Considered modifying call to checkForWinner function so that it only calls past inning 8 however decided not to because some games may be shorter in future versions. Moved plate appearance result generation into its own function. Reset button now also resets display. Obviously this session was more thinking less doing...

Baseball game dev diary version 0.1.0.202011082350: picking a winner

Added call to checkForWinner function after each play, regardless of when. It checks scores when 3 outs in the top of the 9th, 3 outs in the bottom of the 9th and later, and after each play OR PITCH in the bottom of the 9th and later. If there is a winner (returns 0 or 1) it calls postgame procedure, otherwise (returns -1) adjusts inning if required. Added postgame procedure to output winning team to text box and disable plate appearance button.  Added reset button to reset all progress and start from inning 1 for testing purposes. Can be reset at any time. Improved form to have separate labels for each variable (inning, balls, strikes, outs, runs). Created code to convert inning to ‘top of’ (if a ½ value) or ‘bottom of’ (if whole number) and display inning number with correct suffix st, nd, rd, th etc (unless inning 121 is reached, in which case the inning number suffix won’t be the game’s most obvious problem). Moved output of data to form to its own procedure. Added skip to start of

Baseball game dev diary version 0.0.2.202011012359: counting innings

Added two teams, outs count and runs count; switched between teams when 3 outs using activeTeam variable (0 for away team, 1 for home team). Added innings counter, set to increment by 0.5 every 3 outs. I was really proud of the innings counter idea. Top of the inning (away team) is x.5 (eg 1.5 is the top of the 2nd), bottom of the inning (home team) is x.0 (eg 9.0 is bottom of the 9th). Planned it this way to make use of the integer for home team, non-integer for away team. Man, what is with finishing just before midnight? No wonder I'm so tired all the time.

Baseball game dev diary version 0.0.1.202010242359: random plate appearances

Image
Set up as single plate appearance button with randomised ball and strike counts. Result randomised between 0 and 9: 0: strikeout 1-6: in play, out 7-8: hit, run 9: walk or HBP, run Output result and count to text box. Wish I'd taken a screenshot. Just imagine three numbers in a single textbox and that's all it was. That's pretty much it for the first version. Not my proudest moment in programming, but then again, it was probably about 11.30pm when I sat down to start. But hey, the hardest part is starting. It's all smooth sailing from this point on. Incidentally, because it had been so long since I did any program design that relied on user input to progress through the program, it didn't occur to me that using a loop to cycle through innings would be impractical. Hence I pretty much ignored my scratchings above, and will probably ignore the below too.

Writing a baseball game

Image
Baseball is the perfect sport for which to create a simulator. Every season, every game, every play, every pitch, every player  is comprised of a set of numbers. The choice of pitch, the target pitch location, how many outs, the batter's count, who's on base and where... and that's only part of what's going through the pitcher's  head even before a pitch is thrown. Then there's the batter, the baserunners, the fielders, the Manager, the General Manager... It's no wonder those who don't follow the game have trouble getting their heads around how it all works. Who would be crazy enough to try and recreate that in simulation form, AI logic and all? Well, this guy, as it turns out (in addition to a few awesome development studios who have had many years head start).  Yes, I finally pulled my finger out and started the 'baseball simulator' project that has been traversing my thoughts for the last year. The few scribbles I jotted down pre-COVID either

Baseball, but first... Monopoly.

Image
It's been a good five years since I've done any serious algorithm design; most of the effort since I last taught it has been put into databases and querying. No more perfect an opportunity to blend the two than baseball, the thing that occupies around 75-80% of my brain's attention. But more on that next time. Last time I did something of this calibre was create a 2-6 player Monopoly game back in 2014. That went well. Laid the foundation for a full Monopoly game, following the official rulebook rules. Then I decided to disable the user interface and automatically simulate thousands of turns in a game.  Based on simulating seven games of two to six players, with varying maximums of 6,000 to 12,000 rolls per game (see table below for final tallies), I can confirm that which was already known, which is that the most occupied board location is Jail, with nearly three times as many end-of-turn occupants as its nearest competition: Trafalgar Square (red #3), Vine Street (orange #