我正在尝试为我的文字游戏创造一个胜利条件。我有两种方法可以决定球员是否已经建立了赢或输的标准。这些方法在一个类中,而我想在我的控制器类中使用它们。变量是hitTimes和nonHits:
的Class1:
if(choiceC > 0 || choiceR > 0)
{
Character currentCharacter;
currentCharacter= grid[choiceR][choiceC];
gameBoard[choiceR][choiceC] = currentCharacter;
loseTest(currentCharacter);
winTest(currentCharacter);
}
}
}
}
public int loseTest(Character currentCharacter)
{
int hitTimes = 0;
if(currentCharacter.minePresent == true)
{
hitTimes = 1;
}
return hitTimes;
}
public int winTest(Character currentCharacter)
{
int nonHits = 0;
if(currentCharacter.minePresent == false)
{
nonHits++;
}
return nonHits;
}
等级2:
Grid grid = new Grid();
double notMines = grid.notMine;
View view = new View();
result = grid.toString();
view.display(result);
final int ITERATIONS = 13;
final int numGames = 1000;
for (int i=0;i <= numGames; i++)
{
while (hitTimes != 1 || nonHits != notMines )
{
grid.runGame();
result2 = grid.toString();
view.display(result2);
if(nonHits == ITERATIONS)
{
System.out.println("You Win!");
}
if(hitTimes == 1)
{
System.out.println("You Lose!");
}
}
}
答案 0 :(得分:1)
您可以将博客属性设置为gameWon和gameLost,并将它们都设置为false初始值。然后,如果满足条件,则根据具体情况将其中一个变为真。在类中制作getter方法,以便从其他类访问它们。
将其放在第二种方法中:
private boolean gameWon = false;
private boolean gameLost = false;
public boolean getGameWon() {
return gameWon;
}
public boolean getGameLost() {
return gameLost;
}
如果条件也改变:
if(nonHits == ITERATIONS)
{
gameWon = true;
}
if(hitTimes == 1)
{
gameLost = true;
}
在您的其他课程中创建此课程,并通过获取者访问您的gameWon
/ gameLost
值。
SecondClass sc = new SecondClass();
boolean isGameWon = sc.gameWon();
boolean isGameLost = sc.gameLost();
希望我给了你一个想法..我看不到你的整个代码所以这只是我做了什么困扰你的假设。干杯!