gpt4 book ai didi

java - 在 Java 内存游戏中,如何触发已发现的 2 个未知数,并让它们在游戏的其余部分保持显露出来?

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:12 25 4
gpt4 key购买 nike

我目前正在做一个java内存游戏,让用户通过输入2x2、4x4或6x6正方形的坐标来猜测重复的字母,这些正方形有2组重复字母

例如。对于 4x4 来说是

A B C D

EFGH

A B C D

EFGH

但随机

如果他们猜对了,这些字母就会一直显示出来,直到找到所有字母。

我已经成功地随机化了正方形,并用( * )覆盖它,并使它们根据输入坐标显示

但我不知道如何做到一旦显示的 2 个字母相同,程序就会在整个游戏过程中保持它们显示,直到显示所有重复的字母。

现在的代码(您可以复制并粘贴下面的整个内容,所有注释都已注释掉):

    import java.util.Scanner;
import java.io.IOException;

public class a4{

// main method. DO NOT MODIFY





public static void main(String args[]) {
Scanner keyboard = new Scanner( System.in );

System.out.println("Welcome to Memory Game");

int board_side;

//this loop obtains the board size, or more specifically
// the length of the side of the board



do{
System.out.println("\n For 2x2 board game press 2"+
"\n For 4x4 board game press 4"+
"\n For 6x6 board game press 6");
board_side=keyboard.nextInt();
}while(board_side!=2 && board_side!=4 && board_side!=6);


char[][] board = createBoard(board_side);

// a call the the shuffle method
shuffleBoard(board);

// a call to the game playing method
playGame(board);

}



// The following method should shuffle the input 2D array caled board
public static void shuffleBoard(char[][] board)


{

// This creates a 1D array whose size is equal to the size of the board

int N = board.length*board.length;
char[] board1D = new char[N];



// Testing to see the printed square


for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}
System.out.println();

for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}

System.out.println();



// Copy the elements of 2D array into that 1D array here

for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board1D [m] = board[i][j];
}
}
}






// Shuffle 1D array

// Shuffle the array

for (int i = 0; i < N; i++) {

// Generate an index randomly


int index = (int)(Math.random() * N);
char temp = board1D[i];
board1D[i] = board1D[index];
board1D[index] = temp;
}


//Testing to see the 1D array



System.out.println();

for (int i =0; i < board1D.length; i++) {
System.out.print(board1D[i] + " ");
}

System.out.println();



//Copy shuffled 1D array back into 2D array, i.e., back to the board


for (int m = 0; m < N; m++){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board.length; j++, m++){
board[i][j] = board1D [m];
}
}
}


//Testing to print the shuffled square



for ( int i = 0; i < board.length; i++) {
for ( int j = 0; j < board[i].length; j++) {
System.out.print( board[i][j] + " " );
}
System.out.println();
}

System.out.println();
System.out.println();


}








// game playing method



public static void playGame(char[][] board)
{
Scanner keyboard = new Scanner( System.in );

// this createst a 2D array indicating what locations are paired, i.e., discovered
// at the begining none are, so default initializaiton to false is ok

boolean[][]discovered=new boolean[board.length][board[0].length];;




for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");

for ( int j = 0; j < board[i].length; j++) {
System.out.print( "* " );
}


System.out.println();
}
System.out.print(" ");

for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}



System.out.println();
System.out.println();
System.out.println("Enter a pair of undiscovered distinct locations on the board that you want revealed. i.e., a pair of integers in the range [1, 2]");
System.out.println();


int FirstLocationX;
int FirstLocationY;
int SecondLocationX;
int SecondLocationY;


do {
System.out.println("Enter the first location: ");
FirstLocationX=keyboard.nextInt();
FirstLocationY=keyboard.nextInt();

if (FirstLocationX > board.length && FirstLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
} while(FirstLocationX > board.length && FirstLocationY > board.length);




System.out.println();

do {
System.out.println("Enter the second location: ");
SecondLocationX=keyboard.nextInt();
SecondLocationY=keyboard.nextInt();

if (SecondLocationX > board.length && SecondLocationY > board.length){
System.out.println("The location is invalid. It is outside of the board. ");
}
else if (SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY){
System.out.println("The location is invalid. The second location equal to the first. ");
}


} while(SecondLocationX > board.length && SecondLocationY > board.length && SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY);




//reveals the letters based on the coordinate user inputed

for ( int i = 0; i < board.length; i++) {
System.out.print(1 + i + " ");



for (int j = 0; j < board[i].length; j++) {

if (FirstLocationX == i+1 && FirstLocationY == j+1){
System.out.print( board[i][j] + " " );
}

else if (SecondLocationX == i+1 && SecondLocationY == j+1){
System.out.print( board[i][j] + " " );
}

/*This part is wrong, reveals the whole square instead of the found duplicates
else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}

else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}
*/


else {
System.out.print( "* " );


}

}
System.out.println();
}

System.out.print(" ");

for ( int x = 0; x < board.length; x++) {
System.out.print(1 + x + " ");
}

System.out.println();
System.out.println();

char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];




/*GETTING AN ERROR HERE when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/


if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}

waitForPlayer();


do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);



}




// createBoard method. DO NOT MODIFY!
/* this method, createBoard, creates the board filled with letters of alphabet,
where each letter appears exactly 2 times
e.g., for 4 x 4, the returned board would look like:
A B C D
E F G H
A B C D
E F G H */



public static char[][] createBoard(int side)
{
char[][] tmp = new char[side][side];
int letter_count=0;
for (int row = 0; row < tmp.length/2; row++){
for(int col = 0; col < tmp[row].length; col++)
{
tmp[row][col]=(char)('A'+letter_count);
tmp[row+tmp.length/2 ][col]=tmp[row][col];
letter_count++;
}
}
return tmp;
}





// waitForPlayer method. Do not modify!

public static void waitForPlayer()
{
System.out.print("Press enter to continue");
try {
System.in.read();
}
catch (IOException e){
System.out.println("Error reading from user");
}
}

}

当前错误的部分是:

char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
char[][] SecondInput = new char[SecondLocationX][SecondLocationY];




/*when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/


if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
discovered[0][0] = true;
discovered[0][2] = true;
}

waitForPlayer();


do {
playGame(board);
}while(discovered[0][0] == false && discovered[0][2] == false);



}

 else if (discovered[0][0] = true){
System.out.print( board[i][j] + " " );
}

else if (discovered[0][2] = true){
System.out.print( board[i][j] + " " );
}

最佳答案

我建议将每个方 block 作为一个对象,而不仅仅是一个字符。这样你就可以控制它们在显示时是否显示

编辑

如果没有对象,您只需创建另一个 boolean 数组来跟踪显示的内容。每当显示付款时,将该替代板内的值设置为 true,然后在显示函数中检查它是否为 true,然后再显示

关于java - 在 Java 内存游戏中,如何触发已发现的 2 个未知数,并让它们在游戏的其余部分保持显露出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com