home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / TicTacToe / TicTacToe.java < prev   
Encoding:
Java Source  |  1996-04-26  |  7.4 KB  |  307 lines

  1. /*
  2.  * @(#)TicTacToe.java    1.2 95/10/13 
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.awt.*;
  33. import java.awt.image.*;
  34. import java.net.*;
  35. import java.applet.*;
  36.  
  37. /**
  38.  * A TicTacToe applet. A very simple, and mostly brain-dead
  39.  * implementation of your favorite game! <p>
  40.  *
  41.  * In this game a position is represented by a white and black
  42.  * bitmask. A bit is set if a position is ocupied. There are
  43.  * 9 squares so there are 1<<9 possible positions for each
  44.  * side. An array of 1<<9 booleans is created, it marks
  45.  * all the winning positions.
  46.  *
  47.  * @version     1.2, 13 Oct 1995
  48.  * @author Arthur van Hoff
  49.  * @modified 96/04/23 Jim Hagen : winning sounds
  50.  */
  51. public
  52. class TicTacToe extends Applet {
  53.     /**
  54.      * White's current position. The computer is white.
  55.      */
  56.     int white;
  57.  
  58.     /**
  59.      * Black's current position. The user is black.
  60.      */
  61.     int black;
  62.  
  63.     /**
  64.      * The squares in order of importance...
  65.      */
  66.     final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
  67.  
  68.     /**
  69.      * The winning positions.
  70.      */
  71.     static boolean won[] = new boolean[1 << 9];
  72.     static final int DONE = (1 << 9) - 1;
  73.     static final int OK = 0;
  74.     static final int WIN = 1;
  75.     static final int LOSE = 2;
  76.     static final int STALEMATE = 3;
  77.  
  78.     /**
  79.      * Mark all positions with these bits set as winning.
  80.      */
  81.     static void isWon(int pos) {
  82.     for (int i = 0 ; i < DONE ; i++) {
  83.         if ((i & pos) == pos) {
  84.         won[i] = true;
  85.         }
  86.     }
  87.     }
  88.  
  89.     /**
  90.      * Initialize all winning positions.
  91.      */
  92.     static {
  93.     isWon((1 << 0) | (1 << 1) | (1 << 2));
  94.     isWon((1 << 3) | (1 << 4) | (1 << 5));
  95.     isWon((1 << 6) | (1 << 7) | (1 << 8));
  96.     isWon((1 << 0) | (1 << 3) | (1 << 6));
  97.     isWon((1 << 1) | (1 << 4) | (1 << 7));
  98.     isWon((1 << 2) | (1 << 5) | (1 << 8));
  99.     isWon((1 << 0) | (1 << 4) | (1 << 8));
  100.     isWon((1 << 2) | (1 << 4) | (1 << 6));
  101.     }
  102.  
  103.     /**
  104.      * Compute the best move for white.
  105.      * @return the square to take
  106.      */
  107.     int bestMove(int white, int black) {
  108.     int bestmove = -1;
  109.     
  110.       loop:
  111.     for (int i = 0 ; i < 9 ; i++) {
  112.         int mw = moves[i];
  113.         if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
  114.         int pw = white | (1 << mw);
  115.         if (won[pw]) {
  116.             // white wins, take it!
  117.             return mw;
  118.         }
  119.         for (int mb = 0 ; mb < 9 ; mb++) {
  120.             if (((pw & (1 << mb)) == 0) && ((black & (1 << mb)) == 0)) {
  121.             int pb = black | (1 << mb);
  122.             if (won[pb]) {
  123.                 // black wins, take another
  124.                 continue loop;
  125.             }
  126.             }
  127.         }
  128.         // Neither white nor black can win in one move, this will do.
  129.         if (bestmove == -1) {
  130.             bestmove = mw;
  131.         }
  132.         }
  133.     }
  134.     if (bestmove != -1) {
  135.         return bestmove;
  136.     }
  137.  
  138.     // No move is totally satisfactory, try the first one that is open
  139.     for (int i = 0 ; i < 9 ; i++) {
  140.         int mw = moves[i];
  141.         if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
  142.         return mw;
  143.         }
  144.     }
  145.  
  146.     // No more moves
  147.     return -1;
  148.     }
  149.  
  150.     /**
  151.      * User move.
  152.      * @return true if legal
  153.      */
  154.     boolean yourMove(int m) {
  155.     if ((m < 0) || (m > 8)) {
  156.         return false;
  157.     }
  158.     if (((black | white) & (1 << m)) != 0) {
  159.         return false;
  160.     }
  161.     black |= 1 << m;
  162.     return true;
  163.     }
  164.  
  165.     /**
  166.      * Computer move.
  167.      * @return true if legal
  168.      */
  169.     boolean myMove() {
  170.     if ((black | white) == DONE) {
  171.         return false;
  172.     }
  173.     int best = bestMove(white, black);
  174.     white |= 1 << best;
  175.     return true;
  176.     }
  177.  
  178.     /**
  179.      * Figure what the status of the game is.
  180.      */
  181.     int status() {
  182.     if (won[white]) {
  183.         return WIN;
  184.     }
  185.     if (won[black]) {
  186.         return LOSE;
  187.     }
  188.     if ((black | white) == DONE) {
  189.         return STALEMATE;
  190.     }
  191.     return OK;
  192.     }
  193.  
  194.     /**
  195.      * Who goes first in the next game?
  196.      */
  197.     boolean first = true;
  198.  
  199.     /**
  200.      * The image for white.
  201.      */
  202.     Image notImage;
  203.  
  204.     /**
  205.      * The image for black.
  206.      */
  207.     Image crossImage;
  208.  
  209.     /**
  210.      * Initialize the applet. Resize and load images.
  211.      */
  212.     public void init() {
  213.     notImage = getImage(getCodeBase(), "images/not.gif");
  214.     crossImage = getImage(getCodeBase(), "images/cross.gif");
  215.     }
  216.  
  217.     /**
  218.      * Paint it.
  219.      */
  220.     public void paint(Graphics g) {
  221.     Dimension d = size();
  222.     g.setColor(Color.black);
  223.     int xoff = d.width / 3;
  224.     int yoff = d.height / 3;
  225.     g.drawLine(xoff, 0, xoff, d.height);
  226.     g.drawLine(2*xoff, 0, 2*xoff, d.height);
  227.     g.drawLine(0, yoff, d.width, yoff);
  228.     g.drawLine(0, 2*yoff, d.width, 2*yoff);
  229.  
  230.     int i = 0;
  231.     for (int r = 0 ; r < 3 ; r++) {
  232.         for (int c = 0 ; c < 3 ; c++, i++) {
  233.         if ((white & (1 << i)) != 0) {
  234.             g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this);
  235.         } else if ((black & (1 << i)) != 0) {
  236.             g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this);
  237.         }
  238.         }
  239.     }
  240.     }
  241.  
  242.     /**
  243.      * The user has clicked in the applet. Figure out where
  244.      * and see if a legal move is possible. If it is a legal
  245.      * move, respond with a legal move (if possible).
  246.      */
  247.     public boolean mouseUp(Event evt, int x, int y) {
  248.     switch (status()) {
  249.       case WIN:
  250.       case LOSE:
  251.       case STALEMATE:
  252.         play(getCodeBase(), "audio/return.au");
  253.         white = black = 0;
  254.         if (first) {
  255.         white |= 1 << (int)(Math.random() * 9);
  256.         }
  257.         first = !first;
  258.         repaint();
  259.         return true;
  260.     }
  261.  
  262.     // Figure out the row/colum
  263.     Dimension d = size();
  264.     int c = (x * 3) / d.width;
  265.     int r = (y * 3) / d.height;
  266.     if (yourMove(c + r * 3)) {
  267.         repaint();
  268.  
  269.         switch (status()) {
  270.           case WIN:
  271.         play(getCodeBase(), "audio/yahoo1.au");
  272.         break;
  273.           case LOSE:
  274.         play(getCodeBase(), "audio/yahoo2.au");
  275.         break;
  276.           case STALEMATE:
  277.         break;
  278.           default:
  279.         if (myMove()) {
  280.             repaint();
  281.             switch (status()) {
  282.               case WIN:
  283.             play(getCodeBase(), "audio/yahoo1.au");
  284.             break;
  285.               case LOSE:
  286.             play(getCodeBase(), "audio/yahoo2.au");
  287.             break;
  288.               case STALEMATE:
  289.             break;
  290.               default:
  291.             play(getCodeBase(), "audio/ding.au");
  292.             }
  293.         } else {
  294.             play(getCodeBase(), "audio/beep.au");
  295.         }
  296.         }
  297.     } else {
  298.         play(getCodeBase(), "audio/beep.au");
  299.     }
  300.     return true;
  301.     }
  302.  
  303.     public String getAppletInfo() {
  304.     return "TicTacToe by Arthur van Hoff";
  305.     }
  306. }
  307.