Play a game with colleagues-Java LAN wuziqi with chat room (socket connection)

Source: Internet
Author: User

If you want to learn about TCP/UDP network communication, use the online wuziqi to train your hands.

No human-machine combat algorithm is added, which is difficult to estimate.

At noon this day, I went to the station with my colleagues to make a one-stop bid (mainly to see the ending ~ Haha ~)

I 've been playing a few games, but I don't want to change a lot of things that need to be improved for the time being.

 

Two main points:

 

1 socket connection reference

 

2. Related logic of wuziqi (see the code below)

 

Download related code csdn

 

 

Wuziqi logic Code (the UI code will not be pasted ):

 

Package fivechessrefactoring; </P> <p>/** <br/> * Author: lazybone <br/> * Email: lgtwboy@sina.com <br/> * 2009-08 <br/> */<br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; </P> <p> @ suppresswarnings ("serial") <br/> class fivechesscanvas_s extends canvas implements mouselistener {</P> <p> static int blackorwhite = 1; // 1 is a sunspot (server), 2 is a white sub (client), <br/> static final int chessmansize = 10; // the size of the pawnpiece <br/> stati C Final int chesssize = 10, gridsize = 20; // number of cells, each shard <br/> chatserver; // Main Process Handle <br/> int [] [] chess = new int [chesssize] [chesssize]; // a checkerboard array <br/> int chesscount = 0; // The pawn <br/> graphics g; // image <br/> int iswin = 0; // check whether the player wins or loses. 1 indicates that the player wins the game ), 2 is the white sub-Win (client), <br/> int pos_x =-1, pos_y =-1; // current click position </P> <p> Public fivechesscanvas_s (chatserver) {<br/> addmouselistener (this); <br/> This. Chatserver = chatserver; <br/> Init (); <br/>}</P> <p> // you can determine whether to win or lose. 1 indicates that the Sunday wins (yourself ), 2 is a white sub-Win (opponent), <br/> int checkiswin (int x, int y) {<br/> int COUNT = 1; <br/> int TMP = chess [x] [Y]; <br/> If (x <0 | x> chesssize | Y <0 | Y> chesssize) <br/> return 0; <br/> // left <br/> If (X-4> 0) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [X-I] [Y] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> retu Rn TMP; <br/>}< br/> // above <br/> If (Y-4> 0) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [x] [Y-I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br/> // right <br/> If (x + 4 <chesssize) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [x + I] [Y] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br // <Br/> If (Y + 4 <chesssize) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [x] [Y + I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br/> // upper left <br/> If (X-4> 0 & Y-4> 0) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [X-I] [Y-I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br/> // right <Br/> If (x + 4 <chesssize & Y-4> 0) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [x + I] [Y-I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br/> // bottom right <br/> If (x + 4 <chesssize & Y + 4 <chesssize) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [x + I] [Y + I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TM P; <br/>}< br/> // bottom left <br/> If (X-4> 0 & Y + 4 <chesssize) {<br/> COUNT = 1; <br/> for (INT I = 1; I <= 4; I ++) {<br/> If (chess [X-I] [Y + I] = TMP) <br/> count ++; <br/> If (COUNT = 5) <br/> return TMP; <br/>}< br/> return 0; <br/>}</P> <p> // The opponent falls into the pawn, <br/> Public void drawachessman (int x, int y) {<br/> If (chess [x] [Y] = 0) {<br/> chess [x] [Y] = (blackorwhite = 1? 2: 1); <br/> repaint (); <br/> whoiswin (); <br/>}</P> <p> // draw a Board <br/> void drawchess (Graphics g) {<br/> int TMP = chessmansize/2; <br/> for (INT I = 0; I <chesssize; I ++) {<br/> for (Int J = 0; j <chesssize; j ++) {<br/> G. drawline (TMP + I, TMP + J * gridsize, TMP + (chesssize-1) <br/> * gridsize, TMP + J * gridsize); <br/> G. drawline (TMP + I * gridsize, TMP + J, TMP + I * gridsize, TMP <br/> + (Chesssize-1) * gridsize ); <br/>}</P> <p> // draw a chess piece based on the chessboard array <br/> void drawchessman (Graphics g) {<br/> for (INT I = 0; I <chesssize; I ++) {<br/> for (Int J = 0; j <chesssize; j ++) {<br/> If (chess [I] [J]! = 0) {<br/> G. setcolor (chess [I] [J] = 1 )? Color. black: color. blue); <br/> G. fillarc (I * gridsize, J * gridsize, chessmansize, <br/> chessmansize, 0,360 ); <br/>}</P> <p> // initialization <br/> Public void Init () {<br/> pos_x =-1; <br/> pos_y =-1; // current click position <br/> iswin = 0; // chess board status <br/> chesscount = 0; // a pawn <br/> for (INT I = 0; I <chesssize; I ++) {// board array initialization <br/> for (Int J = 0; j <chesssize; j ++) {<br/> chess [I] [J] = 0; <br/>}< br />}< Br/> repaint (); <br/>}</P> <p> Public void mouseclicked (mouseevent E) {<br/>}</P> <p> Public void mouseentered (mouseevent e) {<br/>}</P> <p> Public void mouseexited (mouseevent E) {<br/>}</P> <p> void dropachessman (int x, int y) {</P> <p >}</P> <p> // obtain the position of the child, and set the array to blackorwhite <br/> Public void mousepressed (mouseevent E) {<br/> If (iswin! = 0 |! Chatserver. canmove) {<br/> return; <br/>}< br/> int TMP = chessmansize/2-gridsize/2; // board adjustment <br/> pos_x = (E. getx ()-TMP)/gridsize; <br/> pos_y = (E. gety ()-TMP)/gridsize; <br/> // determine whether the location is within 0-chesssize <br/> If (pos_x <0 | pos_x> chesssize | pos_y <0 | pos_y> chesssize) <br/> return; <br/> If (chess [pos_x] [pos_y] = 0) {// not dropped <br/> chess [pos_x] [pos_y] = blackorwhite; // dropped <br/> chesscount + +; // Chess piece count + 1 <br/> whoiswin (); // whether the game has won <br/> chatserver. sendchessmessage (pos_x, pos_y); <br/>}< br/> repaint (); <br/>}</P> <p> Public void mousereleased (mouseevent E) {<br/>}</P> <p> Public void paint (Graphics g) {<br/> This. G = g; <br/> G. setcolor (color. black); <br/> drawchess (g); // draw a chessboard <br/> drawchessman (g); // draw a pawn Based on the chessboard array <br/> showstate (); <br/> If (iswin = 1) {<br/> system. out. println (iswin + "is win. "); <B R/>}< br/> If (chesscount = 100) {<br/> system. out. println ("nobody win. "); <br/>}</P> <p> Public void showstate () {<br/> If (! Chatserver. isconnected) {<br/> chatserver. fivechess_s.state.settext ("waiting for others to join..."); <br/>} else if (iswin! = 0) {<br/> string S = (iswin = 1 )? "You win": "You stupid ~ Lost ~ "); <Br/> chatserver. fivechess_s.state.settext (s); <br/>}else {<br/> If (chesssize * chesssize = chesscount) {<br/> chatserver. fivechess_s.state.settext ("White for half a day ~ Fail or win ~ "); <Br/> return; <br/>}< br/> string S1, S2; <br/> If (blackorwhite = 1) {<br/> S1 = "you are a sunspot"; <br/>} else {<br/> S1 = "you are a blue child "; <br/>}< br/> If (chatserver. canmove) {<br/> S2 = "you are down"; <br/>}else {<br/> S2 = "wait until the other party falls down... "; <br/>}< br/> chatserver. fivechess_s.state.settext (S1 + "" + S2); <br/>}</P> <p> void whoiswin () {<br/> for (INT I = 0; I <chesssize; I ++) {<br/> for (Int J = 0; j <chesssize; j + +) {<Br/> If (iswin = 0 & chess [I] [J]! = 0) {// if the game fails, continue to judge <br/> iswin = checkiswin (I, j ); <br/>}< br/> If (iswin! = 0 | chesscount = chesssize * chesssize) {<br/> chatserver. canmove = false; <br/>}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.