开发者

Ladder-like word game in Java

开发者 https://www.devze.com 2022-12-30 05:50 出处:网络
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

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(){
 开发者_StackOverflow中文版       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[][] ?


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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消