การสร้างบอร์ดเกมเศรษฐีออนไลน์อย่างง่ายด้วย Spring Boot
ในบทความนี้ เราจะสร้างโปรเจกต์เว็บแอปพลิเคชันบอร์ดเกมเศรษฐีอย่างง่าย
โดยใช้ Spring Boot ซึ่งเป็นเฟรมเวิร์กที่นิยมมากในการพัฒนาเว็บแอปพลิเคชันด้วยภาษา
Java ที่สามารถทำงานได้อย่างรวดเร็วและมีประสิทธิภาพ
1. เตรียมความพร้อม
เครื่องมือที่ต้องใช้
- Java Development Kit (JDK) เวอร์ชัน 17+
- Spring Boot (ใช้ Spring Initializr)
- Maven หรือ Gradle สำหรับจัดการ dependency
- IDE เช่น IntelliJ IDEA หรือ Eclipse
- ฐานข้อมูล เช่น H2 (in-memory database) หรือ MySQL
2. สร้างโปรเจกต์ Spring Boot
- ไปที่ Spring Initializr
- ตั้งค่าโปรเจกต์:
- Project: Maven
- Language: Java
- Spring Boot Version: 3.x
- Dependencies:
- Spring Web
- Spring Data JPA
- H2 Database
- ดาวน์โหลดไฟล์ ZIP และนำเข้าใน IDE ที่ใช้งาน
3. สร้างโครงสร้างเกมเศรษฐี
3.1 สร้างโมเดลข้อมูล (Entities)
Player.java
@Entity
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int balance;
private int position; // ตำแหน่งปัจจุบันของผู้เล่น
// Constructor, Getter, Setter
}
Property.java
@Entity
public class Property {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int price;
private Long ownerId;
// Constructor, Getter, Setter
}
3.2 สร้างคลาสกระดานเกม (Board)
Board.java
import java.util.List;
public class Board {
private final List<String> spaces;
public Board() {
this.spaces = List.of(
"Start", "Property 1", "Property 2", "Chance", "Property 3",
"Jail", "Property 4", "Free Parking", "Property 5", "Go To Jail"
);
}
public int getBoardSize() {
return spaces.size();
}
public String getSpace(int position) {
return spaces.get(position % spaces.size()); // วนกลับไปที่ Start
}
}
3.3 สร้าง Service และ Logic หลัก
GameService.java
@Service
public class GameService {
@Autowired
private PlayerRepository playerRepository;
private final Board board = new Board();
private final Random random = new Random();
public int rollDice() {
return random.nextInt(6) + 1; // ทอยลูกเต๋า 1-6
}
public String movePlayer(Long playerId) {
Player player = playerRepository.findById(playerId).orElseThrow();
int diceRoll = rollDice();
int oldPosition = player.getPosition();
int newPosition = (oldPosition + diceRoll) % board.getBoardSize();
player.setPosition(newPosition);
playerRepository.save(player);
return String.format(
"Player %s rolled a %d and moved from %s to %s.",
player.getName(), diceRoll, board.getSpace(oldPosition), board.getSpace(newPosition)
);
}
}
3.4 สร้าง Controller
GameController.java
@RestController
@RequestMapping("/api")
public class GameController {
@Autowired
private GameService gameService;
@PostMapping("/player/roll")
public String rollDiceAndMove(@RequestParam Long playerId) {
return gameService.movePlayer(playerId);
}
}
4. ทดสอบระบบ
ทดสอบผ่าน Postman หรือ Curl:
POST http://localhost:8080/api/player/roll?playerId=1
ตัวอย่างผลลัพธ์:
Player John rolled a 4 and moved from Start to Property 4.
5. สรุปผลการทำงาน
ในบทความนี้ ยกตัวอย่างระบบทอยลูกเต๋าและเคลื่อนที่ผู้เล่นตามกระดานวงกลมแบบง่าย
โดยใช้ Spring Boot และจัดการข้อมูลด้วย JPA ผู้อ่านสามารถขยายระบบได้อีกในอนาคต เช่น การจ่ายค่าเช่า การซื้อที่ดิน และสถานะต่าง ๆ บนกระดาน 🎲
ความคิดเห็น
แสดงความคิดเห็น