当我运行两个类时,我的老鼠没有移动



我有一个迷宫类和一个Rat类。它们都使用9个字符的字符串协议使用套接字进行通信,老鼠告诉迷宫它想如何移动。迷宫移动它。为了显示老鼠正在我的迷宫中移动,我将迷宫路径的背景颜色设置为红色每当老鼠在特定的路径上行走时。但是,出于某种原因,这种情况并没有发生。我不知道为什么。请帮忙!

package TheMaze;
import java.util.*;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;   
import java.net.*;
import java.awt.*;
import java.io.*;
public class TheMaze {
    static JButton[][] jb;
    //static String r1, r2, r3, r4, r5, r6, r7, r8, r9;
    static String m1, m2, m3, m4, m5, m6, m7, m8, m9;
    static int row = 0;
    static int column = 3;
    static char[][] mProtocol;
    static JFrame jf = new JFrame("Mazifying Maze");
    Container contain = new Container();
    public TheMaze(int length, int width) {
        jb = new JButton[length][width];
        mProtocol = new char[length][width];
        contain = new Container();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < length; y++) {
                jb[y][x] = new JButton();
                jf.add(jb[y][x]);
            }
        }
        jb[2][0].setBackground(Color.black);
        jb[4][0].setBackground(Color.black);
        jb[2][97].setBackground(Color.black);
        jb[2][96].setBackground(Color.black);
        jb[2][95].setBackground(Color.black);
        jb[2][94].setBackground(Color.black);
        jb[2][93].setBackground(Color.black);
        jb[2][92].setBackground(Color.black);
        jb[2][91].setBackground(Color.black);
        .
                .
                .
        Just goes on and on.
                for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                if (jb[i][j].getBackground() == Color.black) {
                    mProtocol[i][j] = 'w';
                } else {
                    mProtocol[i][j] = 'p';
                }
            }
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setLayout(new GridLayout(length, width));
            jf.setSize(1400, 700);
            jf.setVisible(true);
        }
    }
    public static void ratMessage(String ratPos) {
        System.out.println(ratPos);
        String ratMessage = ratPos;
        String r1 = ratMessage.substring(0, 1);
        String r2 = ratMessage.substring(1, 2);
        String r3 = ratMessage.substring(2, 3);
        String r4 = ratMessage.substring(3, 4);
        String r5 = ratMessage.substring(4, 5);
        String r6 = ratMessage.substring(5, 6);
        String r7 = ratMessage.substring(6, 7);
        String r8 = ratMessage.substring(7, 8);
        String r9 = ratMessage.substring(8, 9);
        if (r2.equals('r')) {
            jb[row - 1][column].setBackground(Color.red);
            row = row - 1;
        }
        if (r4.equals('r')) {
            jb[row][column - 1].setBackground(Color.red);
            column = column - 1;
        }
        if (r6.equals('r')) {
            jb[row][column + 1].setBackground(Color.red);
            column = column + 1;
        }
        if (r8.equals('r')) {
            jb[row + 1][column].setBackground(Color.red);
            row = row + 1;
        }
    }
    public static String mazeMessage() {
        if (row - 1 < 0 && column - 1 < 0) {
            m1 = "o";
            m2 = "o";
            m3 = "o";
            m4 = "o";
            m5 = "r";
            m6 = mProtocol[row][column + 1] + "";
            m7 = "o";
            m8 = mProtocol[row + 1][column] + "";
            m9 = mProtocol[row + 1][column + 1] + "";
        } else if (row + 1 > 99 && column + 1 > 99) {
            m1 = mProtocol[row - 1][column - 1] + "";
            m2 = mProtocol[row - 1][column] + "";
            m3 = "o";
            m4 = mProtocol[row][column - 1] + "";
            m5 = "r";
            m6 = "o";
            m7 = "o";
            m8 = "o";
            m9 = "o";
        } else if (row - 1 < 0 && column + 1 > 99) {
            m1 = "o";
            m2 = "o";
            m3 = "o";
            m4 = mProtocol[row][column - 1] + "";
            m5 = "r";
            m6 = "o";
            m7 = mProtocol[row + 1][column - 1] + "";
            m8 = mProtocol[row + 1][column] + "";
            m9 = "o";
        } else if (row + 1 > 99 & column - 1 < 0) {
            m1 = "o";
            m2 = mProtocol[row - 1][column] + "";
            m3 = mProtocol[row - 1][column + 1] + "";
            m4 = "o";
            m5 = "r";
            m6 = mProtocol[row][column + 1] + "";
            m7 = "o";
            m8 = "o";
            m9 = "o";
        } else if (row - 1 < 0) {
            m1 = "o";
            m2 = "o";
            m3 = "o";
            m4 = "" + mProtocol[row][column - 1];
            m5 = "r";
            m6 = "" + mProtocol[row][column + 1];
            m7 = mProtocol[row + 1][column - 1] + "";
            m8 = mProtocol[row + 1][column] + "";
            m9 = mProtocol[row + 1][column + 1] + "";
        } else if (row + 1 > 99) {
            m1 = mProtocol[row - 1][column - 1] + "";
            m2 = mProtocol[row - 1][column] + "";
            m3 = mProtocol[row - 1][column + 1] + "";
            m4 = mProtocol[row][column - 1] + "";
            m5 = "r";
            m6 = mProtocol[row][column + 1] + "";
            m7 = "o";
            m8 = "o";
            m9 = "o";
        } else if (column - 1 < 0) {
            m1 = "o";
            m2 = mProtocol[row - 1][column] + "";
            m3 = mProtocol[row - 1][column + 1] + "";
            m4 = "o";
            m5 = "r";
            m6 = mProtocol[row][column + 1] + "";
            m7 = "o";
            m8 = mProtocol[row + 1][column] + "";
            m9 = mProtocol[row + 1][column + 1] + "";
        } else if (column + 1 > 99) {
            m1 = mProtocol[row - 1][column - 1] + "";
            m2 = mProtocol[row - 1][column] + "";
            m3 = "o";
            m4 = mProtocol[row][column - 1] + "";
            m5 = "r";
            m6 = "o";
            m7 = mProtocol[row + 1][column - 1] + "";
            m8 = mProtocol[row + 1][column] + "";
            m9 = "o";
        } else {
            m1 = mProtocol[row - 1][column - 1] + "";
            m2 = mProtocol[row - 1][column] + "";
            m3 = mProtocol[row - 1][column + 1] + "";
            m4 = mProtocol[row][column - 1] + "";
            m5 = "r";
            m6 = mProtocol[row][column + 1] + "";
            m7 = mProtocol[row + 1][column - 1] + "";
            m8 = mProtocol[row + 1][column] + "";
            m9 = mProtocol[row + 1][column + 1] + "";
        }
        return m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9;
    }
    public static void main(String[] args) {//throws IOException{
        System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
        TheMaze mz = new TheMaze(100, 100);
        try {
            ServerSocket svs = new ServerSocket(13000);
            System.out.println("Has not Connected");
            Socket socks = svs.accept();
            System.out.println("Has Connected");
            DataOutputStream p = new DataOutputStream(socks.getOutputStream());
            InputStreamReader isr = new InputStreamReader(socks.getInputStream());
            DataInputStream d = new DataInputStream(socks.getInputStream());
            String theMazeMessage = "ooowrwwpw";
            System.out.println("first message" + theMazeMessage);
            p.writeUTF(theMazeMessage);
            while (row != 99 && column != 87) {
                System.out.println("in loop");// still going, not yet reached end of maze.
                String recieving = d.readUTF();
                System.out.println(recieving);
                ratMessage(recieving);
                p.writeUTF(theMazeMessage);
                System.out.println(theMazeMessage);
            }
            socks.close();
            p.close();
            isr.close();
            d.close();
        } catch (IOException e) {
            e.printStackTrace();
            String theMazeMessage = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9;
        }
    }
}

