-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
94 lines (82 loc) · 3.08 KB
/
Game.java
File metadata and controls
94 lines (82 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.*;
import java.util.ArrayList;
public class Game implements Serializable {
private Deck deck;
private ArrayList<Player> players;
private Card topCard;
private int currentPlayerIndex;
public Game(int numPlayers) {
deck = new Deck();
players = new ArrayList<>();
for (int i = 1; i <= numPlayers; i++) {
players.add(new Player("Player " + i));
}
for (Player player : players) {
for (int j = 0; j < 5; j++) {
player.addCard(deck.drawCard());
}
}
topCard = deck.drawCard();
currentPlayerIndex = 0;
}
public Card getTopCard() {
return topCard;
}
public void playTurn(Player player, Card card) {
if (isValidMove(card)) {
player.removeCard(card);
topCard = card;
if (player.hasWon()) {
System.out.println(player.getName() + " has won the game!");
}
} else {
System.out.println("Invalid move by " + player.getName() + " with card " + card);
}
}
public boolean isValidMove(Card card) {
return card.getSuit().equals(topCard.getSuit()) || card.getRank().equals(topCard.getRank())
|| card.getRank().equals("8");
}
public Player getPlayer(int index) {
return players.get(index);
}
public ArrayList<Player> getPlayers() {
return players;
}
public Deck getDeck() {
return deck;
}
public int getCurrentPlayerIndex() {
return currentPlayerIndex;
}
public void setCurrentPlayerIndex(int index) {
currentPlayerIndex = index;
}
public void saveGame(String filePath) throws IOException {
File file = new File(filePath);
if (file.getParentFile() != null && !file.getParentFile().exists()) {
throw new IOException("Directory does not exist: " + file.getParentFile());
}
if (!file.getParentFile().canWrite()) {
throw new IOException("No write permission to directory: " + file.getParentFile());
}
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
oos.writeObject(this);
} catch (FileNotFoundException e) {
throw new IOException("File not found: " + filePath, e);
} catch (IOException e) {
throw new IOException("Error saving game to file: " + filePath, e);
}
}
public static Game loadGame(String filePath) throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
return (Game) ois.readObject();
} catch (FileNotFoundException e) {
throw new IOException("File not found: " + filePath, e);
} catch (IOException e) {
throw new IOException("Error loading game from file: " + filePath, e);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Class not found while loading game from file: " + filePath, e);
}
}
}