【骐程】五子棋
附上所有代码:
(如最下)
效果
package wuziqi;
import javax.swing.JOptionPane;
public class Win {
private int[][] chessArray;
public Win(int[][] chessArray) {
this.chessArray = chessArray;
}
public void isWin(int x,int y) {
if(sp(x,y)>=5){
if(chessArray[x][y] == 1) {
System.out.println("黑棋赢了");
JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
}else if(chessArray[x][y] == -1) {
System.out.println("白棋赢了");
JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
}
}
}
public int sp(int x,int y) {
int count = 0;
//当前位置向左向右去找
if(chessArray[x-1][y] == chessArray[x][y] || chessArray[x+1][y] == chessArray[x][y]) {
//向右
for(int i=x+1;i<=chessArray.length;i++) {
if(chessArray[i][y] == chessArray[x][y]) {
count++;
}else {
break;
}
}
//向左
for(int i=x;i>=0;i--) {
if(chessArray[i][y] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x][y-1] == chessArray[x][y] || chessArray[x][y+1] == chessArray[x][y]) {
//向下
for(int i=y+1;i<=chessArray.length;i++) {
if(chessArray[x][i] == chessArray[x][y]) {
count++;
}else {
break;
}
}
//向上
for(int i=y;i>=0;i--) {
if(chessArray[x][i] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x-1][y-1] == chessArray[x][y] || chessArray[x+1][y+1] == chessArray[x][y]) {
//向左上
for(int i=x,j=y;i>=0&&j>=0;i--,j--) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
//System.out.println("i="+i);
}else {
break;
}
}
//向右下
for(int i=x+1,j=y+1;i<=chessArray.length&&j<=chessArray.length;i++,j++) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x-1][y+1] == chessArray[x][y] || chessArray[x+1][y-1] == chessArray[x][y]) {
//向右上
for(int i=x,j=y;i<=chessArray.length&&j>=0;i++,j--) {
//System.out.println("测试");
if(chessArray[i][j] == chessArray[x][y]) {
System.out.println("测试");
count++;
System.out.println("i="+i+" j="+j);
}else {
break;
}
}
//向左下
for(int i=x-1,j=y+1;i>=0&&j<=chessArray.length;i--,j++) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}
return count;
}
}
思路:
1画图工具-画棋盘(paint,防止硬编码)(接口常量)—加监听器,重新定义一个类,画棋子-防止重回–判断输赢
具体实现
需要有一个画图工具,需要在画图工具上画出棋盘,这里需要画图的棋盘不停的显示,需要一直画在上面,所以需要在paint方法中一直实现,
在当前窗体直接使用paint方法,就不需要再保存画的棋盘,在当前这个类直接继承jframe
public void paint(Graphics g) {
super.paint(g);
System.out.println("绘制棋盘");
画线画棋盘(避免硬编码,就使用接口的常量),
public interface config {
public static final int X0 = 62;
public static final int Y0 = 100;
public static final int LINE = 15;
public static final int SIZE = 50;
public static final int CHESS = 40;
}
新定义类不让画出的棋子消失:
public class GameMouse extends MouseAdapter implements config
画在交点上,并且有颜色
if(((x1-X0)%SIZE)<CHESS/2) {
xx=(x1-X0)/SIZE;
}else {
xx=(x1-X0)/SIZE+1;
}
if(((y1-Y0)%SIZE)<CHESS/2) {
yy = (y1-Y0)/SIZE;
}else {
yy = (y1-Y0)/SIZE+1;
}
if(chessArray[xx][yy]==0) {
if(count%2==0) {
gr.setColor(Color.BLACK);
chessArray[xx][yy] = 1;
}else {
gr.setColor(Color.WHITE);
chessArray[xx][yy] = -1;
}
判断输赢:
public void isWin(int x,int y) {
if(sp(x,y)>=5){
if(chessArray[x][y] == 1) {
System.out.println("黑棋赢了");
JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
}else if(chessArray[x][y] == -1) {
System.out.println("白棋赢了");
JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
}
}
}
public int sp(int x,int y) {
int count = 0;
代码
chessUI类:
package wuziqi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class ChessUI extends JFrame implements config{
public static void main(String[] args) {
ChessUI chessUI = new ChessUI();
chessUI.DrawUI();
}
private int[][] chessArray;
public void DrawUI() {
//JFrame jf = new JFrame();
this.setSize(820,850);
this.setTitle("五子棋2.0");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
//边框布局管理器
BorderLayout bla = new BorderLayout();
this.setLayout(bla);
//导入图片
ImageIcon image = new ImageIcon("F:\\图片\\010906.jpg");
//加入标签,放入图片
JLabel jlb = new JLabel(image);
Dimension dim = new Dimension(800,800);
jlb.setPreferredSize(dim);
this.add(jlb);
//菜单栏
JMenuBar jmu = new JMenuBar();
this.add(jmu,BorderLayout.NORTH);
//主菜单选项
JMenu jm = new JMenu("对战选项");
jmu.add(jm);
JMenu jm1 = new JMenu("对战模式");
jmu.add(jm1);
//子菜单
JMenuItem jItem = new JMenuItem("重开");
jm.add(jItem);
JMenuItem jItem1 = new JMenuItem("悔棋");
jm.add(jItem1);
JMenuItem jItem2 = new JMenuItem("认输");
jm.add(jItem2);
JMenuItem jItem3 = new JMenuItem("人机对战");
jm1.add(jItem3);
JMenuItem jItem4 = new JMenuItem("人人对战");
jm1.add(jItem4);
this.setVisible(true);
Graphics g = this.getGraphics();
GameMouse mouse = new GameMouse();
this.addMouseListener(mouse);
mouse.gr=g;
chessArray = mouse.getChessArray();
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("绘制棋盘");
for(int i=0;i<LINE;i++) {
g.drawLine(X0, Y0+i*SIZE,X0+(LINE-1)*SIZE,Y0+i*SIZE);
g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE);
}
for(int i = 0;i< chessArray.length;i++){
for(int j = 0;j< chessArray.length;j++) {
if(chessArray[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(i*SIZE+X0-CHESS/2, j*SIZE+Y0-CHESS/2, CHESS, CHESS);
}else if(chessArray[i][j] == -1){
g.setColor(Color.WHITE);
g.fillOval(i*SIZE+X0-CHESS/2, j*SIZE+Y0-CHESS/2, CHESS, CHESS);
}
}
}
}
}
接口常量:
public interface config {
public static final int X0 = 62;
public static final int Y0 = 100;
public static final int LINE = 15;
public static final int SIZE = 50;
public static final int CHESS = 40;
}
Gamemouse:类
package wuziqi;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameMouse extends MouseAdapter implements config{
public Graphics gr;
public int x1,y1,xx,yy;
private int count;
private int[][] chessArray;
private Win win ;
public GameMouse(){
chessArray = new int[LINE][LINE];
win = new Win(chessArray);
}
public int[][] getChessArray() {
return chessArray;
}
public void mouseClicked(MouseEvent e) {
x1=e.getX();
y1=e.getY();
if(((x1-X0)%SIZE)<CHESS/2) {
xx=(x1-X0)/SIZE;
}else {
xx=(x1-X0)/SIZE+1;
}
if(((y1-Y0)%SIZE)<CHESS/2) {
yy = (y1-Y0)/SIZE;
}else {
yy = (y1-Y0)/SIZE+1;
}
if(chessArray[xx][yy]==0) {
if(count%2==0) {
gr.setColor(Color.BLACK);
chessArray[xx][yy] = 1;
}else {
gr.setColor(Color.WHITE);
chessArray[xx][yy] = -1;
}
win.isWin(xx, yy);
gr.fillOval(xx*SIZE+X0-CHESS/2, yy*SIZE+Y0-CHESS/2, CHESS, CHESS);
count++;
}
}
}
判断输赢:
package wuziqi;
import javax.swing.JOptionPane;
public class Win {
private int[][] chessArray;
public Win(int[][] chessArray) {
this.chessArray = chessArray;
}
public void isWin(int x,int y) {
if(sp(x,y)>=5){
if(chessArray[x][y] == 1) {
System.out.println("黑棋赢了");
JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
}else if(chessArray[x][y] == -1) {
System.out.println("白棋赢了");
JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
}
}
}
public int sp(int x,int y) {
int count = 0;
//当前位置向左向右去找
if(chessArray[x-1][y] == chessArray[x][y] || chessArray[x+1][y] == chessArray[x][y]) {
//向右
for(int i=x+1;i<=chessArray.length;i++) {
if(chessArray[i][y] == chessArray[x][y]) {
count++;
}else {
break;
}
}
//向左
for(int i=x;i>=0;i--) {
if(chessArray[i][y] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x][y-1] == chessArray[x][y] || chessArray[x][y+1] == chessArray[x][y]) {
//向下
for(int i=y+1;i<=chessArray.length;i++) {
if(chessArray[x][i] == chessArray[x][y]) {
count++;
}else {
break;
}
}
//向上
for(int i=y;i>=0;i--) {
if(chessArray[x][i] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x-1][y-1] == chessArray[x][y] || chessArray[x+1][y+1] == chessArray[x][y]) {
//向左上
for(int i=x,j=y;i>=0&&j>=0;i--,j--) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
//System.out.println("i="+i);
}else {
break;
}
}
//向右下
for(int i=x+1,j=y+1;i<=chessArray.length&&j<=chessArray.length;i++,j++) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}else if(chessArray[x-1][y+1] == chessArray[x][y] || chessArray[x+1][y-1] == chessArray[x][y]) {
//向右上
for(int i=x,j=y;i<=chessArray.length&&j>=0;i++,j--) {
//System.out.println("测试");
if(chessArray[i][j] == chessArray[x][y]) {
System.out.println("测试");
count++;
System.out.println("i="+i+" j="+j);
}else {
break;
}
}
//向左下
for(int i=x-1,j=y+1;i>=0&&j<=chessArray.length;i--,j++) {
if(chessArray[i][j] == chessArray[x][y]) {
count++;
}else {
break;
}
}
}
return count;
}
}