java井字棋
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyListener;public class TTTGame { public static void main(String[] args) { MyOXGame myoxgame=new MyOXGame(); }}class MyOXGame extends JFrame implements ActionListener { //主体部分,绘制GUI,编辑有关响应 int co = 0; int cx = 0; int dr = 0; JButton[] button;//对象数组,表示棋盘上的九个格子 JButton restart;//重置键 JLabel recording; JLabel judgement;//裁判,用来显示游戏的结果 JPanel chessboard,basicPanel;//棋盘,棋盘下部区域 int player=0;//显示当前玩家编号 boolean gameOverFlag=false;//记录游戏是否结束,如果结束值为true,停止对事件的响应 MyOXGame(){//生成函数,生成游戏 setTitle("MY OXGAME"); setBounds(50,50,480,580);//窗体基本设置 button=new JButton[10]; recording = new JLabel("<html>P1 Wins:"+co+"<br>P2 Wins:"+cx+"<br>Draws:"+dr+"</html>"); restart=new JButton("New Game"); chessboard=new JPanel(); basicPanel=new JPanel(); judgement=new JLabel(""); chessboard.setLayout(new GridLayout(3,3)); basicPanel.setLayout(new FlowLayout());//组件初始化设置 add(chessboard,BorderLayout.CENTER); add(basicPanel,BorderLayout.NORTH); basicPanel.add(restart); basicPanel.add(judgement);//布局设置 basicPanel.add(recording); initChessboard();//初始化棋盘,使九个按钮按顺序加上1-9的标签, addChessToChessboard(chessboard);//将九个按钮添加至chessboard restart.addActionListener(this);//添加监听器 buttonListener();//为按钮添加监听器 setVisible(true);//设置可视化 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//使关闭键有用 } private void addChessToChessboard(JPanel chessboard){ //添加按钮至棋盘 for(int i=1;i<=9;i++) { chessboard.add(button[i]); } } private void initChessboard(){ //使棋子按钮初始化为1-9 for(int i=1;i<=9;i++){ button[i]=new JButton(); } } private void buttonListener(){ //为棋子按钮添加监听器 for(int i=1;i<=9;i++) { button[i].addActionListener(this); } } private void restartTheGame(){ //重置游戏 chessboard.removeAll();//必须于repaint和updateUI一起用不然要不啥都没有,要不没有改动 //必须注意removll会清空布局 chessboard.setLayout(new GridLayout(3,3)); initChessboard(); addChessToChessboard(chessboard); buttonListener(); chessboard.updateUI(); chessboard.repaint(); judgement.setText(""); gameOverFlag=false; } private boolean checkDogfall(String[] chess){ //检查是否被填满,填满即为平局 for(int i=1;i<chess.length;i++){ if (chess[i].equals("")) return false; } return true; } private int checkWinner(int playerNow) { //检查当前游戏状态,-2表示没有决出胜负,-1表示平局。0表示O玩家胜利,1表示X玩家胜利 String[] chess=new String[10]; for(int i=1;i<=9;i++){ chess[i]=button[i].getLabel(); } if( (chess[1]!=""&&chess[1]==chess[2]&&chess[2]==chess[3])|| (chess[4]!=""&&chess[4]==chess[5]&&chess[5]==chess[6])|| (chess[7]!=""&&chess[7]==chess[8]&&chess[8]==chess[9])|| (chess[1]!=""&&chess[1]==chess[5]&&chess[5]==chess[9])|| (chess[7]!=""&&chess[7]==chess[5]&&chess[5]==chess[3])|| (chess[1]!=""&&chess[1]==chess[4]&&chess[4]==chess[7])|| (chess[2]!=""&&chess[2]==chess[5]&&chess[5]==chess[8])|| (chess[3]!=""&&chess[3]==chess[6]&&chess[6]==chess[9])){ //判断当前玩家是否为赢家 return playerNow; } else if(checkDogfall(chess)) return -1; return -2; } @Override public void actionPerformed(ActionEvent actionEvent) { //对监听的事件进行响应 int statueCode; JButton buttonSource=(JButton)actionEvent.getSource(); if(buttonSource==restart){ //判断是否使restart键,如果是,重置游戏 restartTheGame(); } if (gameOverFlag==true) return; String buttonMark=buttonSource.getText(); if(buttonMark.equals("")) { if(player==0) { buttonSource.setLabel("O"); } else buttonSource.setLabel("X"); player=(player+1)%2; statueCode=checkWinner(player); if(statueCode==0){ co++; judgement.setText("Player2 wins the game! "); gameOverFlag=true; } else if(statueCode==1){ cx++; judgement.setText("Player1 wins the game!"); gameOverFlag=true; } else if(statueCode==-1){ dr++; judgement.setText("The game a draw!"); } recording.setText("<html>P1 Wins:"+co+"<br>P2 Wins:"+cx+"<br>Draws:"+dr+"</html>"); } }}