/////// TicStrategy.java /////// public class TicStrategy { // move generate a move for self against opponent // given the game moves already made (self and opponent) // strategy used here: // Find a wining move. If no such move // take any wining move away from opponent. // If neither, make any valid move public int move(int self, int opponent) { int amove = TicGame.NOMOVE; if ( (self|opponent) == 0 ) return openMove(); for (int i = 0 ; i < 9 ; i++) { int m = moves[i], mv = (1 << m); if ( ((self|opponent) & mv) == 0) // open square { if (TicGame.isWin(self | mv)) return m; // self wins if (TicGame.isWin(opponent | mv)) amove = m; // block opponent win else if (amove == TicGame.NOMOVE) amove = m; } } return amove; // move found } protected int openMove() { return (int)(9*Math.random()); } protected int[] moves = {4, 0, 2, 6, 8, 1, 3, 5, 7}; }