Java中的类似梯子的文字游戏

发布于 2024-09-01 09:34:36 字数 3430 浏览 6 评论 0 原文

我发现了这个问题选择类似梯子的文字游戏的设计方法< /a> 我也想做这样的程序。我已经编写了一些代码,但已经有两个问题。这是我已经拥有的:

GRID :

public class Grid {    
    public Grid(){}
    public Grid( Element e ){}
}

ELEMENT :

public class Element {  
    final int INVISIBLE = 0;
    final int EMPTY = 1;
    final int FIRST_LETTER = 2;
    final int OTHER_LETTER = 3;   
    private int state;
    private String letter;

    public Element(){}  
//empty block    
    public Element(int state){
        this("", 0);
    }  
//filled block
    public Element(String s, int state){
        this.state = state;
        this.letter = s;
    }

    public static void changeState(int s){
    }

    public int getState(){
        return state;
    }

    public boolean equalLength(){
        return true;
    }

    public boolean equalValue(){
        return true;
    }

    @Override
    public String toString(){
        return "["+letter+"]";
    }
}

MAIN:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        System.out.println("Height: ");
        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }
        final int height = sc.nextInt();
        Grid[] game = new Grid[height];

        for(int i = 1; i <= height; i++) {
            String s;
            do {
                System.out.println("Length " + i + ", please!");
                s = sc.next();
            } while (s.length() != i);

            Element[] line = new Element[s.length()+1];
            Element single = null;
            String[] temp = null;

//issue here

            temp = s.split("");
            System.out.println("s.length: "+s.length());
            System.out.println("temp.length: "+temp.length);

//

            for(String str : temp){
                System.out.println("str:"+str);
            }

            for (int k = 0 ; k < temp.length ; k++) {
                if( k == 0 ){
                    single = new Element(temp[k], 2);
                    System.out.println("single1: "+single);
                }
                else{
                    single = new Element(temp[k], 3);
                    System.out.println("single2: "+single);
                }
                line[k] = single;
            }

            for (Element l : line) {
                System.out.println("line:"+l);
            }

//issue here

            game[i] = line;
        }

//

        for (Grid g : game) {
            System.out.println(g);
        }
    }
}

以及调试的示例输出:

Height: 
3
Length 1, please!
A
s.length: 1
temp.length: 2
str:
str:A
single1: []
single2: [A]
line:[]
line:[A]

这就是我认为它应该工作的样子。我从用户那里得到一句话。接下来为整个游戏创建网格元素。然后,我为每一行创建名为 line 的 Element[] 数组。我分割了给定的文本,这是第一个问题。为什么 string.split() 添加空格?您可以在输出中清楚地看到它是无缘无故添加的。我怎样才能摆脱它(现在我必须在行的长度上添加+1才能运行代码)。我继续将分割的文本放入临时字符串数组中,然后从每个字母创建 Element 对象并将其放入行数组中。除了这个空白空间输出看起来还不错。但下一个问题是网格。我已经创建了以 Element 作为参数的构造函数,但由于“不兼容的类型”,我仍然无法将行作为 Grid[] 元素抛出。我该如何解决这个问题?我做得对吗?也许我应该摆脱 Element[] 的行并只创建 Grid[][] ?

I've found this question Choosing design method for ladder-like word game and I would also like to do this kind of program. I've written some code but already have two issues. Here's what I already have :

GRID :

public class Grid {    
    public Grid(){}
    public Grid( Element e ){}
}

ELEMENT :

public class Element {  
    final int INVISIBLE = 0;
    final int EMPTY = 1;
    final int FIRST_LETTER = 2;
    final int OTHER_LETTER = 3;   
    private int state;
    private String letter;

    public Element(){}  
//empty block    
    public Element(int state){
        this("", 0);
    }  
//filled block
    public Element(String s, int state){
        this.state = state;
        this.letter = s;
    }

    public static void changeState(int s){
    }

    public int getState(){
        return state;
    }

    public boolean equalLength(){
        return true;
    }

    public boolean equalValue(){
        return true;
    }

    @Override
    public String toString(){
        return "["+letter+"]";
    }
}

MAIN:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        System.out.println("Height: ");
        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }
        final int height = sc.nextInt();
        Grid[] game = new Grid[height];

        for(int i = 1; i <= height; i++) {
            String s;
            do {
                System.out.println("Length " + i + ", please!");
                s = sc.next();
            } while (s.length() != i);

            Element[] line = new Element[s.length()+1];
            Element single = null;
            String[] temp = null;

//issue here

            temp = s.split("");
            System.out.println("s.length: "+s.length());
            System.out.println("temp.length: "+temp.length);

//

            for(String str : temp){
                System.out.println("str:"+str);
            }

            for (int k = 0 ; k < temp.length ; k++) {
                if( k == 0 ){
                    single = new Element(temp[k], 2);
                    System.out.println("single1: "+single);
                }
                else{
                    single = new Element(temp[k], 3);
                    System.out.println("single2: "+single);
                }
                line[k] = single;
            }

            for (Element l : line) {
                System.out.println("line:"+l);
            }

//issue here

            game[i] = line;
        }

//

        for (Grid g : game) {
            System.out.println(g);
        }
    }
}

And sample output for debug :

Height: 
3
Length 1, please!
A
s.length: 1
temp.length: 2
str:
str:A
single1: []
single2: [A]
line:[]
line:[A]

Here's what I think it should work like. I grab a word from user. Next create Grid element for whole game. Then for each line I create Element[] array called line. I split the given text and here's the first problem. Why string.split() adds a whitespace ? You can see clearly in output that it is added for no reason. How can I get rid of it (now I had to add +1 to the length of line just to run the code). Continuing I'm throwing the splitted text into temporary String array and next from each letter I create Element object and throw it to line array. Apart of this empty space output looks fine. But next problem is with Grid. I've created constructor taking Element as an argument, but still I can't throw line as Grid[] elements because of 'incompatible types'. How can I fix that ? Am I even doing it right ? Maybe I should get rid of line as Element[] and just create Grid[][] ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2024-09-08 09:34:36

为什么 string.split() 添加空格?

您必须为 string.split():

for (String t : s.split(" ")) {
    System.out.println(t);
}

Why does string.split() add whitespace?

You have to specify a non-empty regular expression to string.split():

for (String t : s.split(" ")) {
    System.out.println(t);
}
静待花开 2024-09-08 09:34:36

1 - 您可能想使用 String.charAt() 而不是 split()。

2 - 您得到“不兼容的类型”,因为您已将“游戏”声明为“网格”对象的数组,但您尝试分配给它的“线”对象而不是类型“元素数组” ”。使用二维数组来存储元素可能会更好。

1 - You probably want to use String.charAt() rather than split().

2 - You're getting the "incompatible type" because you've declared "game" as an array of "Grid" objects, but the "line" objects you're trying to assign to it are instead of type "array of Element". You may well be better off with a two-dimensional array to store the elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文