Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions Nguyễn Văn Minh Lực/Product/README.md
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/)
67 changes: 67 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/data/Bxh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package data;

Copy link
Copy Markdown

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


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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}
}
24 changes: 24 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/data/PlayerRank.java
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;
}
}
8 changes: 8 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/data/RankComparator.java
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();
}
}
25 changes: 25 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/main/Main.java
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();
}
}
}
}
13 changes: 13 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/main/system.java
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();
}

}
5 changes: 5 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/player/Bot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package player;

public class Bot extends Player {

}
61 changes: 61 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/player/CheckShoot.java
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("❌"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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"){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}
}
}
28 changes: 28 additions & 0 deletions Nguyễn Văn Minh Lực/Product/src/player/GameManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package player;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}
}
}
Loading