-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
118 lines (101 loc) · 3.62 KB
/
GameBoard.java
File metadata and controls
118 lines (101 loc) · 3.62 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
/**
* This class provides a graphical model of a board game.
* The class creates a rectangular panel of clickable squares.
* If a square is clicked by the user a method is invoked upon the corresponding GameSquare instance.
* The class is intended to be used as a basis for tile based games.
* @author Joe Finney
*/
public class GameBoard extends JFrame
{
private JPanel boardPanel = new JPanel();
private int boardHeight;
private int boardWidth;
private GameSquare[][] board;
private Constructor<?> makeSquare;
/**
* Create a new game board of the given size.
* As soon as an instance of this class is created, it is visualized
* on the screen.
*
* @param title the name printed in the window bar.
* @param width the width of the game area, in squares.
* @param height the height of the game area, in squares.
* @param squareType the name of the class to construct for each position on the board. Required to be a concrete subclass of GameSquare.
*/
public GameBoard(String title, int width, int height, String squareType)
{
super();
try {
Class<?> parameters[] = new Class<?>[] { int.class, int.class, GameBoard.class };
Class<?> c = Class.forName(squareType);
makeSquare = c.getConstructor(parameters);
} catch (Exception e)
{
System.out.println("Could not find a valid subclass of GameSquare called " + squareType);
return;
}
this.boardWidth = width;
this.boardHeight = height;
// Create game state
this.board = new GameSquare[width][height];
// Set up window
setTitle(title);
setSize(width*20,height*20);
setContentPane(boardPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boardPanel.setLayout(new GridLayout(height,width));
for (int y = 0; y<height; y++)
{
for (int x = 0; x<width; x++)
{
try
{
board[x][y] = (GameSquare) makeSquare.newInstance(x, y, this);
}
catch (Exception e)
{
System.out.println("Could not find a valid constructor " +squareType + "(int, int, GameBoard)");
}
board[x][y].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
GameSquare s = (GameSquare) e.getSource();
if (e.getButton() == MouseEvent.BUTTON1)
s.leftClicked();
else
s.rightClicked();
}
});
boardPanel.add(board[x][y]);
}
}
// make our window visible
setVisible(true);
}
/**
* Returns the GameSquare instance at a given location.
* @param x the x co-ordinate of the square requested.
* @param y the y co-ordinate of the square requested.
* @return the GameSquare instance at a given location if the x and y co-ordinates are valid - null otherwise.
*/
public GameSquare getSquareAt(int x, int y)
{
if (x < 0 || x >= boardWidth || y < 0 || y >= boardHeight)
return null;
return board[x][y];
}
/**
* Calls the reset() method on all GameSqaures on this board.
*
* @param n a value to pass as a parameter to each square.
*/
public void reset(int n)
{
for (int y = 0; y<boardHeight; y++)
for (int x = 0; x<boardWidth; x++)
board[x][y].reset(n);
}
}