jeudi 12 décembre 2019

How to iterate through an array for a word search puzzle

I come face with the task of trying to see if the puzzle actually contains my words. Currently let's say the word red is the word we are trying to find; it will tell me it is not there when it is. My code down below is what I have so far. What I have notice is my row and column are always at zero and never moving. How can I make a loop to iterate the entire array, so it could find the words I am looking for.

public static void play(  String word,  char[][] puzzle) {
    //created char array and then made "word" which is a string into a char array
    char[] letter = word.toCharArray();
    int count = 0;
    //created a boolean flag for a while loop
    boolean flag = true;
    int row=0;
    int column=0;


    //while(count < letter.length) {

    //for(int row = 0; row < puzzle.length; row++){

    //for(int column = 0; column < puzzle.length; column++){

    //if (letter[count] == puzzle[row][column]) {
    while(flag == true) {
        if (checkUp(puzzle, word, row, column) == true) {
            row = row -1;
            //count ++;
            System.out.println(letter[count]);
        }
        else if (checkDown(puzzle, word, row, column) == true) {
            //if (checkDown(puzzle, word, row, column) == true) {
            row = row + 1;
            //count ++;
            System.out.println(letter[count]);
        }
        else if (checkLeft(puzzle, word, row, column) == true) {
            //if (checkLeft(puzzle, word, row, column) == true) {
            column = column + 1;
            //count ++;
            System.out.println(letter[count]);
        }
        else if (checkRight(puzzle, word, row, column) == true){
            //if (checkRight(puzzle, word, row, column) == true) {
            column = column -1;
            //count ++;
            System.out.println(letter[count]);
        }
        else {
            System.out.println("This word is not in the puzzle: " + word);
            System.out.println();
            //count = letter.length;
            break;
        }
    }
}


public static boolean checkUp(char [][] puzzle, String word, int row, int col) {
    //created char array and then made "word" which is a string into a char array
    char [] letter = word.toCharArray();
    //A print statement that will print the letter at position 0
    System.out.println(letter[0]);
    //created an if statement that is mean to prevent the search going out of bound
    if (row - 1 < 0) {
        return false;
    }
    //another if statement to check if position 0 is equal to the puzzle which the row and column
    if(letter[0] == puzzle [row-1][col]) {
        return true;
    }
    else {
        // if it is else it returns false
        return false;
    }
}

Aucun commentaire:

Enregistrer un commentaire