我的任务:
Stix 的游戏——类似于前段时间在“泰国幸存者”中玩的游戏——在简化版本中看起来像这样:
它由两个玩家玩(在幸存者中超过两个,但这里我们只处理两个)。许多棍子(如火柴)放在桌子上。第一个玩家拿走 1、2 或 3 根棍子,前提是桌子上有那么多棍子。然后第二个玩家拿走 1、2 或 3 根棍子(如果可能的话),依此类推。谁拿了最后一根棍子,谁就输了。
这是我的课:
public class StixBoard
{
public int number;
public StixBoard(int number)
{
number = number;
}
public int getNumStix()
{
return number;
}
public boolean takeStix(int number)
{
int take = 1;
while(take <= getNumStix())
{
takeStix(take);
take++;
}
if(number >= 1 && number <= 3)
{
number = number - this.number;
System.out.println("Number of sticks on board:" + number);
return(true);
}
else
System.out.println("Illegeal Move");
return(false);
}
public boolean isGameOver()
{
if(number >=1)
{
return(true);
}
else
return false;
}
public String toString()
{
return(getNumStix() + " Stix Remaining.");
}
}
This is my tester:
public class StixGame
{
public static void main(String[] args)
{
StixBoard game1 = new StixBoard(6);
System.out.println(game1.getNumStix());
}
}
Can someone tell my why game1 only returns 0?
*UPDATE*
Now that it constantly displays:
6
Illegeal Move
false
6
I've been playing around with it but can't figure out why =/
Program now looks like this:
public class StixBoard
{
public int number;
public StixBoard(int number)
{
this.number = number;
}
public int getNumStix()
{
return number;
}
public boolean takeStix(int number)
{
int take = 0;
while(take != number && number <= 3 && number > 0)
{
number = this.number - take;
take++;
}
if(this.number >= 1 && this.number <= 3)
{
number = number - this.number;
System.out.println("Number of sticks on board:" + number);
return(true);
}
else
System.out.println("Illegeal Move");
return(false);
}
public boolean isGameOver()
{
if(number >=1)
{
return(true);
}
else
return false;
}
public String toString()
{
return(getNumStix() + " Stix Remaining.");
}
}
这是我的测试仪:
public class StixGame
{
public static void main(String[] args)
{
StixBoard game1 = new StixBoard(6);
System.out.println(game1.getNumStix());
System.out.println(game1.takeStix(3));
System.out.println(game1.getNumStix());
}
}