Posts

Random Game Log

Just posting this as I haven't been able to work on the project for a few months and trying to reconnect with it. The numbers in parentheses before a play eg (20) or (1) represent the batter's outcome. Inning 1, PEN batting (20) 328 grounds out on a 0-1 count.  Error made; 328 reached 1st safely.  (1) 325 strikes out. 1 out.  (1) 306 strikes out. 2 out.  (1) 301 strikes out. 3 out.  End of inning. Runs: 0, Hits: 0, Errors: 1, Walks: 0, Left on base: 1, Pitches: 13 Inning 1, BRS batting (1) 413 strikes out. 1 out.  (11) 415 singles on a 1-2 count.  (11) 417 singles on a 0-1 count. 415 advances to 2nd.  (1) 409 strikes out. 2 out.  (11) 428 singles on a 2-0 count. 415 advances to 3rd. 417 advances to 2nd.  (1) 407 strikes out. 3 out.  End of inning. Runs: 0, Hits: 3, Errors: 0, Walks: 0, Left on base: 3, Pitches: 20 Inning 2, PEN batting (1) 302 strikes out. 1 out.  (12) 322 doubles on a 0-1 count.  (1) 311 strikes out. 2 ou...

V1.2.0.202104122145 Player database!

Image
After a few days of figuring out how to interact with a database, I’m finally confident that I can interact with a database using code-generated SQL. Linked the player creator to an external data source (presently using a 2004 version MS Access file). Player creator generate player button reads a random first name and surname and (state or country) from separate tables. Player creator now has a button to save the randomised player to the database. Player creator also has a button to save randomised players to database in bulk (user-specified number). Temporarily used a pop-up input box just to get it working. Tested out with 100 players – took 10 seconds. Player data is incomplete at present (eg no pitching attributes) but the basic structure is complete and fields can be added/changed as required.

V1.1.7.202104062135 Counting pitches and more player info

Image
Started by counting pitches to output inning pitch counts to the game log. This didn't take long so I moved on to the player creator. A lot of work done on the player creator this time around. Also began the reconstruction of the hit outcome calculations. Renamed StartPitch procedure to StartPlay and separated into two procedures (StartPlay and StartPitch). StartPlay now calls StartPitch if no pre-pitch play has occurred (eg balk, pickoff attempt). Add pitch counts to StartPitch procedure. Array was already there, just needed to add to it. Outputs pitch count for non-batting team to end of inning summary. Tested and is correct. Added complete range of attributes to player creator form and code for hitting, fielding and pitching. Presently pitching attributes do not have any code. Also added to player creator additional player info (names, height/weight, region and state/country). Presently names and state/country are randomly generated using case selection (will event...

V1.1.6.202103231020 Fixing simmed game opening batter

Fixed the issue of opening batter ID showing as 0 when simming to top of last or simming full game. This was fixed by moving the nextbatter procedure call from the end of the simming loops to the beginning of the loops. Fixed the issue when simming full game, the home team’s full row of buttons were disabled in the line score. Removed the ‘if not simming’ conditions in the toggleHomeInningButton procedure. This game log should be all correct after the recent fixes (the numbers in parentheses are the in-game play codes, just ignore them): Inning 1, PEN batting (30) 327 flies out on a 1-0 count.  Error made; 327 reached 1st safely.  (1) 315 strikes out. 1 out.  (20) 323 grounds out on a 0-0 count. 327 advances to 2nd. 2 out.  (1) 309 strikes out. 3 out.  End of inning. Runs: 0, Hits: 0, Errors: 1, Walks: 0, Left on base: 1, Pitches: TBA Inning 1, BRS batting (1) 423 strikes out. 1 out.  (1) 410 strikes out. 2 out.  (12) 421 doubles on a 0-0 co...

V1.1.5.202103211320 Fixing game log after dropped third strikes and errors

After analysing a whole simmed game, I identified several issues created by the inclusion of dropped third strikes and errors. It seemed that outs and baserunners were not being handled correctly. So as predicted, the logic didn’t work as intended. Also I realised that dropped third strikes were not actually errors so I had to stop them being recorded as such. Fixed dropped third strikes being recorded as errors and the text output to game log because of such. Moved the advanced to 3rd from 2nd (or 1st) and advanced to 2nd from 1st code that was in single, double, groundouts, flyouts, strikeouts (and would have been in many plays added later) to their own procedures ( CheckAdvanceTo3rd and CheckAdvanceTo2nd ). Will probably do the same with all runner movement to avoid duplication of code. Added text ‘advances to 3rd’ and ‘advances to 2nd’ to game log when such activity happens. This will help with debugging but also improve the user experience. Did this with two new Boole...

V1.1.4.202103032300 Basic player creator, line score tidy up and additional play results, including errors

