我希望得到一些帮助。我有一个战舰的java游戏,想让玩家在完成后再次玩游戏,但不知道怎么做。任何帮助都会很棒!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text,@"cellText", nil]
[[NSNotificationCenter defaultCenter]postNotificationName:@"PassTextBackToViewController" object:dictionary];
[self.navigationController popViewControllerAnimated:YES];
}
这是我的主要方法。任何批评都会很棒!
答案 0 :(得分:0)
首先,在do-while
方法或其他方法中,将(几乎)置于上述代码的第一部分(直到introPhase()
语句)。
其次,将do-while
放入第二个gamePhase()
方法或其他内容。
最后,用某种条件循环语句包围它们。
你应该得到这样的东西:
boolean finishFlag = false;
while(!finishFlag) {
introPhase();
gamePhase();
finishFlag = getExitInfoFromUser();
}
祝你好运!
答案 1 :(得分:0)
下面是另一个更抽象,更简单的例子。
定义类,方法和变量,以便更轻松地处理您正在编程的事物。
// Game class, here you define what the game would look like
public class Game {
private Scanner scanner = new Scanner(System.in);
// Could probably create a class Board and work on this class.
// private Board board;
private int[][] board;
// Could probably create a class Ship and work on this class.
// Could also include the two dimensional array in the Board class
// private Ship[][] ships board;
private int[][] ships;
// Could probably create a class Player and work on this class.
// private Player board;
private int attemtps;
private int health;
// Define a status the game is currently in
private enum Status {
IDLE, STARTED, ENDED;
}
// Variable for the current game status
private Status gamestatus;
// Nothing done, default is idle when new game class is initialized
// Maybe you have options and stuff and a menu, where you IDLE at.
public Game() {
gamestatus = Status.IDLE;
}
// This method "starts" the game, though defines variables to
// have Game beeing started
public void start() {
System.out.println("Welcome to battleship");
// Change status, maybe you want to validate the current gamestatus
// at some point
gamestatus = Status.STARTED;
// this method could initialize some variables and stuff to default values in order to represent a fresh game
setInitialValuesOfGame();
// Random loop that gets 5 inputs and emulates a game due to this.
int i = 0;
while(gamestatus.equals(Status.STARTED)) {
System.out.println("Test input: ");
String input = scanner.nextLine();
// Just make it look like there would happen something
board[i%5][0] = i;
ships[i%3][0] = i;
// Make game look like it did end after a few iteration
++i;
if(i == 5) {
end();
}
}
// Loop is over, ask for restart
restart();
}
public void restart() {
System.out.println("Hey would you like to restart: yes"); // Making it look like input
String input = "yes"; // Input here, but make it a literal for showing purposes
if (input.equals("yes")) {
start();
} else {
System.out.println("bye bye");
}
}
public void end() {
gamestatus = Status.ENDED;
// Do more stuff that would make the game end
}
// Setting default values
private void setInitialValuesOfGame() {
board = new int[5][5];
ships = new int[3][2];
attemtps = 0;
health = 3;
}
// This is just your starting point, i´d programm as less as possible in here.
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}