Java implementation of a simple two-person Gobang game (v) to determine whether one side wins __java

Source: Internet
Author: User

1) Select Pieces

✔️

2) Painting pieces

✔️

3) Judging the outcome

Goal

4 Exchange Line Chess party


We have already painted the board, the pieces and can be free drop, then the function to be achieved is to determine whether there are five alignment (for the moment do not consider the line chess side ).

We used to traverse the chessboard has been drop position, look at each drop point, in its upper and lower, left and right, the upper-left and right in the four directions in either direction on whether there are five consecutive pieces. The first step, the chess pieces of the transformation, before our pieces class only color information and drop state, now want to add an int type of data, used to record the traversal process is currently a few beads known continuously.


Chessman.java

Package Xchen.test.simpleGobang;

public class Chessman {
	private int color;//1-white,0-black
	Private Boolean placed = false;
	int matchcount = 1;
	
	Public Chessman (int color,boolean placed) {
		this.color=color;
		this.placed=placed;
	}
	
	public Boolean getplaced () {return
		placed;
	}

	public void setplaced (Boolean placed) {
		this.placed = placed;
	}

	public int GetColor () {return
		color;
	}

	public void SetColor (int color) {
		This.color = color;
	}
}

The second step, first in a direction to determine whether there are five alignments, here using the left and right direction as an attempt.

Added a Iswin function to make a winning judgment by traversing a valid piece on the entire chessboard.

Drawchessboard.java

Package Xchen.test.simpleGobang;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.RadialGradientPaint;
Import Java.awt.Image;
Import Java.awt.Toolkit;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.Color;

