如何根据以下条件验证每个输入字符

时间:2016-12-24 19:29:30

标签: java performance validation optimization boggle

我正在开发一款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. 输入的字符必须与前一个字符相邻(垂直,水平或对角线)。

  3. 目前我正在使用扫描仪(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()来获取字符中的字符索引,然后查找在邻接字符串[]中为有效性。

0 个答案:

没有答案