0
コマンドライン引数を使用するtrominoパズルを作成しようとしています。ユーザーは、2が(例えば、3を入力すると2^3になる)1行と5行の間の数字を入力し、行と列を入力します(これはボード上の空きスペースのインデックスを与えます) )。私は再帰的なケースを把握することはできません。これは私がこれまで持っていたものです。 0Tromino puzzle recursion
import java.awt.*;
import java.util.Hashtable;
public class Tromino{
public static int[][] chessBoard;
public static int currentNum=1;
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int row = Integer.parseInt(args[1]);
int column = Integer.parseInt(args[2]);
int length = (int) Math.pow(2,n);
//Check the command line arguments.
if(!checkArgs(args)){
return;}
chessBoard = new int[length][length];
chessBoard[row][column] = -1;
//Solve the puzzle. Need to define a recursive method solve and call it here.
recursiveMethod(length,row,column);
//Display the board in two ways. Assume chessBoard is the 2D array.
printBoard(chessBoard);
printBoardGUI(chessBoard);}
public static int[][] recursiveMethod (int length,int row,int column){
//base case
if(length == 2){
for (int i=0; i<length; i++){
for(int j = 0; j<length; j++){
if(chessBoard[i][j]==0){
System.out.println("Row " + i + "Column " + j);
chessBoard[i][j]=currentNum;
}
}
}
currentNum++;
}
//recursive case
else{
//find coordinates of missing hole
int saveRow = row, saveColumn=column;
for (int x=0; x<length; x++){
for(int y=0; y<length;y++){
if(chessBoard[x][y] == -1){
saveRow = x;
saveColumn = y;}
}
}
//Empty Space in upper left quad
if(saveRow < length/2 && saveColumn < length/2){
//
System.out.print("Upper left");
recursiveMethod(length/2,row,column);
chessBoard[length/2][length/2-1] = currentNum;
chessBoard[length/2][length/2] = currentNum;
chessBoard[length/2-1][length/2] = currentNum;
//increment number
currentNum++;
recursiveMethod(length/2,row,length/2);
recursiveMethod(length/2,length/2,column);
recursiveMethod(length/2,length/2,length/2);
}
//Upper Right
else if (saveRow<length/2 && saveColumn>= length/2){
System.out.print("Upper right");
recursiveMethod(length/2,row,length/2);
chessBoard[length/2][length/2-1] = currentNum;
chessBoard[length/2][length/2]= currentNum;
chessBoard[length/2-1][length/2-1]=currentNum;
currentNum++;
recursiveMethod(length/2,row,column);
recursiveMethod(length/2,length/2,column);
recursiveMethod(length/2,length/2,length/2);
}
//Bottom left
else if (saveRow>=length/2 && saveColumn<length/2){
System.out.println("Bottom left");
recursiveMethod(length/2,length/2,column);
chessBoard[length/2-1][length/2] = currentNum;
chessBoard[length/2][length/2] = currentNum;
chessBoard[length/2-1][length/2-1] = currentNum;
currentNum++;
recursiveMethod(length/2,row,column);
recursiveMethod(length/2,row,length/2);
recursiveMethod(length/2,length/2,length/2);
}
//Bottom right
else{
System.out.println("Bottom right");
recursiveMethod(length/2,length/2,length/2);
chessBoard[length/2-1][length/2] = currentNum;
chessBoard[length/2][length/2-1] = currentNum;
chessBoard[length/2-1][length/2-1] = currentNum;
currentNum++;
recursiveMethod(length/2,length/2,column);
recursiveMethod(length/2,row,length/2);
recursiveMethod(length/2,row,column);
}
}
return chessBoard;
}
public static boolean isNumber(String s) {
return s.matches("[-+]?\\d*\\.?\\d+");
}
public static boolean checkArgs(String[] args){
int n,row,column;
try{
n = Integer.parseInt(args[0]);
row = Integer.parseInt(args[1]);
column = Integer.parseInt(args[2]);
}
catch (Exception e){
System.out.println("You have entered an invalid argument.");
return false;}
if (n>0 && n<6 && row>=0 &&
row < Math.pow(2,n) &&
column >=0 && column< Math.pow(2,n)){
return true;}
else{
System.out.println("The space is out of range.");
return false;}
}
public static int getIndex(int n, int k){
return (int) (Math.pow(2, n-1) - 1 +(int) (k/Math.pow(2, n)) * Math.pow(2,n));
}
public static void printBoard(int[][] board){
int boardLength = board.length;
for(int i=0; i<boardLength; i++){
System.out.print(" ");
for(int j=0; j<boardLength; j++){
System.out.print("----");
}
System.out.println();
for(int j=0; j<boardLength; j++){
System.out.print(" | " + board[i][j]);
}
System.out.println(" |");
}
System.out.print(" ");
for(int j=0; j<boardLength; j++){
System.out.print("----");
}
System.out.println();
}
public static void printBoardGUI(int[][] board){
int boardLength = board.length;
int width = 50;
int boardLengthPx = boardLength * width;
Hashtable<Integer, Color> colors = new Hashtable<Integer, Color>();
DrawingPanel panel = new DrawingPanel(boardLengthPx, boardLengthPx);
panel.setBackground(Color.darkGray);
colors.put(0, Color.white);
Graphics g = panel.getGraphics();
for(int i = 0; i < boardLength; i++) {
for(int j = 0; j < boardLength; j++) {
if (!colors.containsKey(board[i][j])){
colors.put(board[j][i],
Color.getHSBColor((float)Math.random(),
(float)(.4 + Math.random()*0.6),
(float)(.2 + Math.random()*0.8)));
}
g.setColor((Color)colors.get(board[i][j]));
g.fillRect(i * width, j * width, width, width);
g.setColor(Color.black);
g.drawLine(i * width, j * width, i * width, (j * width) + width);
g.drawLine(i * width, j * width, (i * width) + width, j * width);
}
}
}}