I am developing a Boggle game. I want to validate two things on key press/up/down etc.
Using keyboard, a user should be restricted to only enter character that is in the board (and append it to input string/word) else skip it. Boggle board of 4 x 4 is shown to user. For example
A B B A A A B B B B A A A B A B
In this case C - Z are invalid inputs, they should not be appended to the input string, I want user to press ENTER key only once when it has entered complete word.
Entered character must be adjacent (vertically, horizontally or diagonally) to previous character.
At the moment I am taking a string using scanner (system.in
) and validating these conditions using two separate functions, but I want on spot validation.
My code:
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++;
}
}
}
In short, I have no repeated characters in board, I am maintaining a adjacency string for each index location and a string of all board characters, I use indexof()
to get index of character in board, then look up in adjacency string[] for the validity.