home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / nicnacnoe / board.java next >
Encoding:
Java Source  |  1996-08-14  |  5.4 KB  |  208 lines

  1. /*
  2. * @(#)Board.java    1.0 95/11/09 Ulrich Gall & Jan Kautz
  3. *
  4. * Copyright (c) 1996 Ulrich Gall & Jan Kautz
  5. * uhgall@cip.informatik.uni-erlangen.de
  6. * Hofmannstr. 48, D-91052 Erlangen, Germany, Fax: +49-9131-201358
  7. *
  8. * Permission to use, copy, and distribute this software
  9. * and its documentation for NON-COMMERCIAL purposes and without
  10. * fee is hereby granted provided that this copyright notice
  11. * appears in all copies. Please contact us for  further copyright
  12. * and licensing information.
  13. *
  14. * WE MAKE 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. WE 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.  
  22. package como.commlet.nicnacnoe;
  23.  
  24. import java.util.*;
  25. import java.awt.*;
  26. import java.io.*;
  27.  
  28. import como.sys.*;
  29. import como.util.*;
  30. import como.awt.*;
  31. import como.commlet.*;
  32. import como.commlet.draw.*;
  33. import como.commlet.chat.*;
  34. import como.commlet.userlist.*;
  35. import como.commlet.messagelog.*;
  36. import como.io.*;
  37.  
  38. public class Board extends Canvas {
  39.  
  40.  
  41. static final Integer DIMENSION = new Integer(1);
  42. static final Integer NUMPLAYERS = new Integer(2);
  43. static final Integer PLAYERS = new Integer(3);
  44. static final Integer POINTS = new Integer(4);
  45. static final Integer WINSCORE = new Integer(5);
  46. static final Integer DESCRIPTION = new Integer(6);
  47. static final Integer FALL = new Integer(7);
  48. static final Integer IDS = new Integer(8);
  49. static final Integer SCORES = new Integer(9);
  50.  
  51. static final int POINTSLENGTH = 10;
  52.  
  53. static int EMPTY = -1;
  54. int [][] ar = null;
  55. int xs = 0, ys = 0;
  56. Hashtable params = null;
  57.  
  58. public Board() {
  59.     }
  60. public void reset(Hashtable pars) {
  61.     params = pars;
  62.     clear((Dimension)pars.get(DIMENSION));
  63. }
  64. public void updateUserInfo(User u) {
  65.     if (params != null) {    
  66.         Hashtable players = ((Hashtable)params.get(PLAYERS));
  67.         if (players.containsKey(new Integer(u.getID()))) {
  68.             // save old score
  69.             u.put(u.SCORE,new Integer(getScore(u.getID())));
  70.             players.put(new Integer(u.getID()),u);
  71.             repaint();
  72.         }
  73.     }
  74. }
  75. public void clear(Dimension d) {
  76.     xs = d.width;
  77.     ys = d.height;
  78.     ar = new int[xs][ys];
  79.     int x,y;
  80.     for (x=0;x<xs;x++) for (y=0;y<ys;y++) {
  81.         ar[x][y] = EMPTY;
  82.         }
  83.     if (getGraphics() != null) paint(getGraphics());
  84. }
  85. public int getWinScore() {
  86.     return ((Integer)params.get(WINSCORE)).intValue();
  87. }
  88. public int getPoints(int i) {
  89.     if (i>=POINTSLENGTH) i = POINTSLENGTH-1;
  90.     return ((byte[])params.get(POINTS))[i];
  91. }
  92. public User getPlayer(int who) {
  93.     return (User)((Hashtable)params.get(PLAYERS)).get(new Integer(who));
  94. }
  95. public void setScore(int who,int newscore) {
  96.     getPlayer(who).put(User.SCORE,new Integer(newscore));
  97. }
  98. public int getScore(int who) {
  99.     if (getPlayer(who).containsKey(User.SCORE)) return ((Integer)getPlayer(who).get(User.SCORE)).intValue();
  100.     else return 0;
  101. }
  102. public Rectangle getRect(int x, int y) {
  103.     int xsize = size().width;
  104.     int ysize = size().height;
  105.     int csx = (xsize-1)/xs;
  106.     int csy = (ysize-1)/ys;
  107.     int bx = (xsize-csx*xs)/2;
  108.     int by = (ysize-csy*ys)/2;
  109.     return new Rectangle(bx+csx*x+1,by+csy*y+1,csx-2,csy-2);    
  110. }
  111. /**
  112. * Returns the x/y position on the board that contains coordinates x,y
  113. */
  114. public Point whichRect(int x, int y) {
  115. if ((xs!=0)&&(ys!=0)) {
  116.     int xsize = size().width;
  117.     int ysize = size().height;
  118.     int csx = (xsize-1)/xs;
  119.     int csy = (ysize-1)/ys;
  120.     int bx = (xsize-csx*xs)/2;
  121.     int by = (ysize-csy*ys)/2;
  122.     return new Point((x-bx)/csx,(y-by)/csy);
  123.     }
  124.     else return null;
  125. }
  126. public void paint(Graphics g) {
  127. if (params != null) {
  128.     g.clearRect(0,0,size().width,size().height);
  129.     int x,y;
  130.     int xmin = getRect(0,0).x;
  131.     int xmax = getRect(xs,0).x;
  132.     int ymin = getRect(0,0).y;
  133.     int ymax = getRect(0,ys).y;
  134.     for (x=0;x<=xs;x++) {
  135.         Rectangle r = getRect(x,x);
  136.         g.drawLine(r.x-1,ymin-1,r.x-1,ymax-1);
  137.     }
  138.     for (y=0;y<=ys;y++) {
  139.         Rectangle r = getRect(y,y);
  140.         g.drawLine(xmin-1,r.y-1,xmax-1,r.y-1);
  141.     }
  142.     for (x=0;x<xs;x++) {
  143.         for (y=0;y<ys;y++) {
  144.             drawCell(x,y);
  145.         }
  146.     }
  147. }
  148. }
  149. public void drawCell(int x, int y) {
  150.     Rectangle r = getRect(x,y);
  151.     Graphics g = getGraphics();
  152.     if (g != null) {
  153.         g.clearRect(r.x,r.y,r.width,r.height);
  154.         if (ar[x][y] != EMPTY) {
  155.             if (getPlayer(ar[x][y]) != null) {
  156.                 g.setColor((Color)getPlayer(ar[x][y]).get(User.COLOR));
  157.                 if (ar[x][y] != EMPTY) g.fillOval(r.x,r.y,r.width,r.height);
  158.             }
  159.             else
  160.                 Debug.msg(34,"ID tries to paint that is not in player list: "+ar[x][y]);
  161.         }
  162.     }            
  163. }
  164. public int get(int x, int y) {
  165.     if (ar != null) {
  166.         if ((x<0) || (x >=xs)) return EMPTY;
  167.         if ((y<0) || (y >=ys)) return EMPTY;
  168.         return ar[x][y];
  169.     }
  170.     return EMPTY;
  171. }
  172. public void set(Piece p) {
  173.     if (ar != null) {
  174.     ar[p.x][p.y] = p.p;
  175.     drawCell(p.x,p.y);
  176.         }
  177.     }
  178. /**
  179. * Converts the event's coordinates to the field in which the event occured
  180. * and passes it to the parent.
  181. */
  182. public boolean handleEvent(Event evt) {
  183.     if ((getParent() != null) && (params != null)) {
  184.         evt.target = this;
  185.         int x = evt.x;
  186.         int y = evt.y;
  187.         evt.x = whichRect(x,y).x;
  188.         evt.y = whichRect(x,y).y;
  189.         if ((evt.x >=0) && (evt.x <xs)
  190.             && (evt.y >=0) && (evt.y <ys)) {
  191.                 if (params.containsKey(FALL)) {
  192.                     // let the piece fall
  193.                     if (get(evt.x,0) != EMPTY) {
  194.                         return true;
  195.                         }
  196.                     int ny = 1;
  197.                     while ((ny<ys-1) && (get(evt.x,ny+1) == EMPTY)) ny++;
  198.                     evt.y = ny;
  199.                 }
  200.                 evt.target = this;
  201.                 getParent().postEvent(evt);
  202.             }
  203.         }
  204.     return true;
  205. }
  206. }
  207.  
  208.