// // Othecko 1.1 // // Steve Matuszek // Jack Freelander // University of North Carolina at Chapel Hill // // // Vote.java // Jack Freelander 1.0 // // // This class is a very simple encapsulation of the information // describing a vote. // // Like GameState, it is a hard-coded implementation for our // tic-tac-toe game, not an extensible interface. // import java.io.*; import java.util.Vector; public class Vote implements Serializable { public int x,y,count; public Vote(int a, int b, int c) { x=a; y=b; count=c; } public Vote(int a, int b) { x=a; y=b; count=1; } public Vote() { x=0; y=0; count=0; } public boolean sameAs(Vote v) { return (v.x==x && v.y==y); } public boolean equals(Object o) { if (!(o instanceof Vote)) { return false; } else return sameAs((Vote)o); } public String toString() { return("\n(Vote: (" + x + ", " + y + "), count " + count + ")"); } public static Vector combineVoteVectors(Vector a, Vector b) { Vector to = (Vector) a.clone(); Vector from = (Vector) b.clone(); for( ; !from.isEmpty(); from.removeElementAt(0)) { Vote v = (Vote) from.elementAt(0); { if (to.contains(v)) { ((Vote)(to.elementAt(to.indexOf(v)))).count += v.count; } else { to.addElement(v); } } } return (to); } public static void absorbVoteVector(Vector to, Vector from) { for( ; !from.isEmpty(); from.removeElementAt(0)) { Vote v = (Vote) from.elementAt(0); { if (to.contains(v)) { ((Vote)(to.elementAt(to.indexOf(v)))).count += v.count; } else { to.addElement(v); } } } } public static Vote winningVote(Vector v) throws Exception { if (v.isEmpty()) { throw new Exception( "Vector is empty! Use GameState.randomMove()."); } else if (!(v.firstElement() instanceof Vote)) { throw new Exception( "Hey, that Vector doesn't have Votes. I hate you."); } else { Vote challenger; Vote winner = (Vote) v.firstElement(); for (int i = 1; i winner.count) { winner = challenger; } } return winner; } } public static int totalVotesIn(Vector v) { if (v.isEmpty()) { return 0; } else if (!(v.firstElement() instanceof Vote)) { return 0; } else { Vote challenger; int total = 0; for (int i = 0; i