הוראות
יש להעתיק
את שלושת המחלקות
· ConnectFourGame
· ConnectFourGUI
· ConnectFourLogic
ולהשלים את
הפונקציות שלא מלאות במחלקה ConnectFourLogic
המחלקה ConnectFourGame
import javax.swing.SwingUtilities;
public class ConnectFourGame {
private
ConnectFourLogic logic;
private ConnectFourGUI
gui;
public
ConnectFourGame(int rows, int cols) {
logic = new
ConnectFourLogic(rows, cols);
gui = new
ConnectFourGUI(logic);
}
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.createAndShowGUI();
}
});
}
}
המחלקה ConnectFourGUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
public class ConnectFourGUI
extends JFrame {
private
ConnectFourLogic logic;
private JButton[] columnButtons;
private BoardPanel boardPanel;
private JLabel statusLabel;
public
ConnectFourGUI(ConnectFourLogic logic) {
this.logic = logic;
}
public void createAndShowGUI() {
setTitle("Connect Four");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a top panel with one button per column.
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, logic.getCols()));
columnButtons = new JButton[logic.getCols()];
for (int i = 0; i < logic.getCols(); i++) {
final int col = i;
JButton btn = new JButton("↓");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
columnButtonClicked(col);
}
});
columnButtons[i] = btn;
topPanel.add(btn);
}
// Create the board panel that draws the grid and pieces.
boardPanel = new BoardPanel();
boardPanel.setPreferredSize(new Dimension(70 * logic.getCols(), 70 * logic.getRows()));
getContentPane().setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(boardPanel, BorderLayout.CENTER);
statusLabel = new JLabel("Current
Player: Red");
add(statusLabel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void updateStatus(String msg) {
statusLabel.setText(msg);
}
// Called when a column button is clicked.
private void columnButtonClicked(int col) {
if (logic.isGameOver())
return;
if (logic.makeMove(col)) {
boardPanel.repaint();
if (logic.isGameOver()) {
int win = logic.getWinner();
String message;
if (win == -1)
message = "It's a draw!";
else {
message = "Player ";
if (win == 1) {
message += "Red ";
}
else {
message += "Yellow ";
}
message += " wins!";
}
JOptionPane.showMessageDialog(this, message);
}
else {
// Update status label with the next
player's turn.
if (logic.getCurrentPlayer() == 1) {
statusLabel.setText("Current
Player Red");
}
else {
statusLabel.setText("Current
Player Yellow");
}
}
}
}
// Inner class that draws the board.
class BoardPanel
extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int rows = logic.getRows();
int cols = logic.getCols();
int cellSize = Math.min(getWidth() / cols, getHeight() / rows);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int x = c * cellSize;
int y = r * cellSize;
// Draw cell background
g.setColor(Color.BLUE);
g.fillRect(x, y, cellSize, cellSize);
// Draw empty slot
g.setColor(Color.WHITE);
g.fillOval(x + 5, y + 5, cellSize – 10,
cellSize – 10);
int piece = logic.getPieceAt(r, c);
if (piece == 1) {
g.setColor(Color.RED);
g.fillOval(x + 5, y + 5, cellSize – 10,
cellSize – 10);
} else if (piece == 2) {
g.setColor(Color.YELLOW);
g.fillOval(x + 5, y + 5, cellSize – 10,
cellSize – 10);
}
}
}
}
}
}
המחלקה ConnectFourLogic
את המחלקה הזו עליכם להשלים על מנת
שהמשחק ירוץ כהלכה
public class
ConnectFourLogic {
private int rows;
private int cols;
private int[][] board;
private int currentPlayer; // 1 : Red, 2: Yellow
private int winner; // 0: no winner, 1: Red, 2: Yellow, -1: draw
public
ConnectFourLogic(int rows, int cols) {
this.rows = rows;
this.cols = cols;
board = new int[rows][cols];
currentPlayer = 1;
winner = 0;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public int getPieceAt(int row, int col) {
return board[row][col];
}
public int getCurrentPlayer() {
return currentPlayer;
}
public int getWinner() {
return winner;
}
public boolean isBoardFull() {
// להשלים.
}
public boolean isGameOver() {
// להשלים.
}
// Drops a piece in the given column.
// Returns the row index where the piece landed, or -1 if the column is
full.
private int dropPiece(int col) {
// להשלים.
}
// Makes a move in the given column.
// Returns false if the move is invalid.
public boolean makeMove(int col) {
// להשלים.
}
// Check whether the last move at (lastRow, lastCol) resulted in a win.
private boolean checkWin(int lastRow, int lastCol) {
int player = board[lastRow][lastCol];
// Check horizontal
// להשלים.
// Check vertical
// להשלים.
// Diagonal (top-left to bottom-right)
// להשלים.
// Diagonal (bottom-left to top-right)
// להשלים.
return false;
}
}
הקוד להרצה בתוכנית הראשית:
public static void main(String[] args) {
ConnectFourGame game = new ConnectFourGame(6, 7);
game.run();
}