如何使用二维数组在捉迷藏游戏中制作 Children "giggle"?
How to Make Children "giggle" in a Hide and Seek Game with 2D Arrays?
我不知道如何为我的捉迷藏游戏添加“咯咯笑”效果。如果玩家在任何直接 (up/down, left/right/diagonally) 的隐藏 child 的一个“点”内猜测,隐藏的 child 应该“咯咯笑”以提示播放器的输出可以是System.out.println("咯咯笑");。我正在为它背后的逻辑和编码而苦苦挣扎。我想这样做的原因是为了向玩游戏的人表明他们很亲密。如果您猜测 (0,0),则表明 child 接近。它可以在 (1,0)、(1,1) 或 (0,1) 中。 Supposed to look like.
public static int columns = 10;
public static int rows = 10;
public static String[][] map = new String[columns][rows];
public static int [][] missedGuesses = new int[columns][rows];
public static int children = 3;
public static final int maxAttempts = 3;
public static int attempts = 0;
public static int playerAttempts;
public static int childrenFound = 0;
public static void main(String[] args) {
//Welcome text
System.out.println("Welcome to Hide and Seek!");
System.out.println("You have to guess where all the children are. You have 10 atttempts and 3 children to find.");
System.out.println("");
// The phases of the game
createMap();
hidingChildren();
do {
guessPhase();
} while (childrenFound != children && ++ attempts < maxAttempts );
gameOver();
}
public static void createMap() {
// Creates the 10x10 Grid
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for(int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = " ";
if (j == 0)
System.out.print(i + "|" + map[i][j]);
else if (j == map[i].length - 1)
System.out.print(map[i][j] + "|" + i);
else
System.out.print(map[i][j]);
}
System.out.println();
}
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void updateMap() {
//Updates the 10x10 Grid
System.out.println();
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for(int x = 0; x < map.length; x++) {
System.out.print(x + "|");
for (int y = 0; y < map[x].length; y++){
System.out.print(map[x][y]);
}
System.out.println("|" + x);
}
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void hidingChildren() {
//Hides the children
System.out.println("Hiding the Children!");
for (int i = 1; i <= children;) {
int x = (int)(Math.random() * 10);
int y = (int)(Math.random() * 10);
if((x >= 0 && x < rows) && (y >= 0 && y < columns) && (map[x][y] == " "))
{
System.out.println(i + " - Finished Hiding Children!");
//To see the children insert map[x][y] = "x"; into the line below before the }
map[x][y] = "x";
}
i++;
}
}
public static void playerMove() {
//Player inserts x and y coordinates
int x = -1;
int y = -1;
do {
Scanner playerInput = new Scanner(System.in);
System.out.print("Choose a X coordinate: ");
x = playerInput.nextInt();
System.out.print("Choose a Y coordinate: ");
y = playerInput.nextInt();
// Player guesses
if ((x >= 0 && x < rows) && (y >= 0 && y < columns));
{
if (map[y][x] == "x")
{
System.out.println("You found a child!");
map[y][x] = "!";
++childrenFound;
attempts = -1;
} else if (map[x][y] == " ") {
System.out.println("Sorry, you looked in the wrong place!");
map[y][x] = "-";
} else if ((x < 0 || x >= rows) || (y < 0 || y >= columns))
System.out.println("You can't find children outside the " + rows + " by " + columns + " grid");
}
} while((x < 0 || x >= rows) || (y < 0 || y >= columns));
}
public static void amountGuesses () {
//To keep track of guesses
Scanner playerInput = new Scanner(System.in);
playerAttempts = playerInput.nextInt();
}
public static void guessPhase() {
// Guessing phase of the game
int result = maxAttempts - attempts;
System.out.println("Guesses left: " + result);
System.out.println("Children found: " + childrenFound);
playerMove();
amountGuesses();
updateMap();
}
public static void gameOver() {
//If one of these condition are reached, the game ends
if (childrenFound == 3) {
System.out.println("Winner!");
} else if (attempts == maxAttempts)
System.out.println("You lose. The amount of children left are: " + children);
}
}
我修复了一些错误并添加了咯咯笑功能。 isNear(int x, int y)
判断是否咯咯笑。笑声将在网格中显示为 ?
。
根据您的需要更新代码。
输出:
Guesses left: 1
Children found: 1
Choose a X coordinate: 5
Choose a Y coordinate: 1
Giggle!!
0123456789
0| ? |0
1| ? |1
2| |2
3| !|3
4| |4
5| |5
6| |6
7| |7
8| |8
9| |9
0123456789
代码:
public class Test {
public static int columns = 10;
public static int rows = 10;
public static String[][] map = new String[columns][rows];
public static int children = 3;
public static final int maxAttempts = 3;
public static int attempts = 0;
public static int playerAttempts;
public static int childrenFound = 0;
public static void main(String[] args) throws Exception {
// Welcome text
System.out.println("Welcome to Hide and Seek!");
System.out
.println("You have to guess where all the children are. You have 10 atttempts and 3 children to find.");
System.out.println("");
// The phases of the game
initializeMap();
printMap(false);
hidingChildren();
printMap(true); // comment this statement in production
do {
guessPhase();
} while (childrenFound != children && ++attempts < maxAttempts);
gameOver();
}
public static void initializeMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = " ";
}
}
}
public static void printMap(boolean showChildren) {
String data = "";
System.out.print(" ");
for (int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (showChildren)
data = map[i][j];
else
data = map[i][j].equals("x") ? " " : map[i][j];
if (j == 0)
System.out.print(i + "|" + data);
else if (j == map[i].length - 1)
System.out.print(data + "|" + i);
else
System.out.print(data);
}
System.out.println();
}
System.out.print(" ");
for (int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void hidingChildren() {
// Hides the children
System.out.println("Hiding the Children!");
for (int i = 1; i <= children;) {
int x = (int) (Math.random() * 10);
int y = (int) (Math.random() * 10);
if ((x >= 0 && x < rows) && (y >= 0 && y < columns) && (map[x][y] == " ")) {
System.out.println(i + " - Finished Hiding Children!");
map[x][y] = "x";
i++;
}
}
}
public static void playerMove() {
// Player inserts x and y coordinates
int x = -1;
int y = -1;
Scanner playerInput = new Scanner(System.in);
do {
System.out.print("Choose a X coordinate: ");
x = playerInput.nextInt();
System.out.print("Choose a Y coordinate: ");
y = playerInput.nextInt();
// Player guesses
if ((x >= 0 && x < rows) && (y >= 0 && y < columns))
{
if (map[y][x] == "x") {
System.out.println("You found a child!");
map[y][x] = "!";
++childrenFound;
} else if (map[y][x].equals(" ")) {
if (isNear(y, x)) {
System.out.println("Giggle!!");
map[y][x] = "?";
} else {
System.out.println("Sorry, you looked in the wrong place!");
map[y][x] = "-";
}
} else {
System.out.println("Enter new coordinates.");
}
} else {
System.out.println("You can't find children outside the " + rows + " by " + columns + " grid");
}
} while ((x < 0 || x > rows) || (y < 0 || y > columns));
}
public static void amountGuesses() {
// To keep track of guesses
// Scanner playerInput = new Scanner(System.in);
// playerAttempts = playerInput.nextInt();
playerAttempts++;
}
public static void guessPhase() {
// Guessing phase of the game
int result = maxAttempts - attempts;
System.out.println("Guesses left: " + result);
System.out.println("Children found: " + childrenFound);
playerMove();
amountGuesses();
printMap(false);
System.out.println("-----------------------------\n");
}
public static boolean isNear(int x, int y) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j].equals("x")) {
int xl = Math.abs(i - x);
int yl = Math.abs(j - y);
if ((xl == 1 && yl == 1) ||
(xl == 0 && yl == 1) ||
(xl == 1 && yl == 0)) {
return true;
}
}
}
}
return false;
}
public static void gameOver() {
// If one of these condition are reached, the game ends
if (childrenFound == 3) {
System.out.println("Winner!");
} else if (attempts == maxAttempts)
System.out.println("You lose. The amount of children left are: " + (children - childrenFound));
}
}
我不知道如何为我的捉迷藏游戏添加“咯咯笑”效果。如果玩家在任何直接 (up/down, left/right/diagonally) 的隐藏 child 的一个“点”内猜测,隐藏的 child 应该“咯咯笑”以提示播放器的输出可以是System.out.println("咯咯笑");。我正在为它背后的逻辑和编码而苦苦挣扎。我想这样做的原因是为了向玩游戏的人表明他们很亲密。如果您猜测 (0,0),则表明 child 接近。它可以在 (1,0)、(1,1) 或 (0,1) 中。 Supposed to look like.
public static int columns = 10;
public static int rows = 10;
public static String[][] map = new String[columns][rows];
public static int [][] missedGuesses = new int[columns][rows];
public static int children = 3;
public static final int maxAttempts = 3;
public static int attempts = 0;
public static int playerAttempts;
public static int childrenFound = 0;
public static void main(String[] args) {
//Welcome text
System.out.println("Welcome to Hide and Seek!");
System.out.println("You have to guess where all the children are. You have 10 atttempts and 3 children to find.");
System.out.println("");
// The phases of the game
createMap();
hidingChildren();
do {
guessPhase();
} while (childrenFound != children && ++ attempts < maxAttempts );
gameOver();
}
public static void createMap() {
// Creates the 10x10 Grid
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for(int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = " ";
if (j == 0)
System.out.print(i + "|" + map[i][j]);
else if (j == map[i].length - 1)
System.out.print(map[i][j] + "|" + i);
else
System.out.print(map[i][j]);
}
System.out.println();
}
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void updateMap() {
//Updates the 10x10 Grid
System.out.println();
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for(int x = 0; x < map.length; x++) {
System.out.print(x + "|");
for (int y = 0; y < map[x].length; y++){
System.out.print(map[x][y]);
}
System.out.println("|" + x);
}
System.out.print(" ");
for(int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void hidingChildren() {
//Hides the children
System.out.println("Hiding the Children!");
for (int i = 1; i <= children;) {
int x = (int)(Math.random() * 10);
int y = (int)(Math.random() * 10);
if((x >= 0 && x < rows) && (y >= 0 && y < columns) && (map[x][y] == " "))
{
System.out.println(i + " - Finished Hiding Children!");
//To see the children insert map[x][y] = "x"; into the line below before the }
map[x][y] = "x";
}
i++;
}
}
public static void playerMove() {
//Player inserts x and y coordinates
int x = -1;
int y = -1;
do {
Scanner playerInput = new Scanner(System.in);
System.out.print("Choose a X coordinate: ");
x = playerInput.nextInt();
System.out.print("Choose a Y coordinate: ");
y = playerInput.nextInt();
// Player guesses
if ((x >= 0 && x < rows) && (y >= 0 && y < columns));
{
if (map[y][x] == "x")
{
System.out.println("You found a child!");
map[y][x] = "!";
++childrenFound;
attempts = -1;
} else if (map[x][y] == " ") {
System.out.println("Sorry, you looked in the wrong place!");
map[y][x] = "-";
} else if ((x < 0 || x >= rows) || (y < 0 || y >= columns))
System.out.println("You can't find children outside the " + rows + " by " + columns + " grid");
}
} while((x < 0 || x >= rows) || (y < 0 || y >= columns));
}
public static void amountGuesses () {
//To keep track of guesses
Scanner playerInput = new Scanner(System.in);
playerAttempts = playerInput.nextInt();
}
public static void guessPhase() {
// Guessing phase of the game
int result = maxAttempts - attempts;
System.out.println("Guesses left: " + result);
System.out.println("Children found: " + childrenFound);
playerMove();
amountGuesses();
updateMap();
}
public static void gameOver() {
//If one of these condition are reached, the game ends
if (childrenFound == 3) {
System.out.println("Winner!");
} else if (attempts == maxAttempts)
System.out.println("You lose. The amount of children left are: " + children);
}
}
我修复了一些错误并添加了咯咯笑功能。 isNear(int x, int y)
判断是否咯咯笑。笑声将在网格中显示为 ?
。
根据您的需要更新代码。
输出:
Guesses left: 1
Children found: 1
Choose a X coordinate: 5
Choose a Y coordinate: 1
Giggle!!
0123456789
0| ? |0
1| ? |1
2| |2
3| !|3
4| |4
5| |5
6| |6
7| |7
8| |8
9| |9
0123456789
代码:
public class Test {
public static int columns = 10;
public static int rows = 10;
public static String[][] map = new String[columns][rows];
public static int children = 3;
public static final int maxAttempts = 3;
public static int attempts = 0;
public static int playerAttempts;
public static int childrenFound = 0;
public static void main(String[] args) throws Exception {
// Welcome text
System.out.println("Welcome to Hide and Seek!");
System.out
.println("You have to guess where all the children are. You have 10 atttempts and 3 children to find.");
System.out.println("");
// The phases of the game
initializeMap();
printMap(false);
hidingChildren();
printMap(true); // comment this statement in production
do {
guessPhase();
} while (childrenFound != children && ++attempts < maxAttempts);
gameOver();
}
public static void initializeMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = " ";
}
}
}
public static void printMap(boolean showChildren) {
String data = "";
System.out.print(" ");
for (int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (showChildren)
data = map[i][j];
else
data = map[i][j].equals("x") ? " " : map[i][j];
if (j == 0)
System.out.print(i + "|" + data);
else if (j == map[i].length - 1)
System.out.print(data + "|" + i);
else
System.out.print(data);
}
System.out.println();
}
System.out.print(" ");
for (int i = 0; i < columns; i++)
System.out.print(i);
System.out.println();
}
public static void hidingChildren() {
// Hides the children
System.out.println("Hiding the Children!");
for (int i = 1; i <= children;) {
int x = (int) (Math.random() * 10);
int y = (int) (Math.random() * 10);
if ((x >= 0 && x < rows) && (y >= 0 && y < columns) && (map[x][y] == " ")) {
System.out.println(i + " - Finished Hiding Children!");
map[x][y] = "x";
i++;
}
}
}
public static void playerMove() {
// Player inserts x and y coordinates
int x = -1;
int y = -1;
Scanner playerInput = new Scanner(System.in);
do {
System.out.print("Choose a X coordinate: ");
x = playerInput.nextInt();
System.out.print("Choose a Y coordinate: ");
y = playerInput.nextInt();
// Player guesses
if ((x >= 0 && x < rows) && (y >= 0 && y < columns))
{
if (map[y][x] == "x") {
System.out.println("You found a child!");
map[y][x] = "!";
++childrenFound;
} else if (map[y][x].equals(" ")) {
if (isNear(y, x)) {
System.out.println("Giggle!!");
map[y][x] = "?";
} else {
System.out.println("Sorry, you looked in the wrong place!");
map[y][x] = "-";
}
} else {
System.out.println("Enter new coordinates.");
}
} else {
System.out.println("You can't find children outside the " + rows + " by " + columns + " grid");
}
} while ((x < 0 || x > rows) || (y < 0 || y > columns));
}
public static void amountGuesses() {
// To keep track of guesses
// Scanner playerInput = new Scanner(System.in);
// playerAttempts = playerInput.nextInt();
playerAttempts++;
}
public static void guessPhase() {
// Guessing phase of the game
int result = maxAttempts - attempts;
System.out.println("Guesses left: " + result);
System.out.println("Children found: " + childrenFound);
playerMove();
amountGuesses();
printMap(false);
System.out.println("-----------------------------\n");
}
public static boolean isNear(int x, int y) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j].equals("x")) {
int xl = Math.abs(i - x);
int yl = Math.abs(j - y);
if ((xl == 1 && yl == 1) ||
(xl == 0 && yl == 1) ||
(xl == 1 && yl == 0)) {
return true;
}
}
}
}
return false;
}
public static void gameOver() {
// If one of these condition are reached, the game ends
if (childrenFound == 3) {
System.out.println("Winner!");
} else if (attempts == maxAttempts)
System.out.println("You lose. The amount of children left are: " + (children - childrenFound));
}
}