I would like thoughts/advice on the project I am working on. I am trying to build a program that simulates the score of a baseball game given individual player statistics. As of right now, the program only has one team batting for nine innings.
I am a computer science student and I am using this as a learning project. I find this project interesting, and I would like to eventually use this as evidence of my programming skills to potential employers.
So here are a few questions I have for someone with more experience than me...
- Am I using too many classes?
- Do you like the way I have structured the classes or is there a better alternative?
- In terms of difficulty, how does this compare to the projects a typical software engineer would work on from day to day? I assume this is not very difficult. In which ways could I expand the program?
I plan to link this program to a database and pull the statistics in at runtime. I also plan to add a GUI. Any other comments are appreciated!
public class Game {
final static int VISIT = 0;
final static int HOME = 1;
public static void main(String[] args) {
Field field;
field = new Field();
Roster homeRoster;
Roster visitRoster;
int pitchResult;
visitRoster = field.getRoster(VISIT);
homeRoster = field.getRoster(HOME);
Player vPitcher = visitRoster.getPlayer(8);
Player hPitcher = homeRoster.getPlayer(8);
//Test test = new Test();
do{
Scorebook.setOuts(0);
while(Scorebook.getOuts() < 3){
Player batter = visitRoster.getBatter();
pitchResult = field.pitch(hPitcher, batter);
if(pitchResult == 1){
field.advanceBases();
}
else Scorebook.increaseOuts();
System.out.println("Inning: " + Scorebook.getInning() + " Score: " + Scorebook.getVisitScore() + " Outs: " + Scorebook.getOuts());
}
Scorebook.increaseInning();
}while((Scorebook.getInning() < 10) || (Scorebook.getHomeScore() == Scorebook.getVisitScore()));
System.out.println(Scorebook.getVisitScore());
}
// keep track of atBat
//
}
import java.util.Random;
public class Field {
Base bases[];
Base firstBase;
Base secondBase;
Base thirdBase;
Base homeBase;
Roster visitingTeam;
Roster homeTeam;
Field(){
firstBase = new Base("First Base");
secondBase = new Base("Second Base");
thirdBase = new Base("Third Base");
homeBase = new Base("Home Base");
bases = new Base[4];
bases[0] = firstBase;
bases[1] = secondBase;
bases[2] = thirdBase;
bases[3] = homeBase;
visitingTeam = new Roster();
homeTeam = new Roster();
}
// 0 returns visitingRoster, 1 returns homeRoster
public Roster getRoster(int i){
if(i == 0){
return visitingTeam;
}
else {
return homeTeam;
}
}
public int pitch(Player pitcher, Player batter){
int hit = 0;
Random randomGenerator = new Random();
double randomDouble = (double)randomGenerator.nextInt(100)/100;
if(randomDouble < batter.getBattingAvg()){
hit = 1;
}
return hit;
}
public void advanceBases(){
for(int i = 0 ; i < 3; i++)
{ // player on third scores
if(bases[i].getBaseStatus() == true){
if(bases[i].getBaseStatus() == true){
bases[i].setBaseStatus(false);
Scorebook.increaseVisitorsScore();
}
}
else{
if(bases[i].getBaseStatus() == true){
bases[i+1].setBaseStatus(true);
bases[i].setBaseStatus(false);
}
}
}
bases[0].setBaseStatus(true);
}
}
public class Scorebook {
static private int inning;
static private int outs;
static private int homeScore;
static private int visitingScore;
Scorebook(){
inning = 1;
outs = 0;
homeScore = 0;
visitingScore = 0;
}
static public void increaseInning(){
inning++;
}
static public void increaseOuts(){
outs++;
}
static public void increaseHomeScore(){
homeScore++;
}
static public void increaseVisitorsScore(){
visitingScore++;
}
static public int getInning(){
return inning;
}
static public int getOuts(){
return outs;
}
static public int getVisitScore(){
return visitingScore;
}
static public int getHomeScore(){
return homeScore;
}
static public void setOuts(int i){
outs = i;
}
/*
static public int getScore(int i){
if (i == 0){
return visitingScore;
}
else return homeScore;
}
*/
}
import java.util.LinkedList;
import java.util.Queue;
public class Roster {
final int ROSTERSIZE = 9;
Player p0;
Player p1;
Player p2;
Player p3;
Player p4;
Player p5;
Player p6;
Player p7;
Player p8;
Player roster[];
Queue<Player> qe=new LinkedList<Player>();
Roster(){ // Player name, battingAvg, era
p0 = new Player("p1", .085, .350);
p1 = new Player("p1", .075, .350);
p2 = new Player("p2", .330, .350);
p3 = new Player("p3", .275, .350);
p4 = new Player("p4", .280, .350);
p5 = new Player("p5", .260, .350);
p6 = new Player("p6", .060, .350);
p7 = new Player("p7", .050, .350);
p8 = new Player("p8", .000, .350);
roster = new Player[ROSTERSIZE];
roster[0] = p0;
roster[1] = p1;
roster[2] = p2;
roster[3] = p3;
roster[4] = p4;
roster[5] = p5;
roster[6] = p6;
roster[7] = p7;
roster[8] = p8;
// queue for batting line up
for(int i = 0; i < roster.length; i++){
qe.add(roster[i]);
}
}
public Player getPlayer(int i){
Player temp;
temp = roster[i];
return temp;
}
public Player getBatter(){
Player temp;
temp = qe.remove();
qe.add(temp);
return temp;
}
}
public class Player {
String name;
double battingAvg;
double era;
// primary constructor for creating a player
Player(String playerName, double battingAverage, double pitcherEra){
name = playerName;
battingAvg = battingAverage;
era = pitcherEra;
}
// constructor overloaded to take no arguments
Player(){
name = null;
battingAvg = 0;
era = 0;
}
public String getName(){
return name;
}
public double getBattingAvg(){
return battingAvg;
}
public double getERA(){
return era;
}
}
public class Base {
String name;
boolean base;
Base(String n){
name = n;
base = false;
}
void fillBase(){
base = true;
}
void clearBase(){
base = false;
}
boolean getBaseStatus(){
return base;
}
void setBaseStatus(boolean bool){
base = bool;
}
}