我正在尝试编写一个名为Mancala的电路板代码,我想更改winner()函数。
如果一方在其任何一个坑中都没有任何一块石头,则游戏结束,然后确定获胜者是否PlayerOne的mancala比PlayerTwo更多的石头,反之亦然。
但是我不知道如何定位对象中的特定元素,所以我可以扩展win函数...任何帮助将不胜感激!
我想要达到的目标是:
if (sideOne.get(13).number > sideOne.get(14).number) {
return...
}
我知道上面的代码是不正确的,但我想要的目标是数组列表,该列表的特定索引和对象中的整数元素。
我已经包含了游戏板的构造函数和当前的winner()函数。
MancalaPit等级
public class MancalaPit {
String player;
Integer stones;
Boolean pit;
Integer number;
MancalaPit next;
public MancalaPit(String player, int stones, boolean pit, int number, MancalaPit next) {
this.player = player;
this.stones = stones;
this.pit = pit;
this.number = number;
this.next = next;
}
}
public class Board { // Creates the board
private MancalaPit a;
private MancalaPit b;
private MancalaPit c;
private MancalaPit d;
private MancalaPit e;
private MancalaPit f;
private MancalaPit mancalaOne;
private MancalaPit g;
private MancalaPit h;
private MancalaPit i;
private MancalaPit j;
private MancalaPit k;
private MancalaPit l;
private MancalaPit mancalaTwo;
private ArrayList<MancalaPit> sideOne;
private ArrayList<MancalaPit> sideTwo;
public Board() { // Constructs the bad boy
// Constructs the array lists
sideOne = new ArrayList<>();
sideTwo = new ArrayList<>();
// Constructs the pits and Mancala's
a = new MancalaPit("playerOne", 4, false, 1, null);
b = new MancalaPit("playerOne", 4, false, 2, null);
c = new MancalaPit("playerOne", 4, false, 3, null);
d = new MancalaPit("playerOne", 4, false, 4, null);
e = new MancalaPit("playerOne", 4, false, 5, null);
f = new MancalaPit("playerOne", 4, false, 6, null);
mancalaOne = new MancalaPit("playerOne", 0, true, 13, null);
g = new MancalaPit("playerTwo", 4, false, 7, null);
h = new MancalaPit("playerTwo", 4, false, 8, null);
i = new MancalaPit("playerTwo", 4, false, 9, null);
j = new MancalaPit("playerTwo", 4, false, 10, null);
k = new MancalaPit("playerTwo", 4, false, 11, null);
l = new MancalaPit("playerTwo", 4, false, 12, null);
mancalaTwo = new MancalaPit("playerTwo", 0, true, 14, null);
// Constructs the order of the pits
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = mancalaOne;
mancalaOne.next = g;
g.next = h;
h.next = i;
i.next = j;
j.next = k;
k.next = l;
l.next = mancalaTwo;
mancalaTwo.next = a;
// Constructs sides
sideOne.add(a);
sideOne.add(b);
sideOne.add(c);
sideOne.add(d);
sideOne.add(e);
sideOne.add(f);
sideTwo.add(g);
sideTwo.add(h);
sideTwo.add(i);
sideTwo.add(j);
sideTwo.add(k);
sideTwo.add(l);
}
public String winner() {
Boolean one = true;
Boolean two = true;
for(MancalaPit pit : sideOne) {
if(pit.stones == 0) {
continue;
} else {
one = false;
break;
}
}
if(one == true) {
return "Your boy player one is the champion";
}
for(MancalaPit pit : sideTwo) {
if(pit.stones == 0) {
continue;
} else {
two = false;
break;
}
}
if(two == true) {
return "Your boy player two is the champion";
}
return "No winners so far";
}
答案 0 :(得分:0)
我认为您根本不需要定位索引(特别是因为这些坑不在您创建的列表中),您已经定义了变量以比较获胜条件。你应该能够将mancalaOne.stones与mancalaTwo.stones进行比较,如果其中一个&#34;一个&#34;是真的还是&#34;两个&#34;是真的。 BTW一和二应该是布尔而不是布尔。
即。做两个for循环,然后做 if(one || two){ if(mancalaOne.stones&gt; mancalaTwo.stones){ 返回&#34;玩家1获胜!&#34; 如果...... }