5

I am working on cross word algorithm to develop a word app. After doing a lot of googling or search on StackOverflow, I was able to reach this point. But yet I am not able to understand the right implementation for algorithm in Java. Below is the class I used.

public class Crosswords {

    char[][] cross;
    int rows;
    int cols;
    char[][] numberGrid;
    boolean startword;
    final char DEFAULT = ' ';

    public Crosswords() {
        rows = 50;
        cols = 50;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length;i++){
            for (int j = 0; j < cross[i].length;j++){
                cross[i][j] = DEFAULT;
            }
        }
    }

    public Crosswords(int ros, int colls) {
        rows = ros;
        cols = colls;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0;i < cross.length; i++){
            for (int j = 0; j < cross[i].length; j++){
                cross[i][j] = DEFAULT;
            }
        }
    }


    public String toString() {
             String s = new String();
            //String d = new String();
        for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++){
          s = s + cross[i][j] + " ";
            }
          s = s + "\n";
        }
        return s;
    }


    public void addWordh(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;


        if (s.length() > cols) {

            System.out.println(s + " is longer than the grid. Please try another word.");

            return;

        }

        if (c + s.length() > cols) {

            System.out.println(s + " is too long. Please try another word.");

            return;

        }

        if ((r - 2) >= 0) {

            if ((cross[r - 1][c - 1 + s.length()] == DEFAULT) || (cross[r - 1][c - 1 + s.length()] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the beginning of another word!");
                return;
            }
        }

        for (i = 0; i < s.length(); i++) {

            if ((cross[r - 1][c - 1 + i] == DEFAULT) || (cross[r - 1][c - 1 + i] == s.charAt(i))) {

                b = true;

            }

            else {

                b = false;

                System.out.println("Unable to add " + s + ". Please try another word.");

                return;}

        }

        if (b == true) {

            if ((s.length() <= cols) && (c + s.length() <= cols) &&

                    (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                while (j < s.length()) {


                    cross[r - 1][c - 1 + j] = s.charAt(j);

                    if (j==0){
                        startword = true;
                    }

                    cross[rows - 1 - (r - 1)][cols - 1 - (c - 1 + j)] = '*';

                    j++;

                }

            }

        }

    }

    public void addWordv(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;

        if (s.length() > rows) {

            System.out.println(s + " is longer than the grid. Please try another word.");

        }

        if (r + s.length() > rows) {

            System.out.println(s + " is too long. Please try another word.");

        }

        else {

            if ((r - 2) >= 0) {

                if ((cross[r - 2][c - 1] == DEFAULT) || (cross[r - 2][c - 1] == '*')) {

                    intersectsWord = false;

                }

                else { intersectsWord = true;}

                if (intersectsWord == true) {

                    System.out.println("The word " + s + " intersects the end of another word!");

                    return;

                }

            }
            if ((cross[r - 1 + s.length()][c - 1] == DEFAULT) || (cross[r - 1 + s.length()][c - 1] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the end of another word!");
                return;
            }


            for (i = 0; i < s.length(); i++) {

                if ((cross[r - 1 + i][c - 1] == DEFAULT) || (cross[r - 1 + i][c - 1] == s.charAt(i))) {

                    b = true;

                }

                else {

                    b = false;

                    System.out.println("Unable to add " + s + ". Please try another word.");

                    return;}

            }

            if (b == true) {

                if ((s.length() <= rows) && (r + s.length() <= cols) &&

                        (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                    while (j < s.length()) {

                        cross[r - 1 + j][c - 1] = s.charAt(j);

                        if (j==0){
                            startword = true;
                        }

                        cross[rows - 1 - (r - 1 + j)][cols - 1 - (c - 1)] = '*';

                        j++;

                    }

                }

            }
        }

    }

    public void setNumberGrid(){
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (cross[i][j] == DEFAULT){
                    numberGrid[i][j] = (char) 0;
                }
                else if (startword == true){
                    numberGrid[i][j] = (char) -2;
                }
                else {
                    numberGrid[i][j] = (char) -1;
                }
            }
            int count = 1;
            for (i=0; i < cross.length; i++){
                for (int j=0; j < cross[rows].length; j++){
                    if (numberGrid[i][j] == -2){
                        numberGrid[i][j] = (char)count;
                        count++;
                    }
                }
            }
        }
    }

    public String printNumberGrid() {
        for (int i=0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (numberGrid[i][j] == (char)-1){
                    numberGrid[i][j] = ' ';
                }
                else if (numberGrid[i][j] == (char)0){
                    numberGrid[i][j] = '#';
                }
            }
        }
        String d = new String();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++){
                d = d + numberGrid[i][j] + " ";
            }
            d = d + "\n";
        }
        return d;
    }



    public static void main(String[] args) {
        Crosswords g = new Crosswords();
        g.addWordv("rawr", 4, 5);
        g.addWordh("bot", 5, 4);
        g.addWordv("raw", 7, 5);
        g.addWordh("cat", 4, 5);
        g.addWordh("bass", 6, 10);
        System.out.println(g);

        Crosswords c = new Crosswords(20, 20);

        c.addWordh("HELLO", 1, 1);

        c.addWordv("HAPLOID", 1, 1);
        c.addWordh("COMPUTER", 3, 12);

        c.addWordv("CAT", 2, 11);

        c.addWordv("WOAH", 2, 20);
        c.addWordh("PARKING", 20, 5);

        c.addWordv("ARK", 17, 6);
        c.addWordh("AHOY", 6, 18);
        c.addWordv("AHOY", 18, 10);
        c.addWordv("ADVANTAGE", 2, 12);
        c.addWordv("INTERNAL", 2, 18);
        c.addWordh("BANTER", 7, 11);
        c.addWordv("BEAGLE", 5, 12);
        c.addWordh("BASE", 8, 3);
        c.addWordv("BALL", 8, 3);
        c.addWordh("LEFT", 10, 3);
        c.addWordv("SAFE", 8, 5);
        System.out.print(c);
    }
}

