TTTInstance {{ /** Way to properly display a Tic Tac Toe board */ public void show() { System.out.println(this.getFirst().print()); System.out.println(this.getSecond().print()); System.out.println(this.getThird().print()); } /** Check if Solution solution is valid for this Instance */ public double valid(Solution solution) { ttt.TTTSolution s = ((ttt.TTTSolution)solution.getSolution()); return this.valid(s); } /** Law of Demeter method */ private double valid(ttt.TTTSolution s) { // Check to see if the solution contains the expected amount of X's and O's if ( ((s.getUpdated()).xCount() == this.expectedX()) && ((s.getUpdated()).oCount() == this.expectedO()) ) { // If there is only one difference between the previous board and the updated board... if ( this.countDifferences(s) == 1 ) { // ...the updated board is valid return 1; } // Otherwise, it is invalid else return 0; } // If the solution does not contain the expected amount of X's and O's, it is invalid else return 0; } // Helper function for the 'valid' method private int countDifferences(ttt.TTTSolution s) { // Return the number of differences between each of the 3 Rows return countDifferences(this.getFirst(), s.getUpdated().getFirst()) + countDifferences(this.getSecond(), s.getUpdated().getSecond()) + countDifferences(this.getThird(), s.getUpdated().getThird()); } // Helper function for the 'valid' method private int countDifferences(ttt.Row i, ttt.Row s) { // Value to be returned int value = 0; // Compare the first Squares if ( (i.getFirst()).equals(s.getFirst()) ) { value = value + 1; } // Compare the second Squares if ( (i.getSecond()).equals(s.getSecond()) ) { value = value + 1; } // Compare the third Squares if ( (i.getThird()).equals(s.getThird()) ) { value = value + 1; } // Return the resulting value return value; } // Helper function for the 'valid' method private int expectedX() { // If there are equal amounts of Xs and Os, it is X's turn if ( this.xCount() == this.oCount() ) { return (this.xCount() + 1); } // Otherwise, it is O's turn else return this.xCount(); } // Helper function for the 'valid' method private int expectedO() { // If there are equal amounts of Xs and Os, it is X's turn if ( this.xCount() == this.oCount() ) { return this.oCount(); } // Otherwise, it is O's turn else return (this.oCount() + 1); } // Helper function for the 'valid' method private int xCount() { // Return the X count from each of the 3 Rows return xCount(this.getFirst()) + xCount(this.getSecond()) + xCount(this.getThird()); } // Helper function for the 'valid' method private int xCount(ttt.Row r) { // Value to be returned int value = 0; // Check the first Square if ( r.getFirst() instanceof X ) { value = value + 1; } // Check the second Square if ( r.getSecond() instanceof X ) { value = value + 1; } // Check the third Square if ( r.getThird() instanceof X ) { value = value + 1; } // Return the resulting value return value; } // Helper function for the 'valid' method private int oCount() { // Return the O count from each of the 3 Rows return oCount(this.getFirst()) + oCount(this.getSecond()) + oCount(this.getThird()); } // Helper function for the 'valid' method private int oCount(ttt.Row r) { // Value to be returned int value = 0; // Check the first Square if ( r.getFirst() instanceof O ) { value = value + 1; } // Check the second Square if ( r.getSecond() instanceof O ) { value = value + 1; } // Check the third Square if ( r.getThird() instanceof O ) { value = value + 1; } // Return the resulting value return value; } /** Returns the quality of a Solution for this Instance */ public double quality(Solution solution) { if ( (this.valid(solution)) > 0 ) { if ( containsWinner(((ttt.TTTSolution)solution.getSolution())) ) { return 2; } else return 1; } else return 0; } // Helper function for the 'quality' method private boolean containsWinner(ttt.TTTSolution s) { if ( anyRowContainsWinner(s) ) { return true; } if ( anyColumnContainsWinner(s) ) { return true; } if ( anyDiagonalContainsWinner(s) ) { return true; } else return false; } // Helper function for the 'quality' method private boolean anyRowContainsWinner(ttt.TTTSolution s) { if ( rowContainsWinner(s.getUpdated().getFirst()) ) { return true; } if ( rowContainsWinner(s.getUpdated().getSecond()) ) { return true; } if ( rowContainsWinner(s.getUpdated().getThird()) ) { return true; } else return false; } // Helper function for the 'quality' method private boolean rowContainsWinner(ttt.Row r) { if ( !(r.getFirst() instanceof N) ) { if ( (r.getFirst()).equals(r.getSecond()) ) { if ( (r.getFirst()).equals(r.getThird()) ) { return true; } else return false; } else return false; } else return false; } // Helper function for the 'quality' method private boolean anyColumnContainsWinner(ttt.TTTSolution s) { if ( firstColumnContainsWinner(s.getUpdated()) ) { return true; } if ( secondColumnContainsWinner(s.getUpdated()) ) { return true; } if ( thirdColumnContainsWinner(s.getUpdated()) ) { return true; } else return false; } // Helper function for the 'quality' method private boolean firstColumnContainsWinner(ttt.TTTInstance i) { if ( !(i.getFirst().getFirst() instanceof N) ) { if ( (i.getFirst().getFirst()).equals(i.getSecond().getFirst()) ) { if ( (i.getFirst().getFirst()).equals(i.getThird().getFirst()) ) { return true; } else return false; } else return false; } else return false; } // Helper function for the 'quality' method private boolean secondColumnContainsWinner(ttt.TTTInstance i) { if ( !(i.getFirst().getSecond() instanceof N) ) { if ( (i.getFirst().getSecond()).equals(i.getSecond().getSecond()) ) { if ( (i.getFirst().getSecond()).equals(i.getThird().getSecond()) ) { return true; } else return false; } else return false; } else return false; } // Helper function for the 'quality' method private boolean thirdColumnContainsWinner(ttt.TTTInstance i) { if ( !(i.getFirst().getThird() instanceof N) ) { if ( (i.getFirst().getThird()).equals(i.getSecond().getThird()) ) { if ( (i.getFirst().getThird()).equals(i.getThird().getThird()) ) { return true; } else return false; } else return false; } else return false; } // Helper function for the 'quality' method private boolean anyDiagonalContainsWinner(ttt.TTTSolution s) { boolean check = false; if ( !(s.getUpdated().getSecond().getSecond() instanceof N) ) { if ( (s.getUpdated().getFirst().getFirst()).equals(s.getUpdated().getSecond().getSecond()) ) { if ( (s.getUpdated().getFirst().getFirst()).equals(s.getUpdated().getThird().getThird()) ) { check = true; } } if ( !(check == true) ) { if ( (s.getUpdated().getFirst().getThird()).equals(s.getUpdated().getSecond().getSecond()) ) { if ( (s.getUpdated().getFirst().getThird()).equals(s.getUpdated().getThird().getFirst()) ) { check = true; } } } } return check; } }} TTTInstanceSet {{ /** Is this a valid InstanceSet of the given instance? */ public Option belongsTo(Instance instance) { ttt.TTTInstance tttInstance = (ttt.TTTInstance)instance.getInstance(); if( !((this.getClaim()).equals(tttInstance)) ) { return new Some("Instance and claim do not match"); } return new None(); } }} Avatar {{ /** * To Propose new claims and are not present in the passed ProposedRequest * claims returns : ProposeResponse object which contains List of claims */ public ProposeResponse propose(ProposeRequest pr) { List newClaim = new Empty(); if ( !(pr.getForbidden().isEmpty()) ) { try { ttt.TTTInstance current = ((ttt.TTTInstanceSet)((pr.getForbidden().top()).getInstanceSet().getInstanceSet())).getClaim(); ttt.TTTInstance update; int nextMove = getRandomEmptySquare(current); // If there are equal amounts of Xs and Os, it is X's turn if ( xCount(current) == oCount(current) ) { update = updateSquare(current, nextMove, new X()); } // Otherwise, it is O's turn else update = updateSquare(current, nextMove, new O()); FullyQualifiedClassName instanceSetClassName = FullyQualifiedClassName.parse("ttt.TTTInstanceSet"); ProtocolI pi = new protocol.StandardTTT(); double quality = 1; InstanceSetI tttInstance = new TTTInstanceSet(update); InstanceSet newInstanceSet = new InstanceSet(instanceSetClassName, tttInstance, new verbatim("")); newClaim = newClaim.append(new Claim(newInstanceSet, instanceSetClassName, pi, quality)); } catch (scg.ParseException e) { e.printStackTrace(); } } else { try { ttt.Row emptyRow = new Row(new N(), new N(), new N()); ttt.TTTInstance empty = new TTTInstance(emptyRow, emptyRow, emptyRow); FullyQualifiedClassName instanceSetClassName = FullyQualifiedClassName.parse("ttt.TTTInstanceSet"); ProtocolI pi = new protocol.StandardTTT(); double quality = 1; InstanceSetI tttInstance = new TTTInstanceSet(empty); InstanceSet newInstanceSet = new InstanceSet(instanceSetClassName, tttInstance, new verbatim("")); newClaim = newClaim.append(new Claim(newInstanceSet, instanceSetClassName, pi, quality)); } catch (scg.ParseException e) { e.printStackTrace(); } } return new ProposeResponse(newClaim); } private int xCount(ttt.TTTInstance i) { // Return the X count from each of the 3 Rows return xCount(i.getFirst()) + xCount(i.getSecond()) + xCount(i.getThird()); } private int xCount(ttt.Row r) { // Value to be returned int value = 0; // Check the first Square if ( r.getFirst() instanceof X ) { value = value + 1; } // Check the second Square if ( r.getSecond() instanceof X ) { value = value + 1; } // Check the third Square if ( r.getThird() instanceof X ) { value = value + 1; } // Return the resulting value return value; } private int oCount(ttt.TTTInstance i) { // Return the O count from each of the 3 Rows return oCount(i.getFirst()) + oCount(i.getSecond()) + oCount(i.getThird()); } private int oCount(ttt.Row r) { // Value to be returned int value = 0; // Check the first Square if ( r.getFirst() instanceof O ) { value = value + 1; } // Check the second Square if ( r.getSecond() instanceof O ) { value = value + 1; } // Check the third Square if ( r.getThird() instanceof O ) { value = value + 1; } // Return the resulting value return value; } private int getRandomEmptySquare(ttt.TTTInstance board) { if ( !(getLocationsOfEmptySquares(board).isEmpty()) ) { Vector locations = getLocationsOfEmptySquares(board); if ( locations.size() > 1 ) { int random = (new Random()).nextInt(locations.size() - 1); Integer r = locations.get(random); return r.intValue(); } else return locations.firstElement().intValue(); } else return 0; } private Vector getLocationsOfEmptySquares(ttt.TTTInstance board) { Vector locations = getLocationsOfEmptySquares(board.getFirst(), 1); locations.addAll(getLocationsOfEmptySquares(board.getSecond(), 2)); locations.addAll(getLocationsOfEmptySquares(board.getThird(), 3)); return locations; } private Vector getLocationsOfEmptySquares(ttt.Row r, int row) { Vector locations = new Vector(); int x = (row - 1) * 3; if (r.getFirst() instanceof N) { locations.add(new Integer(x + 1)); } if (r.getSecond() instanceof N) { locations.add(new Integer(x + 2)); } if (r.getThird() instanceof N) { locations.add(new Integer(x + 3)); } return locations; } private ttt.TTTInstance updateSquare(ttt.TTTInstance original, int square, Square id) { ttt.TTTInstance update; ttt.Row newRow; if ( square < 4) { newRow = updateSquare(original.getFirst(), square, id); update = new ttt.TTTInstance(newRow, original.getSecond(), original.getThird()); } else if ( (square > 3) && (square < 7) ) { newRow = updateSquare(original.getSecond(), (square - 3), id); update = new ttt.TTTInstance(original.getFirst(), newRow, original.getThird()); } else { newRow = updateSquare(original.getThird(), (square - 6), id); update = new ttt.TTTInstance(original.getFirst(), original.getSecond(), newRow); } return update; } private ttt.Row updateSquare(ttt.Row original, int square, ttt.Square id) { if (square == 1) { return new Row(id, original.getSecond(), original.getThird()); } if (square == 2) { return new Row(original.getFirst(), id, original.getThird()); } else { return new Row(original.getFirst(), original.getSecond(), id); } } public OpposeResponse oppose(OpposeRequest or) { List value = new Empty(); value.append(new NoOpposition()); return new OpposeResponse(value); } public ProvideResponse provide(ProvideRequest pr) { return null; } public SolveResponse solve(SolveRequest sr) { return null; } }}