Java 实现三子棋

要求

  • 与人机对弈三子棋

代码

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Game extends JFrame{
    private JButton sendBtn;
    private JButton backBtn;
    private JTextField inputField;
    char[][][] map = new char[10][][];
    int step = 0;

    public Game(){
        Random rd = new Random();
        sendBtn = new JButton("确定");
        sendBtn.setBorderPainted(true);
        backBtn = new JButton("撤销");
        backBtn.setBorderPainted(true);
        inputField = new JTextField(10);
        map[0] = new char[][] { {'0', '1', '2'}, {'3', '4', '5'}, {'6', '7', '8'} };
        JPanel boardPanel = new JPanel();

        JPanel inputPanel = new JPanel();
        inputPanel.add(inputField);
        inputPanel.add(sendBtn);
        inputPanel.add(backBtn);
        inputPanel.setBackground(new Color(10,10,10));
        String s = "";
        s+="<html> -------------- <br>";
        s+="|  " + map[step][0][0] + "  |  " + map[step][0][1] + "  |  " + map[step][0][2] + "  |" + "<br>";
        s+=" -------------- <br>";
        s+="|  " + map[step][1][0] + "  |  " + map[step][1][1] + "  |  " + map[step][1][2] + "  |" + "<br>";
        s+=" -------------- <br>";
        s+="|  " + map[step][2][0] + "  |  " + map[step][2][1] + "  |  " + map[step][2][2] + "  |" + "<br>";
        s+=" -------------- <br>";

        JLabel label = new JLabel(s);
        boardPanel.add(label);

        sendBtn.addActionListener(new ActionListener() {

            //判断对局情况
            public int judge(){
                //colum
                for(int i=0;i<=2;i++){
                    if(map[step][i][0] == '*' && map[step][i][1] == '*' && map[step][i][2] == '*'){
                        return 1;
                    }
                    if(map[step][i][0] == 'O' && map[step][i][1] == 'O' && map[step][i][2] == 'O'){
                        return -1;
                    }
                    if(map[step][0][i] == '*' && map[step][1][i] == '*' && map[step][2][i] == '*'){
                        return 1;
                    }
                    if(map[step][0][i] == 'O' && map[step][1][i] == 'O' && map[step][2][i] == 'O'){
                        return -1;
                    }
                }
                if(map[step][0][0] == map[step][1][1] && map[step][2][2] == map[step][1][1] && map[step][1][1] == '*'){
                    return 1;
                }
                if(map[step][0][0] == map[step][1][1] && map[step][2][2] == map[step][1][1] && map[step][1][1] == 'O'){
                    return -1;
                }
                if(map[step][0][2] == map[step][1][1] && map[step][2][0] == map[step][1][1] && map[step][1][1] == '*'){
                    return 1;
                }
                if(map[step][0][2] == map[step][1][1] && map[step][2][0] == map[step][1][1] && map[step][1][1] == 'O'){
                    return -1;
                }
                return 0;
            }


            //获取新游戏面板
            public String update(){
                String s = "";
                s+="<html> -------------- <br>" ;
                s+="|  " + map[step][0][0] + "  |  " + map[step][0][1] + "  |  " + map[step][0][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                s+="|  " + map[step][1][0] + "  |  " + map[step][1][1] + "  |  " + map[step][1][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                s+="|  " + map[step][2][0] + "  |  " + map[step][2][1] + "  |  " + map[step][2][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                return s;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                //输入坐标
                int position;
                int x;
                int y;
                try{
                    position = Integer.parseInt(inputField.getText());
                    x = position/3;
                    y = position%3;
                }catch(Exception err){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    return ;
                }
                

                //检查合法
                if(position >= 9 || position < 0){
                    JOptionPane.showMessageDialog(null, "别乱输入数字");
                    inputField.setText("");
                    return ;
                }
                if(map[step][x][y] == '*' || map[step][x][y] == 'O' ){
                    JOptionPane.showMessageDialog(null, "这里已经下过了嗷");
                    inputField.setText("");
                    return ;
                }  

                //更新游戏数据
                step++;
                map[step] = new char[3][];
                for(int i=0;i<=2;i++){
                    map[step][i] = Arrays.copyOf(map[step-1][i],map[step-1][i].length);
                }
                map[step][x][y] = '*';
                int robt = rd.nextInt(9);
                while(map[step][robt/3][robt%3] == '*' || map[step][robt/3][robt%3] == 'O'){
                    robt++;
                    robt%=9;
                    if(step == 5){
                        break;
                    }
                }
                if(step != 5){
                    map[step][robt/3][robt%3] = 'O';
                }

                //更新游戏面板
                inputField.setText("");
                boardPanel.removeAll();
                JLabel label = new JLabel(update()) ;
                boardPanel.add(label);
                boardPanel.updateUI();

                //判断对局情况 
                if(judge() == 1){
                    JOptionPane.showMessageDialog(null, "大聪明,你赢了");
                    return ;
                }
                if(judge() == -1){
                    JOptionPane.showMessageDialog(null, "机器人都打不过?");
                    return ;
                }
                if(step == 5){
                    JOptionPane.showMessageDialog(null, "平局");
                }
            }
        });


        backBtn.addActionListener(new ActionListener() {
            //获取新游戏面板
            public String update(){
                String s = "";
                s+="<html> -------------- <br>" ;
                s+="|  " + map[step][0][0] + "  |  " + map[step][0][1] + "  |  " + map[step][0][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                s+="|  " + map[step][1][0] + "  |  " + map[step][1][1] + "  |  " + map[step][1][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                s+="|  " + map[step][2][0] + "  |  " + map[step][2][1] + "  |  " + map[step][2][2] + "  |" + "<br>";
                s+=" -------------- <br>";
                return s;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                if(step == 0){
                    JOptionPane.showMessageDialog(null, "还点??");
                    return;
                }
                --step;  
                boardPanel.removeAll();
                JLabel label = new JLabel(update());
                boardPanel.add(label);
                boardPanel.updateUI();
            }
        });

        this.add(inputPanel,BorderLayout.SOUTH);
        this.add(boardPanel,BorderLayout.NORTH);
        this.setTitle("三子棋");
        this.setSize(280,220);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

public class Main{
    public static void main(String[] args){  
        new Game();
    }
}

代码分析

  • 通过一个三维数组来储存所有的对局情况
  • 通过对数组的分析来判断对局情况
  • 使用Random函数来实现电脑的落子
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嗯嗯你说的对

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

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

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

打赏作者

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

抵扣说明:

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

余额充值