This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameView.java
163 lines (123 loc) · 4.74 KB
/
GameView.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* The class <b>GameView</b> provides the current view of the entire Game. It extends
* <b>JFrame</b> and lays out a matrix of <b>DotButton</b> (the actual game) and
* two instances of JButton. The action listener for the buttons is the controller.
*
* @author Guy-Vincent Jourdan, University of Ottawa
*/
public class GameView extends JFrame {
// ADD YOUR INSTANCE VARIABLES HERE
private DotButton[][] board;
private GameModel gameModel;
private JLabel nStepsLabel;
/**
* Constructor used for initializing the Frame
*
* @param gameModel
* the model of the game (already initialized)
* @param gameController
* the controller
*/
public GameView(GameModel gameModel, GameController gameController){
this.gameModel = gameModel;
this.board = new DotButton[this.gameModel.getHeigth()][this.gameModel.getWidth()];
setTitle("Minesweeper - ITI1121 Assignment");
JPanel gamePanel = new JPanel(new GridLayout(this.gameModel.getHeigth(), this.gameModel.getWidth()));
add(gamePanel, BorderLayout.CENTER);
for(int i = 0; i < this.gameModel.getHeigth(); i++){
for(int j = 0; j < this.gameModel.getWidth(); j++){
board[i][j] = new DotButton(i,j,11);
board[i][j].setName(i+","+j);
board[i][j].setBorder(null);
board[i][j].addActionListener(gameController);
board[i][j].addMouseListener(gameController);
gamePanel.add(board[i][j]);
}
}
JPanel stepPanel = new JPanel();
add(stepPanel, BorderLayout.SOUTH);
nStepsLabel = new JLabel("Number of steps: " + Integer.toString(this.gameModel.getNumberOfSteps()));
stepPanel.add(nStepsLabel, BorderLayout.WEST);
JButton reset = new JButton("Reset");
reset.addActionListener(gameController);
stepPanel.add(reset, BorderLayout.SOUTH);
JButton quit = new JButton("Quit");
quit.addActionListener(gameController);
stepPanel.add(quit, BorderLayout.SOUTH);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* update the status of the board's DotButton instances based
* on the current game model, then redraws the view
*/
public void update(){
nStepsLabel.setText("Number of steps: " + Integer.toString(this.gameModel.getNumberOfSteps()));
for(int i = 0; i < this.gameModel.getHeigth(); i++){
for(int j = 0; j < this.gameModel.getWidth(); j++){
board[i][j].setIconNumber(getIcon(j,i));
}
}
if(gameModel.isFinished() == true){
Object[] optionButton = {"Play Again", "Quit"};
int option = JOptionPane.showOptionDialog(null, "Hooray! You've beaten the game in " + gameModel.getNumberOfSteps() + " steps!\nWould you like to play again?", "CONGRATULATIONS", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, optionButton, optionButton[0]);
if(option == 0){
gameModel.reset();
update();
}
else{
System.exit(0);
}
}
}
/**
* returns the icon value that must be used for a given dot
* in the game
*
* @param i
* the x coordinate of the dot
* @param j
* the y coordinate of the dot
* @return the icon to use for the dot at location (i,j)
*/
private int getIcon(int i, int j){
if(gameModel.get(i,j).isCovered() == true){
if(gameModel.isFlagged(i,j) == true){
return 12;
}
else{
return 11;
}
}
else{
if(gameModel.get(i,j).isMined() == true){
if(gameModel.get(i,j).hasBeenClicked() == true){
return 10;
}
else{
return 9;
}
}
else{
switch(gameModel.get(i,j).getNeighbooringMines()) {
case 1 : return 1;
case 2 : return 2;
case 3 : return 3;
case 4 : return 4;
case 5 : return 5;
case 6 : return 6;
case 7 : return 7;
case 8 : return 8;
default: return 0;
}
}
}
}
}