Import Javax.swing.JPanel;
	public class Drawchessboard extends JPanel implements mouselistener{final static int black=0;
	final static int white=1;
	public int chesscolor = black;
	
	int chessman_width=30;
	Public Image boardimg;
	Final private int ROWS = 19;	
	
	Chessman[][] Chessstatus=new chessman[rows+1][rows+1];
		Public Drawchessboard () {boardimg = Toolkit.getdefaulttoolkit (). GetImage ("Res/drawable/chessboard2.png");
		
		if (boardimg = = null) System.err.println ("PNG do not Exist");
	Addmouselistener (this); 
		
		@Override protected void Paintcomponent (Graphics g) {//TODO auto-generated Method Stub super.paintcomponent (g);
		int imgwidth = Boardimg.getheight (this); int imgheight = Boardimg.getwidth (this);
		int fwidth = GetWidth ();
		
		int fheight= getheight ();
		int x= (fwidth-imgwidth)/2;
		
		int y= (fheight-imgheight)/2;
		int span_x=imgwidth/rows;
		
		int span_y=imgheight/rows;
		
		G.drawimage (boardimg, x, y, null);
		Draw horizontal for (int i=0;i<rows;i++) {g.drawline (x, y+i*span_y, fwidth-x,y+i*span_y);
		//Draw vertical for (int i=0;i<rows;i++) {g.drawline (x+i*span_x, y, x+i*span_x,fheight-y); ///Draw pieces for (int i=0;i<rows+1;i++) {for (int j=0;j<rows+1;j++) {if CHESSSTATUS[I][J]!=NULL&AMP;&A
					mp;chessstatus[i][j].getplaced () ==true) {//system.out.println ("Draw Chessman" +i+ "" +j);
					int pos_x=x+i*span_x;
					int pos_y=y+j*span_y;
					float radius_b=40;
					float radius_w=80;
					float[] fractions = new float[]{0f,1f};
					java.awt.color[] Colors_b = new Java.awt.color[]{color.black,color.white};
					color[] Colors_w = new Color[]{color.white,color.black};
					Radialgradientpaint paint; if (Chessstatus[i][j].getcolor () ==1) {//system.out.println ("Draw white Chess");
					Paint = new Radialgradientpaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
						}else{//system.out.println ("Draw Black Chess");
					Paint = new Radialgradientpaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);
					
					((GRAPHICS2D) g). Setpaint (paint);
				((GRAPHICS2D) g). Filloval (Pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
		@Override////When the user presses the mouse button, a public void mousepressed (MouseEvent e) {int point_x=e.getx () is occurred;
		
		int point_y=e.gety ();
		int imgwidth = Boardimg.getheight (this);
		int imgheight = Boardimg.getwidth (this);
		int fwidth = GetWidth ();
		
		int fheight= getheight ();
		int x= (fwidth-imgwidth)/2;
		
		int y= (fheight-imgheight)/2;
		int span_x=imgwidth/rows;
		
		int span_y=imgheight/rows;
		System.out.println ("Press");
		int status_x = 0;
int status_y = 0;		if (point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight) {
			System.out.println ("lawful"); for (int i=0;i<rows+1;i++) {if (point_x>=x-chessman_width/2+1+i*span_x) {if (point_x<=x+chessman_w) idth/2-1+i*span_x)//If the WIDTH/2 will appear in the middle point two matching values {//system.out.println ("point x" +i+ "" +point_x+ "" + (X-chessman_widt
						h/2+i*span_x) + "" + (x+chessman_width/2+i*span_x));
					Status_x = i; }} for (int i=0;i<rows+1;i++) {if (point_y>=y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println ("Point y" +i+ "+point_y+" "+ (y-chessman_width/2+1+
						i*span_y) + "" + (y+chessman_width/2-1+i*span_y));
					Status_y = i;
			}} Chessman Chessman = new Chessman (black, true);
			Chessstatus[status_x][status_y]=chessman;
			Repaint ();
	if (Iswin (status_x, status_y, Chessstatus)) {SYSTEM.OUT.PRINTLN ("WIN!!!!!");		}} @Override///When the user presses and releases the mouse button, a public void mouseclicked (MouseEvent e) {/TODO auto-generated method stub} is occurred @Override public void mousereleased (MouseEvent e) {//TODO auto-generated the stub} @Override public void Mouseentered (MouseEvent e) {//TODO auto-generated method stub} @Override public void mouseexited (MouseEvent e)  {//TODO auto-generated Method stub} boolean iswin (int point_x,int point_y,chessman[][] cm) {//int MatchCount = 1;//number of alignments//horizontal lookup for (int i=0;i<rows+1;i++) {for (int j=0;j<rows+1;j++) {if (chessstatus[i][j
					]!=null&&chessstatus[i][j].getplaced () ==true) {//system.out.println ("Iswin: +i+" "+j)"; Look to the right for (int n=1;n<=4;n++) {if (i+n>=0) && (i+n) <=rows) {if (chessstatus[i
								+n][j]!=null&&chessstatus[i+n][j].getplaced () ==true) {chessstatus[i][j].matchcount++; System.out.println ("POS:" +i+ "" +j+ "righT count++: "+ (I+n) +" "+j+" Count: "+chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
							}}else {break;
							Find for (int n=1;n<=4;n++) {if (i-n>=0) && (i-n) <=rows) to the left { if (chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced () ==true) {Chessstatus[i][j].mat
								chcount++;
								System.out.println ("POS:" +i+ "" +j+ "" + "Left count++:" + (I-N) + "" +j+ "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
								}}else {if (chessstatus[i-n][j]!=null) {chessstatus[i][j].matchcount = 1;
							} break;
		
	}} Chessstatus[i][j].matchcount=1;//refresh Count}} return false;
 }
}

The third step, the main module does not change, run test our algorithm is correct

Main.java

Package Xchen.test.simpleGobang;

Import Java.awt.Container;
Import Javax.swing.JFrame;

Import Xchen.test.simpleGobang.DrawChessBoard;

public class Main extends jframe{
	private Drawchessboard drawchessboard;
	Public Main () {		
		Drawchessboard = new Drawchessboard ();
		
		Frame title
		Settitle ("stand-alone Gobang");
		
		Container Containerpane =getcontentpane ();
		Containerpane.add (Drawchessboard);		
	}
	public static void Main (string[] args) {
		main m = new main ();
		M.setsize (a);
		M.setvisible (True);
	}




The fourth step, now we have a direction of the judgment has been done, and then complement the other three directions on the Judgment code

To complement the Iswin () function in Drawchessboard.java

Package Xchen.test.simpleGobang;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.RadialGradientPaint;
Import Java.awt.Toolkit;
Import java.awt.event.MouseEvent;

Import Java.awt.event.MouseListener;

Import Javax.swing.JPanel;
	public class Drawchessboard extends JPanel implements mouselistener{final static int black=0;
	final static int white=1;
	public int chesscolor = black;

	int chessman_width=30;
	Public Image boardimg;
	Final private int ROWS = 19;	

