Reputation: 5817
I am trying to create a win condition for my text game. I have two methods that decide whether a player has established criteria for a win or loss. These methods are in one class, while I want to use them in my controller class. The variables are hitTimes and 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;
}
class2:
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!");
}
}
}
Upvotes: 0
Views: 99
Reputation: 6577
You could make boolean attributes gameWon and gameLost, and put them both at false initial value. Then if conditions are fulfilled you turn one of them into true depending on the case of course. Make also getter methods in your class so you can access to them from another class.
Place this in your second method:
private boolean gameWon = false;
private boolean gameLost = false;
public boolean getGameWon() {
return gameWon;
}
public boolean getGameLost() {
return gameLost;
}
Change if condition to also have:
if(nonHits == ITERATIONS)
{
gameWon = true;
}
if(hitTimes == 1)
{
gameLost = true;
}
In your other class create this class and access your gameWon
/gameLost
values through getters.
SecondClass sc = new SecondClass();
boolean isGameWon = sc.gameWon();
boolean isGameLost = sc.gameLost();
Hopefully I gave you an idea.. I can't see your whole code so this is just an assumption I made of what's bothering you. Cheers!
Upvotes: 1