Problem with scrabble

0

I am new in both programming and this page, I am designing a scrabble with my basic knowledge and few, the roll is that I do not know how to check if the word that I have written on the keyboard contains the letters provided by the program by a random assigned to the index of a array with the alphabet. for now I have this: D regards!

import java.util.Scanner;

public class Scrabble {

static String palabra;
static char[] letras = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'ñ', 'o', 'p', 'q',
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', };
static char[] opciones = new char[7];
static int puntos = 0;
static int puntoLetra;
static int puntoL[] = new int [7];
public static void Random() {

    System.out.println("Tus letras son: ");
    for (int i = 0; i < 7; i++) {
        puntoLetra = (int) Math.round(Math.random() * 2) + 1;
        int rand = (int) Math.round(Math.random() * 26);
        System.out.println(letras[rand] + " = " + puntoLetra + " puntos.");
        opciones[i] = letras[rand];
        puntoL[i] = puntoLetra;
    }
    System.out.println();
}

public static void Pedir() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Si has conseguido formar una palabra, introducela, de lo contrario teclea \"siguiente\" para pasar de ronda.");
    palabra = sc.nextLine();
    if(palabra.contains("siguiente")) {
        System.out.println("No has conseguido formar una palabra y no has ganado ningun punto \nTu puntuación actual es de " + puntos + " puntos.\n\nSiguiente ronda.\n");
        Random();
        Pedir();

    }else{
        for(int i = 0; i < palabra.length(); i++) {

        }
    }
}





public static void main(String[] args) {
    Random();
    Pedir();
 }

}
    
asked by Miguel Angel 06.02.2018 в 02:36
source

1 answer

0

There would be several ways to validate that the letters in the entry are contained in the array with random letters you selected above. As you build the Array of type char[] you could iterate over the word and get character by character by means of charAt and go comparing

if(palabra.contains("siguiente")) {
    System.out.println("No has conseguido formar una palabra y no has ganado"
               + " ningun punto \nTu puntuación actual es de " + puntos + ""
               + " puntos.\n\nSiguiente ronda.\n");
    Random();
    Pedir();

}else{
    boolean correcto = true;
    String options = new String(opciones);
    for(int i = 0; i < palabra.length() && correcto; i++) {
        if(!options.contains(String.valueOf(palabra.charAt(i)))){
            correcto=false; 
        }
    }
    if(correcto){
        System.out.println("Perfecto , ha completo la palabra");
    }
    else{
        System.out.println("Su Palaba contiene letras que no fueron seleccionadas");
    }
}
    
answered by 06.02.2018 в 04:38