	Chessman[][] Chessstatus=new chessman[rows+1][rows+1];
		Public Drawchessboard () {boardimg = Toolkit.getdefaulttoolkit (). GetImage ("Res/drawable/chessboard2.png");

		if (boardimg = = null) System.err.println ("PNG do not Exist");
	Addmouselistener (this); 

		@Override protected void Paintcomponent (Graphics g) {//TODO auto-generated Method Stub super.paintcomponent (g);
		int imgwidth = Boardimg.getheight (this); int imgheight = Boardimg.getwidth (This);
		int fwidth = GetWidth ();

		int fheight= getheight ();
		int x= (fwidth-imgwidth)/2;

		int y= (fheight-imgheight)/2;
		int span_x=imgwidth/rows;

		int span_y=imgheight/rows;

		G.drawimage (boardimg, x, y, null);
		Draw horizontal for (int i=0;i<rows;i++) {g.drawline (x, y+i*span_y, fwidth-x,y+i*span_y);
		//Draw vertical for (int i=0;i<rows;i++) {g.drawline (x+i*span_x, y, x+i*span_x,fheight-y); ///Draw pieces for (int i=0;i<rows+1;i++) {for (int j=0;j<rows+1;j++) {if chessstatus[i][j]!=null&&amp
					; chessstatus[i][j].getplaced () ==true) {//system.out.println ("Draw Chessman" +i+ "" +j);
					int pos_x=x+i*span_x;
					int pos_y=y+j*span_y;
					float radius_b=40;
					float radius_w=80;
					float[] fractions = new float[]{0f,1f};
					java.awt.color[] Colors_b = new Java.awt.color[]{color.black,color.white};
					color[] Colors_w = new Color[]{color.white,color.black};
					Radialgradientpaint paint;
						if (Chessstatus[i][j].getcolor () ==1) {System.out.println ("Draw white Chess");
					Paint = new Radialgradientpaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
						}else{//system.out.println ("Draw Black Chess");
					Paint = new Radialgradientpaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);

					((GRAPHICS2D) g). Setpaint (paint);
				((GRAPHICS2D) g). Filloval (Pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
		@Override////When the user presses the mouse button, a public void mousepressed (MouseEvent e) {int point_x=e.getx () is occurred;

		int point_y=e.gety ();
		int imgwidth = Boardimg.getheight (this);
		int imgheight = Boardimg.getwidth (this);
		int fwidth = GetWidth ();

		int fheight= getheight ();
		int x= (fwidth-imgwidth)/2;

		int y= (fheight-imgheight)/2;
		int span_x=imgwidth/rows;

		int span_y=imgheight/rows;
		System.out.println ("Press");
		int status_x = 0;
		int status_y = 0; if (point_x>=x && Point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight) {//system.out.println ("lawful"); for (int i=0;i<rows+1;i++) {if (point_x>=x-chessman_width/2+1+i*span_x) {if (Point_x<=x+chessma) n_width/2-1+i*span_x)//If the WIDTH/2 will appear in the middle point two matching values {//system.out.println ("point x" +i+ "" +point_x+ "" + (X-chessman_w
						idth/2+i*span_x) + "" + (x+chessman_width/2+i*span_x));
					Status_x = i; }} for (int i=0;i<rows+1;i++) {if (point_y>=y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println ("Point y" +i+ "+point_y+" "+ (y-chessman_width/2+1+
						i*span_y) + "" + (y+chessman_width/2-1+i*span_y));
					Status_y = i;
			}} Chessman Chessman = new Chessman (black, true);
			Chessstatus[status_x][status_y]=chessman;
			Repaint ();
			if (Iswin (status_x, status_y, Chessstatus)) {SYSTEM.OUT.PRINTLN ("WIN!!!!!"); }} @Override//When the user pressesThe public void mouseclicked (MouseEvent e) {//TODO auto-generated a stub} @Override public void MOUs occurs when the mouse button is released
		ereleased (MouseEvent e) {//TODO auto-generated method stub} @Override public void mouseentered (MouseEvent e) {  Todo auto-generated method stub} @Override public void mouseexited (MouseEvent e) {//TODO auto-generated method Stub} boolean iswin (int point_x,int point_y,chessman[][] cm) {for (int i=0;i<rows+1;i++) {for (int j=0;j <rows+1;j++) {//Traverse to find if (chessstatus[i][j]!=null&&chessstatus[i][j].getplaced () ==true) {// Look to the right for (int n=1;n<=4;n++) {if (i+n>=0) && (i+n) <=rows) {if (chessstatus[i+n
								][j]!=null&&chessstatus[i+n][j].getplaced () ==true) {chessstatus[i][j].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "right count++:" + (I+n) + "" +j+ "Count:" +chessstatus[i][j].matchcount);
			if (chessstatus[i][j].matchcount==5)					{return true;
							}}else {break;
							Find for (int n=1;n<=4;n++) {if (i-n>=0) && (i-n) <=rows) to the left { if (chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced () ==true) {Chessstatus[i][j].mat
								chcount++;
								System.out.println ("POS:" +i+ "" +j+ "" + "Left count++:" + (I-N) + "" +j+ "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
								}}else {if (chessstatus[i-n][j]!=null) {chessstatus[i][j].matchcount = 1;
							} break;
			}} Chessstatus[i][j].matchcount=1;//refresh Count}}} for (int i=0;i<rows+1;i++) {
				for (int j=0;j<rows+1;j++) {//Portrait if (chessstatus[i][j]!=null&&chessstatus[i][j].getplaced () ==true)
	{///down lookup, coordinate origin in upper left corner, Y-direction downward for (int n=1;n<=4;n++) {					if ((j+n>=0) && (j+n) <=rows) {if (CHESSSTATUS[I][J+N]!=NULL&AMP;&AMP;CHESSSTATUS[I][J+N].GETP
								Laced () ==true) {chessstatus[i][j].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "up count++:" + (i) + "" + (J+n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
							}}else {break;
							Look up for (int n=1;n<=4;n++) {if (j-n>=0) && (j-n) <=rows) { if (chessstatus[i][j-n]!=null&&chessstatus[i][j-n].getplaced () ==true) {CHESSSTATUS[I][J].MATC
								hcount++;
								System.out.println ("POS:" +i+ "" +j+ "" + "Left count++:" + (i) + "" + (J-n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
		}}else {if (chessstatus[i][j-n]!=null) {chessstatus[i][j].matchcount = 1;						} break; }} Chessstatus[i][j].matchcount=1;//refresh count}}//direction: Top Left right for (int i=0;i<rows+1 i++) {for (int j=0;j<rows+1;j++) {//left upper if (chessstatus[i][j]!=null&&chessstatus[i][j].getplace D () ==true) {//down lookup, coordinate origin in upper left corner, Y-direction downward for (int n=1;n<=4;n++) {if (j-n>=0) && (j-n) < =rows&& (i-n) >=0&& (i-n) <=rows) {if (chessstatus[i-n][j-n]!=null&&chessstatus[i-n
								][j-n].getplaced () ==true) {chessstatus[i][j].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "up count++:" + (I-N) + "" + (J-n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
							}}else {break; }}//Right down for (int n=1;n<=4;n++) {if (j+n>=0) && (j+n) <=ROWS&& (i+ N) >=0&& (i+n) <=rows) {
							if (chessstatus[i+n][j+n]!=null&&chessstatus[i+n][j+n].getplaced () ==true) {Chessstatus[i][j
								].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "" + "Left count++:" + (I+n) + "" + (J+n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
								}}else {if (chessstatus[i+n][j+n]!=null) {chessstatus[i][j].matchcount = 1;
							} break; }} Chessstatus[i][j].matchcount=1;//refresh count}}//direction: Bottom left-right for (int i=0;i<rows+1 ; i++) {for (int j=0;j<rows+1;j++) {//left lower if (chessstatus[i][j]!=null&&chessstatus[i][j].getplace D () ==true) {//down lookup, coordinate origin in upper left corner, Y-direction downward for (int n=1;n<=4;n++) {if (j+n>=0) && (J+n) < =rows&& (i-n) >=0&& (i-n) <=rows) {if (chessstatus[i-n][j+n]!=null&&chessstatus[i-n ][j+n].getplaced () ==trUE) {chessstatus[i][j].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "up count++:" + (I-N) + "" + (J+n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
							}}else {break; }}//Right for (int n=1;n<=4;n++) {if (j-n>=0) && (j-n) <=ROWS&& (i+ N) >=0&& (i+n) <=rows) {if (chessstatus[i+n][j-n]!=null&&chessstatus[i+n][j-n].getplaced ()
								==true) {chessstatus[i][j].matchcount++;
								System.out.println ("POS:" +i+ "" +j+ "" + "Left count++:" + (I+n) + "" + (J-n) + "Count:" +chessstatus[i][j].matchcount);
								if (chessstatus[i][j].matchcount==5) {return true;
								}}else {if (chessstatus[i+n][j-n]!=null) {chessstatus[i][j].matchcount = 1;
							} break; }}} Chessstatus[i][j].matchcount=1;//refresh count}} return false;
 }
}

Run it again.


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.