我一直在帮助一个学生做练习,快速输入这些内容,并认为这将是改进我自己的编程风格和解决问题的方法的理想机会。
在这个练习中,网格代表了其中一个幻灯片谜题。这些模式已被数字所取代。零表示允许移动的缺失部分。到目前为止还没有游戏,目的是简单地交换丢失的部分,就像游戏开始前所做的那样。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Puzzle {
public static void main(String[] args) {
System.out.println("Please enter dimensions of the puzzle.");
System.out.println("Enter length (>0) ");
Scanner sc = new Scanner(System.in);
int width = sc.nextInt();
System.out.println("Enter height (>0)");
int height = sc.nextInt();
int[][] puzzle = new int[width][height];
populatePuzzle( puzzle );
System.out.println("Enter position of missing piece");
int position = sc.nextInt();
placeMissingPiece( position, puzzle );
System.out.println( "Missing piece is at " + Arrays.toString( findNumber( puzzle, 0 ) ) );
displayPuzzle( puzzle );
int number = 0;
while( true ){
System.out.println("Enter the number to be swapped around");
number = sc.nextInt();
swapElementValues( puzzle, findNumber( puzzle, 0 ), findNumber( puzzle, number ) );
displayPuzzle( puzzle );
}
}
public static void swapElementValues( int[][] puzzle, int[] missing, int[] target ){
int tempValue = puzzle[ missing[0] ][ missing[1] ];
puzzle[ missing[0] ][ missing[1] ] = puzzle[ target[0] ][ target[1] ];
puzzle[ target[0] ][ target[1] ] = tempValue;
}
public static int[] findNumber( int[][] puzzle, int number ){
for( int i = 0; i < puzzle.length; i++ ){
for( int j = 0; j < puzzle[0].length; j++ ){
if( puzzle[i][j] == number ){
int[] foundCoords = new int[2];
foundCoords[0] = i;
foundCoords[1] = j;
return foundCoords;
}
}
}
return null;
}
public static int getRandomInteger(){
Random randomGenerator = new Random();
return randomGenerator.nextInt(99) + 1;
}
public static void populatePuzzle( int[][] puzzle ){
for( int i = 0; i < puzzle.length; i++ ){
for( int j = 0; j < puzzle[0].length; j++ ){
puzzle[i][j] = getRandomInteger();
}
}
}
public static void displayPuzzle( int[][] puzzle ){
for( int i = 0; i < puzzle.length; i++ ){
for( int j = 0; j < puzzle[0].length; j++ ){
System.out.print( puzzle[i][j] + "," );
}
System.out.println( "" ); // Carriage return
}
}
public static void placeMissingPiece( int elementNumber, int[][] puzzle ){
int counter = 0;
for( int i = 0; i < puzzle.length; i++ ){
for( int j = 0; j < puzzle[0].length; j++ ){
counter++;
if( counter == elementNumber ){
puzzle[i][j] = 0;
}
}
}
}
}发布于 2012-02-29 08:01:30
System.out.println("Enter height (>0)");
int height = sc.nextInt();此模式在代码中重复多次。将其转换为方法,例如(假设Scanner被转换为静态字段):
private static int promptForInt(String message) {
System.out.println(message);
return sc.nextInt();
}-
public static int getRandomInteger(){
Random randomGenerator = new Random();
return randomGenerator.nextInt(99) + 1;
}从性能的角度来看,为每个调用创建一个新的Random是没有意义的。应该只创建一次对象。
public static void populatePuzzle( int[][] puzzle ){
public static void displayPuzzle( int[][] puzzle ){
public static void placeMissingPiece( int elementNumber, int[][] puzzle ){你不是在想面向对象的东西。上面的模式非常明显地表明,每个方法都应该属于一个Puzzle类。这样,您的main方法就可以关注程序的核心逻辑,而不是谜题的实现细节。例如:
Puzzle puzzle = new Puzzle(); // .populate() should probably be called from the constructor
// ...
puzzle.placeMissingPiece(position);
// ...
puzzle.display();
// ...
puzzle.swapElementValues(0, number); // findNumber() called as a private method
// etc...https://codereview.stackexchange.com/questions/9533
复制相似问题