2

我正在玩的程序有一些问题。我正在尝试做这个弹珠游戏,玩家在电脑上玩并移除弹珠,直到棋盘为空。我的问题是我用来移除弹珠的课程似乎没有移除它。我认为这很简单,但我只是不明白。我认为我遇到问题的部分是大写字母:

Pile tiles = new Pile();
tiles.createMarbles();
marbles = tiles.getMarbles();
System.out.println("There are now " + marbles + " left!");

HumanPlayer hum = new HumanPlayer();
SmartMode cpu = new SmartMode();
StupidMode badcpu = new StupidMode();

//ComputerMove cpu = new ComputerMove();
//PlayerMove hum = new PlayerMove();
do {
    if (compMove == true && marbles > 1) {
        if (compSmart) {
            //marbles = cpu.move(marbles);
            tiles.removesMarbles(cpu.move(marbles)); //THIS RIGHT HERE
            System.out.println("There are now " + marbles + " left!");
            compMove = false;
        }  if (!compSmart) {
            //marbles = badcpu.move(marbles);
            tiles.removesMarbles(badcpu.move(marbles)); // THIS RIGHT HERE
            System.out.println("There are now " + marbles + " left!");
            compMove = false;

        }
        /* marbles = (cpu.ComputerMove(marbles, compSmart));
         System.out.println("There are now " + marbles + " left!");
         compMove = false;*/
    } else if (compMove == false && marbles > 1) {
        //marbles = (hum.move(marbles));
        tiles.removesMarbles(hum.move(marbles)); // THIS RIGHT HERE
        System.out.println("There are now " + marbles + " left!");
        compMove = true;
    }

} while (marbles > 1);

其余的不需要回答

4

1 回答 1

0

条件while (marbles > 1)不会改变,因为marbles没有在循环中分配。

采用:

while (tiles.getMarbles() > 1)

您使用以下构造:

do {
  if (... && marbles > 1 ) {}
  ...
} while(marbles > 1)

可能这也是一样的:

while (tiles.getMarbles() > 0 ) {
  // additional condition on marbles not required.
  if (...) {}
}

interface Movable {
   public int move();
}

class SmartPlayer implements Movable {
   public int move() {...}
}

Movable m = new SmartPlayer();
m.move();
于 2012-10-13T20:53:12.967 回答