gpt4 book ai didi

java - 我如何将数组的值设置为我想通过命令调用的变量的名称(在java中)?

转载 作者:行者123 更新时间:2023-12-01 11:51:58 25 4
gpt4 key购买 nike

简介:

我正在制作一个小型国际象棋游戏,我使用图片来代表所有棋子,我的所有棋子都由将它们绘制在边框上的函数控制。所有棋子的位置都记录在一个数组中,但现在我想使用数组中记录的内容来选择棋盘上的哪个图 block 上需要什么图像,到目前为止,我已经得到了一个 99% 的工作脚本,只有一个问题在其中。

主要问题:

java 不会排除我的数组中的内容来选择我之前加载的正确图片:

void tekenstukken(){
for(int x = 0 ; x < 8 ; x++){
for(int y = 0 ; y < 8 ; y++){

image(schaakstukken[y][x], x*blokgroote, y*blokgroote, blokgroote, blokgroote);
// -------- blokgroote is dutch for blocksize --------
} } }

在此代码中,唯一不起作用的是 schaakstukken[y][x] 它保存了我之前创建的 IPmage 变量的名称:

String[][] schaakstukken = new String[][]{      //schaakstukken means chesspieces in dutch
{"Tb","Hb","Rb","Qb","Kb","Rb","Hb","Tb"}, //Tb is my name for the Tower on the Black side, the other names are simulair
{"Pb","Pb","Pb","Pb","Pb","Pb","Pb","Pb"},
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{"Pw","Pw","Pw","Pw","Pw","Pw","Pw","Pw"},
{"Tw","Hw","Rw","Qw","Kw","Rw","Hw","Tw"}
};

这是我的完整代码:

String[][] schaakstukken = new String[][]{
{"Tb","Hb","Rb","Qb","Kb","Rb","Hb","Tb"},
{"Pb","Pb","Pb","Pb","Pb","Pb","Pb","Pb"},
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{ "" , "" , "" , "" , "" , "" , "" , "" },
{"Pw","Pw","Pw","Pw","Pw","Pw","Pw","Pw"},
{"Tw","Hw","Rw","Qw","Kw","Rw","Hw","Tw"}
};
int breedte = 800;
int hoogte = breedte;
int blokgroote = breedte/8;
int blokkleur1 = #ffffff;
int blokkleur2 = #999999;
PImage Tb;
PImage Hb;
PImage Rb;
PImage Qb;
PImage Kb;
PImage Pb;
PImage Tw;
PImage Hw;
PImage Rw;
PImage Qw;
PImage Kw;
PImage Pw;


void setup(){
size(breedte, hoogte);
noStroke();
Tb = loadImage("Tb.png");
Hb = loadImage("Hb.png");
Rb = loadImage("Rb.png");
Qb = loadImage("Qb.png");
Kb = loadImage("Kb.png");
Pb = loadImage("Pb.png");
Tw = loadImage("Tw.png");
Hw = loadImage("Hw.png");
Rw = loadImage("Rw.png");
Qw = loadImage("Qw.png");
Kw = loadImage("Kw.png");
Pw = loadImage("Pw.png");
tekenbord();
tekenstukken();
}

void draw(){

}

void tekenbord(){
for(int x = 0 ; x < 8 ; x++){
for(int y = 0 ; y < 8 ; y++){
if((x+y)%2 == 0){
fill(blokkleur1);
}else{
fill(blokkleur2);
}
rect(x*blokgroote, y*blokgroote, blokgroote, blokgroote);
} } }

void tekenstukken(){
for(int x = 0 ; x < 8 ; x++){
for(int y = 0 ; y < 8 ; y++){
image(schaakstukken[y][x], x*blokgroote, y*blokgroote, blokgroote, blokgroote);
} } }

当我禁用 tekenstukken() 时,我的代码运行良好并绘制一个空棋盘。如果我将 schaakstukken[y][x] 替换为 Tb 或其他名称之一,我会得到一个充满一种棋子的棋盘。

注意:我的代码尚未完成,稍后我将添加一个函数,根据玩家在棋盘上的更改来更改控制棋子的数组。

最佳答案

我不确定这对于编写国际象棋程序是否真的有必要;)但是您可以通过使用 Java's Reflection API 来实现这一点:

Class chessClass = ChessClass.class;
Field[] fields= chessClass.getFields();

for (Field f : fields) {
// ... assign name to array: array[...] = f.getName();
}

更多信息请查看ORACLEs Java Reflection tutorial

关于java - 我如何将数组的值设置为我想通过命令调用的变量的名称(在java中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28730301/

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