与老鼠类:

package therat;
import java.util.*;
import java.net.*;
import java.io.*;
import java.net.*;
public class TheRat {
    static String m1, m2, m3, m4, m5, m6, m7, m8, m9;
    static String theMazeMessage;
    static String identifier;
    public static void theMazeMessage(String mazePos) {
        String theMazeMessage = mazePos;
        m1 = theMazeMessage.substring(0, 1);
        m2 = theMazeMessage.substring(1, 2);
        m3 = theMazeMessage.substring(2, 3);
        m4 = theMazeMessage.substring(3, 4);
        m5 = theMazeMessage.substring(4, 5);
        m6 = theMazeMessage.substring(5, 6);
        m7 = theMazeMessage.substring(6, 7);
        m8 = theMazeMessage.substring(7, 8);
        m9 = theMazeMessage.substring(8, 9);
        System.out.println(mazePos);
    }
    public static String Move() {
        if (m8.equals("p")) {
            identifier = goDown();
        } else if (m2.equals("p")) {
            identifier = goUp();
        } else if (m4.equals("p")) {
            identifier = goLeft();
        } else if (m6.equals("p")) {
            identifier = goRight();
        }
        return identifier;
    }
    public static String goDown() {
        return m1 + m2 + m3 + m4 + 'p' + m6 + m7 + 'r' + m9;
    }
    public static String goUp() {
        return m1 + 'r' + m3 + m4 + 'p' + m6 + m7 + m8 + m9;
    }
    public static String goLeft() {
        return m1 + m2 + m3 + 'r' + 'p' + m6 + m7 + m8 + m9;
    }
    public static String goRight() {
        return m1 + m2 + m3 + m4 + 'p' + 'r' + m7 + m8 + 9;
    }
    public static void main(String args[]) {
        try {
            Socket sox = new Socket("localhost", 13000);
            System.out.println("Connected");
            DataOutputStream ps = new DataOutputStream(sox.getOutputStream());
            DataInputStream d = new DataInputStream(sox.getInputStream());
            boolean done = false;
            while (!done) {
                System.out.println("in loop");
                String mazeMessage = d.readUTF();
                System.out.println("mazeMessage is " + mazeMessage);
                theMazeMessage(mazeMessage);
                String s = Move();
                System.out.println(s);
                ps.writeUTF(s);
                ps.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我能看到的主要问题是在ratMessage方法中,您将String值与char 进行比较

if (r2.equals('r'))

这是行不通的,因为Stringchar永远不可能相等,它们是错误的类型。如果你改成更像。。。

if (r2.equals("r"))

你应该取得更大的成功。

还有许多其他的陷阱会引起一些悲伤。static不是一个解决方案。过度使用static会回来狠狠地咬你一口。在可能的情况下,您应该依赖于传递需要与之交互的对象的有效引用。

就我个人而言,我会设计一系列interface来描述预期的合同,例如ratMessage。你的UI将实现这个接口,它的引用应该传递给主"comms"类,该类从rat中读取信息。

您也违反了Swing的单线程规则。Swing要求对任何UI组件的所有交互和更新都必须在事件调度线程的上下文中完成。因为您从"main"线程运行主循环并调用ratMessage,然后更新按钮,所以您违反了此规则。

查看Swing和Initial Threads中的并发以了解更多详细信息。

为了简单起见,解决方案可能涉及SwingWorker,或者为了快速更改而涉及SwingUtilities.invokeLater。。。

相关内容

  • 没有找到相关文章

最新更新