home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / DOTGAME / DOTGAME.EXE / DotGame.java < prev   
Encoding:
Java Source  |  1996-05-09  |  14.3 KB  |  499 lines

  1. // DotGame.java
  2.  
  3. // Art Matheny <matheny@usf.edu>
  4. // Academic Computing Department
  5. // University of South Florida
  6.  
  7. // 4/17/96
  8.  
  9. import java.applet.*;
  10. import java.awt.*;
  11. import java.util.Random;
  12.  
  13. class dgVertex extends Canvas {
  14.    public int group;
  15.  
  16.    public dgVertex (int x, int y, int vertex_size) {
  17.       resize (vertex_size, vertex_size);
  18.       move (x, y + 40);       // why 30? beats me!
  19.       group = 0;
  20.    }
  21.  
  22.    public void paint (Graphics g) {
  23.       setBackground (Color.black);
  24.       g.setColor (Color.black);
  25.       g.drawRect (location().x, location().y,
  26.             size().width, size().height );
  27.    }
  28. }
  29.  
  30. class dgEdge extends Canvas {
  31.    private boolean drawn;
  32.    private dgVertex v1, v2;
  33.  
  34.    public dgEdge (dgVertex end_point1, dgVertex end_point2) {
  35.       drawn = false;
  36.       v1 = end_point1;
  37.       v2 = end_point2;
  38.  
  39.       Point p1 = v1.location();
  40.       Point p2 = v2.location();
  41.       int vertex_size = v1.size().width;
  42.       if (p1.x == p2.x) {           // verticle edge
  43.          resize (vertex_size, p2.y - p1.y - vertex_size);
  44.          move (p1.x, p1.y + vertex_size);
  45.       } else {                      // horizontal egde
  46.          resize (p2.x - p1.x - vertex_size, vertex_size);
  47.          move (p1.x + vertex_size, p1.y);
  48.       }
  49.    }
  50.  
  51.    public boolean isDrawn() {
  52.       return drawn;
  53.    }
  54.  
  55.    public void turnOn (Color c) {
  56.       setBackground (c);
  57.       drawn = true;
  58.    }
  59.  
  60.    public void turnOff () {
  61.       setBackground (Color.lightGray);
  62.       drawn = false;
  63.    }
  64.  
  65.    public void paint (Graphics g) {
  66.       g.drawRect (location().x, location().y,
  67.             size().width, size().height );
  68.    }
  69.  
  70.    public int[] getGroups() {
  71.       int grp[] = new int[2];
  72.       if (v1.group > v2.group) {
  73.          grp[0] = v1.group;
  74.          grp[1] = v2.group;
  75.       } else {
  76.          grp[0] = v2.group;
  77.          grp[1] = v1.group;
  78.       }
  79.       return grp;
  80.    }
  81.  
  82.    public boolean mouseDown (Event evt, int mouse_x, int mouse_y) {
  83.       if ( ! drawn) {
  84.          turnOn (Color.black);
  85.          return false;
  86.       } return true;
  87.    }
  88.  
  89.    public boolean mouseEnter (Event evt, int mouse_x, int mouse_y) {
  90.       if ( ! drawn) setBackground (Color.orange);
  91.       return true;
  92.    }
  93.  
  94.    public boolean mouseExit (Event evt, int mouse_x, int mouse_y) {
  95.       if ( ! drawn) setBackground (Color.lightGray);
  96.       return true;
  97.    }
  98. }
  99.  
  100. class dgCell extends Canvas {
  101.    private boolean claimed;
  102.    private dgEdge left, top, right, bottom;
  103.  
  104.    public dgCell (dgEdge l, dgEdge t, dgEdge r, dgEdge b) {
  105.       claimed = false;
  106.       left = l;
  107.       top = t;
  108.       right = r;
  109.       bottom = b;
  110.       resize (top.size().width, left.size().height);
  111.       move (top.location().x, left.location().y);
  112.    }
  113.  
  114.    public void turnOn (Color c) {
  115.       setBackground (c);
  116.       claimed = true;
  117.    }
  118.  
  119.    public void turnOff () {
  120.       setBackground (Color.lightGray);
  121.       claimed = false;
  122.    }
  123.  
  124.    public int weight () {
  125.       if (claimed) return 0;
  126.       int w = 0;
  127.       if (left.isDrawn() ) w++;
  128.       if (top.isDrawn() ) w++;
  129.       if (right.isDrawn() ) w++;
  130.       if (bottom.isDrawn() ) w++;
  131.       return w;
  132.    }
  133.  
  134.    public dgEdge openSide () {
  135.       if ( ! left.isDrawn() ) return left;
  136.       if ( ! top.isDrawn() ) return top;
  137.       if ( ! right.isDrawn() ) return right;
  138.       if ( ! bottom.isDrawn() ) return bottom;
  139.       return null;
  140.    }
  141.  
  142.    public void paint (Graphics g) {
  143.       g.drawRect (location().x, location().y,
  144.             size().width, size().height );
  145.    }
  146. }
  147.  
  148. // This is the window where the game is played
  149. class dgGame extends Frame {
  150.    private dgVertex vertex[];
  151.    private dgEdge edge[];
  152.    private dgCell cell[];
  153.    private int n_vertices;
  154.    private int n_edges;
  155.    private int n_cells;
  156.    private int player;
  157.    private int score[];
  158.    private Color player_color[];
  159.    private Label board_name[];
  160.    private Label board_score[];
  161.    private Random random;
  162.    private Label status;
  163.  
  164.    public dgGame (int width, int height, int window_width,
  165.          int window_height, String name[]) {
  166.       super ("Dot Game Grid");
  167.       n_vertices = width*height;
  168.       n_edges = 2*n_vertices - width - height;
  169.       n_cells = (width - 1)*(height - 1);
  170.       vertex = new dgVertex[n_vertices];
  171.       edge = new dgEdge[n_edges];
  172.       cell = new dgCell[n_cells];
  173.       score = new int[2];
  174.       player_color = new Color[2];
  175.       player_color[0] = Color.green;
  176.       player_color[1] = Color.yellow;
  177.       board_name = new Label[2];
  178.       board_name[0] = new Label (name[0], Label.RIGHT);
  179.       board_name[1] = new Label (name[1], Label.RIGHT);
  180.       board_score = new Label[2];
  181.       board_score[0] = new Label("0");
  182.       board_score[0].setBackground (player_color[0]);
  183.       board_score[1] = new Label("0");
  184.       board_score[1].setBackground (player_color[1]);
  185.       random = new Random();
  186.       status = new Label();
  187.       int cell_size, vertex_size, border, row, col, vrtx;
  188.  
  189.       setBackground (Color.lightGray);
  190.       resize (window_width, window_height);
  191.  
  192.       // Get grid dimensions
  193.       cell_size = window_width/width;
  194.       if (cell_size > window_height/height)
  195.             cell_size = window_height/height;
  196.       vertex_size = cell_size/6;
  197.       if (vertex_size < 3) vertex_size = 3;
  198.       cell_size -= vertex_size;
  199.       border = (window_width - (width - 1)*cell_size - vertex_size)/2;
  200.  
  201.       // Define vertices
  202.       int vertex_count = 0;
  203.       for (row=0 ; row<height ; row++) {
  204.          for (col=0 ; col<width ; col++) {
  205.             vertex[vertex_count] = new dgVertex (
  206.                   border + col*cell_size,
  207.                   border + row*cell_size,
  208.                   vertex_size);
  209.             add (vertex[vertex_count]);
  210.             vertex_count++;
  211.          }
  212.       }
  213.  
  214.       // Define edges
  215.       int edge_count = 0;
  216.       for (row=0 ; row<height ; row++) {
  217.          for (col=1 ; col<width ; col++) {
  218.             vrtx = row*width + col;
  219.             edge[edge_count] = new dgEdge (
  220.                   vertex[vrtx - 1], vertex[vrtx]);
  221.             add (edge[edge_count]);
  222.             edge_count++;
  223.          }
  224.       }
  225.       for (row=1 ; row<height ; row++) {
  226.          for (col=0 ; col<width ; col++) {
  227.             vrtx = row*width + col;
  228.             edge[edge_count] = new dgEdge (
  229.                   vertex[vrtx - width], vertex[vrtx]);
  230.             add (edge[edge_count]);
  231.             edge_count++;
  232.          }
  233.       }
  234.  
  235.       // Define cells
  236.       int cell_count = 0;
  237.       for (row=0 ; row<height-1 ; row++) {
  238.          for (col=0 ; col<width-1 ; col++) {
  239.             cell[cell_count] = new dgCell (
  240.                   edge[n_edges/2 +row*width + col],
  241.                   edge[row*(width-1) + col],
  242.                   edge[n_edges/2 +row*width + col + 1],
  243.                   edge[(row+1)*(width-1) + col]);
  244.             add (cell[cell_count]);
  245.             cell_count++;
  246.          }
  247.       }
  248.  
  249.       // Score board
  250.       Panel panel = new Panel();
  251.       panel.setLayout (new GridLayout (2, 3) );
  252.       panel.add (board_name[0]);
  253.       panel.add (board_score[0]);
  254.       panel.add (new Button ("Clear") );
  255.       panel.add (board_name[1]);
  256.       panel.add (board_score[1]);
  257.       panel.add (new Button ("Quit") );
  258.       this.add ("South", panel);
  259.  
  260.       // Initialize the scores
  261.       resetGame();
  262.  
  263.       // Status line
  264.       this.add ("North", status);
  265.    }
  266.  
  267.    public void clearBoard () {
  268.       int k;
  269.       for (k=0 ; k<n_cells ; k++) cell[k].turnOff  ();
  270.       for (k=0 ; k<n_edges ; k++) edge[k].turnOff  ();
  271.    }
  272.  
  273.    public void resetGame () {
  274.       player = 0;
  275.       score[0] = score[1] = 0;
  276.       board_score[0].setText ("0");
  277.       board_score[1].setText ("0");
  278.  
  279.       // Highlight first player on score board
  280.       showPlayer();
  281.  
  282.       // Initialize the vertex groups
  283.       for (int v=0 ; v<n_vertices ; v++) vertex[v].group = v;
  284.  
  285.       // Does Java Man make the first move?
  286.       while (board_name[player].getText().equalsIgnoreCase ("Java Man")
  287.             && JavaMan() != null) {
  288.          ScanGrid();
  289.       }
  290.    }
  291.  
  292.    // Java Man's move
  293.    private dgEdge JavaMan () {
  294.  
  295.       // Take cells if possible
  296.       int m;
  297.       for (m=0 ; m<n_cells ; m++) {
  298.          if (cell[m].weight() == 3) {
  299.             dgEdge edg = cell[m].openSide();
  300.             edg.turnOn (Color.magenta);
  301.             mergeGroup (edg);
  302.             return edg;
  303.          }
  304.       }
  305.  
  306.       // Count group connections
  307.       int grp[];
  308.       int v, w;
  309.       int connect[][] = new int[n_vertices][n_vertices];
  310.       for (v=0 ; v<n_vertices ; v++)
  311.          for (w=0 ; w<=v ; w++) connect[v][w] = 0;
  312.       for (m=0 ; m<n_edges ; m++) {
  313.          grp = edge[m].getGroups();
  314.          connect[grp[0]][grp[1]]++;
  315.       }
  316.  
  317.       // Find how many cells we have to concede
  318.       int best = n_vertices;
  319.       for (v=1 ; v<n_vertices ; v++)
  320.          for (w=0 ; w<v ; w++)
  321.             if (connect[v][w] > 0 && best > connect[v][w])
  322.                best = connect[v][w];
  323.  
  324.       // Pick a move that concedes the minimum
  325.       int r = random.nextInt() % n_edges;
  326.       if (r < 0) r = -r;
  327.       for (m=0 ; m<n_edges ; m++) {
  328.          if ( ! edge[r].isDrawn() ) {
  329.             grp = edge[r].getGroups();
  330.             if (connect[grp[0]][grp[1]] == best) {
  331.                edge[r].turnOn (Color.magenta);
  332.                mergeGroup (edge[r]);
  333.                return edge[r];
  334.             }
  335.          }
  336.          r++;
  337.          if (r >= n_edges) r = 0;
  338.       }
  339.       return null;
  340.    }
  341.  
  342.    private void showPlayer () {
  343.       board_name[player].setBackground (Color.orange);
  344.       board_name[player ^ 1].setBackground (Color.lightGray);
  345.    }
  346.  
  347.    private void ScanGrid() {
  348.       Color c = player_color[player];
  349.       int filled_cells, count;
  350.  
  351.       filled_cells = 0;
  352.       do {
  353.          count = 0;
  354.          for (int k=0 ; k<n_cells ; k++) {
  355.             if (cell[k].weight() == 4) {
  356.                count++;
  357.                cell[k].turnOn (c);
  358.             }
  359.          }
  360.          filled_cells += count;
  361.       } while (count > 0);
  362.       if (filled_cells > 0) {
  363.          score[player] += filled_cells;
  364.          board_score[player].setText (
  365.                (new Integer (score[player])).toString() );
  366.       } else {
  367.          player ^= 1;
  368.          showPlayer();
  369.       }
  370.    }
  371.  
  372.    private void mergeGroup (dgEdge edg) {
  373.       int grp[] = edg.getGroups();
  374.       for (int v=0 ; v<n_vertices ; v++)
  375.          if (vertex[v].group == grp[0]) vertex[v].group = grp[1];
  376.    }
  377.       
  378.    public boolean mouseDown (Event evt, int mouse_x, int mouse_y) {
  379.       if (evt.target instanceof dgEdge) {
  380.          for (int k=0 ; k<n_edges ; k++) if (edge[k].isDrawn() )
  381.             edge[k].setBackground (Color.black);
  382.          mergeGroup ( (dgEdge)(evt.target) );
  383.          ScanGrid();
  384.          while (board_name[player].getText().equalsIgnoreCase ("Java Man")
  385.                && JavaMan() != null) {
  386.             ScanGrid();
  387.          }
  388.          return true;
  389.       }
  390.       return false;
  391.    }
  392.  
  393.    public boolean action (Event evt, Object arg) {
  394.       if (evt.target instanceof Button) {
  395.          String button_value = (String) arg;
  396.          clearBoard();
  397.          resetGame();
  398.          if (button_value.equals ("Quit") ) dispose();
  399.          return true;
  400.       }
  401.       return false;
  402.    }
  403. }
  404.  
  405. public class DotGame extends Applet {
  406.    private dgGame game;
  407.    private TextField name[];
  408.    private Choice grid_size;
  409.  
  410.    public void init () {
  411.       game = null;
  412.       name = new TextField[2];
  413.       name[0] = new TextField ("You", 15);
  414.       name[1] = new TextField ("Java Man", 15);
  415.  
  416.       // Options panel
  417.       Panel options_panel = new Panel();
  418.       GridBagLayout gridbag = new GridBagLayout();
  419.       GridBagConstraints c = new GridBagConstraints();
  420.       options_panel.setLayout (gridbag);
  421.       c.fill = GridBagConstraints.BOTH;
  422.       c.weightx = 1.0;
  423.  
  424.       // Player names
  425.       Label player1_label =
  426.          new Label ("Player 1 Name", Label.RIGHT);
  427.       c.gridwidth = 1;
  428.       gridbag.setConstraints (player1_label, c);
  429.       options_panel.add (player1_label);
  430.       c.gridwidth = GridBagConstraints.REMAINDER;
  431.       gridbag.setConstraints (name[0], c);
  432.       options_panel.add (name[0]);
  433.       Label player2_label =
  434.          new Label ("Player 2 Name", Label.RIGHT);
  435.       c.gridwidth = 1;
  436.       gridbag.setConstraints (player2_label, c);
  437.       options_panel.add (player2_label);
  438.       c.gridwidth = GridBagConstraints.REMAINDER;
  439.       gridbag.setConstraints (name[1], c);
  440.       options_panel.add (name[1]);
  441.  
  442.       // Grid size selector
  443.       Label game_size_label =
  444.          new Label ("Grid Size", Label.RIGHT);
  445.       c.gridwidth = 1;
  446.       gridbag.setConstraints (game_size_label, c);
  447.       options_panel.add (game_size_label);
  448.       grid_size = new Choice();
  449.          grid_size.addItem ("four");
  450.          grid_size.addItem ("five");
  451.          grid_size.addItem ("six");
  452.          grid_size.addItem ("seven");
  453.          grid_size.addItem ("eight");
  454.          grid_size.addItem ("nine");
  455.          grid_size.addItem ("ten");
  456.          grid_size.select ("six");
  457.       c.gridwidth = GridBagConstraints.REMAINDER;
  458.       gridbag.setConstraints (grid_size, c);
  459.       options_panel.add (grid_size);
  460.  
  461.       // Play button
  462.       Button play_button = new Button ("Play the Game");
  463.       gridbag.setConstraints (play_button, c);
  464.       options_panel.add (play_button);
  465.  
  466.       // Add the options panel
  467.       add (options_panel);
  468.       resize (preferredSize() );
  469.    }
  470.  
  471.    public void start() {
  472.       if (game != null && ! game.isVisible() ) game.show();
  473.    }
  474.  
  475.    public void stop () {
  476.       if (game != null && game.isVisible() ) game.hide();
  477.    }
  478.  
  479.    public boolean action (Event evt, Object arg) {
  480.       if (evt.target instanceof Button) {
  481.          showStatus ("Setting up the game");
  482.          int grid = grid_size.getSelectedIndex() + 4;
  483.          if (name[0].getText().length()==0) name[0].setText ("You");
  484.          if (name[1].getText().length()==0) name[1].setText ("Java Man");
  485.          String handle[] = new String[2];
  486.          handle[0] = name[0].getText();
  487.          handle[1] = name[1].getText();
  488.          if (game != null) game.dispose();
  489.          game = new dgGame (grid, grid, 224, 340, handle);
  490.          if ( ! game.isVisible() ) {
  491.             game.show();
  492.          }
  493.          showStatus (handle[0] + " vs " + handle[1]);
  494.          return true;
  495.       }
  496.       return false;
  497.    }
  498. }
  499.