-
Notifications
You must be signed in to change notification settings - Fork 1
Nguyễn Văn Minh Lực #81
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
5ff5a2e
3a0c888
c620bde
97f8a1c
45673cf
c3c8aaf
a23611f
c150626
eee8c0f
233c86c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # 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 Văn A](https://github.com/naa-ntu), 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 | ||
| │ │ │ ├── controller | ||
| │ │ │ │ ├── Controller.java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ └── ....java | ||
| │ │ │ ├── model | ||
| │ │ │ │ ├── Board.java | ||
| │ │ │ │ ├── Cell.java | ||
| │ │ │ │ ├── Game.java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ ├── ....java | ||
| │ │ │ │ └── ....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/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package data; | ||
|
|
||
| import player.Player; | ||
|
|
||
| import java.io.*; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| public class Bxh { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Em nên đặt biến rõ ràng hơn nhé, với lại mình không nên viết tắt -> không rõ ràng -> em có thể đặt Leaderboard gì đó, cái này tùy em thôi |
||
| public static List<PlayerRank> rank; | ||
|
|
||
| public static void loadFromFile() throws IOException | ||
| { | ||
| rank = new ArrayList<PlayerRank>(); | ||
| File f = new File("rank.txt"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. các biến a đang thấy e viết tắt khá nhiều -> không thể hiện được ý nghĩa của biến, ở chỗ này e có thể đặt fileRank,... gì đó để thể hiện rõ ràng hơn nhé, bên dưới cũng thế |
||
| if (!f.exists()) | ||
| { | ||
| f.createNewFile(); | ||
| } | ||
| FileReader fr = new FileReader(f); | ||
| BufferedReader br = new BufferedReader(fr); | ||
| while(true) { | ||
| String name = br.readLine(); | ||
| if (name == null) break; | ||
| int score = Integer.parseInt(br.readLine()); | ||
| rank.add(new PlayerRank(name, score)); | ||
| } | ||
| br.close(); fr.close(); | ||
| } | ||
|
|
||
| public static void showRank(){ | ||
| System.out.printf("|%-5s|%-20s|%-5s|%n", "Rank", "Tên", "Score"); | ||
| System.out.println("|-----|--------------------|-----|"); | ||
| for(int i = 0; i < rank.size(); i++) { | ||
| System.out.printf("|%-5d|%-20s|%-5d|%n", (i + 1), rank.get(i).getName(), rank.get(i).getScore()); | ||
| } | ||
| } | ||
| public static void sortBxh() { | ||
| Collections.sort(rank, new RankComparator()); | ||
| } | ||
|
|
||
| public static void updateBxh(Player player){ | ||
| for(int i = 0; i < rank.size(); i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nếu i không sử dụng thì e nên dùng for each nhé |
||
| if (player.getName().equals(rank.get(i).getName())) { | ||
| if (rank.get(i).getScore() < player.getSoODaBan()) { | ||
| rank.get(i).setScore(player.getSoODaBan()); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| rank.add(new PlayerRank(player.getName(), player.getSoODaBan())); | ||
| sortBxh(); | ||
| } | ||
|
|
||
| public static void saveToFile() throws IOException | ||
| { | ||
| File f = new File("rank.txt"); | ||
| FileWriter fw = new FileWriter(f); | ||
| BufferedWriter bw = new BufferedWriter(fw); | ||
| for(PlayerRank p : rank ) { | ||
| bw.write(p.getName() + "\n" + p.getScore()); | ||
| } | ||
| bw.close();fw.close(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package data; | ||
|
|
||
| public class PlayerRank { | ||
| private String name; | ||
| private int score; | ||
|
|
||
| PlayerRank(){} | ||
|
|
||
| PlayerRank(String name, int score) { | ||
| this.name = name; | ||
| this.score = score; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getScore() { | ||
| return score; | ||
| } | ||
|
|
||
| public void setScore(int score) { | ||
| this.score = score; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package data; | ||
|
|
||
| public class RankComparator implements java.util.Comparator<PlayerRank>{ | ||
| @Override | ||
| public int compare(PlayerRank p1, PlayerRank p2) { | ||
| return p1.getScore() - p2.getScore(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package main; | ||
|
|
||
| import template.Template; | ||
| import player.GameManager; | ||
| import data.*; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) throws IOException { | ||
| Template.printSeaBattle(); | ||
| GameManager Game = new GameManager(); | ||
| Bxh.loadFromFile(); | ||
| while(true){ | ||
| Template.showGameMenu(); | ||
| int selection = Integer.parseInt(system.scanner.nextLine()); | ||
| switch(selection){ | ||
| case 1 : Bxh.showRank(); break; | ||
| case 2 : Game.start(); break; | ||
| case 3 : Bxh.saveToFile(); return; | ||
| default : Template.enterAgain(); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package main; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class system { | ||
| public static Scanner scanner = new Scanner(java.lang.System.in); | ||
|
|
||
| public static void clearScreen() { | ||
| java.lang.System.out.print("\033[H\033[2J"); | ||
| java.lang.System.out.flush(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package player; | ||
|
|
||
| public class Bot extends Player { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package player; | ||
|
|
||
| public class CheckShoot { | ||
| public static boolean shoot(ToaDo toado, Player player, Player enemy) { | ||
| int x = toado.getX(); | ||
| int y = toado.getY(); | ||
| player.updateSoODaBan(); | ||
| if (enemy.getBoard()[x][y].equals("\uD83C\uDF0A") || player.getEnemyBoard()[x][y].equals("\uD83D\uDD25")||player.getEnemyBoard()[x][y].equals("❌")) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a đang thấy các chuỗi như "\uD83C\uDF0A", "\uD83D\uDC25" đang sử dụng lại nhiều lần nhưng nó đang không thể hiện ra được là nó có ý nghĩa thế nào -> em nên tạo 1 class Constant gì đó lưu giá trị của các cái này, mình chỉ lần mỗi lần so sánh thì sẽ lấy ra thôi |
||
| { | ||
| if (enemy.getBoard()[x][y].equals("\uD83C\uDF0A")) | ||
| player.setEnemyBoard("❌", x, y); | ||
| return false; | ||
| } | ||
| else{ | ||
| String symbol = enemy.getBoard()[x][y]; | ||
| if (symbol == "\uD83D\uDC24"){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. đoạn này e nên viết hàm để tái sử dụng nếu được nhé, với lại nếu nhiều if else thì em nên dùng swithcase thì rõ ràng hơn |
||
| enemy.decreasePatrolBoat1Point(); | ||
| if(enemy.getPatrolBoat1Point() == 0) { | ||
| System.out.println("Bạn đã hạ gục 1 tàu của đối thủ"); | ||
| player.updateSoTauDaPha(); | ||
| enemy.decreaseSoTauConLai(); | ||
| } | ||
| } | ||
| else if (symbol == "\uD83D\uDC25"){ | ||
| enemy.decreasePatrolBoat2Point(); | ||
| if(enemy.getPatrolBoat2Point() == 0) { | ||
| System.out.println("Bạn đã hạ gục 1 tàu của đối thủ"); | ||
| player.updateSoTauDaPha(); | ||
| enemy.decreaseSoTauConLai(); | ||
| } | ||
| } | ||
| else if (symbol == "\uD83D\uDC26"){ | ||
| enemy.decreaseDestroyerBoatPoint(); | ||
| if(enemy.getDestroyerBoatPoint() == 0) { | ||
| System.out.println("Bạn đã hạ gục 1 tàu của đối thủ"); | ||
| player.updateSoTauDaPha(); | ||
| enemy.decreaseSoTauConLai(); | ||
| } | ||
| } | ||
| else if (symbol == "\uD83D\uDC27"){ | ||
| enemy.decreaseSubmarinePoint(); | ||
| if(enemy.getSubmarinePoint() == 0) { | ||
| System.out.println("Bạn đã hạ gục 1 tàu của đối thủ"); | ||
| player.updateSoTauDaPha(); | ||
| enemy.decreaseSoTauConLai(); | ||
| } | ||
| } | ||
| else if (symbol == "\uD83D\uDC14"){ | ||
| enemy.decreaseBattleShipPoint(); | ||
| if(enemy.getBattleShipPoint() == 0) { | ||
| System.out.println("Bạn đã hạ gục 1 tàu của đối thủ"); | ||
| player.updateSoTauDaPha(); | ||
| enemy.decreaseSoTauConLai(); | ||
| } | ||
| } | ||
| enemy.setBoard("\uD83D\uDD25", x, y); | ||
| player.setEnemyBoard("\uD83D\uDD25", x, y); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package player; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a thấy cái này e đặt package là player thì trong nó sẽ chứa model Player các thứ thôi, a thấy nó đang có cả checkShoot, ShowBoard các thứ nữa -> không thể hiện được sử liên quan class đến package, em có thể tham khảo mô hình MVC nhé |
||
|
|
||
| import main.system; | ||
| import template.Template; | ||
| import ship.ListOfShips; | ||
|
|
||
| import java.util.TreeMap; | ||
|
|
||
| public class GameManager { | ||
| ListOfShips ships = new ListOfShips(); | ||
| Player player1, player2; | ||
| public static int kichThuoc = 10; | ||
|
|
||
| public void start() | ||
| { | ||
| // System.out.println("Nhập kích thước bảng (10 -> 20): "); | ||
| // kichThuoc = Integer.parseInt(system.scanner.nextLine()); | ||
| Template.showBattleMenu(); | ||
| int selection = Integer.parseInt(system.scanner.nextLine()); | ||
| if (selection == 1) { | ||
|
|
||
| } | ||
| else if (selection == 2) { | ||
| TwoPlayer twoPlayer = new TwoPlayer(); | ||
| twoPlayer.play(); | ||
| } | ||
| } | ||
| } | ||
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.
Data thường là package để chứa dữ liệu kiểu như em muốn chơi tiếp thì em lưu dữ liệu vào đó, bxh là cái e thể hiện ra cho người dùng thấy, a nghĩ e nên đặt nó trong package view thì hợp lý hơn,
ngoài ra em có thể tạo 1 class chuyển load dữ liệu thôi -> a thấy bxh của e đang có cả load dữ liệu và hiển thị ra màn hình nữa
Các đường dẫn e nên truyền vào -> tái sử dụng
Em có thể lưu theo object bằng file nhị phân nhé -> em đọc 1 phát object luôn chứ không đọc từng dòng thế này