This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
98 lines (91 loc) · 2.13 KB
/
Copy pathGame.java
File metadata and controls
98 lines (91 loc) · 2.13 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
95
96
97
98
package gameCore;
public class Game <T extends API>{
// Attributes
private Board board;
private T user;
private boolean finished;
private boolean win;
private int round;
/**
* Constructs a new game.
* @param board the board in which the game is played.
* @param user the user that will play the game. Must implement API.
*/
public Game(Board board, T user){
this.board = board;
this.user = user;
Cell first = user.firstCell();
board.distributeMines(first.getPositionX(), first.getPositionY());
board.getCell(first.getPositionX(), first.getPositionY()).reveal();
this.finished = false;
this.win = false;
this.round = 0;
user.receiveRound(this.round);
}
/**
* Plays the game until the player either wins or loses.
*/
public void play(){
user.showBoard();
user.showTrueBoard();
while (!finished){
boolean isActionCorrect = false;
while (!isActionCorrect){
Cell selectedCell = user.selectCell();
int action = user.action();
if (selectedCell.isChangeValid(action)){
switch (action){
case 0:
selectedCell.unflag();
break;
case 1:
selectedCell.flag();
break;
case 2:
selectedCell.reveal();
break;
}
isActionCorrect = true;
this.round++;
user.receiveRound(this.round);
}
else {
System.out.println("Incorrect Action");
}
if (board.isGameFinished()) {
finished = true;
if (board.playerWon()){
this.win = true;
}
}
user.showBoard();
user.showTrueBoard();
}
}
}
/**
* Checks if the player won.
* @return true if and only if the player won.
*/
public boolean winner(){
return this.win;
}
/**
* Getter for round number
* @return the round number.
*/
public int getRound(){
return this.round;
}
public Game(Board board, T user, int x, int y){
this.board = board;
this.user = user;
Cell first = board.getCell(x, y);
board.distributeMines(first.getPositionX(), first.getPositionY());
board.getCell(first.getPositionX(), first.getPositionY()).reveal();
this.finished = false;
this.win = false;
this.round = 0;
user.receiveRound(this.round);
}
}