i have written this code but i am not relly that good with. so any pro who can do it for me so i can improve my one.
public class Tournament {
static Course course;
public static final int NUMPLAYERS = 2;
static Player [] players;
public static void main(String[] args) {
String coursename;
int length;
//Code here to enter coursename and
//length interactively
course = new Course(coursename, length);
players = new Player[NUMPLAYERS];
for (int i = 0; i < players.length; ++i) {
String playername;
int hand;
//Code here to enter playername and
// hand (the player's handicap) interactively
players[i] = new Player(playername, hand, course);
}
for (int i = 0; i < course.getLength(); ++i) {
for (int j = 0; j < players.length; ++j) {
players[j].playHole(i);
}
}
for (int i = 0; i < players.length; ++i) {
players[i].print();
}
int minScore = Integer.MAX_VALUE;
int pos = 0;
for (int i=0; i<players.length; ++i) {
if (players[i].getFinalScore() < minScore)
pos = i;
}
System.out.println("The winner is "+players[pos].getName());
}
} // Tournament
public class Course {
private String name;
private Hole [] course;
public Course(String n, int hnum) {
name = n;
course = new Hole [hnum];
Stream in = new Stream(System.in);
for (int i = 0; i < course.length; ++i) {
int tmp;
//Code here to enter tmp (the par for the current
// hole) interactively
course[i] = new Hole (tmp);
}
}
public String getName () {
return name;
}
public int getLength () {
return course.length;
}
public int getPar(int i) {
return course[i].getPar();
}
} // Course
public class Player {
private String name;
private int handicap;
private ScoreCard scard;
private static Random r = new Random();
public Player(String n, int hand, Course c) {
name = n;
handicap = hand;
scard = new ScoreCard(c);
}
public String getName () {
return name;
}
public void playHole (int num) {
int score = r.nextInt(7) + 1;
scard.setScore(num, score);
}
public void print () {
System.out.println("I'm " + name);
scard.print();
System.out.println("My handicap is "+handicap+
", so my final score is "+getFinalScore());
}
public int getFinalScore () {
return scard.getTotal() - handicap;
}
} // Player
public class ScoreCard {
private HoleScore [] scores;
private Course course;
public ScoreCard(Course c) {
course = c;
scores = new HoleScore [course.getLength()];
}
public void setScore (int i, int s) {
scores[i] = new HoleScore(s);
}
public int getScore (int h) {
return scores[h-1].getScore();
}
public int getTotal () {
int total = 0;
for (int i=0; i<course.getLength(); ++i)
total += scores[i].getScore();
return total;
}
public void print () {
System.out.println("Hole\tPar\tScore\t+/-Par");
int courseTotal = 0;
int gross = 0;
int againstPar = 0;
for (int i = 0; i < course.getLength(); i++) {
int iPar = course.getPar(i);
int iScore = getScore(i+1);
int iplusminus = getScore(i+1) - course.getPar(i);
courseTotal = courseTotal + iPar;
gross = gross + iScore;
againstPar = againstPar + iplusminus;
System.out.print(i+1 + "\t");
System.out.print(iPar + "\t");
System.out.print(iScore + "\t");
System.out.print(iplusminus);
System.out.println();
}
System.out.print("Total\t");
System.out.print(courseTotal + "\t");
System.out.print(gross + "\t");
System.out.println(againstPar + "\t");
}
} // ScoreCard
public class HoleScore {
private int score;
public HoleScore(int s) {
score = s;
}
public int getScore () {
return score;
}
} // HoleScore