// TT4DRow.java /** * * @author Sean Bridges * @version 1.0 * * The row is how the game keeps track of the score. * * Each row listens to three TT4DSquares. When their contents change * the row informs its stat object what to do. * * This class is where most of the logic for analyzing the boards state is. * It really doesnt run an algorithm, its just an accounting method. * * The game */ public class TT4DRow implements TT4DSquareListener { //--------------------------------------------- //instance variables private int xCount = 0; //the number of x's in this row private int oCount = 0; //the number of o' in this row private TT4DStats stats; private TT4DSquare s1,s2,s3; //--------------------------------------------- //construtor /** Creates new TT4DRow * * s1,s2,s3 are the three squares that make up the row, * the stats object is the object that the row informs of changes. * */ public TT4DRow(TT4DSquare s1, TT4DSquare s2, TT4DSquare s3, TT4DStats stats) { this.stats = stats; this.s1 = s1; this.s2 = s2; this.s3 = s3; s1.addListener(this); s2.addListener(this); s3.addListener(this); } //--------------------------------------------- //instance methods //--------------------------------------------- //TT4DSquareListener methods public void contentsChanged(String oldContents,String newContents) { //case 1, old = blank, new = x if( oldContents == TT4DBoard.NULL_PLAYER_STRING && newContents == TT4DBoard.X_PLAYER_STRING ) { //if we used to have some o's and no x's //then we used to have a score for o, remove that if(xCount == 0 && oCount != 0) { switch(oCount) { case 1: stats.decOWith1(); break; case 2: stats.decOWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end switch }//end if //if we have no o's, we now have a better score for x //remove the old score, add teh new score. else if(oCount == 0) { switch(xCount) { case 0: stats.incXWith1(); break; case 1: stats.incXWith2(); stats.decXWith1(); break; case 2: stats.incXRows(); stats.decXWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end case }//end if xCount++; }//end if old = null, new = x //case 2, old = blank, new = o else if( oldContents == TT4DBoard.NULL_PLAYER_STRING && newContents == TT4DBoard.O_PLAYER_STRING ) { //if we used to have some x's and no o's //then we used to have a score for o, remove that if(oCount == 0 && xCount != 0) { switch(xCount) { case 1: stats.decXWith1(); break; case 2: stats.decXWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end case }//end if //if we have no x's, we now have a better score for o //remove the old score, add teh new score. else if(xCount == 0) { switch(oCount) { case 0: stats.incOWith1(); break; case 1: stats.incOWith2(); stats.decOWith1(); break; case 2: stats.incORows(); stats.decOWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end case }//end if oCount++; }//end if old = null, new = o //case 3, old = x, new = null else if( oldContents == TT4DBoard.X_PLAYER_STRING && newContents == TT4DBoard.NULL_PLAYER_STRING ) { //if we have no o's, we had an x score, decrement it. if(oCount == 0) { switch(xCount) { case 1: stats.decXWith1(); break; case 2: stats.decXWith2(); stats.incXWith1(); break; case 3: stats.decXRows(); stats.incXWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end switch }//end if //if we have 1 x and some y's increment the y score. if(xCount == 1 && oCount != 0) { switch(oCount) { case 1: stats.incOWith1(); break; case 2: stats.incOWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end switch }//end if xCount--; }//end if old = x, new = null //case 4, old = o, new = null else if( oldContents == TT4DBoard.O_PLAYER_STRING && newContents == TT4DBoard.NULL_PLAYER_STRING ) { //if we have no x's, we had an o score, decrement it. if(xCount == 0) { switch(oCount) { case 1: stats.decOWith1(); break; case 2: stats.decOWith2(); stats.incOWith1(); break; case 3: stats.decORows(); stats.incOWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end switch }//end if //if we have 1 o and some x's increment the x score. if(oCount == 1 && xCount != 0) { switch(xCount) { case 1: stats.incXWith1(); break; case 2: stats.incXWith2(); break; default : System.out.println("Invalid board stats state: " + toString()); Thread.dumpStack(); }//end switch }//end if oCount--; }//end if old = x, new = null }//end contentsChanged(...) public String toString() { return "Row containg x:" + xCount + " o:" + oCount + "\n" + s1 + "\n" + s2 + "\n" + s3; } }//end class TT4DRow