-
Notifications
You must be signed in to change notification settings - Fork 1
D23 Nguyễn Mạnh Dũng #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DungNguyenCoder
wants to merge
4
commits into
develop
Choose a base branch
from
d23_nguyen_manh_dung
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
|
|
||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: main.MainClass | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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