Java 变量hp未在20x20网格中更新
我目前正在开发一个20x20 2d阵列,用户可以在其中输入WASD来控制他们在网格上的移动。我想让他们的网格中的某些元素可以影响他们的hp,但是hp不会在我的网格上更新。请帮我看看出了什么问题 下面的代码将为一个数组设置固定数量的特定字符,并用E填充其余字符。将有一个人站在一个随机位置,该位置用空格表示。然后,用户必须输入W、A、S或D来控制人员的移动。当这个人沿着网格移动时,他将与他所站的元素交互,因此他的生命将被更新,然后他所站的角色将被替换为空白Java 变量hp未在20x20网格中更新,java,Java,我目前正在开发一个20x20 2d阵列,用户可以在其中输入WASD来控制他们在网格上的移动。我想让他们的网格中的某些元素可以影响他们的hp,但是hp不会在我的网格上更新。请帮我看看出了什么问题 下面的代码将为一个数组设置固定数量的特定字符,并用E填充其余字符。将有一个人站在一个随机位置,该位置用空格表示。然后,用户必须输入W、A、S或D来控制人员的移动。当这个人沿着网格移动时,他将与他所站的元素交互,因此他的生命将被更新,然后他所站的角色将被替换为空白 package quest; import
package quest;
import java.util.*;
public class Quest
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
char[][] board = new char[20][20];
int [] current = new int[2];
int hp = 100;
int food = 0;
int weapon = 0;
int health = 0;
int threats = 0;
int turns = (int)(Math.random() * 76) + 25;
int turnsCount = turns;
System.out.println(turns);
for (int i = 0; i < 10; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'F';
}
for (int i = 0; i < 10; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'W';
}
for (int i = 0; i < 10; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'M';
}
for (int i = 0; i < 5; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'G';
}
for (int i = 0; i < 5; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'S';
}
for (int i = 0; i < 10; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'K';
}
for (int i = 0; i < 10; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'C';
}
for (int i = 0; i < 1; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = 'P';
}
for (int i = 0; i < 1; i++)
{
int x = (int)(Math.random() * 20);
int y = (int)(Math.random() * 20);
while (board[x][y] != '\u0000')
{
x = (int)(Math.random() * 20);
y = (int)(Math.random() * 20);
}
board[x][y] = ' ';
current[0] = x;
current[1] = y;
}
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
board[x][y] = intializeGameBoard(board[x][y]);
System.out.print(board[x][y]+" ");
}
System.out.println();
}
for (int j = 0; j < turns; j++)
{
char move =sc.next().charAt(0);
int finalMove[] = makeAMove(move);
current[0] = current[0]+finalMove[1];
current[1] = current[1]+finalMove[0];
int newhp = updateHealth(board[0][1], hp);
board[current[0]][current[1]] = ' ';
turnsCount = turnsCount - 1;
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
board[x][y] = intializeGameBoard(board[x][y]);
System.out.print(board[x][y]+" ");
}
System.out.println();
}
System.out.println("Turns left: " + turnsCount);
System.out.println("Health: " + newhp);
}
}
public static char intializeGameBoard(char element)
{
if (element == '\u0000')
{
element = 'E';
}
return element;
}
public static int[] makeAMove(char move)
{
int x = 0;
int y = 0;
if (move == 'w')
{
y = y - 1;
}
if (move == 'a')
{
x = x - 1;
}
if (move == 's')
{
y = y + 1;
}
if (move == 'd')
{
x = x + 1;
}
int [] finalMove = new int [2];
finalMove[0] = x;
finalMove[1] = y;
return finalMove;
}
public static int updateHealth(char element, int hp)
{
int newhp;
if (element == 'F')
{
newhp = hp+5;
}
if (element == 'M')
{
newhp = hp+10;
}
if (element == 'G')
{
newhp = 0;
}
if (element == 'S')
{
newhp = hp-3;
}
if (element == 'K')
{
newhp = hp-5;
}
if (element == 'C')
{
newhp = hp-5;
}
else
{
newhp = hp;
}
return newhp;
}
}
packagequest;
导入java.util.*;
公开课探索
{
公共静态void main(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
字符[][]板=新字符[20][20];
int[]当前=新的int[2];
int hp=100;
int食品=0;
int武器=0;
int-health=0;
int威胁=0;
整数圈=(整数)(Math.random()*76)+25;
int TURNSCONT=匝数;
系统输出打印项次(圈数);
对于(int i=0;i<10;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
板[x][y]=“F”;
}
对于(int i=0;i<10;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
板[x][y]=“W”;
}
对于(int i=0;i<10;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
董事会[x][y]=“M”;
}
对于(int i=0;i<5;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
董事会[x][y]=“G”;
}
对于(int i=0;i<5;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
板[x][y]=“S”;
}
对于(int i=0;i<10;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
板[x][y]=“K”;
}
对于(int i=0;i<10;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
板[x][y]=“C”;
}
对于(int i=0;i<1;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
董事会[x][y]=“P”;
}
对于(int i=0;i<1;i++)
{
int x=(int)(Math.random()*20);
int y=(int)(Math.random()*20);
而(板[x][y]!='\u0000')
{
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
}
董事会[x][y]='';
电流[0]=x;
电流[1]=y;
}
对于(int x=0;x<20;x++)
{
对于(int y=0;y<20;y++)
{
董事会[x][y]=初始董事会(董事会[x][y]);
系统输出打印(单板[x][y]+”);
}
System.out.println();
}
对于(int j=0;jpublic static int updateHealth(char element, int hp)
{
int newhp;
if (element == 'F')
{
newhp = hp+5;
}
else if (element == 'M')
{
newhp = hp+10;
}
else if (element == 'G')
{
newhp = 0;
}
else if (element == 'S')
{
newhp = hp-3;
}
else if (element == 'K')
{
newhp = hp-5;
}
else if (element == 'C')
{
newhp = hp-5;
}
else
{
newhp = hp;
}
return newhp;
}
public static int updateHealth(char element, int hp)
{
int newhp;
switch(element){
case(F):
newhp=hp+5
break;
case(M):
newhp=hp+10
break;
case(G):
newhp=0
break;
case(S):
newhp=hp-3
break;
case(K):
newhp=hp-5
break;
case(C):
newhp=hp-5
break;
default:
newhp = hp;
}
return newhp;
}
if (element == 'S')
{
newhp = hp-3;
}
if (element == 'K')
{
newhp = hp-5;
}
if (element == 'C')
{
newhp = hp-5;
}
else
{
newhp = hp;
}
updateHealth(board[0][1], hp);
updateHealth(board[current[0]][current[1]], hp);
int newhp = updateHealth(..., hp);
int y = 0;
if (move == 'w') {
y = y - 1;
}
int y = 0;
if (move == 'w') {
y--; // or y = -1;
}
public static int[] makeAMove(char move) {
int[] finalMove = new int[2];
if (move == 'w') {
finalMove[1] = -1;
}
else if (move == 'a') {
finalMove[0] = -1;
}
else if (move == 's') {
finalMove[1] = 1;
}
else if (move == 'd') {
finalMove[0] = 1;
}
return finalMove;
}