Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

========================================================================== To pr

ID: 3539071 • Letter: #

Question

==========================================================================

To practice using recursion to solve a problem. To practice using recursion to find a palindrome. To review using two-dimensional arrays Boggle is a word game designed by Allan Turoff and trademarked by Parker Brothers, a division of Hasbro. The game is played using a plastic grid of lettered dice, in which players attempt to find words in sequences of adjacent letters. The game begins by shaking a covered tray of sixteen cubic dice, each with a different letter printed on each of its sides. The dice settle into a 4times4 tray so that only the top letter of each cube is visible: Players search for words that can be constructed from the letters of sequentially adjacent cubes, where "adjacent" cubes are those horizontally or vertically neighboring (the real game also allows diagonals, but you do not need to for this assignment). Each letter of the word is a neighbor of the previous letter and players may not use the same letter cube more than once per word. For example, in the above configuration of letters, the following words (and possibly more) can be found: You are to write a program that runs a game of Boggle for a single player. Your program must perform the following tasks: Ask the player to enter a seed for randomizing the letters. A seed is useful for testing, because each run will generate the same letters. The above board was generated using 9999 for the seed. Generate a random 4times4 board of letters. Display the board. Allow the player to guess a word and then verify its validity on the board. Valid words start at any position, and then each subsequent letter of the word is a neighbor of the previous letter You only need to consider horizontal and vertical neighbors (NOT diagonal) Indicate to the player whether or not the word is valid, and if so, print the board again with the verified letters marked. Indicate to the player whether or not the word is a palindrome. This recursive method receives just 1 String argument You MUST use recursion for this assignment. Recall that recursion is when a method calls itself (one or more times), and each call might again call itself, over and over until some base condition is met and the methods return. This will be useful for finding a word on the board. You should have a class called BoggleBoard. which includes the array of letters, methods for filling and printing it, and a public method that returns whether or not a given word is valid for the board. This last method should make use of another private recursive method for verifying words. You should have a separate class (in a separate .java file) containing your Main() method, which creates an instance of the board and runs the game (i.e. displays the board, asks player to find words, tells player if word is valid or not. etc.). Generating a random character is a lot like generating a random integer. Characters all have an ASCII value, which is an integer. Capitals range from ASCII values 65 to 90. For example, you could use the following to generate a random character. You may want to use two coinciding 2-dimensional arrays to represent the boggle board - one that contains the letters, and one that keeps track of which cells have been visited already when verifying a word. For the recursion, think about how you might check the board if you were doing it by hand. You might start by scanning the array for the first letter of the word. If you find a match for the first letter, you would then check each of its neighbors for the next letter. If one of them matches, you check its neighbors, and so on, until either reaching the end of the word or finding no matches. Think about which part of this is the recursive step, and what the terminating condition(s) would be. You MUST use a recursive method with just 1 String argument to determine if the word is a palindrome.

Explanation / Answer

import java.util.*;

import java.io.*;


class Mukesh

