-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameContainer.java
157 lines (145 loc) · 5.43 KB
/
GameContainer.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
package kablewie;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* @file -GameContainer.java
* @author -Tereza Stoyanova, Thomas Fisher, Ben Harris
* @date -28/11/2015
* @see -Game.java
*
* A container class for the game core. Also adds small
* control panel at the foot of the container.
*/
public class GameContainer extends JFrame {
public static final int DEFAULT_GAME_SIZE = 10;
public static final int NAME_CHAR_LIM = 20;
public static final int CONTROL_PANEL_X = 2;
public static final int CONTROL_PANEL_Y = 3;
public static final int TEXT_FIELD_SIZE = 10;
/**
* Main method for this class. Simply instantiates this class with
* the DEFAULT_GAME_SIZE as parameters
* @see GameContainer(int, int)
*/
public static void main( String args[] ) {
GameContainer gc =
new GameContainer(DEFAULT_GAME_SIZE,DEFAULT_GAME_SIZE);
}
/**
* Constructor for this class, taking in two integers
* @param s Size of game board measured in tiles
* @param m Amount of mines to be added to board
* @see InputHandler.actionPerformed(actionEvent)
* @see isNumeric(String)
* @see resetGame(Game)
*/
public GameContainer(int s, int m) {
final int INPUT_HEIGHT = 100;
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
setTitle("Kablewie!");
Game g = new Game(s,m);
String playerName = JOptionPane.showInputDialog("Enter your name: ",
"New Challenger");
if (playerName != null) {
if(playerName.length() > NAME_CHAR_LIM) {
playerName = JOptionPane.showInputDialog("Sorry character "+
"limit is 20.\n Enter your name: ", "New Challenger");
}
} else {playerName = "N/A";}
g.newPlayer(playerName);
JPanel input = new JPanel(); input.setSize(g.getWidth(), INPUT_HEIGHT);
input.setLayout(new GridLayout(CONTROL_PANEL_Y, CONTROL_PANEL_X));
JLabel lblSize = new JLabel("Size: ");
input.add(lblSize);
JTextField tfSize = new JTextField(TEXT_FIELD_SIZE);input.add(tfSize);
JLabel lblMines = new JLabel("Number of mines: ");input.add(lblMines);
JTextField tfMines = new JTextField(TEXT_FIELD_SIZE);input.add(tfMines);
JButton btnNewGame = new JButton("New Game");input.add(btnNewGame);
JButton btnExit = new JButton("Exit");input.add(btnExit);
class InputHandler implements ActionListener {
/**
* Implementation of abstract method inherited from ActionListener
* This method adds functionality to the buttons at the footer
* of game container
* @param e An ActionEvent
*/
public void actionPerformed(ActionEvent e) {
int size = 10;
int mines = -1;
boolean valid = true;
boolean defaultValue = false;
int sizeInput = 10;
int mineInput = -1;
if (e.getSource() == btnNewGame) {
if (tfSize.getText().equals("") ||
!isNumeric(tfSize.getText())) {
size = 10; defaultValue = true;
} else {sizeInput = Integer.parseInt(tfSize.getText());}
if ((sizeInput <= 30) && (sizeInput > 0) &&
(isNumeric(tfSize.getText()) || defaultValue)){
size = sizeInput; } else {valid = false;}
if (tfMines.getText().equals("") ||
!isNumeric(tfSize.getText())){
mines = size; mineInput = size;
} else {mineInput = Integer.parseInt(tfMines.getText()); }
if ((mineInput <= 150) &&
(mineInput < (sizeInput*sizeInput)) &&
(isNumeric(tfSize.getText()) || defaultValue)){
mines = mineInput;
} else {valid = false;}
if (valid){new GameContainer(size, mines); dispose();}else{
JOptionPane.showMessageDialog(getGameContainer(),
"The size must be a number between 1 and 30. "+
"\n\nThe number of mines must be between 0 \n"
+ "and the size squared and must not exceed 150.",
"Incorrect value(s) entered",
JOptionPane.WARNING_MESSAGE);
}
tfSize.setText(""); tfMines.setText("");
} else if (e.getSource() == btnExit) { dispose();}
}
}
InputHandler handler = new InputHandler();
btnNewGame.addActionListener(handler);
btnExit.addActionListener(handler);
getContentPane().add(g, BorderLayout.NORTH);
getContentPane().add(input, BorderLayout.CENTER);
g.getScoreboard().updateTime(); setResizable(false);
pack(); setLocationRelativeTo(null); setVisible(true);
}
/**
* A getter method that returns this instance of GameContainer
* @return this instance of GameContainer
*/
public GameContainer getGameContainer() {
return this;
}
/**
* Method to reset the entire game
* @param g Game object to be reapplied
* @see Board.getBoardSize()
* @see Board.getBoardSize()
*/
public void resetGame(Game g) {
int size = g.getBoard().getBoardSize();
int mines = g.getBoard().getNumberOfMines();
g = new Game(size,mines);
}
/**
* Method to test if inputs are numeric
* @param s String to test
* @return whether string is numeric
*/
public static boolean isNumeric(String string) {
try {
int testNumber = Integer.parseInt(string);
} catch(NumberFormatException e) {
return false;
}
return true;
}
}