我的程序创建了一个4x4的JButton网格,它们均具有随机字母作为其文本值。这些JButton存储在称为diceLayout[][]
的2D数组中。该功能由用户提交的由所述字母组成的单词传递。
它需要检查周围的字母,以查看它们的字母是否等于单词的下一个字母。如果单词完全连接,函数将返回true
。否则,该函数将返回false
。
但是,由于某种原因,该函数将仅返回false
。我已附上以下功能,如果您需要更多程序,请告诉我。谢谢!
private boolean checkWordConnected(String word) {
word = word.toLowerCase();
String letters[] = word.split("");
boolean conn = false;
outerloop: for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (letters[0].equals(diceLayout[i][j].getText().toLowerCase())
&& diceLayout[i][j].getIsClicked()) {
pos[0] = i;
pos[1] = j;
break outerloop;
} else if (i == 3 && j == 3) {
return false;
}
}
}
for (int a = 1; a < letters.length - 1; a++) {
loop: for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int checkPos[] = { pos[0] + i, pos[1] + j };
if (checkPos[0] < 4 && checkPos[0] > 0 && checkPos[1] > 0 && checkPos[1] < 4) {
if (letters[a].equals(diceLayout[checkPos[0]][checkPos[1]].getText().toLowerCase())
&& diceLayout[checkPos[0]][checkPos[1]].getIsClicked()) {
conn = true;
System.out.println(conn);
pos[0] = checkPos[0];
pos[1] = checkPos[1];
break loop;
}
}
}
}
}
return conn;
}
样本输入和输出: here
此函数的值(带有输入的单词)返回时,在输出栏中显示“ false”。
按下的JButton的文本添加到String变量createWord中,在SubmitButton ActionListener中,将其带入该单词并将其传递给函数checkValidWord,该函数将值再次传递给checkWordConnected和其他根据条件测试该单词的函数>
Controller类的完整代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
import Model.Die;
import Model.Scoreboard;
import View.GameWindow;
import View.HelpWindow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
public class Controller {
private GameWindow view;
private static int pos[] = new int[2];
private Die diceLayout[][];
private String createdWord;
public Controller(){
view = new GameWindow();
addHelpListener();
addSubmitListener();
diceLayout = view.getDice();
addDiceListeners();
}
private void submitWord(String word){
boolean valid = checkValidWord(word);
System.out.println("Word Validity: " + valid);
if(valid=true){
System.out.println("The word ‘"+word+"‘ is valid.");
setDieClicked(false);
createdWord="";
}else{System.out.println("The word ‘"+word+"‘ is not valid.");}
}
private boolean checkValidWord(String word){
boolean validSpell = validWordDic(word);
boolean connected = checkWordConnected(word);
System.out.println("Connected?: "+validSpell);
if(validSpell&&connected&&word.length()>3){
return true;
}else{
return false;
}
}
private static boolean validWordDic(String word){
word=word.toLowerCase();
try {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\sambe\\Desktop\\IPT\\BoggleGame\\res\\dictionary.txt"));
String str;
while ((str = in.readLine()) != null) {
if (str == word) {
return true;
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private boolean checkWordConnected(String word){
word = word.toLowerCase();
String letters[] = word.split("");
boolean conn = false;
outerloop:
for(int i = 0; i<4; i++){
for(int j = 0; j<4; j++){
if(letters[0].equals(diceLayout[i][j].getText().toLowerCase())&&diceLayout[i][j].getIsClicked()){
pos[0]=i;
pos[1]=j;
break outerloop;
}else if(i==3&&j==3){
return false;
}
}
}
for(int a = 1; a<letters.length-1; a++){
loop:
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
int checkPos[] = {pos[0]+i,pos[1]+j};
if(checkPos[0]<4&&checkPos[0]>0&&checkPos[1]>0&&checkPos[1]<4){
if(letters[a].equals(diceLayout[checkPos[0]][checkPos[1]].getText().toLowerCase())&&diceLayout[checkPos[0]][checkPos[1]].getIsClicked()){
conn=true;
System.out.println(conn);
pos[0]=checkPos[0];
pos[1]=checkPos[1];
break loop;
}
}
}
}
}
return conn;
}
private void addHelpListener(){
view.getHelpButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
HelpWindow helpWin = new HelpWindow();
System.out.println("done");
}
});
}
private void addSubmitListener(){
view.getSubmitButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("done");
if(!"".equals(view.getSubmitField().getText())){
submitWord(view.getSubmitField().getText());
}else{
submitWord(createdWord);
}
view.getSubmitField().setText("");
setDieColourNull();
}
});
}
private void addDiceListeners(){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
Die die = diceLayout[i][j];
die.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(!die.getIsClicked()){
die.setClicked(true);
if(createdWord==null){
createdWord=die.getText();
}else{
createdWord=createdWord+die.getText();
}
System.out.println(createdWord);
}
}
});
}
}
}
private void setDieColourNull(){
for(int i =0; i<4; i++){
for(int j = 0; j<4; j++){
diceLayout[i][j].setBackground(new JButton().getBackground());
}
}
}
private void setDieClicked(boolean bool){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
diceLayout[i][j].setClicked(bool);
}
}
}
private int scoreWord(String word){
int score=0;
if(word.length()==3){
score=1;
}else if(word.length()==4){
score=1;
}else if(word.length()==5){
score=2;
}else if(word.length()==6){
score=3;
}else if(word.length()==7){
score=5;
}else if(word.length()>=8){
score=11;
}
return score;
}
}
哦,我知道该程序还有很多其他问题,但这是我目前关注的问题。
答案 0 :(得分:0)
让我向您展示一个示例,说明如何通过检查是否在当前字母附近找到下一个字母来逆向搜索。我简化了周围的程序,只使用了静态数组diceLayout
而不是按钮。为了向您展示该算法的工作原理,我还添加了一些无用的System.out.println
。
public class Dices {
private static String[][] diceLayout = new String[4][4]; // instead of the button array a static String array
public static void main(String[] args) {
diceLayout[0] = new String[]{"a", "b", "c", "d"};
diceLayout[1] = new String[]{"e", "f", "g", "h"};
diceLayout[2] = new String[]{"i", "j", "k", "l"};
diceLayout[3] = new String[]{"m", "n", "o", "p"};
String word = "bfkl"; // simple test
System.out.println(checkWordConnected(word));
}
private static boolean checkWordConnected(String word) {
word = word.toLowerCase();
String[] letters = word.split("");
int[] nextPosition = getFirstPosition(letters[0]); // nextPosition will always hold the i and j indices of the letter you are looking for
if (nextPosition != null) { // if it is not null, then the very first letter of the word is in the array
System.out.println("found " + letters[0]+ " at " + Arrays.toString(nextPosition));
} else {
return false; // if it is null, then we don't even have the first letter and the result is false
}
for (int i = 1; i < letters.length; i++) { // now let's check the other letters
nextPosition = getNextPosition(letters[i], nextPosition[0], nextPosition[1]);
if (nextPosition != null) {
System.out.println("found " + letters[i] + " at " + Arrays.toString(nextPosition)); // we continue when we found our next position
} else {
return false; // otherwise the word is not complete
}
}
return true;
}
// this is a helper method to find the first i and j index (the first position)
// it is only called once and when it finds the indices of the very first letter,
// it will return these indices as an int array. If the letter is not in the
// array, our position is null (the resulting int[] will be null)
private static int[] getFirstPosition(String letter) {
for (int i = 0; i < diceLayout.length; i++) {
for (int j = 0; j < diceLayout[i].length; j++) {
if (diceLayout[i][j].toLowerCase().equals(letter)) {
return new int[]{i, j};
}
}
}
return null;
}
// this will find the index of the next letter, if it is near the current index
// meaning, for every i we check if the letter is somewhere in between
// i-1 and i+1
// and for every j we check if the letter is in between j-1 and j+1
private static int[] getNextPosition(String letter, int i, int j) {
for (int k = i - 1; k <= i + 1; k++) {
for (int l = j - 1; l <= j + 1; l++) {
if (check(letter, k, l)) {
return new int[]{k, l};
}
}
}
return null;
}
// thats the actual method where we check if the letter is at the
// indicated index. But first we have to make sure that the index is valid
// and does not go out of bounds (not smaller than 0 and not greater than the array's length)
private static boolean check(String letter, int i, int j) {
System.out.println("checking " + i + " " + j);
if (i >= 0 && i < diceLayout.length && j >= 0 && j < diceLayout[i].length) {
return diceLayout[i][j].equals(letter);
}
return false;
}
}
我知道,这个答案不能直接应用于您的问题-而且理解起来相当长且复杂。但我仍然希望它能帮助您找到自己的解决方案。 (这次没有标签,并且循环太多:)