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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

# IntelliJ IDEA files
.idea/

# IntelliJ project file
seabattle.iml

# Compiled class file
*.class

Expand Down
Binary file added IMG/Boat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IMG/PlayMode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IMG/PlayerTurn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions Nguyễn Mạnh Dũng/Nguyen Manh Dung.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Product/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/Product/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Product/SaveGame" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 3 additions & 0 deletions Nguyễn Mạnh Dũng/Product/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

em git rebase để xử lý conflict nhé
https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Main-Class: main.MainClass

56 changes: 56 additions & 0 deletions Nguyễn Mạnh Dũng/Product/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Sea Battle

Sea Battle is a game for two players. The game is played on four grids, two for each player. The grids are typically square – usually 10×10 – and the individual squares in the grid are identified by letter and number. On one grid the player arranges ships and records the shots by the opponent. On the other grid the player records their own shots.

The project was done by [Nguyễn Mạnh Dũng](https://github.com/DungNguyenCoder), in the course of Object Oriented Programming at [ProPTIT](https://proptit.com/).


## Tech Stack

- [Java](https://www.java.com/en/)


## Installation

- Clone the repo
- Open the project in your IDE
- Run the project


## Usage

- Project Structure

```bash

├── src
│ ├── main
│ │ ├── java
│ │ │ ├── entities
│ │ │ │ ├── Board.java
│ │ │ │ ├── Bot.java
│ │ │ │ ├── Player.java
│ │ │ ├── system
│ │ │ │ ├── Audio.java
│ │ │ │ ├── ClearScreen.java
│ │ │ │ ├── InputManager.java
│ │ │ │ ├── RandomCase.java
| | | ├── ui
│ │ │ │ ├── TextEffect.java
│ │ │ │ ├── TextScreen.java
│ │ │ │ ├── Player.java



```

## Contributing

Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License

[MIT](https://choosealicense.com/licenses/mit/)
8 changes: 8 additions & 0 deletions Nguyễn Mạnh Dũng/Product/SaveGame/leaderboard.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dung,0,4
han,0,4
haha,5,6
DUNG,0,5
Dung,16,5
player1,17,3
nguoiChoi2,20,0
playWithBot,48,2
3 changes: 3 additions & 0 deletions Nguyễn Mạnh Dũng/Product/res/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: main.MainClass

Binary file not shown.
168 changes: 168 additions & 0 deletions Nguyễn Mạnh Dũng/Product/src/entities/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package entities;

import main.Game;
import system.ClearScreen;
import system.InputManager;
import system.RandomCase;
import ui.TextScreen;

import java.util.Scanner;

import static utilz.Constants.gameConstants.*;
import static utilz.Constants.textConstants.GREEN_TEXT;
import static utilz.Constants.textConstants.RESET;

public class Board {
private Game game;
private boolean switchTurn = true;
private final int[] shipSizes = {PATROL_BOAT_SIZE, PATROL_BOAT_SIZE, DESTROYER_BOAT_SIZE, SUBMARINE_SIZE, BATTLE_SHIP_SIZE};
private final String[] ships = {PATROL_BOAT, PATROL_BOAT, DESTROYER_BOAT, SUBMARINE, BATTLE_SHIP};
private final String[] shipNames = {"Patrol Boat", "Patrol Boat", "Destroyer Boat", "Submarine", "Battle Ship"};

public Board(Game game) {
this.game = game;
}

public void setupShips(String[][] board, TextScreen screen, Player player) {
for(int i = 0; i < shipSizes.length; i++) {
char a = (char) ('A' + Game.boardSize - 1);
System.out.println();
System.out.print(GREEN_TEXT + "Enter the beginning cell of " + shipNames[i] + " (A-" + a + ", 1-" + Game.boardSize + ") and direction (H/V): " + RESET);
Scanner sc = InputManager.getScanner();
String input = sc.next().toUpperCase();
boolean check = placeShip(board, input, shipSizes[i], ships[i]);
if(!check) {
i--;
System.out.println();
System.out.println("Try again !!!");
continue;
}
ClearScreen.clearConsole();
screen.printBoard(board);
}
}

private boolean placeShip(String[][] board, String input, int size, String name) {
try {
char startRow = input.charAt(0);
int startCol = Integer.parseInt(input.substring(1, input.length() - 1));
char direction = input.charAt(input.length() - 1);

int row = startRow - 'A';
int col = startCol - 1;

if (direction == 'H') {
if (col + size > Game.boardSize) return false;
for (int i = 0; i < size; i++) {
if (!board[row][col + i].equals(EMPTY_BOAT)) return false;
}
for (int i = 0; i < size; i++) {
board[row][col + i] = name;
}
} else if (direction == 'V') {
if (row + size > Game.boardSize) return false;
for (int i = 0; i < size; i++) {
if (!board[row + i][col].equals(EMPTY_BOAT)) return false;
}
for (int i = 0; i < size; i++) {
board[row + i][col] = name;
}
} else {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}

public boolean playTurn(String[][] fog, String[][] opponentBoard, Player currentPlayer) {
TextScreen.printBoard(fog);
char a = (char) ('A' + Game.boardSize - 1); //name of row
System.out.print(GREEN_TEXT + "Enter fire cell (A-" + a + ", 1-" + Game.boardSize + "): " + RESET);
Scanner sc = InputManager.getScanner();
String input = sc.next().toUpperCase();
try {
char rowChar = input.charAt(0);
int col = Integer.parseInt(input.substring(1)) - 1;

int row = rowChar - 'A';
if (fog[row][col].equals(MISS_CELL) || fog[row][col].equals(DESTROYER_CELL)) {
System.out.println("You already shot a fire cell");
switchTurn = false;
return false;
}

if (opponentBoard[row][col].equals(PATROL_BOAT) || opponentBoard[row][col].equals(DESTROYER_BOAT) || opponentBoard[row][col].equals(SUBMARINE) || opponentBoard[row][col].equals(BATTLE_SHIP)) {
System.out.println("Bull's eye !!!");
currentPlayer.setScore(currentPlayer.getScore() + 1);
fog[row][col] = DESTROYER_CELL;
opponentBoard[row][col] = DESTROYER_CELL;
switchTurn = false;
}
else {
System.out.println("Miss !!!");
fog[row][col] = MISS_CELL;
switchTurn = true;
}
} catch (Exception e) {
System.out.println("Invalid input");
switchTurn = false;
return false;
}

return checkWin(opponentBoard);
}

public boolean checkWin(String[][] board) {
for (int i = 0; i < Game.boardSize; i++) {
for (int j = 0; j < Game.boardSize; j++) {
if (board[i][j].equals(PATROL_BOAT) || board[i][j].equals(DESTROYER_BOAT) || board[i][j].equals(SUBMARINE) || board[i][j].equals(BATTLE_SHIP)) {
return false;
}
}
}
return true;
}

public void randomSetupShips(String[][] board, Player currentPlayer) {
for(int i = 0; i < shipSizes.length; i++) {
String input = RandomCase.randomChar('A',(char)('A' + Game.boardSize - 1)) + String.valueOf(RandomCase.randomNumber(1,Game.boardSize)) + RandomCase.randomDir();
boolean check = placeShip(board, input, shipSizes[i], ships[i]);
if(!check) {
i--;
continue;
}
}
}

public int checkShipLeft(String [][] board) {
int cntShip = 5;
int cntP = 0, cntD = 0, cntS = 0, cntB = 0;
for(int i = 0; i < Game.boardSize; i++) {
for (int j = 0; j < Game.boardSize; j++) {
switch (board[i][j]) {
case PATROL_BOAT -> ++cntP;
case DESTROYER_BOAT -> ++cntD;
case SUBMARINE -> ++cntS;
case BATTLE_SHIP -> ++cntB;
}
}
}
if(cntP < 2) cntShip -= 2;
else if(cntP < 4) cntShip--;
if(cntD < 4) cntShip--;
if(cntS < 3) cntShip--;
if(cntB < 5) cntShip--;
return cntShip;
}

public boolean getSwitchTurn() {
return switchTurn;
}

public void setSwitchTurn(boolean switchTurn) {
this.switchTurn = switchTurn;
}

}
71 changes: 71 additions & 0 deletions Nguyễn Mạnh Dũng/Product/src/entities/Bot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package entities;

import main.Game;
import system.RandomCase;
import ui.TextScreen;

import java.util.HashSet;
import java.util.Set;

import static utilz.Constants.gameConstants.*;

public class Bot {
private Board board;
private int i;
private int j;
private final int[] dx = new int[]{-1, 0, 1, 0};
private final int[] dy = new int[]{0, -1, 0, 1};
private boolean greedy = false;
private Set<String> visitedPoints = new HashSet<>();

public Bot(Board board) {
this.board = board;
}

public boolean botPlay(String[][] opponentBoard) {
do {
if(!greedy) {
i = RandomCase.randomNumber(0,Game.boardSize-1);
j = RandomCase.randomNumber(0,Game.boardSize-1);
}
} while(visitedPoints.contains(i + "," + j));

visitedPoints.add(i + "," + j);

System.out.println("I will make move " + (char)('A' + i) + String.valueOf(j+1));

// TextScreen.printBoard(opponentBoard);

if(!opponentBoard[i][j].equals(EMPTY_BOAT) && !opponentBoard[j][i].equals(DESTROYER_CELL) && !opponentBoard[i][j].equals(MISS_CELL)) {
opponentBoard[i][j] = DESTROYER_CELL;
System.out.println("Yay bull's eye");
greedy = true;
findCell(opponentBoard,opponentBoard[i][j]);
}
else {
System.out.println("OMG I'm miss");
greedy = false;
}

board.setSwitchTurn(true);

return board.checkWin(opponentBoard);
}

private void findCell(String[][] opponentBoard, String cell) {
for(int k = 0; k < 4; k++) {
int i1 = i + dx[k];
int j1 = j + dy[k];
if (i1 >= 0 && i1 < Game.boardSize && j1 >= 0 && j1 < Game.boardSize && !visitedPoints.contains(i1 + "," + j1)) {
if(opponentBoard[i1][j1].equals(cell)) {
i = i1;
j = j1;
visitedPoints.add(i1 + "," + j1);
greedy = true;
return;
}
}
}
greedy = false;
}
}
Loading