182 lines
7.0 KiB
Java
182 lines
7.0 KiB
Java
package interfaces;
|
|
|
|
import board.IBoard;
|
|
import events.Block;
|
|
import events.Bomb;
|
|
import objects.GameObject;
|
|
import objects.Player;
|
|
import objects.Token;
|
|
|
|
import javax.swing.*;
|
|
import javax.swing.border.EmptyBorder;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.WindowAdapter;
|
|
import java.awt.event.WindowEvent;
|
|
|
|
public class NInARowGUI extends GUI {
|
|
private Bomb bomb = new Bomb();
|
|
private Block block = new Block();
|
|
private JLabel eventLabel;
|
|
|
|
public NInARowGUI(int width, int height, IBoard board) {
|
|
if (width < 3 || height < 3 || height > 50 || width > 50) {
|
|
throw new IllegalArgumentException("Invalid game size.");
|
|
}
|
|
if (width != board.getWidth() || height != board.getHeight()) {
|
|
throw new IllegalArgumentException("The board does not match the gui.");
|
|
}
|
|
this.board = board;
|
|
int playerNum = 0;
|
|
while (playerNum == 0) {
|
|
try {
|
|
String num = JOptionPane.showInputDialog("Number of players: ");
|
|
if (num == null) {
|
|
System.exit(0);
|
|
}
|
|
playerNum = Integer.parseInt(num);
|
|
if (playerNum > width) {
|
|
JOptionPane.showMessageDialog(null, "Number of players can't be higher than " + width, "Error", JOptionPane.ERROR_MESSAGE);
|
|
playerNum = 0;
|
|
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
JOptionPane.showMessageDialog(null, "Invalid input.", "Error", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
for (int i = 0; i < playerNum; i++) {
|
|
String playerName = "";
|
|
while (playerName.equals("")) {
|
|
playerName = JOptionPane.showInputDialog(String.format("Name of player %d: ", i+1));
|
|
if (playerName == null) {
|
|
System.exit(0);
|
|
}
|
|
}
|
|
Color color = null;
|
|
while (color == null) {
|
|
color = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
|
if (color == null) {
|
|
System.exit(0);
|
|
}
|
|
if (color == Color.lightGray || color == Color.blue || color == Color.black) {
|
|
JOptionPane.showMessageDialog(null, "The chosen color is reserved. Please choose another color.", "Error", JOptionPane.ERROR_MESSAGE);
|
|
color = null;
|
|
}
|
|
}
|
|
Player player = new Player(playerName, color);
|
|
if (!this.board.addPlayer(player)) {
|
|
JOptionPane.showMessageDialog(null, "Player is not unique. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
|
|
i--;
|
|
}
|
|
}
|
|
initialize(width, height);
|
|
}
|
|
|
|
private void initialize(int width, int height) {
|
|
JFrame frame = new JFrame("Four in a row");
|
|
frame.addWindowListener(new WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(WindowEvent e) {
|
|
System.exit(0);
|
|
}
|
|
});
|
|
int fullWidth = BUTTON_WIDTH * width + (WIDTH_GAP + 1) * width;
|
|
int fullHeight = BUTTON_HEIGHT + (TILE_HEIGHT * height) + HEIGHT_GAP * (height) + TILE_HEIGHT;
|
|
|
|
this.frame = frame;
|
|
JPanel container = new JPanel();
|
|
container.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
|
container.setPreferredSize(new Dimension(fullWidth, fullHeight + TILE_HEIGHT + 2 * BUTTON_HEIGHT));
|
|
frame.add(container);
|
|
|
|
JPanel panel3 = new JPanel();
|
|
this.resetButton = new JButton("Reset");
|
|
this.resetButton.addActionListener(this);
|
|
panel3.add(this.resetButton);
|
|
for (Player player : board.getPlayers()) {
|
|
JLabel playerLabel = new JLabel(player.toString());
|
|
playerLabel.setForeground(player.getColor());
|
|
panel3.add(playerLabel);
|
|
}
|
|
container.add(panel3);
|
|
|
|
JPanel panel2 = new JPanel();
|
|
this.mainPanel = panel2;
|
|
panel2.setBackground(Color.BLUE);
|
|
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, HEIGHT_GAP, WIDTH_GAP));
|
|
panel2.setPreferredSize(new Dimension(fullWidth, fullHeight));
|
|
dropButtons = new JButton[width];
|
|
for (int i = 0; i < width; i++) {
|
|
JButton button = new JButton();
|
|
button.addActionListener(this);
|
|
button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
|
|
dropButtons[i] = button;
|
|
panel2.add(button, BorderLayout.PAGE_START);
|
|
}
|
|
tiles = new JTextPane[width * height];
|
|
readTiles();
|
|
container.add(panel2);
|
|
|
|
infoLabel = new JLabel();
|
|
infoLabel.setPreferredSize(new Dimension(fullWidth, BUTTON_HEIGHT));
|
|
infoLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
|
container.add(infoLabel);
|
|
|
|
eventLabel = new JLabel();
|
|
eventLabel.setPreferredSize(new Dimension(fullWidth, BUTTON_HEIGHT));
|
|
eventLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
|
container.add(eventLabel);
|
|
|
|
frame.validate();
|
|
frame.pack();
|
|
frame.setVisible(true);
|
|
this.currentPlayer = board.nextPlayer();
|
|
displayMessage("It's " + this.currentPlayer + "'s turn.");
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (e.getSource() == this.resetButton) {
|
|
this.board.flush();
|
|
displayBoard();
|
|
this.currentPlayer = this.board.nextPlayer();
|
|
displayMessage("It's " + this.currentPlayer + "'s turn.");
|
|
}
|
|
if (!(this.board.won() || this.board.isFull())) {
|
|
for (int i = 0; i < this.dropButtons.length; i++) {
|
|
if (e.getSource() == this.dropButtons[i]) {
|
|
if (dropObject(i)) {
|
|
displayBoard();
|
|
if (this.board.won()) {
|
|
GameObject winnerObject = this.board.get(i, this.board.getTopObject(i));
|
|
displayMessage(((Token)winnerObject).getOwner() + " won.");
|
|
} else {
|
|
triggerEvents();
|
|
if (board.isFull()) {
|
|
displayMessage("It's a draw");
|
|
} else {
|
|
this.currentPlayer = board.nextPlayer();
|
|
displayMessage("It's " + this.currentPlayer + "'s turn.");
|
|
}
|
|
}
|
|
} else {
|
|
displayMessage("That column is full.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void triggerEvents() {
|
|
if (bomb.rollDie(this.board, 2)) {
|
|
eventLabel.setText(bomb.announcement());
|
|
displayBoard();
|
|
} else if (block.rollDie(this.board, 1)) {
|
|
eventLabel.setText(block.announcement());
|
|
displayBoard();
|
|
} else {
|
|
eventLabel.setText("");
|
|
}
|
|
}
|
|
}
|