From af2a62a7afb56dd7b05eda7f50412798e4e66319 Mon Sep 17 00:00:00 2001 From: school Date: Fri, 5 Oct 2018 18:33:21 -0500 Subject: [PATCH] Added neccesary methods for BattleshipMain Home stretch I think I might be done --- Battleship.java | 54 ++++++++++++++++++++++++++++++++++++++++----- BattleshipMain.java | 2 +- GameBoard.java | 4 ---- Ship.java | 4 ++-- ShipTest.java | 21 ------------------ 5 files changed, 52 insertions(+), 33 deletions(-) delete mode 100644 GameBoard.java delete mode 100644 ShipTest.java diff --git a/Battleship.java b/Battleship.java index 82b686f..969d306 100644 --- a/Battleship.java +++ b/Battleship.java @@ -12,11 +12,13 @@ public class Battleship{ private int[][] computerBoardDisplay; private int[][] computerBoardHidden; private static int playerHits; - private static int ComputerHits; + private static int computerHits; public Battleship(int x, int y){ this.playerBoard = new int[x][y]; - this.computerBoardDisplay = new int[x][y]; + randomizingBoard(this.playerBoard); this.computerBoardHidden = new int[x][y]; + randomizingBoard(this.computerBoardHidden); + this.computerBoardDisplay = new int[x][y]; } public int[][] getComputerBoardHidden(){ @@ -31,6 +33,47 @@ public class Battleship{ return this.playerBoard; } + public boolean checkPlayerWin() { + return this.playerHits == 17; + } + + public boolean checkComputerWin() { + // 2 + 3 + 3 + 4 + 5 = 17 + return this.computerHits == 17; + } + + public void computerTurn() { + int x; + int y; + boolean done = false; + while (!done) { + x = Ship.randint(0,this.playerBoard[0].length-1); + y = Ship.randint(0,this.playerBoard.length-1); + if (playerBoard[x][y] == 3 || playerBoard[x][y] == 0) { + done = true; + if (playerBoard[x][y] == 3) { + playerBoard[x][y] = 2; + computerHits += 1; + } + else { + playerBoard[x][y] = 1; + } + } + } + } + + public void updateComputerBoards(int x, int y,int[][] computerActual,int[][] computerDisplay) { + boolean hit = computerActual[x][y] == 3; + if (hit) { + computerDisplay[x][y] = 2; + computerActual[x][y] = 2; + playerHits += 1; + } + else { + computerDisplay[x][y] = 1; + computerActual[x][y] = 1; + } + } public static void randomizingBoard(int[][] board){ Scanner input = new Scanner(System.in); int[] boundTL = {0,0}; @@ -63,7 +106,6 @@ public class Battleship{ // we need to clone since start is modified in place int[] stationary = cur_ship.getStart().clone(); while ((rot < 4) && !placed) { - cur_ship.print(); // checks if ship is out of bounds out_of_bounds = !cur_ship.isInside(boundTL,boundBR); if (!out_of_bounds) { @@ -119,12 +161,14 @@ public class Battleship{ System.out.println(s + "\n"); } } - public static void main(String[] args){ + /* + public static void main(String[] args){ int[][] testBoard = new int[10][10]; randomizingBoard(testBoard); printBoard(testBoard); //printBoard(testBoard); - } + } + */ } diff --git a/BattleshipMain.java b/BattleshipMain.java index 5bda322..5fb25bb 100644 --- a/BattleshipMain.java +++ b/BattleshipMain.java @@ -191,4 +191,4 @@ public class BattleshipMain extends Application { Application.launch(args); } -} \ No newline at end of file +} diff --git a/GameBoard.java b/GameBoard.java deleted file mode 100644 index 715a916..0000000 --- a/GameBoard.java +++ /dev/null @@ -1,4 +0,0 @@ -public class GameBoard{ - private int xDim; - private int yDim; - private Battleship[5] ships; \ No newline at end of file diff --git a/Ship.java b/Ship.java index c993749..35007b1 100644 --- a/Ship.java +++ b/Ship.java @@ -180,7 +180,7 @@ public class Ship{ board[c][x_col] = this.length; } else { - board[c][x_col] = 4; + board[c][x_col] = 3; } } } @@ -190,7 +190,7 @@ public class Ship{ board[y_col][c] = this.length; } else { - board[y_col][c] = 4; + board[y_col][c] = 3; } } } diff --git a/ShipTest.java b/ShipTest.java deleted file mode 100644 index 504ae41..0000000 --- a/ShipTest.java +++ /dev/null @@ -1,21 +0,0 @@ -import java.util.Scanner; -public class ShipTest{ - public static void createTestShips(int[] start,int[] end, Ship[] fill){ - for (int i = 0; i < fill.length; i++){ - fill[i] = Ship.randomShip(start,end,Ship.randint(2,4),true); - } - } - - public static void main(String[] args){ - Ship[] test_ships = new Ship[5]; - createTestShips(new int[] {0,0}, new int[] {5,5},test_ships); - for (int i = 0; i < test_ships.length-1;i++){ - test_ships[i].print(); - System.out.println(); - test_ships[i+1].print(); - - System.out.println(test_ships[i].isIntersecting(test_ships[i+1])); - System.out.println(); - } - } -} \ No newline at end of file