Home > Back-end >  Behind the code to add comments to explain
Behind the code to add comments to explain

Time:09-21

import java.awt.*;
The import of java.net. *;
Import the Java. IO. *;
import java.util.*;
import java.awt.event.*;
Import the Java. The awt. The java.awt.geom. *;
The class OmokBoard extends Canvas {
Public static final ints BLACK=1, WHITE=1;//define the initial value
black and white colorPrivate int [] [] the map;//define a two-dimensional array is used to record the map coordinates,
Private int the size, the cell;//define the size and form
Private String info="end game";//is due to the tooltip

Private int color=BLACK;//define colors the initial value
Private Boolean enable=false;//define the type of Boolean values
Private Boolean running=false;//define the type of Boolean values
Private PrintWriter writer;
Private Graphics gboard gbuff;//define graphic abstract class
Private Image buff.
OmokBoard (int s, int) c {
This. The size=s; Enclosing cell=c;
The map=new int [] [size + 2];//instantiate the map
for(int i=0; iThe map [I]=new int [size + 2];//set the map element
SetBackground (new Color (200200100));//set the background color and size
SetSize (size * (cell + 1) + the size, the size * (cell + 1) + size);//set size
AddMouseListener (new MouseAdapter () {//mouse to monitor events
Public void mousePressed (MouseEvent me) {//mouse after event
if(! The enable) return;//determine the enable the value of the
Int x=(int) Math. Round me. (getX ()/(double) cell);//round get value
Int y=(int) Math. Round (me. GetY ()/(double) cell);
If (x==0 | | y==0 | | x==size + 1 | | y==size + 1) return;//and return
If (map [x] [y]==BLACK | | map [x] [y]==WHITE) return;//and return
Writer. Println (" [STONE] + x "+" "+ y);//print out the stones
The map [x] [y]=color;//set the color of the specified location
If (check (new Point (x, y), color)) {
Info="win."//set the message
Writer. Println (" [Windows] ");//print the information
}
The else info="wait for each other move later."//set the message
Repaint ();
The enable=false;//set the value of the enable
}
});
}
Public Boolean set () {
Return running;
}
Public void the startGame (String col) {
Running=true;
If (col) equals (" BLACK ")) {//judge the color
The enable=true; Color=BLACK;
Info="to start the game... Please move later. ";
}
The else {
The enable=false; Color=WHITE;
Info="to start the game... Please wait. ";
}
}
Public void stopGame () {
The reset ();//weight value defaults to the initial value
Writer. Println (" [STOPGAME] ");
The enable=false;
Running=false;
}
Public void putOpponent (int x, int y) {
The map [x] [y]=- color;
Info="rivals have move later... Please move later. ";
Repaint ();
}
Public void setEnable (Boolean enable) {
Enclosing the enable=enable;
}
Public void setWriter (PrintWriter writer) {
This. The writer=writer;
}
Public void update (Graphics g) {
Paint (g);
}
Public void paint (Graphics g) {
If (gbuff==null) {
Buff=createImage (getWidth () and getHeight ());
Gbuff=buff. GetGraphics ();
}
DrawBoard (g);
}
Public void reset () {
for(int i=0; ifor(int j=0; JThe map [I] [j]=0;
Info="end game";
Repaint ();
}
Private void drawLine () {
Gbuff. SetColor (Color black);
for(int i=1; i<=the size; I++) {
Gbuff. DrawLine (cell, I * cell, the cell size, I * cell);
Gbuff. DrawLine (I * cell, the cell, I * cell, the cell * size);
}
}
Private void drawBlack (int x, int y) {
Graphics2D gbuff=(Graphics2D) enclosing gbuff;
Gbuff. SetColor (Color black);
Gbuff. FillOval (* cell - cell/2 x, y * cell - cell/2, the cell, the cell).
Gbuff. SetColor (Color. White);
Gbuff. DrawOval (* cell - cell/2 x, y * cell - cell/2, the cell, the cell).
}
Private void drawWhite (int x, int y) {
Gbuff. SetColor (Color. White);
Gbuff. FillOval (* cell - cell/2 x, y * cell - cell/2, the cell, the cell).
Gbuff. SetColor (Color black);
Gbuff. DrawOval (* cell - cell/2 x, y * cell - cell/2, the cell, the cell).
}
Private void drawStones () {
For (int x=1; x<=the size; X++)
For (int y=1; y<=the size; Y++) {
If (map [x] [y]==BLACK)
DrawBlack (x, y);
Else if (map [x] [y]==WHITE)
DrawWhite (x, y);
}
}
Synchronized private void drawBoard (Graphics g) {
Gbuff. ClearRect (0, 0, getWidth () and getHeight ());
DrawLine ();
DrawStones ();
Gbuff. SetColor (Color. Red);
Gbuff. DrawString (info, 20, 15);
G.d rawImage (buff, 0, 0, this);
}
Private Boolean check Point p, int (col) {
If (count (p, 1, 0, col) + count (p, 1, 0, col)==4)
return true;
If (count (p, 0, 1, col) + count (p, 0, 1, col)==4)
return true;
If (count (p, 1, 1, col) + count (p, 1, 1, col)==4)
return true;
If (count (p, 1, 1, col) + count (p, 1, 1, col)==4)
return true;
return false;
}
Private int count (Point p, int dx, int dy, int col) {
int i=0;
for(; The map [p.x + (I + 1) * dx] [dy] p.y + (I + 1) *==col; i++);
return i;
}
}
Public class OmokClient extends Frame implements Runnable, ActionListener {
Private TextArea msgView=new TextArea,1,1 (" ", 1);
Private TextField sendBox=new TextField (" ");
Private TextField nameBox=new TextField ();
Private TextField roomBox=new TextField (" 0 ");
Private Label pInfo=new Label (" waiting room: ");
Private Java. The awt. List pList=new Java awt. List ();
Private Button startButton=new Button (" began to fight ");
Private Button stopButton=new Button (" abstention ");
Private Button enterButton=new Button (" enter ");
Private Button exitButton=new Button (" to standby room "); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • J2ME
  • Related