Source code: com/textflex/txtfl/DrivePlay.java
1 /* tXtFL: a text-based football simulator
2 * Copyright (C) 2002-3 Text Flex
3
4 * This file is part of tXtFL.
5
6 * tXtFL is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22 package com.textflex.txtfl;
23
24 import java.io.*;
25
26 /** A record of the statistics for a given play during the given drive.
27 The record contains values such as the time, team scores, play call,
28 and yardage. Coaches can refer back to these records to keep a
29 tally on team performance and to track individual players' statistics.
30 */
31 public class DrivePlay implements Serializable {
32
33 /* Background game-time stats */
34 // All values refer to those at the start of the play.
35 // of = offense
36 // df = defense
37 private int time = 0;
38 private int quarter = 0;
39 private String offense = null;
40 private String defense = null;
41 private String yardline = null;
42 private int down = 0;
43 private int startYdsToGo = 0;
44 private String ofPlayType = null; // category of the play call
45 private String dfPlayType = null;
46 private String ofPlay = null; // the specific play call
47 private String dfPlay = null;
48 private int ofScore = 0;
49 private int dfScore = 0;
50
51 /* The play's outcome */
52 // A primary play stat is the basic data relevant to every play.
53 // Supplemental play stats are added as necessary to record extra
54 // players and outcomes for individual components of each play,
55 // such as an interception or safety.
56 private String result = null;
57 private PrimaryPlayStat primPlayStat = null;
58 private PlayStat[] supplPlayStats = new PlayStat[1];
59 private int supplPlayStatsIndex = 0;
60 // true if the next play should be part of a new drive, as after a
61 // kick-off, eg
62 private boolean nextPlayNewDrive = false;
63
64 /* Constructs a drive play from game-time stats
65 @param aOffense the team on offense
66 @param aDefense the team on defense
67 @param aDown the down
68 @param aStartYdsToGo the yards to go to reach a first-down
69 @param aYardline the yardline, on a scale from 0-100
70 @param aQuarater the game's quarter
71 */
72 public DrivePlay(String aOffense, String aDefense, int aDown,
73 int aStartYdsToGo, String aYardline,
74 int aQuarter,
75 int aTime, int aOfScore, int aDfScore) {
76 offense = aOffense;
77 defense = aDefense;
78 down = aDown;
79 startYdsToGo = aStartYdsToGo;
80 yardline = aYardline;
81 quarter = aQuarter;
82 time = aTime;
83 ofScore = aOfScore;
84 dfScore = aDfScore;
85 }
86
87 /** Sets the play call and category.
88 @param aOfPlayType offensive play category
89 @param aOfPlay specific offensive play call
90 @param aDfPlayType defensive play category
91 @param aDfPlay specific defensive play call
92 */
93 public void setPlays(String aOfPlayType, String aOfPlay,
94 String aDfPlayType, String aDfPlay) {
95 ofPlayType = aOfPlayType;
96 ofPlay = aOfPlay;
97 dfPlayType = aDfPlayType;
98 dfPlay = aDfPlay;
99 }
100
101
102
103 /** Adds a primary play stat.
104 Primary play stats consist of the parameters relevant to every
105 play, such as the players who start with, end with, or defend
106 against the ball as well as distance information.
107 @param playType the type of play
108 @param deliverer the player who starts with the ball and delivers
109 it to someone else or keeps it
110 @param deliveryDist the distance the ball travels to the recipient
111 @param recipient the player who receives the ball; if the deliverer
112 keeps the ball, the deliverer is the same as the recipient
113 @param recipientDist the distance the recipient carries the ball
114 @param defender the player who defends against the recipient
115 */
116 public void addPrimPlayStat(String playType, String deliverer,
117 int deliveryDist, String recipient,
118 int recipientDist, String defender) {
119 primPlayStat
120 = new PrimaryPlayStat(playType, deliverer, deliveryDist,
121 recipient, recipientDist, defender);
122 }
123
124 /** Adds a supplementary play stat.
125 An unlimited number of supplementary play stats can be added,
126 each one referring to a separate component on top of the primary
127 play stats for each play. For example, interceptions warrant
128 records of who caught the ball, how far the player carried it,
129 and who eventually tackled him.
130 @param playType the type of play
131 @param recipient the player who receives the ball; if the deliverer
132 keeps the ball, the deliverer is the same as the recipient
133 @param recipientDist the distance the recipient carries the ball
134 @param defender the player who defends against the recipient
135
136 */
137 public void addSupplPlayStat(String playType, String recipient,
138 int recipientDist, String defender) {
139 if (supplPlayStatsIndex >= supplPlayStats.length)
140 supplPlayStats = (PlayStat[])Team.growArray(supplPlayStats);
141 supplPlayStats[supplPlayStatsIndex++]
142 = new PlayStat(playType,
143 recipient, recipientDist, defender);
144 }
145
146 /** Sets the result of the play, such as "complete" for completed
147 passes or "no good" for missed field goals.
148 @param aResult the result
149 @see Txtfl
150 */
151 public void setResult(String aResult) { result = aResult; }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 /** Gets the result.
168 @return the result
169 @see #setResult(String)
170 */
171 public String getResult() { return result; }
172
173 /** Gets the flag for whether the next play should be part
174 of a new drive.
175 @return <code>true</code> if the next play will be part
176 of a new drive
177 */
178 public boolean getNextPlayNewDrive() { return nextPlayNewDrive; }
179
180 /** Gets a summary of the game-time statistics in prose form,
181 including scores and down information.
182 @return summary statement
183 */
184 public String getSummaryGameStats() {
185 String summary = "Offense: " + offense + ", "
186 + Txtflo.convertDown(down) + " and " + startYdsToGo
187 + " at the " + yardline
188 + "\n" + Txtflo.convertTime(time) + " in "
189 + Txtflo.convertQuarter(quarter) + ", "
190 + Txtflo.convertScores(ofScore, dfScore, offense, defense);
191 return summary;
192
193 }
194
195 /** Gets a summary of the teams' play choices in prose form.
196 @return summary statement
197 */
198 public String getSummaryPlayChoices() {
199 String summary =
200 "Offensive play: " + ofPlayType + " - " + ofPlay
201 + "\nDefensive play: " + dfPlayType + " - " + dfPlay
202 + "\nResult: " + result;
203 return summary;
204 }
205
206 /** Gets a summary of the play's events, including key players
207 and distances.
208 @return summary statement
209 */
210 public String getSummaryPlay() {
211 String type = primPlayStat.getPlayType();
212 String deliverer = primPlayStat.getDeliverer();
213 int deliveryDist = primPlayStat.getDeliveryDist();
214 String recipient = primPlayStat.getRecipient();
215 int recipientDist = primPlayStat.getRecipientDist();
216 String defender = primPlayStat.getDefender();
217 // TODO: make summaries specific for each play type;
218 // eg field goals have no necessary recipient or defender vals,
219 // and rushes have no valid result fields
220 String summary =
221 deliverer + " attempted a " + type + " of "
222 + deliveryDist + " yards to "
223 + "\n" + recipient + " while "
224 + defender + " defended,"
225 + "\nresulting in a " + result
226 + " with a carry for " + recipientDist + " yards.";
227
228 // add a sentence for each supplementary info
229 if (supplPlayStatsIndex > 0) {
230 for (int i = 0; i < supplPlayStatsIndex; i++) {
231 String supplType = supplPlayStats[i].getPlayType();
232 String supplRecipient = supplPlayStats[i].getRecipient();
233 int supplRecipientDist = supplPlayStats[i].getRecipientDist();
234 String supplDefender = supplPlayStats[i].getDefender();
235 summary = summary
236 + "\nA " + supplType + " occurred, where "
237 + supplRecipient + " brought the ball "
238 + supplRecipientDist + " yards until tackled by "
239 + supplDefender + ".";
240 }
241 }
242 return summary;
243 }
244
245 /** Parses together all the summary statements, consisting of
246 the game-time stats, play calls, and play events.
247 @return conglomerate summary statement
248 @see #getSummaryGameStats()
249 @see #getSummaryPlayChoices()
250 @see #getSummaryPlay()
251 */
252 // TODO: include all info
253 public String getSummary() {
254 String summary = "\n" + getSummaryGameStats() + "\n"
255 + getSummaryPlayChoices() + "\n" + getSummaryPlay();
256 return summary;
257 }
258
259 }
260
261
262 /** Record of all the major play statistics.
263 */
264 class PlayStat implements Serializable {
265 private String playType = null; // eg pass, kick-off
266 private String recipient = null; // player receiving the ball
267 private int recipientDist = 0; // distance recipient carries ball
268 private String defender = null; // player defending against recipient
269
270 /** Constructs an empty play statistical record.
271 */
272 public PlayStat() {
273 }
274
275 /** Constructs a complete statistical record.
276 @param aPlayType the type of play, eg pass, kick-off
277 @param aRecipient the player who receives the ball
278 @param aRecipentDist the distance that the recipient carries the ball
279 @param aDefender the player who defends against the recipient
280 */
281 public PlayStat(String aPlayType,
282 String aRecipient, int aRecipientDist, String aDefender) {
283 playType = aPlayType;
284 recipient = aRecipient;
285 defender = aDefender;
286 }
287
288 /** Gets the play type.
289 @return playType the play type, eg pass, kick-off
290 */
291 public String getPlayType() { return playType; }
292
293 /** Gets the name of the recipient.
294 @return name
295 */
296 public String getRecipient() { return recipient; }
297
298 /** Gets the distance that the recipient carried the ball.
299 @return the distance, in yards
300 */
301 public int getRecipientDist() { return recipientDist; }
302
303 /** Gets the name of the defender.
304 @return name
305 */
306 public String getDefender() { return defender; }
307
308 }
309
310
311
312
313
314
315 /** Record of the play statistics common to and necessary for almost
316 all plays. Adds the ball deliverer and delivery distance to
317 the <code>PlayStat</code>
318 */
319 class PrimaryPlayStat extends PlayStat {
320 private String deliverer = null; // player who starts w/ the ball
321 private int deliveryDist = 0; // distance deliverer sends the ball
322
323 /** Constructs the complete statistic.
324 @param aPlayType the type of play, eg pass, kick-off
325 @param aDeliver the player who starts with the ball
326 @param aDeliveryDist the distance the deliverer sends the ball
327 @param aRecipient the player who receives the ball
328 @param aRecipentDist the distance that the recipient carries the ball
329 @param aDefender the player who defends against the recipient
330 */
331 public PrimaryPlayStat(String aPlayType, String aDeliverer,
332 int aDeliveryDist, String aRecipient,
333 int aRecipientDist, String aDefender) {
334 super(aPlayType, aRecipient, aRecipientDist, aDefender);
335 deliverer = aDeliverer;
336 deliveryDist = aDeliveryDist;
337 }
338
339 /** Gets the name of the ball deliverer.
340 @return name
341 */
342 public String getDeliverer() { return deliverer; }
343
344 /** Gets the distance that the ball deliverer sends the ball.
345 @return distance in yards
346 */
347 public int getDeliveryDist() { return deliveryDist; }
348 }