Browse Source

Added neccesary methods for BattleshipMain

Home stretch I think I might be done
master
school 7 years ago
parent
commit
af2a62a7af
  1. 54
      Battleship.java
  2. 2
      BattleshipMain.java
  3. 4
      GameBoard.java
  4. 4
      Ship.java
  5. 21
      ShipTest.java

54
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);
}
}
*/
}

2
BattleshipMain.java

@ -191,4 +191,4 @@ public class BattleshipMain extends Application
{
Application.launch(args);
}
}
}

4
GameBoard.java

@ -1,4 +0,0 @@
public class GameBoard{
private int xDim;
private int yDim;
private Battleship[5] ships;

4
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;
}
}
}

21
ShipTest.java

@ -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();
}
}
}
Loading…
Cancel
Save