ここ数日、大学向けの Java プロジェクトに取り組んでいます。いくつかの障害にぶつかりましたが、このサイトのユーザーの助けを借りて克服しました。私がやっているのは、ユーザーが入力した単語を文字列の配列リストとして受け取り、それらを2次元配列にランダムにマップする単語検索です。行と列を選択するメソッド、単語が進む方向を選択するメソッド、およびその単語に十分な空きスペースがあるかどうかをチェックするメソッドを作成しました。そのコードをコンパイルする際にいくつかのエラーが発生しています。これが私の以前の質問です。ご覧のとおり、それ以来、いくつかの進歩を遂げました:) https://stackoverflow.com/questions/10193291/adding-words-to-a-2-d-array
具体的には、問題は、乱数を使用して doitfit メソッド (上、下、左、または右) を参照して、選択するメソッドを決定しようとしていることです。コンパイルされていません:/
import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
private char[][] puzzle ;
private ArrayList<String> puzzleWords ;
private int letterCount = 0 ;
private int gridDimensions;
public static int row, column;
public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
{
this.puzzleWords = userSpecifiedWords ;
}
private void createPuzzleGrid()
{
int i, itemLength;
String item;
for (i = 0; i < puzzleWords.size(); i++) {
item = puzzleWords.get(i);
itemLength = item.length();
letterCount = letterCount + itemLength;
}
gridDimensions = letterCount * 2;
puzzle = new char[gridDimensions][gridDimensions] ;
}
private void generateWordSearchPuzzle()
{
}
public void northSouthEastWest(String word)
{
int upDownLeftRight, north, south, east, west;
north = 1;
south = 2;
east = 3;
west = 4;
String Word;
upDownLeftRight = (int)(Math.random() * 4);
if(upDownLeftRight == north){
fitWordNorth(word);
}else if(upDownLeftRight == south){
fitWordSouth(word);
}else if(upDownLeftRight == east){
fitWordEast(word);
}else if(upDownLeftRight == west){
fitWordWest(word);
}
}
public void firstSpace(String word)
{
row = (int)(Math.random() * gridDimensions);
column = (int)(Math.random() * gridDimensions);
if(puzzle[row][column] != ' ') {
firstSpace(word);
} else {
northSouthEastWest(word);
}
}
public void fitWordNorth(String word)
{
boolean clear = false;
int p, i;
if(row >= word.length()){
for(i = row - 1; i < word.length(); i--){
if(puzzle[i][column] != ' '){
firstSpace(word);
}else{
clear = true;
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row - p][column] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
}
}
public void fitWordSouth(String word)
{
boolean clear = false;
int row, column, p, i;
if(row >= word.length()){
for(i = row + 1; i < word.length(); i++){
if(puzzle[i][column] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row + p][column] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
public void fitWordWest(String word)
{
boolean clear = false;
int row, column, p, i;
if(column >= word.length()){
for(i = column - 1; i < word.length(); i--){
if(puzzle[row][i] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row][column - p] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
public void fitWordEast(String word)
{
boolean clear = false;
int row, column, p, i;
if(column >= word.length()){
for(i = column + 1; i < word.length(); i++){
if(puzzle[row][i] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row][column + p] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
}