Source code: com/textflex/txtfl/Play.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 package com.textflex.txtfl;
22
23 import java.util.*;
24 import java.io.*;
25
26 /** Football plays, whether offensive or defensive, passing, rushing,
27 kickoff, or the like.
28 */
29 public class Play implements Cloneable, Serializable {
30 private String name = ""; // name of play; unique for category and side
31 private String category = ""; // type of play; eg pass or rush
32 private String side = ""; // offense or defense
33 private int dist = 0; // focal point of the play; eg throwing distance
34 private String width = ""; // side of field, from offense's perspective
35 private String[][] players = new String [15][2]; // player assignments
36 private int playersIndex = 0; // number of players assigned
37
38 /** Constructs a play to set manually.
39 */
40 public Play() {
41 }
42
43 /** Gets the yards along the width of the field from the centerline.
44 Right is taken as positive; left is negative.
45 @return width in yards from centerline
46 */
47 // TEMP: generalize width designations and yardage
48 public int getWidthYds() {
49 if (width.equalsIgnoreCase("l")) {
50 return -15;
51 } else if (width.equalsIgnoreCase("r")) {
52 return 15;
53 } else {
54 return 0;
55 }
56 }
57
58 /** Gets the name of the play.
59 @return name of play
60 */
61 public String getName() { return name; }
62 /** Gets the category of the play.
63 @return category of play
64 */
65 public String getCategory() { return category; }
66 /** Gets the side of the play.
67 @return side of play
68 */
69 public String getSide() { return side; }
70 /** Gets the distance of the play.
71 The distance refers to the focal point of the play from the line
72 of scrimmage.
73 For example, the distance of a passing play would be the point of
74 reception, and that of a defensive pass/run blocking play would
75 be where the defense concentrates its coverage.
76 @return distance of play
77 */
78 public int getDist() { return dist; }
79 /** Gets the width setting of the play.
80 The setting can be either left, middle, or right, referring to the
81 side of the field from the offense's perspective.
82 @return width setting of play
83 */
84 public String getWidth() { return width; }
85 /** Gets the player assignments of the play.
86 The array consists of 2-element arrays, which in turn consist of
87 first a position and then the player assignged to it.
88 @return player assignments of play
89 */
90 public String[][] getPlayers() { return players; }
91
92 /** Sets the name of the play.
93 @param aName name of play
94 @see #getName()
95 */
96 public void setName(String aName) { name = aName; }
97 /** Sets the category of the play.
98 @param aCategory category of play
99 @see #getSide()
100 */
101 public void setCategory(String aCategory) { category = aCategory; }
102 /** Sets the side of the play.
103 @param aSide side of play
104 @see #getDist()
105 */
106 public void setSide(String aSide) { side = aSide; }
107 /** Sets the distance of the play.
108 @param aDist distance of play
109 @see #getSide()
110 */
111 public void setDist(int aDist) { dist = aDist; }
112 /** Sets the width setting of the play.
113 @param aWidth width of play
114 @see #getWidth()
115 */
116 public void setWidth(String aWidth) { width = aWidth; }
117
118 /** Adds the player to the player assignment list, a array of
119 2-element arrays, consisting of first the position and then
120 the name of a specifically assigned player, if necessary.
121 Default maximum of 15 players in the player assignment list.
122 @param player player to add
123 */
124 public void setPlayer(String player) {
125 // position=player assigns a specific player to the position
126 StringTokenizer t = new StringTokenizer(player, "=");
127 if (playersIndex < players.length) { // max 15 by default
128 players[playersIndex][0] = t.nextToken();
129 if (t.hasMoreTokens())
130 players[playersIndex][1] = t.nextToken();
131 else
132 players[playersIndex][1] = ""; // "" if no specific assignment
133 playersIndex++;
134 }
135 }
136
137 /** Checks if the position has already been assigned in the play.
138 @param position position to check
139 @return true if the position has been assigned
140 */
141 public boolean isPositionSet(String position) {
142 boolean found = false;
143 for (int i = 0; i < playersIndex
144 && !(found = players[i][0].equalsIgnoreCase(position)); i++);
145 return found;
146 }
147
148 /** Checks if the play is an offensive one.
149 @return true if the play is offensive
150 */
151 public boolean isOffense() {
152 return (side.equalsIgnoreCase("offense")) ? true : false;
153 }
154
155 /** Clones the object in compliance with the Cloneable interface.
156 @return this object as a clone
157 */
158 public Object clone() {
159 try {
160 Play cloned = (Play)super.clone();
161 return cloned;
162 } catch (CloneNotSupportedException e) {
163 return null;
164 }
165 }
166
167 }