Fehler beim Versuch, ein Spiel in Java für ein Magic Square -Puzzle zu codieren
Posted: 11 Mar 2025, 23:48
Nachdem ich meinen Code für ein Magic Square Puzzle -Spiel beendet hatte, bittet es mich immer wieder, einen Schritt zu machen und den Platz nicht zu aktualisieren. Hier ist der Code: < /p>
Code: Select all
import java.util.Arrays; import java.util.Random; import java.util.Scanner;
public class game {
static Random rand = new Random();
public static int[][] shuffleSquare( int[][] square ) {
int size = square.length;
int row = rand.nextInt( size );
int col = rand.nextInt( size );
int direction = rand.nextInt( 4 );
return playerMove( square, row, col, direction );
}
public static int[][] playerMove( int[][] square, int row, int col, int direction ) {
int size = square.length;
int temp = square[ row ][ col ];
int newRow = row;
int newCol = col;
//Check direction
switch( direction ) {
case 0: //Up
newRow = ( row - 1 + size ) % size;
break;
case 1: //Down
newRow = ( row + 1 + size ) % size;
break;
case 2: //Left
newCol = ( col - 1 + size ) % size;
break;
case 3: //Right
newCol = ( col + 1 + size ) % size;
break;
}
square[ row ][ col ] = square[ newRow ][ newCol ];
square[ newRow ][ newCol ] = temp;
return square;
}
public static int[] getInput( int size ) {
int row = 0;
int col = 0;
int dirNum = 0;
boolean checkMove = false;
while( checkMove == false ) {
try {
System.out.println( "Enter a row, column and a direction: U, D, L or R. For example, '3 2 U'. Enter a number from 1 to " + size + " for Row and Column" );
Scanner input = new Scanner( System.in );
String move = input.nextLine();
String[] array = move.split( "" );
row = Integer.parseInt( array[ 0 ] ) - 1;
col = Integer.parseInt( array[ 1 ] ) - 1;
String direction = array[ 2 ];
switch( direction ) {
case "U" -> {
if( row > 0 ) {
dirNum = 0;
checkMove = true;
}
}
case "D" -> {
if( row < ( size - 1 ) ) {
dirNum = 1;
checkMove = true;
}
}
case "L" -> {
if( col > 0 ) {
dirNum = 2;
checkMove = true;
}
}
case "R" -> {
if( col < ( size - 1 ) ) {
dirNum = 3;
checkMove = true;
}
}
}
}
catch( Exception e ) {
checkMove = false;
}
}
int[] returnArr = { row, col, dirNum };
return returnArr;
}
public static void main( String args[] ) {
int n = square.getInput();
int[][] ogSquare = square.genSquare( n );
int[][] newSquare = Arrays.stream( ogSquare ).map( int[]::clone ).toArray( int[][]::new );
System.out.println( "Og square" );
square.displaySquare( ogSquare );
for( int i = 0; i < ( n * n ); i ++ ) {
newSquare = shuffleSquare( newSquare );
}
System.out.println( "Shuffled square:" );
square.displaySquare( newSquare );
int moves = 0;
System.err.println( Arrays.deepEquals( ogSquare, newSquare ) );
while( Arrays.deepEquals( ogSquare, newSquare ) == false ) {
System.out.println( "New Square:" );
square.displaySquare( newSquare );
int[] moveData = getInput( ogSquare.length );
newSquare = playerMove( newSquare, moveData[ 0 ], moveData[ 1 ], moveData[ 2 ] );
moves += 1;
}
System.out.println( "You solved the magic square in " + moves + " moves." );
}
}
< /code>
Zweite Datei verwendet: < /p>
import java.util.Scanner;
public class square {
static int getInput() {
Scanner input = new Scanner( System.in );
System.out.println( "Enter an odd number: " );
int n = input.nextInt();
if( n % 2 == 0 ) {
System.out.println( "The number must be odd." );
}
return n;
}
static void displaySquare( int[][] square ) {
for( int i = 0; i < square.length; i ++ ) {
for( int j = 0; j < square.length; j ++ ) {
System.out.print( square[ i ][ j ] + " " );
}
System.out.println();
}
}
public static int[][] genSquare( int n ) {
//Fills square with zeroes
int[][] square = new int[ n ][ n ];
//Sets initial position
int x = 0;
int y = n / 2;
square[ x ][ y ] = 1;
//Complete rest of square
for( int i = 2; i