Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Battleship/bin/BoatTypes/Boats.class
Binary file not shown.
Binary file modified Battleship/bin/Main.class
Binary file not shown.
Binary file modified Battleship/bin/Minimax.class
Binary file not shown.
Binary file modified Battleship/bin/Player.class
Binary file not shown.
Binary file modified Battleship/bin/TicTacToe.class
Binary file not shown.
Binary file modified Battleship/bin/TicTacToeBoard.class
Binary file not shown.
Binary file not shown.
Binary file added Battleship/bin/TicTacToeMainFrame.class
Binary file not shown.
Binary file added Battleship/bin/tickTackToeButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 7 additions & 8 deletions Battleship/src/BoatTypes/Boats.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package BoatTypes;
import java.util.Random;

public class Boats
{
Expand All @@ -13,7 +12,7 @@ public class Boats
boolean isVertical = false;
boolean[] isHit;
boolean isSunk;
int health;
public int health;


public void setBoatVar(int row, int col, boolean isVertical)
Expand All @@ -23,12 +22,12 @@ public void setBoatVar(int row, int col, boolean isVertical)
this.isVertical = isVertical;
if (isVertical)
{
this.rowEnd = rowBegin + spaces;
this.rowEnd = rowBegin + (spaces - 1);
this.colEnd = colBegin;
}
else
{
this.colEnd = colBegin + spaces;
this.colEnd = colBegin + (spaces - 1);
this.rowEnd = rowBegin;
}
this.isSunk = false;
Expand All @@ -38,14 +37,16 @@ public boolean isHere(int x, int y)
boolean condition = false;
if (isVertical)
{
if (this.colBegin <= y && y <= this.colEnd )
// if the boat is vertical the col is constant, and the row varies between the boat rowBegin and boat rowEnd
if (this.colBegin == this.colEnd && this.colEnd == y && this.rowBegin <= x && x <= this.rowEnd)
{
condition = true;
}
}
else
{
if (this.rowBegin <= x && x <= this.rowEnd)
// if the boat is not vertical the row remains the same and the col varies between colBegin and colEnd
if (this.rowBegin == this.rowEnd && this.rowEnd == x && this.colBegin <= y && y <= this.colEnd)
{
condition = true;
}
Expand All @@ -54,9 +55,7 @@ public boolean isHere(int x, int y)
}
public void setHit()
{
System.out.println("Boat health before: " + this.health);
this.health -= 10;
System.out.println("Boat health now: " + this.health);
if (health <= 0)
{
System.out.println("BOAT SUNK");
Expand Down
20 changes: 14 additions & 6 deletions Battleship/src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void main(String[] args)
player1.enemyBoard.printBoard();
System.out.println();
AIPlayer.board.printBoard();
askForCoordenates(player1.enemyBoard, AIPlayer);
askForCoordinates(player1.enemyBoard, AIPlayer);
AIPlay(player1);
}
}
Expand All @@ -87,12 +87,16 @@ public static void AIPlay(Player player)
col = random.nextInt(size);

//check if coordenates hit something
if(player.board.board[row][col] != ' ')
if(player.board.board[row][col] != ' ' && player.board.board[row][col] != '!')
{
System.out.println("Enemy hit your boat!!");
Boats boat = player.getBoat(row, col);
player.board.setHit(row, col);
boat.setHit();
if(boat.health <= 0)
{
player.killBoat();
}
lastoneHit = true;
pointHitLast = new int[]{row, col};
}
Expand All @@ -103,23 +107,27 @@ public static void AIPlay(Player player)
}
}

public static void askForCoordenates(Board seeableEnemyBoard, Player AIPlayer)
public static void askForCoordinates(Board seeableEnemyBoard, Player AIPlayer)
{
//ask for coordenates
//ask for coordinates
Scanner in = new Scanner(System.in);
System.out.println("Enter row coordenate: ");
int row = in.nextInt();
System.out.println("Enter col coordenate: ");
int col = in.nextInt();

//check if coordenates hit something
if(AIPlayer.board.board[row][col] != ' ')
//check if coordinates hit something
if(AIPlayer.board.board[row][col] != ' ' && AIPlayer.board.board[row][col] != '!')
{
System.out.println("You got a hit!!");
seeableEnemyBoard.setHit(row, col);
Boats boat = AIPlayer.getBoat(row, col);
AIPlayer.board.setHit(row, col);
boat.setHit();
if (boat.health <= 0)
{
AIPlayer.killBoat();
}
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions Battleship/src/Move.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

public class Move
{
int row;
int col;
public Move(int row, int col)
{
this.row = row;
this.col = col;
}
}
14 changes: 12 additions & 2 deletions Battleship/src/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Player
Board board;
Board enemyBoard;
int boardSize;
int boatsAlive = 10;


public Player(int size)
{
Expand All @@ -31,7 +33,15 @@ public Player(int size)
boats[9] = new Submarine();
this.boardSize = size;
}

public void killBoat()
{
boatsAlive--;
if (boatsAlive <= 0)
{
System.out.println("GAME OVER!");
System.exit(0);
}
}
public void setBoard()
{
for(int i = 0; i < boats.length; i++)
Expand Down Expand Up @@ -93,7 +103,7 @@ public void setRandomBoard()

public Boats getBoat(int row, int col)
{
Boats boatFound = new Boats();
Boats boatFound = null;
for (int i = 0; i < boats.length; i++)
{
if (boats[i].isHere(row, col))
Expand Down
Loading