1

我正在开发一个 Boggle 游戏。我想在按键/向上/向下等上验证两件事。

  1. 使用键盘,用户应该被限制只能输入板中的字符(并将其附加到输入字符串/单词),否则跳过它。向用户展示 4 x 4 的 Boggle 板。例如

    A B B A
    A A B B
    B B A A
    A B A B
    

    在这种情况下 C - Z 是无效输入,它们不应该附加到输入字符串中,我希望用户在输入完整单词后只按一次 ENTER 键。

  2. 输入的字符必须与前一个字符相邻(垂直、水平或对角线)。

目前我正在使用扫描仪(system.in)获取字符串并使用两个单独的函数验证这些条件,但我想要现场验证。

我的代码:

package com.boggle;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
public class BoggleBoard {
    String[] Cons = {"B","C","D","F","G","H","J","K","L","M",
                    "N","P","Q","R","S","T","V","W","X","Y","Z"};
    String[] Vow = {"A","E","I","O","U"};
    String[][] board = new String[4][4];
    String[] adjcntStr = new String[16];
    boolean[] ConsMark = new boolean[21];
    String Bstring = "";
    boolean isValid;
    Random rand = new Random(System.currentTimeMillis());
    public void ShowNear(){
    for(int i=0; i<16; i++){
        cout(adjcntStr[i]+"\n");
        }
    }   
    public void Play(){
        cout("Enter Your Word\n");
        Scanner read = new Scanner(System.in);  
        String xYz = read.next();
        if(!ValidChar(xYz))
        {
            cout("Invalid Choice\n");
            Play();
        }
        else
        {
            if(!ValidSqnc(xYz, 0))          
            {
                cout("\nInvalid Sequence\n");
                Play();
            }
            else
            {
                cout("\n\nWord: "+xYz);
            }           
        }
        read.close();
    }
    public boolean ValidChar(String t){
        for(int i = 0; i<t.length();i++){
            if(!Bstring.contains(""+t.charAt(i)+"")){
                return false;
            }
        }
        return true;
    }
    public boolean ValidSqnc(String S, int i){
        if(i+1 == S.length())
        {
            isValid = true;
            return true;
            }
        if(adjcntStr[Bstring.indexOf(Character.toString(S.charAt(i)))].contains(Character.toString(S.charAt(i+1))))
        {
            ValidSqnc(S, ++i);
        }
        else
        {           
            isValid = false;
        }
        return isValid;
    }
    public void cout(String T){
        System.out.print(T);
    }
    public String GetStr(){
        return Bstring;
    }
    public BoggleBoard(){
                setBoard();
                getBoard();
                near();
            }
    public void setBoard(){
        int c=0;
        for(int i=0;i<4;i++){   
            for(int j=0;j<4;j++){               
                if( (int)rand.nextInt(2)==0 && c<5){
                    board[i][j] = Vow[c];
                    Bstring = Bstring + Vow[c];
                    ++c;
                }
                else
                {
                    board[i][j]= Cons[UniqueInt()];
                    Bstring = Bstring + board[i][j];
                }
            }
        }
    }
    public int UniqueInt(){
        int index;
        while(true)
        {
            index = (int)(rand.nextInt(21));
            if(ConsMark[index]==false)
            {
                ConsMark[index] = true;
                break;
            }
        }
        return index;
 }
    public void getBoard(){
        for(int i=0;i<4;i++){
            for(int f=0;f<4;f++){
                    cout(board[i][f]+" ");
            }
                    System.out.println();           
        }
    }
    public void near(){     
        int row = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                adjcntStr[row] =  "";
                for (int x = Math.max(0, i - 1); x <= Math.min(i + 1, board.length); x++) {
                    for (int y = Math.max(0, j - 1); y <= Math.min(j + 1,
                            board[i].length); y++) {
                        if (x >= 0 && y >= 0 && x < board.length
                                && y < board[i].length) {
                            if(x!=i || y!=j){                            
                            adjcntStr[row] = adjcntStr[row] + board[x][y];
                            }
                        }
                    }
                }
                row++;
            }
        }       
    }

简而言之,我在棋盘中没有重复的字符,我为每个索引位置维护一个邻接字符串和所有棋盘字符的字符串,我indexof()用来获取棋盘中字符的索引,然后在邻接字符串 [] 中查找有效性.

4

0 回答 0