2014-02-12 150 views
0

我正在尝试为我的文本游戏创建一个赢条件。我有两种方法可以决定一个球员是否建立了胜利或失败的标准。这些方法在一个类中,而我想在我的控制器类中使用它们。该变量是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!"); 
      } 


     } 
    } 
+1

什么问题? –

+0

你想要使用什么变量?什么是第一类名字? – user2277872

回答

1

你可以做布尔属性gameWon和gameLost,并把它们无论是在错误的初始值。然后,如果条件满足,当然根据情况将其中一个变为真。也可以在你的班级中设置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(); 

希望我给你出个主意。我看不到你的整个代码,所以这只是一个假设,我做了什么困扰你的。干杯!

相关问题