Yurttas/PL/OOL/Java/F/09/02/01/00/StraightSeeding.java
Jump to navigation
Jump to search
1import java.util.*;
2
3public class StraightSeeding extends Seeding {
4
5 protected Vector swimmers;
6 protected Swimmer[] swmrs;
7 protected int numLanes;
8 protected int[] lanes;
9 protected int count;
10 protected int numHeats;
11
12 public StraightSeeding(Vector sw, int lanes) {
13 swimmers = sw;
14 numLanes = lanes;
15 count = sw.size();
16 calcLaneOrder();
17 seed();
18 }
19 //--------------------------------
20 protected void seed() {
21 //loads the swmrs array and sorts it
22 sortUpwards();
23
24 int lastHeat = count % numLanes;
25 if (lastHeat < 3)
26 lastHeat = 3; //last heat must have 3 or more
27 int lastLanes = count - lastHeat;
28 numHeats = count / numLanes;
29 if (lastLanes > 0)
30 numHeats++;
31 int heats = numHeats;
32
33
34 //place heat and lane in each swimmer's object
35 int j = 0;
36
37 for (int i = 0; i < lastLanes; i++) {
38 Swimmer sw = swmrs[i];
39
40 sw.setLane(lanes[j++]);
41 sw.setHeat(heats);
42 if (j >= numLanes) {
43 heats--;
44 j=0;
45 }
46 }
47
48 //Add in last partial heat
49 if (j < numLanes)
50 heats--;
51 j = 0;
52 for (int i = lastLanes-1; i<count; i++) {
53
54 Swimmer sw = swmrs[i];
55 sw.setLane(lanes[j++]);
56 sw.setHeat(heats);
57 }
58 //copy from array back into Vector
59
60 swimmers = new Vector();
61 for (int i=0; i< count; i++)
62 swimmers.addElement(swmrs[i]);
63
64 }
65 //--------------------------------
66 private void calcLaneOrder() {
67 lanes = new int[numLanes];
68 int mid = numLanes / 2;
69 if (odd(numLanes))
70 mid = mid + 1; //start in middle lane
71 int incr = 1;
72 int ln = mid;
73 for (int i=0; i< numLanes; i++) {
74 lanes[i] = ln;
75
76 ln = mid + incr;
77 incr = - incr;
78 if (incr > 0)
79 incr=incr+1;
80 }
81 }
82 //--------------------------------
83 private boolean odd(int x) {
84 return(((x / 2)*2) != x);
85 }
86 //--------------------------------
87 protected void sortUpwards() {
88 swmrs = new Swimmer[count];
89 for (int i=0; i < count; i++)
90 swmrs[i] = (Swimmer)swimmers.elementAt(i);
91 for ( int i=0; i < count; i++) {
92 for (int j = i; j < count; j++) {
93 if (swmrs[i].getTime() > swmrs[j].getTime()) {
94 Swimmer swtemp = swmrs[i];
95 swmrs[i] = swmrs[j];
96 swmrs[j] = swtemp;
97 }
98 }
99 }
100 }
101 //--------------------------------
102 public int getCount() {
103 return swimmers.size();
104 }
105
106 public Enumeration getSwimmers() {
107 return swimmers.elements();
108 }
109 //----------------------------------
110 public int getHeats() {
111 return numHeats;
112 }
113}