Java source code Gobang network (1)
Java source code Gobang network (1)
[Source: CSDN hits: 555 Author: NA]
Below the source code is divided into four documents;
ChessClient.java: Client main program.
ChessInterface.java: Client interface.
ChessPad.java: the drawing board.
ChessServer.java: server-side.
Can accommodate 50 people at the same time online playing chess, chatting.
Notes with no detail, but can definitely run through j2sdk1.4.
/************************************************* The following
1.chessClient.java
************************************************** ********************************************/
Import java.awt .*;
Import java.awt.event .*;
Import java.io. *;
Import java.net .*;
Import java.util .*;
Class clientThread extends Thread
(
ChessClient chessclient;
ClientThread (chessClient chessclient)
(
This.chessclient = chessclient;
)
Public void acceptMessage (String recMessage)
(
If (recMessage.startsWith ( "/ userlist"))
(
StringTokenizer userToken = new StringTokenizer (recMessage, "");
Int userNumber = 0;
Chessclient.userpad.userList.removeAll ();
Chessclient.inputpad.userChoice.removeAll ();
Chessclient.inputpad.userChoice.addItem ( "all");
While (userToken.hasMoreTokens ())
(
String user = (String) userToken.nextToken ( "");
If (userNumber> 0 & &! User.startsWith ( "[inchess]"))
(
Chessclient.userpad.userList.add (user);
Chessclient.inputpad.userChoice.addItem (user);
)
UserNumber + +;
)
Chessclient.inputpad.userChoice.select ( "all");
)
Else if (recMessage.startsWith ( "/ yourname"))
(
Chessclient.chessClientName = recMessage.substring (10);
Chessclient.setTitle ( "Java client Gobang" + "ID:" + chessclient.chessClientName);
)
Else if (recMessage.equals ( "/ reject"))
(
Try
(
Chessclient.chesspad.statusText.setText ( "can not join the game");
Chessclient.controlpad.cancelGameButton.setEnabled (false);
Chessclient.controlpad.joinGameButton.setEnabled (true);
Chessclient.controlpad.creatGameButton.setEnabled (true);
)
Catch (Exception ef)
(
Chessclient.chatpad.chatLineArea.setText ( "chessclient.chesspad.chessSocket.close not closed");
)
Chessclient.controlpad.joinGameButton.setEnabled (true);
)
Else if (recMessage.startsWith ( "/ peer"))
(
Chessclient.chesspad.chessPeerName = recMessage.substring (6);
If (chessclient.isServer)
(
Chessclient.chesspad.chessColor = 1;
Chessclient.chesspad.isMouseEnabled = true;
Chessclient.chesspad.statusText.setText ( "Please Heiji 13361");
)
Else if (chessclient.isClient)
(
Chessclient.chesspad.chessColor =- 1;
Chessclient.chesspad.statusText.setText ( "have joined the games, waiting for the other 13361 …");
)
)
Else if (recMessage.equals ( "/ youwin"))
(
Chessclient.isOnChess = false;
Chessclient.chesspad.chessVictory (chessclient.chesspad.chessColor);
Chessclient.chesspad.statusText.setText ( "each other out, please give up the game from connecting points");
Chessclient.chesspad.isMouseEnabled = false;
)
Else if (recMessage.equals ( "/ OK"))
(
Chessclient.chesspad.statusText.setText ( "Creating successful game, waiting for others to join …");
)
Else if (recMessage.equals ( "/ error"))
(
Chessclient.chatpad.chatLineArea.append ( "transmission error: Please withdraw from the process, to re-enter \ n");
)
Else
(
Chessclient.chatpad.chatLineArea.append (recMessage + "\ n");
Chessclient.chatpad.chatLineArea.setCaretPosition (
Chessclient.chatpad.chatLineArea.getText (). Length ());
)
)
Public void run ()
(
String message = "";
Try
(
While (true)
(
Chessclient.in.readUTF message = ();
AcceptMessage (message);
)
)
Catch (IOException es)
(
)
)
)
Public class chessClient extends Frame implements ActionListener, KeyListener
(
UserPad userpad = new userPad ();
ChatPad chatpad = new chatPad ();
ControlPad controlpad = new controlPad ();
ChessPad chesspad = new chessPad ();
InputPad inputpad = new inputPad ();
Socket chatSocket;
DataInputStream in;
DataOutputStream out;
String chessClientName = null;
String host = null;
Int port = 4331;
Boolean isOnChat = false; / / chatting?
Boolean isOnChess = false; / / chess?
Boolean isGameConnected = false; / / chess the client?
Boolean isServer = false; / / If this is a chess game console
Boolean isClient = false; / / If the client is a chess game
Panel southPanel = new Panel ();
Panel northPanel = new Panel ();
Panel centerPanel = new Panel ();
Panel westPanel = new Panel ();
Panel eastPanel = new Panel ();
ChessClient ()
(
Super ( "Gobang Java Client");
SetLayout (new BorderLayout ());
Host = controlpad.inputIP.getText ();
WestPanel.setLayout (new BorderLayout ());
WestPanel.add (userpad, BorderLayout.NORTH);
WestPanel.add (chatpad, BorderLayout.CENTER);
WestPanel.setBackground (Color.pink);
Inputpad.inputWords.addKeyListener (this);
Chesspad.host = controlpad.inputIP.getText ();
CenterPanel.add (chesspad, BorderLayout.CENTER);
CenterPanel.add (inputpad, BorderLayout.SOUTH);
CenterPanel.setBackground (Color.pink);
Controlpad.connectButton.addActionListener (this);
Controlpad.creatGameButton.addActionListener (this);
Controlpad.joinGameButton.addActionListener (this);
Controlpad.cancelGameButton.addActionListener (this);
Controlpad.exitGameButton.addActionListener (this);
Controlpad.creatGameButton.setEnabled (false);
Controlpad.joinGameButton.setEnabled (false);
Controlpad.cancelGameButton.setEnabled (false);
SouthPanel.add (controlpad, BorderLayout.CENTER);
SouthPanel.setBackground (Color.pink);
AddWindowListener (new WindowAdapter ()
(
Public void windowClosing (WindowEvent e)
(
If (isOnChat)
(
Try
(
ChatSocket.close ();
)
Catch (Exception ed)
(
)
)
If (isOnChess | | isGameConnected)
(
Try
(
Chesspad.chessSocket.close ();
)
Catch (Exception ee)
(
)
)
System.exit (0);
)
Public void windowActivated (WindowEvent ea)
(
)
));
Add (westPanel, BorderLayout.WEST);
Add (centerPanel, BorderLayout.CENTER);
Add (southPanel, BorderLayout.SOUTH);
Pack ();
SetSize (670,548);
SetVisible (true);
SetResizable (false);
Validate ();
)
Public boolean connectServer (String serverIP, int serverPort) throws Exception
(
Try
(
ChatSocket = new Socket (serverIP, serverPort);
In = new DataInputStream (chatSocket.getInputStream ());
Out = new DataOutputStream (chatSocket.getOutputStream ());
ClientThread clientthread = new clientThread (this);
Clientthread.start ();
IsOnChat = true;
Return true;
)
Catch (IOException ex)
(
Chatpad.chatLineArea.setText ( "chessClient: connectServer: unable to connect the proposed restart procedures \ n");
)
Return false;
)
Public void actionPerformed (ActionEvent e)
(
If (e.getSource () == controlpad.connectButton)
(
Host = = chesspad.host controlpad.inputIP.getText ();
Try
(
If (connectServer (host, port))
(
Chatpad.chatLineArea.setText ("");
Controlpad.connectButton.setEnabled (false);
Controlpad.creatGameButton.setEnabled (true);
Controlpad.joinGameButton.setEnabled (true);
Chesspad.statusText.setText ( "a successful connection, create a game or join the game");
)
)
Catch (Exception ei)
(
Chatpad.chatLineArea.setText ( "controlpad.connectButton: unable to connect the proposed restart procedures \ n");
)
)
If (e.getSource () == controlpad.exitGameButton)
(
If (isOnChat)
(
Try
(
ChatSocket.close ();
)
Catch (Exception ed)
(
)
)
If (isOnChess | | isGameConnected)
(
Try
(
Chesspad.chessSocket.close ();
)
Catch (Exception ee)
(
)
)
System.exit (0);
)
If (e.getSource () == controlpad.joinGameButton)
(
String selectedUser = userpad.userList.getSelectedItem ();
If (selectedUser == null | | selectedUser.startsWith ( "[inchess]") | |
SelectedUser.equals (chessClientName))
(
Chesspad.statusText.setText ( "must first select an effective user");
)
Else
(
Try
(
If (! IsGameConnected)
(
If (chesspad.connectServer (chesspad.host, chesspad.port))
(
IsGameConnected = true;
IsOnChess = true;
IsClient = true;
Controlpad.creatGameButton.setEnabled (false);
Controlpad.joinGameButton.setEnabled (false);
Controlpad.cancelGameButton.setEnabled (true);
Chesspad.chessthread.sendMessage ( "/ joingame" userpad.userList.getSelectedItem + () + "" + chessClientName);
)
)
Else
(
IsOnChess = true;
IsClient = true;
Controlpad.creatGameButton.setEnabled (false);
Controlpad.joinGameButton.setEnabled (false);
Controlpad.cancelGameButton.setEnabled (true);
Chesspad.chessthread.sendMessage ( "/ joingame" userpad.userList.getSelectedItem + () + "" + chessClientName);
)
)
Catch (Exception ee)
(
IsGameConnected = false;
IsOnChess = false;
IsClient = false;
Controlpad.creatGameButton.setEnabled (true);
Controlpad.joinGameButton.setEnabled (true);
Controlpad.cancelGameButton.setEnabled (false);
Chatpad.chatLineArea.setText ( "chesspad.connectServer can not connect \ n" + ee);
)
)
)
If (e.getSource () == controlpad.creatGameButton)
(
Try
(
If (! IsGameConnected)
(
If (chesspad.connectServer (chesspad.host, chesspad.port))
(
IsGameConnected = true;
IsOnChess = true;
IsServer = true;
Controlpad.creatGameButton.setEnabled (false);
Controlpad.joinGameButton.setEnabled (false);
Controlpad.cancelGameButton.setEnabled (true);
Chesspad.chessthread.sendMessage ( "/ creatgame" + "[inchess]" + chessClientName);
)
)
Else
(
IsOnChess = true;
IsServer = true;
Controlpad.creatGameButton.setEnabled (false);
Controlpad.joinGameButton.setEnabled (false);
Controlpad.cancelGameButton.setEnabled (true);
Chesspad.chessthread.sendMessage ( "/ creatgame" + "[inchess]" + chessClientName);
)
)
Catch (Exception ec)
(
IsGameConnected = false;
IsOnChess = false;
IsServer = false;
Controlpad.creatGameButton.setEnabled (true);
Controlpad.joinGameButton.setEnabled (true);
Controlpad.cancelGameButton.setEnabled (false);
Ec.printStackTrace ();
Chatpad.chatLineArea.setText ( "chesspad.connectServer can not connect \ n" + ec);
)
)
If (e.getSource () == controlpad.cancelGameButton)
(
If (isOnChess)
(
Chesspad.chessthread.sendMessage ( "/ giveup" + chessClientName);
Chesspad.chessVictory (-1 * chesspad.chessColor);
Controlpad.creatGameButton.setEnabled (true);
Controlpad.joinGameButton.setEnabled (true);
Controlpad.cancelGameButton.setEnabled (false);
Chesspad.statusText.setText ( "create a game or join the game");
)
If (! IsOnChess)
(
Controlpad.creatGameButton.setEnabled (true);
Controlpad.joinGameButton.setEnabled (true);
Controlpad.cancelGameButton.setEnabled (false);
Chesspad.statusText.setText ( "create a game or join the game");
)
IsServer isClient = = false;
)
)
Public void keyPressed (KeyEvent e)
(
TextField inputWords = (TextField) e.getSource ();
If (e.getKeyCode () == KeyEvent.VK_ENTER)
(
If (inputpad.userChoice.getSelectedItem (). Equals ( "all"))
(
Try
(
Out.writeUTF (inputWords.getText ());
InputWords.setText ("");
)
Catch (Exception ea)
(
Chatpad.chatLineArea.setText ( "chessClient: KeyPressed unable to connect, to re-connect \ n");
Userpad.userList.removeAll ();
Inputpad.userChoice.removeAll ();
InputWords.setText ("");
Controlpad.connectButton.setEnabled (true);
)
)
Else
(
Try
(
Out.writeUTF ("/"+ inputpad.userChoice.getSelectedItem () + "" + inputWords.getText ());
InputWords.setText ("");
)
Catch (Exception ea)
(
Chatpad.chatLineArea.setText ( "chessClient: KeyPressed unable to connect, to re-connect \ n");
Userpad.userList.removeAll ();
Inputpad.userChoice.removeAll ();
InputWords.setText ("");
Controlpad.connectButton.setEnabled (true);
)
)
)
)
Public void keyTyped (KeyEvent e)
(
)
Public void keyReleased (KeyEvent e)
(
)
Public static void main (String args [])
(
ChessClient chessClient = new chessClient ();
)
)
/************************************************* Attachments
Below are: chessInteface.java
************************************************** ****************************************/
Import java.awt .*;
Import java.awt.event .*;
Import java.io. *;
Import java.net .*;
Class userPad extends Panel
(
List userList = new List (10);
UserPad ()
(
SetLayout (new BorderLayout ());
For (int i = 0; i <50; i + +)
(
UserList.add (i +"."+" without user ");
)
Add (userList, BorderLayout.CENTER);
)
)
Class chatPad extends Panel
(
TextArea chatLineArea = new TextArea ( "", 18, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
ChatPad ()
(
SetLayout (new BorderLayout ());
Add (chatLineArea, BorderLayout.CENTER);
)
)
Class controlPad extends Panel
(
Label IPlabel = new Label ( "IP", Label.LEFT);
TextField inputIP = new TextField ( "localhost", 10);
Button connectButton = new Button ( "connect mainframe");
Button creatGameButton = new Button ( "Establishment of the game");
Button joinGameButton = new Button ( "Join the game");
Button cancelGameButton = new Button ( "abandon the game");
Button exitGameButton = new Button ( "Close procedures");
ControlPad ()
(
SetLayout (new FlowLayout (FlowLayout.LEFT));
SetBackground (Color.pink);
Add (IPlabel);
Add (inputIP);
Add (connectButton);
Add (creatGameButton);
Add (joinGameButton);
Add (cancelGameButton);
Add (exitGameButton);
)
)
Class inputPad extends Panel
(
TextField inputWords = new TextField ( "", 40);
Choice userChoice = new Choice ();
InputPad ()
(
SetLayout (new FlowLayout (FlowLayout.LEFT));
For (int i = 0; i <50; i + +)
(
UserChoice.addItem (i +"."+" without user ");
)
UserChoice.setSize (60,24);
Add (userChoice);
Add (inputWords);
)
)
/************************************************* *********************************************
Below are: chessPad.java
************************************************** ********************************************/
Import java.awt .*;
Import java.awt.event .*;
Import java.io. *;
Import java.net .*;
Import java.util .*;
Class chessThread extends Thread
(
ChessPad chesspad;
ChessThread (chessPad chesspad)
(
This.chesspad = chesspad;
)
Public void sendMessage (String sndMessage)
(
Try
(
Chesspad.outData.writeUTF (sndMessage);
)
Catch (Exception ea)
(
System.out.println ( "chessThread.sendMessage:" + ea);
)
)
Public void acceptMessage (String recMessage)
(
If (recMessage.startsWith ( "/ chess"))
(
StringTokenizer userToken = new StringTokenizer (recMessage, "");
String chessToken;
ChessOpt String [] = ( "-1", "-1", "0");
Int chessOptNum = 0;
While (userToken.hasMoreTokens ())
(
ChessToken = (String) userToken.nextToken ( "");
If (chessOptNum> = 1 & & chessOptNum <= 3)
(
ChessOpt [chessOptNum-1] = chessToken;
)
ChessOptNum + +;
)
Chesspad.netChessPaint (Integer.parseInt (chessOpt [0]), Integer.parseInt (chessOpt [1]), Integer.parseInt (chessOpt [2]));
)
Else if (recMessage.startsWith ( "/ yourname"))
(
Chesspad.chessSelfName = recMessage.substring (10);
)
Else if (recMessage.equals ( "/ error"))
(
Chesspad.statusText.setText ( "error: there is no such users, please withdraw from the process, to re-enter");
)
Else
(
/ / System.out.println (recMessage);
)
)
Public void run ()
(
String message = "";
Try
(
While (true)
(
Chesspad.inData.readUTF message = ();
AcceptMessage (message);
)
)
Catch (IOException es)
(
)
)
)
Class chessPad extends Panel implements MouseListener, ActionListener
(
Int chessPoint_x =- 1, chessPoint_y =- 1, chessColor = 1;
Int chessBlack_x [] = new int [200];
Int chessBlack_y [] = new int [200];
Int chessWhite_x [] = new int [200];
Int chessWhite_y [] = new int [200];
Int chessBlackCount = 0, chessWhiteCount = 0;
Int chessBlackWin = 0, chessWhiteWin = 0;
Boolean isMouseEnabled = false, isWin = false, isInGame = false;
TextField statusText = new TextField ( "Please connects to the server");
Socket chessSocket;
DataInputStream inData;
DataOutputStream outData;
String chessSelfName = null;
String chessPeerName = null;
String host = null;
Int port = 4331;
ChessThread chessthread = new chessThread (this);
ChessPad ()
(
SetSize (440,440);
SetLayout (null);
SetBackground (Color.pink);
AddMouseListener (this);
Add (statusText);
StatusText.setBounds (40,5,360,24);
StatusText.setEditable (false);
)
Public boolean connectServer (String ServerIP, int ServerPort) throws Exception
(
Try
(
ChessSocket = new Socket (ServerIP, ServerPort);
InData = new DataInputStream (chessSocket.getInputStream ());
OutData = new DataOutputStream (chessSocket.getOutputStream ());
Chessthread.start ();
Return true;
)
Catch (IOException ex)
(
StatusText.setText ( "chessPad: connectServer: unable to connect \ n");
)
Return false;
)
Public void chessVictory (int chessColorWin)
(
This.removeAll ();
For (int i = 0; i <= chessBlackCount; i + +)
(
ChessBlack_x [i] = 0;
ChessBlack_y [i] = 0;
)
For (int i = 0; i <= chessWhiteCount; i + +)
(
ChessWhite_x [i] = 0;
ChessWhite_y [i] = 0;
)
ChessBlackCount = 0;
ChessWhiteCount = 0;
Add (statusText);
StatusText.setBounds (40,5,360,24);
If (chessColorWin == 1)
(ChessBlackWin + +;
StatusText.setText ( "Heiji-black: white" chessBlackWin +":"+ chessWhiteWin + + ", re-start, and wait for the Baiji 13361 …");
)
Else if (chessColorWin ==- 1)
(
ChessWhiteWin + +;
StatusText.setText ( "Baiji-black: white" chessBlackWin +":"+ chessWhiteWin + + ", re-start, and wait for Heiji 13361 …");
)
)
Public void getLocation (int a, int b, int color)
(
If (color == 1)
(
ChessBlack_x [chessBlackCount] = a * 20;
ChessBlack_y [chessBlackCount] = b * 20;
ChessBlackCount + +;
)
Else if (color ==- 1)
(
ChessWhite_x [chessWhiteCount] = a * 20;
ChessWhite_y [chessWhiteCount] = b * 20;
ChessWhiteCount + +;
)
)
Public boolean checkWin (int a, int b, int checkColor)
(
Int step = 1, chessLink = 1, chessLinkTest = 1, chessCompare = 0;
If (checkColor == 1)
(
ChessLink = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a + step) * 20 == chessBlack_x [chessCompare]) & & ((b * 20) == chessBlack_y [chessCompare]))
(
ChessLink chessLink = +1;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a-step) * 20 == chessBlack_x [chessCompare]) & & (b * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If ((a * 20 == chessBlack_x [chessCompare]) & & ((b + step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If ((a * 20 == chessBlack_x [chessCompare]) & & ((b-step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a-step) * 20 == chessBlack_x [chessCompare]) & & ((b + step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a + step) * 20 == chessBlack_x [chessCompare]) & & ((b-step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a + step) * 20 == chessBlack_x [chessCompare]) & & ((b + step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare + +)
(
If (((a-step) * 20 == chessBlack_x [chessCompare]) & & ((b-step) * 20 == chessBlack_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
)
Else if (checkColor ==- 1)
(
ChessLink = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a + step) * 20 == chessWhite_x [chessCompare]) & & (b * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a-step) * 20 == chessWhite_x [chessCompare]) & & (b * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If ((a * 20 == chessWhite_x [chessCompare]) & & ((b + step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If ((a * 20 == chessWhite_x [chessCompare]) & & ((b-step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a-step) * 20 == chessWhite_x [chessCompare]) & & ((b + step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a + step) * 20 == chessWhite_x [chessCompare]) & & ((b-step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
ChessLink = 1;
ChessLinkTest = 1;
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a + step) * 20 == chessWhite_x [chessCompare]) & & ((b + step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
For (step = 1; step <= 4; step + +)
(
For (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare + +)
(
If (((a-step) * 20 == chessWhite_x [chessCompare]) & & ((b-step) * 20 == chessWhite_y [chessCompare]))
(
ChessLink + +;
If (chessLink == 5)
(
Return (true);
)
)
)
If (chessLink == (chessLinkTest +1))
ChessLinkTest + +;
Else
Break;
)
)
Return (false);
)
Public void paint (Graphics g)
(
For (int i = 40; i <= 380; i = i +20)
(
G.drawLine (40, i, 400, i);
)
G.drawLine (40,400,400,400);
For (int j = 40; j <= 380; j = j +20)
(
G.drawLine (j, 40, j, 400);
)
G.drawLine (400,40,400,400);
G.fillOval (97,97,6,6);
G.fillOval (337,97,6,6);
G.fillOval (97,337,6,6);
G.fillOval (337,337,6,6);
G.fillOval (217,217,6,6);
)
Public void chessPaint (int chessPoint_a, int chessPoint_b, int color)
(
ChessPoint_black chesspoint_black = new chessPoint_black (this);
ChessPoint_white chesspoint_white = new chessPoint_white (this);
If (color == 1 & & isMouseEnabled)
(
GetLocation (chessPoint_a, chessPoint_b, color);
IsWin = checkWin (chessPoint_a, chessPoint_b, color);
If (isWin == false)
(
Chessthread.sendMessage ("/"+ chessPeerName + "/ chess" chessPoint_a + + "" + chessPoint_b + "" + color);
This.add (chesspoint_black);
Chesspoint_black.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
StatusText.setText ( "black (" + + chessBlackCount "Step)" chessPoint_a + + "" + chessPoint_b + ", please Baiji 13361");
IsMouseEnabled = false;
)
Else
(
Chessthread.sendMessage ("/"+ chessPeerName + "/ chess" chessPoint_a + + "" + chessPoint_b + "" + color);
This.add (chesspoint_black);
Chesspoint_black.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
ChessVictory (1);
IsMouseEnabled = false;
)
)
Else if (color & isMouseEnabled ==- 1)
(
GetLocation (chessPoint_a, chessPoint_b, color);
IsWin = checkWin (chessPoint_a, chessPoint_b, color);
If (isWin == false)
(
Chessthread.sendMessage ("/"+ chessPeerName + "/ chess" chessPoint_a + + "" + chessPoint_b + "" + color);
This.add (chesspoint_white);
Chesspoint_white.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
StatusText.setText ( "White (" + + chessWhiteCount "Step)" chessPoint_a + + "" + chessPoint_b + ", please Heiji 13361");
IsMouseEnabled = false;
)
Else
(
Chessthread.sendMessage ("/"+ chessPeerName + "/ chess" chessPoint_a + + "" + chessPoint_b + "" + color);
This.add (chesspoint_white);
Chesspoint_white.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
ChessVictory (-1);
IsMouseEnabled = false;
)
)
)
Public void netChessPaint (int chessPoint_a, int chessPoint_b, int color)
(
ChessPoint_black chesspoint_black = new chessPoint_black (this);
ChessPoint_white chesspoint_white = new chessPoint_white (this);
GetLocation (chessPoint_a, chessPoint_b, color);
If (color == 1)
(
IsWin = checkWin (chessPoint_a, chessPoint_b, color);
If (isWin == false)
(
This.add (chesspoint_black);
Chesspoint_black.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
StatusText.setText ( "black (" + + chessBlackCount "Step)" chessPoint_a + + "" + chessPoint_b + ", please Baiji 13361");
IsMouseEnabled = true;
)
Else
(
This.add (chesspoint_black);
Chesspoint_black.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
ChessVictory (1);
IsMouseEnabled = true;
)
)
Else if (color ==- 1)
(
IsWin = checkWin (chessPoint_a, chessPoint_b, color);
If (isWin == false)
(
This.add (chesspoint_white);
Chesspoint_white.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
StatusText.setText ( "White (" + + chessWhiteCount "Step)" chessPoint_a + + "" + chessPoint_b + ", please Heiji 13361");
IsMouseEnabled = true;
)
Else
(
Chessthread.sendMessage ("/"+ chessPeerName + "/ victory" + color);
This.add (chesspoint_white);
Chesspoint_white.setBounds (chessPoint_a * 20-7, 20-7,16,16 chessPoint_b *);
ChessVictory (-1);
IsMouseEnabled = true;
)
)
)
Public void mousePressed (MouseEvent e)
(
If (e.getModifiers () == InputEvent.BUTTON1_MASK)
(
ChessPoint_x = (int) e.getX ();
ChessPoint_y = (int) e.getY ();
Int a = (chessPoint_x +10) / 20, b = (chessPoint_y +10) / 20;
If (chessPoint_x/20 <2 | | chessPoint_y/20 <2 | | chessPoint_x/20> 19 | | chessPoint_y/20> 19)
()
Else
(
ChessPaint (a, b, chessColor);
)
)
)
Public void mouseReleased (MouseEvent e) ()
Public void mouseEntered (MouseEvent e) ()
Public void mouseExited (MouseEvent e) ()
Public void mouseClicked (MouseEvent e) ()
Public void actionPerformed (ActionEvent e)
(
)
)
Class chessPoint_black extends Canvas implements MouseListener
(
ChessPad chesspad = null;
ChessPoint_black (chessPad p)
(
SetSize (20,20);
Chesspad = p;
AddMouseListener (this);
)
Public void paint (Graphics g)
(
G.setColor (Color.black);
G.fillOval (0,0,14,14);
)
Public void mousePressed (MouseEvent e)
(
/ / If (e.getModifiers () == InputEvent.BUTTON3_MASK)
/ / (
/ / Chesspad.remove (this);
/ / Chesspad.chessColor = 1;
/ / Chesspad.text_2.setText ("");
/ / Chesspad.text_1.setText ( "Please Heiji 13361");
/ /)
)
Public void mouseReleased (MouseEvent e) ()
Public void mouseEntered (MouseEvent e) ()
Public void mouseExited (MouseEvent e) ()
Public void mouseClicked (MouseEvent e) ()
)
Class chessPoint_white extends Canvas implements MouseListener
(
ChessPad chesspad = null;
ChessPoint_white (chessPad p)
(
SetSize (20,20);
AddMouseListener (this);
Chesspad = p;
)
Public void paint (Graphics g)
(
G.setColor (Color.white);
G.fillOval (0,0,14,14);
)
Public void mousePressed (MouseEvent e)
(
/ / If (e.getModifiers () == InputEvent.BUTTON3_MASK)
/ / (
/ / Chesspad.remove (this);
/ / Chesspad.chessColor =- 1;
/ / Chesspad.text_2.setText ( "Please white flag 13361");
/ / Chesspad.text_1.setText ("");
/ /)
)
Public void mouseReleased (MouseEvent e) ()
Public void mouseEntered (MouseEvent e) ()
Public void mouseExited (MouseEvent e) ()
Public void mouseClicked (MouseEvent e)
(
/ / If (e.getClickCount ()> = 2)
/ / Chesspad.remove (this);
)
)
Tags: java code, network, source






