Import Java.awt.color;import java.awt.font;import Java.awt.graphics;import Java.awt.toolkit;import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;import Java.awt.image.bufferedimage;import Javax.swing.jframe;import Javax.swing.joptionpane;public class Fivechessframe extends JFrame implements Runnable, mouselistener{/** * */private static final long Serialversionuid = 1l;//game interface size int Width=toolkit.getdefaulttoolkit (). Getscreensize (). Width;int Height=toolkit.getdefaulttoolkit (). Getscreensize (). Height;int x, y; Mouse coordinates int[][]allchess=new INT[15][15]; Save the chessboard, 0 means No child, 1 for sunspots, 2 for white-letters Boolean isblack=true; The current is a sunspot or white, true for the sunspot, false to represent the Canplay=true Boolean; Marks the beginning of the current game whether the end of string message= "black First"; String blackmessage= "Unrestricted"; String whitemessage= "unlimited";//Save the game, record each step Lazi position int[]chessx=new int[255]; Int[]chessy=new int[255]; int countx,county;//default setting no time limit int maxtime=0; Save max Time int blacktime=0;int whitetime=0; Save the time remaining in the black and white side thread timer=new thread (this); Public Fivechessframe () {this.settitle ("Gobang Game"); This.setsize (This.setlocation (width-500)/2, (height-500)/2); This.setresizable (FALSE); This.setdefaultcloseoperation (Jframe.exit_on_close); This.setvisible (TRUE); This.repaint (); This.addmouselistener (this); Timer.start (); Turn on timed thread timer.suspend ();} Draw board Interface public void Paint (Graphics g) {//double buffering technology bufferedimage buf=new BufferedImage (500,500,BUFFEREDIMAGE.TYPE_INT_RGB) ; Graphics G1=buf.creategraphics (); Create a brush G1.setcolor (new Color (0,169,158)); Sets the brush color g1.fill3drect (43,60,375,375,true); for (int i=0;i<=15;i++) {g1.setcolor (color.white); G1.drawline (43,60+ I*25,375+43,60+I*25);//Draw Checkerboard G1.drawline (43+i*25,60,43+i*25,375+60); Draw the Checkerboard Bar}g1.setfont (new Font ("bold", font.bold,20)); G1.drawstring ("Game info:" +message,50,50); G1.drawrect (30,440,180,40) ; G1.drawrect (250,440,180,40); Draw black square time with white time string border G1.setfont (new Font ("Arial", 0,12)), g1.drawstring ("Black Time:" +blackmessage,40,465); G1.drawstring (" White time: "+whitemessage,260,465"); G1.drawrect (428,66, 54,20); G1.drawstring ("Re-start", 432,80); Restart button g1.drawrect (428,106,54,20); G1.drawstring ("Game Settings", 432,120); Game Settings button G1.drawrect (428, 146, 54, 20); G1.drawstring ("Game description", 432, 160); Game description button G1.drawrect (428, 186, 54, 20); G1.drawstring ("Exit Game", 432, 200); Exit the game button G1.drawrect (428, 246, 54, 20); G1.drawstring ("undo", 442, 260); Undo G1.drawrect (428, 286, 54, 20); G1.drawstring ("Concede", 442, 300); Defeat for (int i=0;i<15;i++) {for (int j=0;j<15;j++) {//Draw solid sunspots if (allchess[i][j]==1 ) {int tempx=i*25+47; int tempy=j*25+64; G1.setcolor (Color.Black); G1.filloval (tempx,tempy,16,16); G1.setcolor (Color.Black); G1.drawoval (tempx,tempy,16,16); }//Draw solid white if (allchess[i][j]==2) {int tempx=i*25+47; int tempy=j*25+64; G1.setcolor (Color.White); G1.filloval (TEMPX, Tempy, 16, 16); G1.setcolor (Color.Black); G1.drawoval (TEMPX, Tempy, 16, 16); }}}g.drawimage (buf, 0, 0,this);} @Overridepublic void mousepressed (MouseEvent e) {if (CanPlay) {x=e.getx (); y=e.gety ();//Get mouse coordinates if (x>=55 && x <=405 && y>=72 && y<=420) {//make x, y in the range of 0-15 if ((x-55)%25>12) {x= (x-55)/25+1;} else{x= (x-55)/25;} if ((y-72)%25>12) {y= (y-72)/25+1;} else{y= (y-72)/25;} Lazi if (allchess[x][y]==0) {chessx[countx++]=x;chessy[county++]=y;if (isblack) {allchess[x][y]=1;isblack=false; message= "White";} Else{allchess[x][y]=2;isblack=true;message= "Black Side";} This.repaint (), if (This.iswin ()) {if (allchess[x][y]==1) {Joptionpane.showmessagedialog (this, "Game over, Black wins");} Else{joptionpane.showmessagedialog (This, "Game over, White wins");} This.canplay=false; Logo game End}}}}//restart game if (E.getx () >=428 && e.gety () <= (428+54) && e.gety () >=66 && E.gety () <= (66+20)) {int Result=joptionpane.showconfirmdialog (this, "Do you want to start the game again?") "); 0 means restart if (result==0) {restartgame ();}} Game Countdown Setting if (E.getx () >=428 &&Amp E.getx () <= (428+54) && e.gety () >= 106 && e.gety () <= (106+20)) {String time= Joptionpane.showinputdialog ("Enter the maximum time (in minutes) of the game, if you enter 0, indicating there is no time limit:"); maxtime=integer.parseint-*60; Convert minutes to seconds so that you can later calculate if (maxtime<0) {Joptionpane.showmessagedialog (this, "input game time is wrong, please reset!");} else if (maxtime==0) {int Result=joptionpane.showconfirmdialog (this, "game time set successfully, do you want to start the game again?"); /restart if (result==0) {restartgame (); Timer.suspend ();//suspend thread so that after reboot}}else if (maxtime>0) {int result = Joptionpane.showconfirmdialog (This, "game time set successfully, do you want to start the game again?"); if (result==0) {for (int. i=0;i<15;i++) {for (int j=0;j<15;j++) {allchess[i][j]=0;}} for (int i=0;i<15;i++) {chessx[i]=-1;chessy[i]=-1;} Countx=0;county=0;message= "Black side First"; isblack=true;blackmessage=maxtime/3600+ ":" + (MAXTIME/60-MAXTIME/3600*60) + ":" + ( MAXTIME-MAXTIME/60*60); whitemessage = maxtime/3600 + ":" + (MAXTIME/60-MAXTIME/3600*60) + ":" + (MAXTIME-MAXTIME/60*60); Blacktime=maxtime; Whitetime=maxtime;timer.resume (); this.canplay=true; This.repaint ();}}} Game saysif (E.getx () >=428 && e.gety () <= (428+54) && e.gety () >=146 && e.gety () <= (146+20)) { Joptionpane.showmessagedialog (This, "rule: the first and the only five children wins!");} Exit the game if (E.getx () >=428 && e.getx () <= (428+54) && e.gety () >=186 && e.gety () <= (186+ ) {int Result=joptionpane.showconfirmdialog (this, "Do you want to quit the game?"); if (result==0) {system.exit (0);}} Undo if (E.getx () >=428 && e.getx () <= (428+54) && e.gety () >=246 && e.gety () <= (246+20 ) {int Result=joptionpane.showconfirmdialog (this, (isblack==true? ") White square undo, black side agree? ":" Black Fang Undo, white agree? ")); Result=0 is undo if (Result==0) {allchess[chessx[--countx]][chessy[--county]]=0; if (isblack==true) {isblack=false;} else{Isblack=true;} This.repaint (); Redraw the Board}}//Throw out if (E.getx () >=428 && e.getx () <= (428+54) && e.gety () >=286 && e.gety () < = (286+20)) {int Result=joptionpane.showconfirmdialog (this, "Do you concede?"); if (result==0) {Joptionpane.showmessagedialog ( This, "tourThe play is over, "+ (isblack==true? "Black party defeat, White win!": "White party to defeat, Black win!"); }}}//timed @overridepublic void Run () {if (this.maxtime>0) {while (true) {if (isblack) {blacktime--;if (blacktime==0) { Joptionpane.showmessagedialog (This, "Black timeout, Game Over"); Restartgame (); Timer.suspend ();}} Else{whitetime--;if (whitetime==0) {Joptionpane.showmessagedialog (this, "Bai Fangsu, Game Over"); Restartgame (); Timer.suspend ( );}} blackmessage=blacktime/3600+ ":" + (BLACKTIME/60-BLACKTIME/3600*60) + ":" + (blacktime-blacktime/60*60); whiteMessage= whitetime/3600+ ":" + (WHITETIME/60-WHITETIME/3600*60) + ":" + (WHITETIME-WHITETIME/60*60); This.repaint (); try { Thread.Sleep (1000); Set synchronization with clock, 1 seconds} catch (Interruptedexception e) {e.printstacktrace ();}}}} @Overridepublic void mouseclicked (MouseEvent e) {//TODO auto-generated method stub} @Overridepublic void Mousereleased ( MouseEvent e) {//Todo auto-generated method stub} @Overridepublic void Mouseentered (MouseEvent e) {//Todo auto-generated Method stub} @Overridepublic void Mouseexited (MouseEvent e) {//TODO auto-generated methodstub}public void Restartgame () {for (int. i=0;i<15;i++) {for (int j=0;j<15;j++) {allchess[i][j]=0; Clear the Chess piece}}//the record for (int i=0;i<15;i++) {chessx[i]=0;chessy[i]=0;} Countx=0;county=0;message= "Black side First", blackmessage= "unrestricted", whitemessage= "unrestricted"; blacktime=maxtime;whitetime=maxtime; Isblack=true;canplay=true;this.repaint ();} public Boolean Iswin () {Boolean flag=false; Defines a flag bit int count=1; Save common colors How many pieces are connected, the initial value is 1int color=allchess[x][y]; Color=1 (Sunspots). color=2 (white sub)//judge whether there are 5 pieces of the transverse, features: The ordinate is the same, that is, Allchess[x][y] y value is the same count=this.checkcount (1,0,color); count>=5) {flag=true;} else{//Judging longitudinal count=this.checkcount (0, 1, color); if (count>=5) {flag=true;} else{//judgment right, lower left count=this.checkcount (1,-1, color); if (count>=5) {flag=true;} else{//judgment right, upper left count=this.checkcount (1,1,color); if (count>=5) {flag=true;}}} return flag;} public int checkcount (int xchange,int ychange,int color) {int count=1;int tempx=xchange;int tempy=ychange;//Save initial value// The global variable x, y is initially the coordinates of the mouse click, the range of x, Y has been changed to 0-15 (traversing the entire board, looking for a piece of the same color) while (x+xchange>=0 && x+xchange<15 && y+ychange>=0 && y+ychange<15 && color==allchess[x+ Xchange][y+ychange]) {count++;if (xchange!=0) xchange++;if (ychange!=0) {if (ychange!=0) {if (ychange>0) yChange++; else {ychange--;}}} Xchange=tempx;ychange=tempy;while (x-xchange>=0 && x-xchange<15 && y-ychange>=0 && Y-ychange<15 && Color==allchess[x-xchange][y-ychange]) {count++;if (xchange!=0) {xchange++;} if (ychange!=0) {if (ychange>0) ychange++;else{ychange--;}}} return count;} public static void Main (string[] args) {new Fivechessframe ();}}
Java Swing Two-person Gobang source code