{

static String[][] Board=new String[4][4];

static Scanner s=new Scanner(System.in);

public static void StartGame()

{

System.out.println("Enter seed");

int n=s.nextInt();

Random rand=new Random(n);

for(int i=0;i<4;i++)

{

for(int j=0;j<4;j++)

{

char b=((char)(65 + (int)(Math.random() * ((90 - 65) + 1))));

Board[i][j]=Character.toString(b);

System.out.print(Board[i][j]);

System.out.print(" ");

}

System.out.println();

}

System.out.println("Enter the word (in Uppercase):");

String str=s.next();

if(CheckSolution(str))

{

System.out.println("Nice Job!");

}

else

{

System.out.println("I don't see that word.");

}

}

public static boolean CheckChar(String a,int x,int y)

{

if(!a.substring(0,1).equals(Board[x][y]))

{

return false;

}

if(a.length()==1)

{

return true;

}

else

{

if((x<=2)&&(x>=1)&&(y<=2)&&(y>=1))

{

if(a.substring(1, 2).equals(Board[x+1][y]))

{

return CheckChar(a.substring(1, a.length()),x+1,y);

}

if(a.substring(1, 2).equals(Board[x-1][y]))

{

return CheckChar(a.substring(1, a.length()),x-1,y);

}

if(a.substring(1, 2).equals(Board[x][y+1]))

{

return CheckChar(a.substring(1, a.length()),x,y+1);

}

if(a.substring(1, 2).equals(Board[x][y-1]))

{

return CheckChar(a.substring(1, a.length()),x,y-1);

}

}

else if((x<1)&&(y<=2)&&(y>=1))

{

if(a.substring(1, 2).equals(Board[x+1][y]))

{

return CheckChar(a.substring(1, a.length()),x+1,y);

}

if(a.substring(1, 2).equals(Board[x][y+1]))

{

return CheckChar(a.substring(1, a.length()),x,y+1);

}

if(a.substring(1, 2).equals(Board[x][y-1]))

{

return CheckChar(a.substring(1, a.length()),x,y-1);

}

}

else if((x>2)&&(y<=2)&&(y>=1))

{

if(a.substring(1, 2).equals(Board[x-1][y]))

{

return CheckChar(a.substring(1, a.length()),x-1,y);

}

if(a.substring(1, 2).equals(Board[x][y+1]))

{

return CheckChar(a.substring(1, a.length()),x,y+1);

}

if(a.substring(1, 2).equals(Board[x][y-1]))

{

return CheckChar(a.substring(1, a.length()),x,y-1);

}

}

else if((y<1)&&(x<=2)&&(x>=1))

{

if(a.substring(1, 2).equals(Board[x-1][y]))

{

return CheckChar(a.substring(1, a.length()),x-1,y);

}

if(a.substring(1, 2).equals(Board[x][y+1]))

{

return CheckChar(a.substring(1, a.length()),x,y+1);

}

if(a.substring(1, 2).equals(Board[x+1][y]))

{

return CheckChar(a.substring(1, a.length()),x+1,y);

}

}

else if((y>2)&&(x<=2)&&(x>=1))

{

if(a.substring(1, 2).equals(Board[x-1][y]))

{

return CheckChar(a.substring(1, a.length()),x-1,y);

}

if(a.substring(1, 2).equals(Board[x][y-1]))

{

return CheckChar(a.substring(1, a.length()),x,y-1);

}

if(a.substring(1, 2).equals(Board[x][y]))

{

return CheckChar(a.substring(1, a.length()),x,y);

}

}

else if((x==0)&&(y==0))

{

if(a.substring(1, 2).equals(Board[0][1]))

{

return CheckChar(a.substring(1, a.length()),0,1);

}

if(a.substring(1, 2).equals(Board[1][0]))

{

return CheckChar(a.substring(1, a.length()),1,0);

}

}

else if((x==3)&&(y==0))

{

if(a.substring(1, 2).equals(Board[2][0]))

{

return CheckChar(a.substring(1, a.length()),2,0);

}

if(a.substring(1, 2).equals(Board[3][1]))

{

return CheckChar(a.substring(1, a.length()),3,1);

}

}

else if((x==0)&&(y==3))

{

if(a.substring(1, 2).equals(Board[1][3]))

{

return CheckChar(a.substring(1, a.length()),1,3);

}

if(a.substring(1, 2).equals(Board[0][2]))

{

return CheckChar(a.substring(1, a.length()),0,2);

}

}

else if((x==3)&&(y==3))

{

if(a.substring(1, 2).equals(Board[2][3]))

{

return CheckChar(a.substring(1, a.length()),2,3);

}

if(a.substring(1, 2).equals(Board[3][2]))

{

return CheckChar(a.substring(1, a.length()),3,2);

}

}

return false;

}

}

public static boolean CheckSolution(String str)

{

for(int i=0;i<4;i++)

{

for(int j=0;j<4;j++)

{

if(CheckChar(str,i,j))

{

return true;

}

}

}

return false;

}

public static void main(String[] args)

{

StartGame();

}


}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote