Java用awt编写井字棋(新手编写)

Java用awt编写井字棋(新手编写)

结果演示:
结果演示

public class Player {
	private int x;
	private int y;
	public Player(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}//存入棋子的x,y的位置
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
}

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class ChessBoard extends Frame {
	public TextField txt;//提示文本(显示当前应下棋的玩家)
	public int flag=1,p1=0,p2=0;//判断轮到哪名玩家,记录该玩家的下棋次数
	public int[][] board= new int[3][3];//对应棋盘的二维数组
	public Button[] btn=new Button[9];//棋盘
	public ArrayList<Player> player1=new ArrayList<>();
	public ArrayList<Player> player2=new ArrayList<>();//建立对应玩家的集合
	public ChessBoard(String title) {
		super(title);
		init();
	}
	private void init() {
		this.setBounds(100,100,300,340);
		txt=new TextField();
		Button restart=new Button("重新开始");
		this.add(txt, BorderLayout.SOUTH);
		this.add(restart, BorderLayout.EAST);
		txt.setText("当前玩家:Player1");
		TextField name=new TextField();
		this.add(name, BorderLayout.NORTH);
		name.setText("Player1:O;     Player2:X;");
		Panel panel=new Panel();
		GridLayout gl=new GridLayout(3,3,1,1);
		panel.setLayout(gl);
		this.add(panel);
		for(int i=0;i<btn.length;i++) {
			btn[i]=new Button();
			panel.add(btn[i]);
		}//建立棋盘
		for(int i=0;i<btn.length;i++) {
				btn[i].addMouseListener(new ChessButtonListener(this,i));
		}//将棋子在棋盘上打印出来
		new Result1(this);
		new Result2(this);//启动线程
		restart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<btn.length;i++)
					btn[i].setLabel(null);
				for(int i=0;i<3;i++) {
					for(int j=0;j<3;j++)
						board[i][j]=0;
				}
				for(int i=0;i<p1;i++)
					player1.remove(0);
				for(int i=0;i<p2;i++)
					player2.remove(0);
				p1=0;
				p2=0;
				if(flag==1) {
					flag=1;
					txt.setText("当前玩家:Player1");
				}else if(flag==2) {
					flag=2;
					txt.setText("当前玩家:Player2");
				}
			}
		});//判断是否重新开始并清空棋盘和清空集合
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				ChessBoard.this.dispose();
			}
		});//关闭游戏
	}
}

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ChessButtonListener extends MouseAdapter {
	private ChessBoard frame;
	private int i;
	public ChessButtonListener(ChessBoard frame,int i) {
		this.frame =frame;
		this.i=i;
	}//引入数据

	public void mouseClicked(MouseEvent e) {
		if(frame.board[i/3][i%3]==0) {//判断是否为空棋盘
			if(frame.flag==1) {//判断轮到哪名玩家
				frame.board[i/3][i%3]=1;//改变对应二维数组的值
				frame.btn[i].setLabel("O");//输出棋子
				frame.flag=2;
				frame.p1++;//记录次数
				frame.txt.setText("当前玩家:Player2");//输出提示
				frame.player1.add(new Player(i/3,i%3));//将棋子位置存入集合
			}else if(frame.flag==2) {
				frame.board[i/3][i%3]=2;//改变对应二维数组的值
				frame.btn[i].setLabel("X");//输出棋子
				frame.flag=1;
				frame.p2++;//记录次数
				frame.txt.setText("当前玩家:Player1");//输出提示
				frame.player2.add(new Player(i/3,i%3));//将棋子位置存入集合
			}
		}
		super.mouseClicked(e);
	}
	
}
public class Result1 extends Thread {
	private ChessBoard frame;
	private boolean result=false;//判断输赢
	private int[] count=new int[4];

	public Result1(ChessBoard frame) {
		this.frame = frame;
		start();
	}//引入数据并启动线程
	public void run() {
	while(true) {//判断是否赢了
		synchronized("") {
		for(int i=0;i<3;i++) {
			for(Player p:frame.player1) {
				if(p.getX()==p.getY()) //左斜
					count[0]++;
				if(p.getX()+p.getY()==2)//右斜
					count[1]++;
				if(p.getX()==i)//横行
					count[2]++;
				if(p.getY()==i)//纵列
					count[3]++;
			}
			for(int j=0;j<4;j++) {
				if(count[j]==3) {
					result=true;
				}
				count[j]=0;
			}
		}
		if(result) {
			frame.txt.setText("Player1获胜!");//输出结果
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++)
					frame.board[i][j]=1;//停止落棋
			}
			result=false;
		}
	}
		try {
			this.sleep(1000);//上锁并休眠节约内存
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	}
}
public class Result2 extends Thread {
	private ChessBoard frame;
	private boolean result=false;//判断输赢
	private int[] count=new int[4];

	public Result2(ChessBoard frame) {
		this.frame = frame;
		start();
	}//引入数据并启动线程
	public void run() {
	while(true) {//判断是否赢了
		synchronized("") {
			for(int i=0;i<3;i++) {
				for(Player p:frame.player2) {
					if(p.getX()==p.getY()) //左斜
						count[0]++;
					if(p.getX()+p.getY()==2)//右斜
						count[1]++;
					if(p.getX()==i)//横行
						count[2]++;
					if(p.getY()==i)//纵列
						count[3]++;
				}
				for(int j=0;j<4;j++) {
					if(count[j]==3) {
						result=true;
					}
					count[j]=0;
				}
			}
		if(result) {
			frame.txt.setText("Player2获胜!");//输出结果
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++)
					frame.board[i][j]=2;//停止落棋
			}
			result=false;
		}
	}
		try {
			this.sleep(1000);//上锁并休眠节约内存
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	}
}
public class Game {

	public static void main(String[] args) {
		new ChessBoard("井字棋");//启动游戏
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
一个五子棋游戏需要考虑以下几个方面: 1. 游戏界面:需要使用图形界面(GUI)来展示棋盘,并实现玩家下棋的功能。 2. 游戏逻辑:需要实现五子棋的规则,判断谁获胜。 3. 游戏数据:需要储存棋盘上每个点的状态,以及记录玩家下棋的步数。 下面是一份简单的五子棋代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Gobang extends JFrame { private static final long serialVersionUID = 1L; private int width = 15; private int height = 15; private int blockSize = 30; private JButton[][] chessBoard; private boolean isBlack = true; public Gobang() { chessBoard = new JButton[height][width]; Container container = getContentPane(); container.setLayout(new GridLayout(height, width)); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { chessBoard[i][j] = new JButton(); chessBoard[i][j].setSize(blockSize, blockSize); chessBoard[i][j].addActionListener(new ChessAction(i, j)); container.add(chessBoard[i][j]); } } setSize(blockSize * width, blockSize * height); setTitle("五子棋"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } private class ChessAction implements ActionListener { private int x; private int y; public ChessAction(int x, int y) { this.x = x; this.y = y; } @Override public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); if (button.getText().length() == 0) { if (isBlack) { button.setText("●"); } else { button.setText("○"); } isBlack = !isBlack; button.setEnabled(false); } } } public static void main(String[] args) {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wslxc00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值