Managing turns in java battleships

Discussion in 'Java Forums' started by Dakota Joe, Apr 10, 2013.

  1. Dakota Joe

    Dakota Joe New Member

    Basically I am trying to make a Battleships game in java. The final aim is to implement a GUI version (MVC) but at present I am trying to get the model of the game working using the console output. I have also created a simple game engine to manage the game. I have got the majority of the game working in the sense that the game will automatically place ships (random) for both players, and the user can enter co-ordinates to hit for each player (turn by turn).The problem I have is that when a player specifies a co-ordinate that they have already hit (be it a miss or a hit on a battleship), it specifies the correct message to the console (You have already hit here, TRY AGAIN) but it passes the turn to the other player without allowing the player to specify another set of co-ordinates to hit. I am not really sure how to get around this. In the GUI version, I would imagine that this is not incredibly important as when a shot has been fired, the JButton on the grid will just disable so it is unclickable???I have included code snippets for my PlayingBoard class:\[code\]public int shootAtShip(int x, int y) {//0 = empty, not hit//10 = not empty, missed//11 = hit on a battleship //Check if co-ordinates are out of bounds if(x >= playingStatus.length || x < 0 || y >= playingStatus[0].length || y < 0) { System.out.println("Your specified move was invalid"); } //If grid location was empty, player has missed if(playingStatus[x][y] == 0) { playingStatus[x][y] = 10; System.out.println("You missed..."); return playingStatus[x][y]; } //If grid location has already been hit if(playingStatus[x][y] == 10 || playingStatus[x][y] == 11) { System.out.println("You have already shot here! Try again..."); } //Hit in a field with Aircraft Carrier if(playingStatus[x][y] == 1){ playingStatus[x][y] = 11; this.carrierHit++; System.out.println("You hit an Aircraft Carrier"); if(this.carrierHit == 5) { System.out.println("You destroyed the Aircraft Carrier"); fleet.remove(carrier); } } //Hit in a field with Battleship if(playingStatus[x][y] == 2) { playingStatus[x][y] = 11; this.battleshipHit++; System.out.println("You hit a Battleship"); if(this.battleshipHit == 4) { System.out.println("You destroyed the Battleship"); fleet.remove(battleship); } } //Hit in a field with 1st Destroyer if(playingStatus[x][y] == 3) { playingStatus[x][y] = 11; this.destroyerHit++; System.out.println("You hit Destroyer #1"); if(this.destroyerHit == 3) { System.out.println("You destroyed the #1 Destroyer"); fleet.remove(destroyer); } } //Hit in a field with 2nd Destroyer if(playingStatus[x][y] == 4) { playingStatus[x][y] = 11; this.destroyer2Hit++; System.out.println("You hit Destroyer #2"); if(this.destroyer2Hit == 3) { System.out.println("You destroyed the #2 Destroyer"); fleet.remove(destroyer2); } } //Hit in a field with Patrol Boat if(playingStatus[x][y] == 5) { playingStatus[x][y] = 11; this.patrolHit++; System.out.println("You hit a patrol boat"); if(this.patrolHit == 2) { System.out.println("You destroyed the Patrol Boat"); fleet.remove(patrol); } } return playingStatus[x][y]; }\[/code\]Game engine:\[code\]public void play() { while(player1.board.isGameOver() == false || player2.board.isGameOver() == false) { System.out.println("Player 1 ----------------------------"); player1.board.printBoard(); player1.board.printFleet(); System.out.println("Player2 -----------------------------"); player2.board.printBoard(); player2.board.printFleet(); System.out.println(currentTurn + " - It is this players turn"); System.out.println("Please enter target co-ordinates: "); String move = userinput.nextLine(); int movex, movey; movex = -2; movey = -2; StringTokenizer tokenizer = new StringTokenizer(move, ","); if(tokenizer.countTokens() == 2) { movex = Integer.parseInt(tokenizer.nextToken()); movey = Integer.parseInt(tokenizer.nextToken()); } if(currentTurn == "player1") { player2.board.shootAtShip(movex, movey); } if(currentTurn == "player2") { player1.board.shootAtShip(movex, movey); } if(player1.board.isGameOver() == true) { System.out.println("Player2 has won - Sorry"); break; } if(player2.board.isGameOver() == true) { System.out.println("Player1 has won - Sorry"); break; } nextTurn(); }}public void nextTurn() { if(this.currentTurn == "player2") { currentTurn = "player1"; } else { currentTurn = "player2"; }}\[/code\]Any help would be greatly appreciated,Thanks
     

Share This Page