0% found this document useful (0 votes)
4 views6 pages

Monopoly Full Java Code

The document contains the full Java source code for a Monopoly game, including classes for managing the game, players, the game board, and various types of tiles. Key functionalities include player movement, property transactions, and handling game events like taxes and chance occurrences. The game supports 2 to 4 players and determines a winner based on bankruptcy status.

Uploaded by

95phungdinhduc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Monopoly Full Java Code

The document contains the full Java source code for a Monopoly game, including classes for managing the game, players, the game board, and various types of tiles. Key functionalities include player movement, property transactions, and handling game events like taxes and chance occurrences. The game supports 2 to 4 players and determines a winner based on bankruptcy status.

Uploaded by

95phungdinhduc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Monopoly Game - Full Java Source Code

[Link]
public class Main {
public static void main(String[] args) {
GameManager game = new GameManager();
[Link]();
}
}

[Link]
import [Link].*;

public class GameManager {


private List<Player> players = new ArrayList<>();
private Board board;
private Dice dice = new Dice();
private Scanner sc = new Scanner([Link]);

public void startGame() {


[Link]("=== MONOPOLY GAME ===");

[Link]("Nhập số người chơi (2-4): ");


int n = [Link]();
[Link]();

for (int i = 0; i < n; i++) {


[Link]("Tên người chơi " + (i+1) + ": ");
String name = [Link]();
[Link](new Player(name));
}

board = new Board();

boolean playing = true;


while (playing) {
for (Player p : players) {
if ([Link]()) continue;

[Link]("\n--- Lượt của " + [Link]() + " ---");


int roll = [Link]();
[Link]("Xúc xắc: " + roll);

[Link](roll);
Tile tile = [Link]([Link]());

[Link](p, this);

if (checkWin()) {
playing = false;
break;
}
}
}
}

public boolean checkWin() {


int active = 0;
Player winner = null;

for (Player p : players) {


if (![Link]()) {
active++;
winner = p;
}
}

if (active == 1) {
[Link]("🏆 Winner: " + [Link]());
return true;
}
return false;
}
}

[Link]
import [Link].*;

public class Player {


private String name;
private int money = 1500;
private int position = 0;
private boolean bankrupt = false;
private List<PropertyTile> properties = new ArrayList<>();

public Player(String name) {


[Link] = name;
}

public void move(int steps) {


position = (position + steps) % 40;
}

public void pay(int amount) {


money -= amount;
if (money < 0) bankrupt = true;
}

public void receive(int amount) {


money += amount;
}

public void addProperty(PropertyTile p) {


[Link](p);
}

public String getName() { return name; }


public int getPosition() { return position; }
public int getMoney() { return money; }
public boolean isBankrupt() { return bankrupt; }
}

[Link]
import [Link].*;

public class Board {


private List<Tile> tiles = new ArrayList<>();

public Board() {
[Link](new GoTile());

for (int i = 1; i < 40; i++) {


if (i % 10 == 0)
[Link](new JailTile());
else if (i % 5 == 0)
[Link](new TaxTile(100));
else if (i % 3 == 0)
[Link](new ChanceTile());
else
[Link](new PropertyTile("Đất " + i, 200));
}
}

public Tile getTile(int pos) {


return [Link](pos);
}
}

[Link]
public abstract class Tile {
public abstract void landOn(Player player, GameManager game);
}

[Link]
import [Link];

public class PropertyTile extends Tile {


private String name;
private int price;
private Player owner;

public PropertyTile(String name, int price) {


[Link] = name;
[Link] = price;
}

@Override
public void landOn(Player player, GameManager game) {
Scanner sc = new Scanner([Link]);

if (owner == null) {
[Link]("Bạn vào ô " + name + " giá " + price);
[Link]("Mua không? (y/n): ");
String choice = [Link]();
if ([Link]("y")) {
[Link](price);
owner = player;
[Link](this);
[Link]("Đã mua!");
}
} else if (owner != player) {
[Link]("Đất của " + [Link]());
[Link](50);
[Link](50);
}
}
}

[Link]
public class TaxTile extends Tile {
private int tax;

public TaxTile(int tax) {


[Link] = tax;
}

@Override
public void landOn(Player player, GameManager game) {
[Link]("ảTrế thuế : " + tax);
[Link](tax);
}
}

[Link]
import [Link];

public class ChanceTile extends Tile {


private Random rand = new Random();

@Override
public void landOn(Player player, GameManager game) {
int r = [Link](3);

if (r == 0) {
[Link]("ậNhậ n 200");
[Link](200);
} else if (r == 1) {
[Link]("ấMấ t 100");
[Link](100);
} else {
[Link]("ưĐi thêm 3 bớ c");
[Link](3);
}
}
}

[Link]
public class JailTile extends Tile {
@Override
public void landOn(Player player, GameManager game) {
[Link]("ấVào tù (mưt lợ t)");
}
}

[Link]
public class GoTile extends Tile {
@Override
public void landOn(Player player, GameManager game) {
[Link]("ậQua GO nhậ n 200");
[Link](200);
}
}

[Link]
import [Link];

public class Dice {


private Random rand = new Random();

public int roll() {


return [Link](6) + 1 + [Link](6) + 1;
}
}

You might also like