This was a pretty huge four days! Player creator: This idea came to me when I started wondering how I could fulfil my plan of creating 10,000 players at random, at the same time giving them realistic attributes. Added a separate form to generate players which can eventually be saved to database. Presently just generates attributes for contact and power vs L/R based on an entered minimum number of points to allocate, and entered maximum value for bonus points. Bonus points are calculated at random and added to total points to allocate across all attributes. If an attribute is already on 100, that bonus point is wasted. Works pretty well and will be expanded upon. Line score tidy up: This was something that was mostly a quick fix so I thought I may as well get it out of the way now. Now the button for the bottom of the current inning is disabled if playing the top of the inning. Now puts an X in the bottom of the 9th if not played at game end and disables tha...

V1.1.3.202102212105 Fielding positions filled

Image
A long time between sessions. I messed up the post-sim forward display trying to make it more efficient and spent hours trying to fix it. The problem was that I couldn’t revert back to the previous version because I had not made a backup after doing the fielder position logic. So it was either lose all of that work or fix it. So I chose to fix it. Which I did, seven weeks later. Unfortunately in between starting and finishing, there was a week-long camping trip, a change of computer (downgrading from 2017 iMac 27" to 2020 M1 MacBook Air) and (most disruptingly) returning to work. The first four weeks minimum after returning to work are basically a write-off timewise. As a consequence the familiarity with the code and the confidence in making a correction decreases as the time away from it increases. Ironically though in this case it only took a few minutes to come up with a solution once I sat down and revised how it worked. I did resort to a much-resented ‘band-aid’. I can justi...

Baseball game dev diary: V1.1.2.202101012249 0x-1+1 =1 and 1x-1+1=0

Image
Added a team class, primarily to make it so that I could make the buttons on the diamond show up as team colours. ie When team 0 is batting, the base buttons have team 0 colours and the fielding buttons have team 1 colours, and vice versa when the teams switch. Batting buttons (bases etc) worked first go. Fielding buttons took a little longer as there was no existing logic to manage them. 0*-1+1 is 1 and 1*-1+1 is 0. Basically a guaranteed way to produce a 0 when a value is a 1 and get a 1 when a value is a 0. Used here to reverse the batting team value to avoid Ifs when working out the fielding team.

Baseball game dev diary: V1.1.1.202101011620 Adding a diamond

Image
Having overcome the frustration of the last lot of changes, I shifted my focus to improving the interface somewhat. Game log and line score now visible by default. Fixed base buttons getting messed up when simming forward. Was progressing to next batter before checking which base buttons needed disabling, so batter, on deck and in hole were always enabled after simming forward. Line score buttons now clear when rewinding game. Replaced function to generate a rounded up inning number (called 29 times) with an integer variable and add to it when incrementing inning only if int(inning) is not equal to inning number (ie only when progressing to top of an inning). Probably more efficient. Made the start game, sim forward, rewind and sim game menu items not buttons, and the show game log and show line score menu items, not checkboxes. Will remove buttons once I’m satisfied menu items are working correctly. Game log and line score toggles work as required. Moved the call to ButtonsTogg...

Baseball game dev diary: V1.1.0.202012302330 Innings buttons on the fly

Image
All innings buttons (regular and extra) are now generated as the game progresses. So innings 2-9 for example do not show until these innings are started. These arrays of objects are quite complicated to set up (especially when replacing existing variables or arrays with new ones) but they make for much more efficient code.  Generating of innings buttons was done incorrectly resulting in innings of 2 or more over the starting game length not being removed correctly. Code seemed to be creating a whole new set of innings buttons with each inning that was added, instead of just the one inning worth.  Hits and runs now update in the line score (as too will errors when they are included). Per-inning runs also update in the line score buttons.  Per innings totals for hits, runs, errors, walks, runners left on (LOBs) stored in two-dimensional arrays (0,1 is team 0 inning 1 etc) with 0,0 and 1,0 being the totals, making use of what would have been an empty array space anyway. Thes...

Baseball game dev diary: V1.0.2.202012232200 Lineup listboxes put to use and line score

Image
 When selecting a player in the lineup list boxes, that player’s info is shown in the player info box. Switches between the two lineups and base buttons (last player clicks deselects any player selected in the lineup) effectively. Reset game button text changed to ‘rewind game’ to better communicate its purpose (rewind to top of the first, no outs, same lineup).  Fixed bugs when reset game button is clicked, the player info did not clear if showing, and the lineup listboxes did not repopulate after being cleared. Array of buttons created for game log form, for line score. Procedure added to increment it by one, which will be added to extra innings code.  Recreated listboxes for lineups as array of listboxes.  Short demo video showing player info when clicking bases, on deck, in hole and lineups, as well as innings being added to line score (presently a button but will be made automatic when extra innings are required. 

Baseball game dev diary: V1.0.1.202012220933 Addition of on deck / in hole player info

Image
Added buttons for on deck and in hole player info. Runs existing procedure for show player info when clicked. Uses bases array spaces 8 and 9 which were intended for this purpose right from the start. Adjusted code to toggle buttons and on-screen info at various situations such as immediately after simming to last inning or end of game. Removed call to ‘buttons toggle simmed entire’ as it was the same code as ‘buttons toggle game over’ and will probably not differ in the future. Added listboxes showing lineups to make it easier to confirm it is cycling through lineups correctly. Tidied up the end of game and end of inning code so that simmed and manual games used the same code. See below. Well, this was an absolute nightmare of a 'minor' addition. The on deck/in hole stuff was no problem as I had already planned for it with the bases array. The lineup listboxes took literally five minutes. But in amongst all of that I decided to try and refine the end of game code as I realised...

Baseball game dev diary game log: 'the one that got away in the bottom of the 9th'

Simulating a few games to test the walk-off code and the following game was generated. The away team was ahead 11-6 going in to the bottom of the 9th: Inning 9, Home Team batting 412 doubles on a 0-1 count.  427 singles on a 0-0 count.  409 doubles on a 1-2 count. 412 scores.  422 strikes out. 1 out. 420 doubles on a 0-0 count. 427 scores. 409 scores.  401 singles on a 1-1 count.  423 singles on a 0-0 count. 420 scores.  417 singles on a 0-2 count.  411 singles on a 1-0 count. 401 scores.  412 strikes out. 2 out. 427 strikes out. 3 out. Inning 10, Away Team batting 312 singles on a 0-0 count.  304 singles on a 0-0 count.  306 strikes out. 1 out. 303 strikes out. 2 out. 323 singles on a 0-2 count.  317 strikes out. 3 out. Inning 10, Home Team batting 409 homers on a 0-1 count.  Final:  Team 0 (away): 11 Team 1 (home): 12 Winner: Team 1 Pure randomness at play right there :)

Baseball game dev diary: V1.0.0.202012171941 Addition of the game log (with video!)

Image
I decided to call this version the first release as it's technically simulating complete games now. This turned out to be a significant update in terms of the amount of work involved to get the game log to a reasonably informative state. Due to the sequence of updates to data and outputting of data, lots of debugging and rearranging of code was required for things to display correct without compromising the integrity of the code (in other words – no band aids allowed!). The whole game can now be traced through the game log, play by play. Functionality will be added later to be able to export as a text file. Each plate appearance is also documented temporarily in the plate appearance log (ie ball 1, ball 2, strike 1, strike 2, strike 3 etc). The result of the plate appearance is output to both the plate appearance log and the game log. The PA log will strictly be for batter stuff only – ie steals, wild pitches etc when they are implemented will go in the game log only.  After clicki...

Baseball game dev diary: Short demo video

Image
Recorded in PowerPoint on Windows, edited in iMovie on MacOS, many audio snippets replaced using Voice Memos on iPad. Gotta love technology. Subscribe to my YouTube channel while you're there. If I get over 100 I win a prize, or something like that. While I'm here I'll address the elephant in the room - yes, I'm using Visual Basic. It makes it easy for me, I get valuable feedback from the IDE and the debugging it offers and I get to experiment with using classes and objects in a familiar environment. All of this will end up being converted to some algorithms anyway.

Baseball game dev diary V0.5.3.202012090015 Giving the base buttons meaning

Image
Disabled base buttons at beginning of plate appearance unless occupied. Remove the code to reset bases in StartGame as that procedure called ResetGame procedure, which called ResetBases anyway.  Base buttons now display some player info when clicked, by searching for the playerID within the array of objects.  Added public variable for squadSize instead of hardcoding 28 into several places in code.  All player statuses set to 3 (benched) at start of game. Changed to 1 when put into game. Lineups now populate from squads array of player class objects with random players, not just first nine. Used a while loop to generate a random number up to squadSize and if player status was not 3, repeat random number generation. Ended up spending a lot longer than intended on this one (about 3 hours). Worth the extra time as I now better understand creating instances of classes and am fairly satisfied with the random generation of the lineup (even though that part really only took about...

Baseball game dev diary V0.5.2.202012032305 Prepping for pitchers

Created a player class. No such thing as a record anymore. Chose Class over Structure due to some web page recommendation. Was reminded that when variables within classes are set to private, you can’t change them outside of that class. So created a Property for each variable within the class that uses the Get and Set commands to read and write the values in the instances of a class. The private variable starts with an underscore, the property is the same name without the underscore. This should make coding the main game much easier. Presently, properties are: id, last name, first name, primary position, secondary positions, player type, pitches thrown, energy, stamina and game status. Created 28 instances of players for each team (scaled back from 40 for now). Made the base buttons clickable and each calls a procedure that is designed to show info for the batter/runner, but only when base is occupied. Later on will disable the buttons (including batter) if base is not occupied (or in b...

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 a...

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...