Ladder-like word game in Java

Posted by sasquatch90 on Stack Overflow See other posts from Stack Overflow or by sasquatch90
Published on 2010-05-16T18:52:16Z Indexed on 2010/05/16 19:00 UTC
Read the original article Hit count: 310

I've found this question http://stackoverflow.com/questions/2844190/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[][] ?

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays