Source code: com/textflex/txtfl/Txtflo.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.text.*;
25
26 /** The tXtFL simulator is the talk of the crowd. Or, if not, tXtFL
27 is at least doing a lot of the talking. As a text-based football
28 simulator, the tXtFL code needs to implement a lot of talking
29 to communicate with the user. Some talk is generic, robotic, such as
30 the statistical displays, but most of the rest needs to be fluid,
31 dynamic, <i>flo</i>wing. <code>Txtflo</code> tried to let the talk
32 flow like a river, melliflous as a sportscaster. Of course,
33 the talk can get technical, too, but never boring. The game
34 itself hopefully ensures that.
35
36 <p><code>Txtflo</code> mostly consists of static methods to access
37 from anywhere within tXtFL classes. Just supply the necessary
38 team names, scores, or the like, and <code>Txtflo</code> should
39 do the rest. Then the coder can rest.
40 */
41 public class Txtflo {
42 // downs
43 private static MessageFormat ordinalDowns
44 = new MessageFormat("{0,choice,1#1st down|2#2nd down|3#3rd down|"
45 + "4#4th down|4<something bigger}");
46 // quarters
47 private static MessageFormat ordinalQuarters
48 = new MessageFormat("{0,choice,0#the pre-game|1#the 1st quarter|"
49 + "2#the 2nd quarter|3#the 3rd quarter|"
50 + "4#the 4th quarter|4<overtime}");
51 // numbers
52 private static NumberFormat numFormat = NumberFormat.getNumberInstance();
53
54 /** Contructs an empty object.
55 */
56 public Txtflo() {
57 }
58
59 /** Converts a down number into a ordinal format.
60 @param down down number
61 @return down number in ordinal format
62 */
63 public static String convertDown(int down) {
64 return ordinalDowns.format(new Object[] { new Integer(down) });
65 }
66
67 /** Converts a quarter number into a ordinal format.
68 @param quarter quarter number
69 @return quarter number in ordinal format
70 */
71 public static String convertQuarter(int quarter) {
72 return ordinalQuarters.format(new Object[] { new Integer(quarter) });
73 }
74
75 /** Converts the time from seconds to <code><i>min</i>:<i>sec</i></code>
76 format.
77 @param seconds time in seconds
78 @return time in minutes and seconds
79 */
80 public static String convertTime(int seconds) {
81 numFormat.setMinimumIntegerDigits(2);
82 return (int)(seconds / 60) + ":" + numFormat.format(seconds % 60);
83 }
84
85 public static String convertScores(int score1, int score2,
86 String team1, String team2) {
87 String winning = null;
88 if (score1 > score2) {
89 winning = team1;
90 } else if (score1 == score2) {
91 winning = "tied";
92 } else {
93 winning = team2;
94 }
95 return score1 + " to " + score2 + ", " + winning;
96 }
97
98 /** Converts the given yardline relative to the offense's goal
99 into a string showing the yardline relative to which half of
100 the field the offense is on.
101 For example, the 56 yardline is translated into the "Defense's 44".
102 The 50 yardline is simply called "The 50".
103 @param ydline yardline relative to offense's own goal
104 @param offense name of team currently on offense
105 @param defense name of team currently on defense
106 @return yardline relative to each team's half of the field
107 */
108 public static String relativeYardline(int ydline, String offense,
109 String defense) {
110 String line = null;
111 if (ydline > 50) {
112 line = defense + "\'s " + (100 - ydline);
113 } else if (ydline == 50) {
114 line = "The 50";
115 } else {
116 line = offense + "\'s " + ydline;
117 }
118 return line;
119 }
120
121 public static String answerChoices(String[] answers, boolean ignoreDefault) {
122 if (ignoreDefault)
123 answers = (String[])Team
124 .truncateArray(answers, answers.length - 1);
125 if (answers.length < 1) {
126 return "";
127 } else if (answers.length < 2) {
128 return '\'' + answers[0] + '\'';
129 } else if (answers.length < 3) {
130 return '\'' + answers[0] + "\' or \'" + answers[1] + '\'';
131 } else {
132 String choices = "";
133 int i = 0;
134 for (i = 0; i < answers.length - 1; i++)
135 choices = choices + '\'' + answers[i] + "\'', ";
136 return choices.concat("or \'" + answers[i] + '\'');
137 }
138 }
139 }