# [BaseBotGame] 2022-10-10 Pitcher (Revised) ### Object Pitcher pitch - Input: `Pitcher`, `PitchSpace` - Output: a pitch throwed by a pitcher and if it's a strike ### Behavior 1. Pitcher pitches a ball randomly in pitch space 2. Pitch space object determines whether it's a strike. ### Architecture Basic Class: 1. `Grid` object has `x`, `y` coordinate 2. `Pitch` object stands for a pitching result including position represented by `Grid` 3. `FieldPosition` enum represents defense position of baseball player ```mermaid classDiagram Pitch --> "1" Grid : pos class Grid { Int x Int y } class Pitch { Grid position() } class FieldPosition{ <<enumeration>> P C B1 B2 B3 SS LF CF RF DH } ``` Pitcher: 1. `BaseballPlayer` has `name`, `number`, and `fieldPosition` 2. `Pitcher` inherits from `BaseballPlayer` and extends `pitch` method 3. `pitch` method takes `PitchSpace` as input and return pitch results in class of `Pitch` PitchSpace: | | | | | | |---|---|---|---|---| | x | x | x | x | x | | x | o | o | o | x | | x | o | o | o | x | | x | o | o | o | x | | **x** | x | x | x | x | 1. Origin is at the left bottom grid(bold **x**) and goes up and right. 2. Attribute `width` and `height` limit the range of space. 3. 2 `Grid` form a strike zone plane: `leftBotOfStrikeZone` and `rightTopOfStrikeZone`. 4. `isStrike` method takes a `Pitch` as input and return it's a strike if the position in the strike zone. ```mermaid classDiagram Pitcher <|-- BaseballPlayer Pitch --> "1" Grid : pos PitchSpace --> "1" Grid : leftBotOfStrikeZone PitchSpace --> "1" Grid : rightTopOfStrikeZone class Grid { Int x Int y } class Pitch { Grid position() } class BaseballPlayer { String name Int number FieldPosition fieldPos String getName() Int getNumber() FieldPosition getPos() } class Pitcher { Pitch pitch(PitchSpace space) } class PitchSpace { Int width Int height void setupStrikeZone(Int leftBoundary, Int rightBoundary, Int bottomBoundary, Int topBoundary) Int getWidth() Int getHeight() Bool isStrike(Pitch pitch) } ``` ### Implementation Random pitch - pitch result is consist of position only now, but it will contains stuff, trajectory ... in the future. ```c++ Pitcher.pitch(const PitchSpace& space) { const int max_x = space.getWidth(); const int max_y = space.getHeight(); pitch.pos.x = rand() % max_x pitch.pos.y = rand() % max_y } ``` Strike Rule: ```c++ bool isStrike = leftBot.x <= pitch.pos.x <= rightTop.x && leftBot.y <= pitch.pos.y <= rightTop.y; ```