Commit 13ad0204 authored by 刘家荣's avatar 刘家荣 💬
Browse files

frame(AI, Game, Account):AI 账号 退出事件

parent 51419c4d
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -5,12 +5,15 @@ import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import model.History;
import model.game.Game;
import model.game.GameType;
import view.AppFrame;
import view.GameForm;
import view.RoomsForm;
import view.StartForm;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -41,6 +44,12 @@ public class AppController {
    
        SwingUtilities.invokeLater(() -> {
            AppFrame.instance = new AppFrame();
            AppFrame.instance.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    super.windowClosing(e);
                    
                }
            });
        });
    }
    
@@ -72,7 +81,7 @@ public class AppController {
                            Game.instance = savedGame;
                            History.instance = savedHistory;
                        } else {
                            Game.instance = new Game(true);
                            Game.instance = new Game(GameType.LOCAL_2P, true);
                            History.instance = new History(Game.instance.clone());
                        }
                        break;
+13 −0
Original line number Diff line number Diff line
package controller;

/**
 * 一个处理退出前事务的Controller
 * 不知道有没有用。。。
 */
public class ExitController {
    
    public static void onExit(){
    
    }
    
}

src/model/AI.java

0 → 100644
+26 −0
Original line number Diff line number Diff line
package model;

import model.game.Chess;
import model.game.Chessboard;
import model.game.Game;
import model.game.Square;

public class AI extends Thread{
    @Override
    public void run(){
        try {
            Thread.sleep(2000);
            Game.instance.resolveStep(eval());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static Step eval(){
        for (Square square : Chessboard.instance.squareList) {
            if(square instanceof Chess chess && !chess.isReversal())
                return new FlipStep(chess.id);
        }
        return new FlipStep(0);
    }
}

src/model/Account.java

0 → 100644
+96 −0
Original line number Diff line number Diff line
package model;

import com.alibaba.fastjson2.JSON;
import network.Client;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 表示一个用户信息的账户类
 * 同时也维护一个代表本机账号的self静态对象,self可以自动与外部存档同步
 */
public class Account {
    
    /**
     * 本机账号信息的存档
     */
    public final static String SELF_ACCOUNT_PATH = "res/save/selfAccount.json";
    
    /**
     * 名称长度限制
     */
    public static final int MAX_NAME_LENGTH = 20;
    
    /**
     * 合法名称的正则表达式
     */
    public static final String PASS_REGEX = "[^\\u0000-\\u001F\\u007F-\\u009F]*";
    
    public final String uuid;   //uuid
    
    private String name;     //名称
    
    /**
     * 本机账号对象
     */
    public final static Account SELF = loadSelfAccount();
    
    /**
     * @param name 名称
     * @throws IllegalArgumentException 非法名称异常,可以直接拿异常的message输出到GUI
     */
    public Account(String name) throws IllegalArgumentException {
        if (name == null || name.length() == 0) {
            throw new IllegalArgumentException("名称不能为空");
        }
        if(name.length() > MAX_NAME_LENGTH) {
            throw new IllegalArgumentException(String.format("名称不能超过%d个字符", MAX_NAME_LENGTH));
        }
        if (!Pattern.matches(PASS_REGEX, name)) {
            throw new IllegalArgumentException("非法名称");
        }
        this.name = name;
        this.uuid = UUID.randomUUID().toString();
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        if(this.equals(SELF)){
            this.writeAccount();
        }
    }
    
    private static Account loadSelfAccount(){
        Account res = new Account("游戏玩家" +
            String.format("%03d",new Random().nextInt(0, 1000))
        );
        try {
            Account tmp = JSON.parseObject(
                Files.readString(Path.of(SELF_ACCOUNT_PATH)),
                Account.class);
            if(tmp != null) {
                UUID.fromString(tmp.uuid);
                if (tmp.name != null) {
                    res = tmp;
                }
            }
        } catch (IOException | IllegalArgumentException ignored){}
        return res;
    }
    
    private void writeAccount(){
        try {
            Files.writeString(Path.of(SELF_ACCOUNT_PATH), JSON.toJSONString(this));
        }catch (Exception ignored){}
    }
    
}
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ public class Chessboard implements Serializable, Cloneable {
    
    private int[][] idGrid = new int[ROW_SIZE][COL_SIZE];  //方格子列表

    ArrayList<Square> squareList = new ArrayList<>();
    public ArrayList<Square> squareList = new ArrayList<>();

    //两个亡子堆->亡子堆组
    private SideStack[] sideStacks;
Loading