As you can see in Main method that i am adding the words but also giving the row and column number to place the words like c.addWordv("Safe",8,5); where 8 and 5 is column number.

Now Question is how can i implement cross word algorithm which just take words and place them on board randomly without taking the row and column numbers. Thanks in advance

EDIT:
I want to modify this class algo the way that i dont have to give away the rows and columns number..

npace
  • 4,218
  • 1
  • 25
  • 35
Asad AndyDev
  • 481
  • 5
  • 23
  • I'm sorry, but I do not understand what you are trying to accomplish – Zoe Jul 18 '16 at 07:44
  • Actually, I see major errors. Posting an answer – Zoe Jul 18 '16 at 07:46
  • Try googling for brute force search. What you're trying to do isn't trivial, but as a start, you put some word at a location and then try put others according to the letters already on the crossword. If you fail to do so, you cancel your last selection and do it again with a different word. – dodo Jul 18 '16 at 07:47
  • @dodo okay i will look into the brute force and will do as you explain. – Asad AndyDev Jul 18 '16 at 07:54
  • @Polarbear0106 I am actually trying to implement cross word algorithm in android using JAVA. – Asad AndyDev Jul 18 '16 at 07:55
  • If you cannot start it on Android, what good is it? – Zoe Jul 18 '16 at 08:06
  • @Polarbear0106 Sir problem is not how to start it on android actually i am simply implementing it in console first and also i am facing problem as i insert words to this algo i also need to give it the column number and row number. I want to achieve the results that i simply insert the words to the cross word algo and it can fill the board with these words checking their intersections. – Asad AndyDev Jul 18 '16 at 08:11
  • ok then. Have fun when it works and you cannot start it – Zoe Jul 18 '16 at 08:13
  • @Polarbear0106 i was working using your answer which i saw later you removed it?? – Asad AndyDev Jul 18 '16 at 08:15
  • You literally said that it was not what you were asking for – Zoe Jul 18 '16 at 08:17
  • 'Sir problem is not how to start it on android' – Zoe Jul 18 '16 at 08:17
  • Okay sorry sir i might not able to convey my self well but can you please post your answer again? I will be grateful – Asad AndyDev Jul 18 '16 at 08:20

1 Answers1

2

//Pseudo Code

If the crossword size is maxSize and any word's length is stored in wordLength ,then you can use random method as below int maxSize=20; int wordLength=4;

    Random random =new Random();
    int r,c;
    //for horizontal
     r=random.nextInt(maxSize-wordLength);
     c=random.nextInt(maxSize);
    //for vertical
     r=random.nextInt(maxSize);
     c=random.nextInt(maxSize-wordLength);

You can store the row and column and generate the new one if its already present.

Loki
  • 801
  • 5
  • 13
  • Thanks. It is a good way to generate random rows and columns but when i do like this there isn't even one word which is placed on grid – Asad AndyDev Jul 18 '16 at 07:33
  • And whats the point if we are giving the column and row number ourselves becasue then we are also defining their intersected words too its. Wouldn't it be like hard coding the words in puzzle – Asad AndyDev Jul 18 '16 at 07:39
  • Could you please explain your doubt because the nextInt method will generate random number for the given word, its length and Crossword size is used so that word does not get outside the board. – Loki Jul 18 '16 at 09:20