JAVA Flip Chess Game __java

Source: Internet
Author: User
Tags gety stub

This is another small game I wrote, the interface of what is not beautiful, first look at the effect of God horse.

Once saw a cartoon, called the Qin Moon, there is such a chess, inside the name is the ink to attack chess, suddenly thought, I would like to write and read, and spent some small time to write this goods ...

Look at the source code:

Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;

Import Java.awt.image.BufferedImage;

Import javax.swing.*;
	public class Reversi extends JFrame implements actionlistener{JButton = new Jbstart ("Start Game");
	JButton jbstop = new JButton ("End Game");
	Mypanel panel = new Mypanel ();
	
	Container C = This.getcontentpane ();
	int width = Toolkit.getdefaulttoolkit (). Getscreensize (). width;
	int height = Toolkit.getdefaulttoolkit (). Getscreensize (). Height;
	Boolean canplay = false;
	Boolean isblack = false;
	int count = 0;
	int f = 1;
	int cntblack = 0;
	int cntwhite = 0;
	
	int reversi[][] = new INT[9][9];
		Public Reversi () {Init ();
	AddListener ();
		public void Init () {this.settitle ("Reversi");
		This.setsize (500, 500);
		This.setresizable (FALSE);
		This.setlayout (NULL);
		This.setvisible (TRUE); This.setdefaultcloseoperation (this.
		Exit_on_close); This.setlocation ((width-500)/2, (height-500)/2);
		Jbstart.setbounds (135, 430, 100, 30);
		Jbstop.setbounds (265, 430, 100, 30);
		Panel.add (Jbstart);
		
		Panel.add (Jbstop);
		Panel.setlayout (NULL);
		Panel.setsize (500, 500);
	C.add (panel);
		public void AddListener () {This.jbStart.addActionListener (this);
		This.jbStop.addActionListener (this); This.addmouselistener (New MouseListener () {public void mousereleased (MouseEvent e) {//TODO auto-generated Me Thod stub} public void Mousepressed (MouseEvent e) {//TODO auto-generated Method stub if (can Play) {if (E.getx () >= && e.getx () <= 450 && e.gety () >= && e.gety () 450
						) {//system.out.println (E.getx () + "---" + e.gety ());
						int x = E.getx ();
						int y = e.gety ();
						x = (x-50)/50;
y = (y-50)/50;
						SYSTEM.OUT.PRINTLN (x + "-" + y);
									if (reversi[x][y] = = 0) {if (isblack) {if (!isok (x, Y, 1))Return
								Reversi[x][y] = 1;
								SYSTEM.OUT.PRINTLN (x + "-" + y + reversi[x][y]);
								System.out.println (Reversi[4][3]);
								iseated (x, y);
								Repaint ();
								System.out.println (Reversi[4][3]);
								Isblack = false;
									if (Check (2)) {if (Cntblack > Cntwhite) joptionpane.showmessagedialog (null, "sunspots more than white, black side wins");
									else if (Cntblack < cntwhite) Joptionpane.showmessagedialog (null, "white son excess sunspots, white win"); else Joptionpane.showmessagedialog (null, "Both sides are tied.")
								");
							} System.out.println (Cntblack + "---" + cntwhite);
								else {if (!isok (x, Y, 2)) return;
								Reversi[x][y] = 2;
								iseated (x, y);
								
								Repaint ();
								Isblack = true;
									if (Check (1)) {if (Cntblack > Cntwhite) joptionpane.showmessagedialog (null, "sunspots more than white, black side wins"); else if (Cntblack < cntwhite) Joptionpane.showmessagedialog (null, "white child excess sunspot, white sideWin "); else Joptionpane.showmessagedialog (null, "Both sides are tied.")
								");
							} System.out.println (Cntblack + "---" + cntwhite);
					} repaint ();  }} public void mouseexited (MouseEvent e) {//TODO auto-generated ' stub} public void mouseentered (MouseEvent e) {//TODO auto-generated method stub} public void Mouseclicked (Mousee
	Vent e) {//TODO auto-generated Method stub}});
			The public void actionperformed (ActionEvent e) {if (e.getsource () = = Jbstart) {Canplay = true;
			Jbstart.setenabled (FALSE);
			Initframe ();
		Repaint ();
		} if (E.getsource () = = Jbstop) {system.exit (0); } public class Mypanel extends JPanel {public void paintcomponent (Graphics scr) {bufferedimage bi = new Bu
			Fferedimage (+, BUFFEREDIMAGE.TYPE_INT_ARGB);
			
			Graphics g = bi.creategraphics ();
			
			G.setcolor (Color.Black); for (int i = 1; i <= 9;
			i++) {g.drawline (i*50, i*50, 420);
			for (int i = 1; I <= 9; i++) {g.drawline (M, i*50-30, 450, i*50-30);
					for (int i = 0; I <= 7; i++) {for (int j = 0; J <= 7; j +) {int x;
					int y;
					x = i * 50 + 50;
					y = J * 50 + 20;
					if (reversi[i][j] = = 1) {g.filloval (x, Y, 50, 50);
						else if (reversi[i][j] = = 2) {g.setcolor (color.white);
						G.filloval (x, Y, 50, 50);
						G.setcolor (Color.Black);
					G.drawoval (x, Y, 50, 50);
		}} scr.drawimage (bi, 0, 0, this);
		
		} public void iseated (int x, int y) {int color;
		if (isblack) color = 1;
		
		else color = 2;
		int flag = 0;
		int tempx = x;
		int tempy = y;
			while (tempx > 0) {//left tempx--; if (reversi[tempx][tempy] = = color) {for (int i = tempx + 1; I <= x; i++) {if (reversi[i][tempy) = = 0) fl
				AG = 1;
				} if (flag = = 1) break; for (int i = tempx + 1; I <= x; i++) {Reversi[i][tempy] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (TEMPX < 7) {//Right tempx++; if (reversi[tempx][tempy] = = color) {for (int i = x + 1; I <= tempx i++) {if (reversi[i][tempy) = = 0) fl
				AG = 1;
				} if (flag = = 1) break;
				for (int i = x + 1; I <= tempx i++) {reversi[i][tempy] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (Tempy > 0) {//upper tempy--; if (reversi[tempx][tempy] = = color) {for (int i = tempy + 1; I <= y; i++) {if (reversi[tempx][i) = = 0) F
				lag = 1;
				} if (flag = = 1) break;
				for (int i = tempy + 1; I <= y; i++) {reversi[tempx][i] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (Tempy < 7) {//tempy++; if (reversi[tempx][tempy] = = color) {for (int i = y + 1; I <= tempy i++) {if (reversi[tempx][i) = 0) fl
				AG = 1;
				} if (flag = = 1) break; Forint i = y + 1; I <= tempy;
				i++) {Reversi[tempx][i] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (tempx > 0 && tempy > 0) {//left upper tempx--;
			tempy--; if (reversi[tempx][tempy] = = color) {for (int i = tempx + 1, j = tempy + 1; I <= x && j <= y; i++, j + +)
				{if (reversi[i][j] = = 0) flag = 1;
				} if (flag = = 1) break;
				for (int i = tempx + 1, j = tempy + 1; I <= x && j <= y; i++, J + +) {Reversi[i][j] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (tempx > 0 && tempy < 7) {//lower left tempx--;
			tempy++; if (reversi[tempx][tempy] = = color) {for (int i = x-1, j = y + 1; j < Tempy I--, J + +) {if (reversi[i][j] = =
				0) flag = 1;
				} if (flag = = 1) break;
				for (int i = x-1, j = y + 1; j < Tempy I--, J + +) {Reversi[i][j] = color;
			} break;
		} flag = 0;
		TEMPX = x; Tempy= y;
			while (TEMPX < 7 && tempy > 0) {//upper right tempx++;
			tempy--;
				if (reversi[tempx][tempy] = = color) {//system.out.println ("11");
					for (int i = x + 1, j = y-1 i < tempx; i++, j--) {//system.out.println (i + "--" + j + reversi[i][j]);
						if (reversi[i][j] = = 0) {flag = 1;
					SYSTEM.OUT.PRINTLN (i + "----" + j + reversi[i][j]);
				} if (flag = 1) break;
				for (int i = x + 1, j = y-1 i < tempx; i++, j--) {reversi[i][j] = color;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (TEMPX < 7 && Tempy < 7) {//lower right tempx++;
			tempy++; if (reversi[tempx][tempy] = = color) {for (int i = x + 1, j = y + 1; I <= tempx && J <= Tempy; i++, J +)
				{if (reversi[i][j] = = 0) flag = 1;
				} if (flag = = 1) break;
				for (int i = x + 1, j = y + 1; I <= tempx && J <= Tempy; i++, J + +) {Reversi[i][j] = color;
			} break;
}
		}
	}	
public boolean IsOK (int x, int y, int color) {f = 0;
		int flag = 0;
		int tempx = x;
		int tempy = y;
			while (tempx > 0) {//left tempx--; if (reversi[tempx][tempy] = = color) {for (int i = tempx + 1; i < x; i++) {if (Reversi[i][tempy] = 0) fla
				g = 1;
				} if (flag = = 1) break;
					for (int i = tempx + 1; i < x; i++) {//reversi[i][tempy] = color;
				f = 1;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (TEMPX < 7) {//Right tempx++; if (reversi[tempx][tempy] = = color) {for (int i = x + 1; i < tempx i++) {if (Reversi[i][tempy] = 0) fla
				g = 1;
				} if (flag = = 1) break;
					for (int i = x + 1; i < tempx i++) {//reversi[i][tempy] = color;
				f = 1;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (Tempy > 0) {//upper tempy--; 
			if (reversi[tempx][tempy] = = color) {for (int i = tempy + 1; i < Y; i++) {if (reversi[tempx][i] = 0)			flag = 1;
				} if (flag = = 1) break;
					for (int i = tempy + 1; i < Y; i++) {//reversi[tempx][i] = color;
				f = 1;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (Tempy < 7) {//tempy++; if (reversi[tempx][tempy] = = color) {for (int i = y + 1; i < Tempy; i++) {if (reversi[tempx][i] = 0) fla
				g = 1;
				} if (flag = = 1) break;
					for (int i = y + 1; i < Tempy i++) {//reversi[tempx][i] = color;
				f = 1;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (tempx > 0 && tempy > 0) {//left upper tempx--;
			tempy--;
					if (reversi[tempx][tempy] = = color) {for (int i = tempx + 1, j = tempy + 1; i < x && J < y; i++, J + +) {
				if (reversi[i][j] = = 0) flag = 1;
				} if (flag = = 1) break;
					for (int i = tempx + 1, j = tempy + 1; i < x && J < y; i++, J + +) {//reversi[i][j] = color;
				f = 1;
			} break; }
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (tempx > 0 && tempy < 7) {//lower left tempx--;
			tempy++; if (reversi[tempx][tempy] = = color) {for (int i = x-1, j = y + 1; j < Tempy I--, J + +) {if (reversi[i][j] = =
				0) flag = 1;
				} if (flag = = 1) break;
					for (int i = x-1, j = y + 1; j < Tempy I--, J + +) {//REVERSI[I][J] = color;
				f = 1;
			} break;
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (TEMPX < 7 && tempy > 0) {//upper right tempx++;
			tempy--;
				if (reversi[tempx][tempy] = = color) {//system.out.println ("11");
					for (int i = x + 1, j = y-1 i < tempx; i++, j--) {//system.out.println (i + "--" + j + reversi[i][j]);
						if (reversi[i][j] = = 0) {flag = 1;
					SYSTEM.OUT.PRINTLN (i + "----" + j + reversi[i][j]);
				} if (flag = 1) break;
					for (int i = x + 1, j = y-1 i < tempx; i++, j--) {//reversi[i][j] = color;
				f = 1;
				}
				Break
		} flag = 0;
		TEMPX = x;
		Tempy = y;
			while (TEMPX < 7 && Tempy < 7) {//lower right tempx++;
			tempy++;
					if (reversi[tempx][tempy] = = color) {for (int i = x + 1, j = y + 1; i < tempx && J < Tempy; i++, J + +) {
				if (reversi[i][j] = = 0) flag = 1;
				} if (flag = = 1) break;
					for (int i = x + 1, j = y + 1; i < tempx && J < Tempy; i++, J + +) {//reversi[i][j] = color;
				f = 1;
			} break;
		} if (f = = 1) return true;
	else return false;				public boolean Check (int color) {//for (int i = 0; I <= 7; i++) {//for (int j = 0; J <= 7; j +) {//
if (reversi[i][j] = = color)//return false;
else if (reversi[i][j] = = Color% 2 + 1)//count++;
		}////return true;
		Cntblack = 0;
		Cntwhite = 0;
		int cnt = 0; for (int i = 0; I <= 7; i++) {for (int j = 0; J <= 7; j +) {if (reversi[i][j] = 1) {cntblack+
				+;
			}	else if (reversi[i][j] = = 2) {cntwhite++;
				else {cnt++; (int i = 0; I <= 7; i++) {for (int j = 0; J <= 7; j +) {if (reversi[i][j] = 0 &&
			IsOK (i, J, color)) return false; } joptionpane.showmessagedialog (NULL, color = 1?)
		There is no place for the sunspot, the White Child action ":" White son nowhere to change the action "");
		
		color = color% 2 + 1; for (int i = 0; I <= 7; i++) {for (int j = 0; J <= 7; j +) {if (reversi[i][j] = 0 && IsOK (i, J, color
					) {if (color = = 1) {Isblack = true;
					else {isblack = false;
				return false;
		}} joptionpane.showmessagedialog (null, "Both sides have nowhere to play, Game Over");
	return true;
		public void Initframe () {reversi[3][3] = reversi[4][3] = 1;
	REVERSI[3][4] = reversi[4][4] = 2;
	public static void Main (string[] args) {//TODO auto-generated method Stub new Reversi ();
 }
}

The game basically no bugs, I changed a long time t, comments or something did not write, ah, good failure-. -Forget it, that's it.

What's the problem?

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.