import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; import java.applet.*; /** * A Simple TicTacToe applet. A Tic Tac Toe applet. * A very simple, and mostly brain-dead * implementation of your favorite game! */ // get object for the arrays this is in 3 // make two methods in paint in 4.1 // /** * A bitmask is used for the two players denoting positions occupied. * Each bit represents a square on the board 0..8. * The bit is 0 if not occupied and 1 if occupied. * Winning is determined by comparing the bitmask with * an array of Booleans that marks all the winning positions. * @version 1.2, 13 Oct 1995 * @author Arthur van Hoff * @modified 96/04/23 Jim Hagen : winning sounds * @modified 03/07/21 Sara Stoecklin : updated java version */ public class TTTV41 extends Applet implements MouseListener { // CONSTANTS static final int NINE_SHIFTING_BITS = 9; static final int ENDINGSTATE = (1 << NINE_SHIFTING_BITS) - 1; // sentinel state 111 111 111 static final int LOSE = 2; // status for user wins static final int CONTINUE = 0; // OLD OK status for game continues static final int STALEMATE = 3; // status for a tie static final int WIN = 1; // status for computer wins static final int NUMBER_OF_COLUMNS = 3; static final int NUMBER_OF_ROWS = 3; // VARIABLES private int computerStatus; ; // computer bitmask denotes squares occupied private Image userImage; // user image private boolean computerFirst = true; // who goes first next game CHANGE TO UserFirst private Image computerImage; // computer image private int userStatus; // user bitmask denotes user squares occupied private static boolean winningState[] = new boolean[1 << 9]; // winning states private static void setWinningState(int winState) { // mark winning squares as true win for (int i = 0 ; i < ENDINGSTATE ; i++) { if ((i & winState) == winState) { winningState[i] = true; } // end if (i & winState) } // end for } // end isWon // for lesson three we need to replace array with object // need to know how to use collection // here and to initialize the collection // then access winning state with the collection // using a get and set // LESSON TWo 3.1 then copy and paste into her // update lessone 3 in the ppt last slide static { // initialize winning squares by shifting a one n bits setWinningState((1 << 0) | (1 << 1) | (1 << 2)); // row one 000 000 111 setWinningState((1 << 3) | (1 << 4) | (1 << 5)); // row two 000 111 000 setWinningState((1 << 6) | (1 << 7) | (1 << 8)); // row three 111 000 000 setWinningState((1 << 0) | (1 << 3) | (1 << 6)); // col one 100 100 100 setWinningState((1 << 1) | (1 << 4) | (1 << 7)); // col two 010 010 010 setWinningState((1 << 2) | (1 << 5) | (1 << 8)); // col three 001 001 001 setWinningState((1 << 0) | (1 << 4) | (1 << 8)); // dia right 100 010 001 setWinningState((1 << 2) | (1 << 4) | (1 << 6)); // dia left 001 010 100 } // end static // GETS AND SETS public int getComputerStatus () { return computerStatus; } public Image getUserImage (){return userImage;} public boolean getComputerFirst () { return computerFirst; } public Image getComputerImage () { return computerImage;} public int getUserStatus () { return userStatus; } public void setComputerStatus (int computerStatus) { this.computerStatus = computerStatus; } public void setUserImage (Image userImage) { this.userImage = userImage;} public void setComputerFirst (boolean computerFirst) { this.computerFirst = computerFirst; } public void setComputerImage (Image computerImage) { this.computerImage = computerImage; } public void setUserStatus (int userStatus) { this.userStatus = userStatus; } //METHODS static final int firstCell = 0; // first cell at row 1 col 1 static final int lastCell = 8; // last cell at row 3, col 3 static final int bestMoveNotFound = -1; // value indicating best move not yet found int bestMove(int computerStatus, int userStatus) { // compute best move return square int mostStrategicMove[] = {4,0,2,6,8,1,3,5,7};// square order of importance int bestmove = bestMoveNotFound; loop: for (int i = firstCell ; i <= lastCell ; i++) { int potentialComputerMove = mostStrategicMove[i]; if (((computerStatus & (1 << potentialComputerMove)) == 0) && ((userStatus & (1 << potentialComputerMove)) == 0)) { int potentialComputerStatus = computerStatus | (1 << potentialComputerMove); if (winningState[potentialComputerStatus]) { // computer wins, take it! return potentialComputerMove; } /// end if (not user taken ) && ( not computer taken ) for (int j = firstCell ; j <= lastCell ; j++) { // for square = 0 to < 9 if (((potentialComputerStatus & (1 << j)) == 0) && ((userStatus & (1 << j)) == 0)) { int potentialUserStatus = userStatus | (1 << j); if (winningState[potentialUserStatus]) { // user wins, take anothe continue loop; } // end if won } // end if && } // end for if (bestmove == bestMoveNotFound) { // neither can win, this move will do bestmove = potentialComputerMove; } // end if } // end if && } // end for if (bestmove != bestMoveNotFound) { // if no move found return the best one return bestmove; } // end if bestmove for (int i = firstCell ; i <= lastCell ; i++) { // no great move, try first one open int firstAvailableComputerMove = mostStrategicMove[i]; if (((computerStatus & (1 << firstAvailableComputerMove)) == 0) && ((userStatus & (1 << firstAvailableComputerMove)) == 0)) { return firstAvailableComputerMove; } // end if && } // end for return bestMoveNotFound; // return no more moves } // end best move boolean legalUserMove(int computerStatus, int userStatus, int canidateMove) { // user move return true if legal move if ((canidateMove < 0) || (canidateMove > 8)) { return false; } // end if if (((userStatus | computerStatus) & (1 << canidateMove)) != 0) { return false; } // end if setUserStatus (userStatus | 1 << canidateMove); return true; } // end legalUserMove boolean legalComputerMove(int computerStatus, int userStatus) { // computer move return true if legal move if ((userStatus | computerStatus) == ENDINGSTATE) { return false; } // end if setComputerStatus (computerStatus | 1 << bestMove(computerStatus, userStatus)); return true; } // end legalComputerMove int gameStatus(int computerStatus, int userStatus ) { // determine game status (win, loose, tie) if (winningState[computerStatus]) { return WIN; } // end if if (winningState[userStatus]) { return LOSE; } // end if if ((userStatus | computerStatus) == ENDINGSTATE) { return STALEMATE; } // end if return CONTINUE; } // end gameStatus public void init() { // initialize applet, load images, add listener setComputerImage(getImage(getCodeBase(), "oimage.gif")); setUserImage(getImage(getCodeBase(), "ximage.gif")); addMouseListener(this); } // end init public void destroy() { // destroy listener needed for closing removeMouseListener(this); } // end destroy public void drawGrid(Graphics g, Dimension d, int xoff, int yoff){ g.drawLine(xoff, 0, xoff, d.height); // draw first horizontal line g.drawLine(2*xoff, 0, 2*xoff, d.height); // draw second horizontal line g.drawLine(0, yoff, d.width, yoff); // draw first verticle line g.drawLine(0, 2*yoff, d.width, 2*yoff); // draw second verticle line } // end drawGrid public void drawImages(Graphics g, Dimension d, int xoff, int yoff){ int i = 0; for (int row = 0 ; row < NUMBER_OF_ROWS ; row++) { // draw computer and user images for (int col = 0 ; col < NUMBER_OF_COLUMNS ; col++, i++) { if ((getComputerStatus() & (1 << i)) != 0) { // if computer bitmask square taken g.drawImage(getComputerImage(), col*xoff + 1, row*yoff + 1, this); } else if ((getUserStatus() & (1 << i)) != 0) { // if user bitmask square taken g.drawImage(getUserImage(), col*xoff + 1, row*yoff + 1, this); } // end if }// end for } // end for } // end draw Images public void paint(Graphics g) { // paint the screen Dimension d = getSize(); g.setColor(Color.black); int xoff = d.width / NUMBER_OF_ROWS; // compute x offsets int yoff = d.height / NUMBER_OF_COLUMNS; // compute y offsets drawGrid (g, d, xoff, yoff); drawImages (g, d, xoff, yoff); } // end paint public boolean resetFirst (boolean computerFirst) { if (computerFirst) { // reset who is first setComputerStatus ( 1 << (int)(Math.random() * 9)); }// end if return !computerFirst; } // end resetStatus public boolean gameOver(int status) { // check and reset if there WAS a WIN, LOSE, or STALEMATE after computer move switch (status) { case WIN: case LOSE: case STALEMATE: play(getCodeBase(), "audio/return.au"); setComputerStatus(0); setUserStatus(0); setComputerFirst (resetFirst(getComputerFirst())); repaint(); return true; default: return false; } // end switch } // end gameOver public void postGameStatus (int status) { switch (status) { case WIN: System.out.println ("I win #1"); break; case LOSE: System.out.println ("You win #1"); break; case STALEMATE: System.out.println ("No Winner #1"); break; default: } // end switch } // end checkGameStatus /** * The user has clicked in the applet. Figure out where * and see if a legal move is possible. If it is a legal * move, respond with a legal move (if possible). */ public void mouseReleased(MouseEvent e) { // user clicked applet int x = e.getX(); // get mouse x location int y = e.getY(); // get mouse y location Dimension d = getSize(); int col = (x * NUMBER_OF_COLUMNS) / d.width; // determine the column int row = (y * NUMBER_OF_ROWS) / d.height; // determine the row if (gameOver (gameStatus(getComputerStatus(), getUserStatus()))) { return; } // end if if (legalUserMove(getComputerStatus(), getUserStatus(), col + row * 3)) { repaint(); int status = gameStatus(getComputerStatus(), getUserStatus()); if (!(status == CONTINUE)) { postGameStatus (status); } else { if (legalComputerMove(getComputerStatus(), getUserStatus())) { repaint(); postGameStatus(gameStatus(getComputerStatus(), getUserStatus())); } else { System.out.println ("after if #1"); play(getCodeBase(), "audio/beep.au"); } // end else } // end else } else { // not legal user move System.out.println ("after if #2"); play(getCodeBase(), "audio/beep.au"); }// end else } // end mouseReleased public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public String getAppletInfo() { return "TicTacToe by Arthur van Hoff"; } // end getAppletInfo } // end TTT