Project BE1 — Spider Game
S7 Inf A3 — Java for Graphical and Mobile Programming
Stéphane Derrode — Centrale Lyon
Part 2 — GUI with Swing
Prerequisite: TD4 (Swing layouts & events) and TD5 (GridPanel with mouse input).
Duration: 2h autonomous session + personal work.
Submission: Moodle — zip of the Maven project + PDF report (see Section 5).
1. Game Rules
Spider is a two-player abstract strategy game on a 3×3 grid.
Setup: Each player has 3 pieces. Player 1 uses blue pieces, Player 2 uses red pieces.
Phase 1 — Placement (3 turns each, alternating): - Players take turns placing one piece on any empty cell. - Player 1 places first. - The phase ends when all 6 pieces have been placed (3 cells remain empty).
Phase 2 — Movement (alternating turns): - On each turn, a player selects one of their own pieces, then moves it to an adjacent empty cell. - Adjacency: horizontal or vertical only (not diagonal). - A player cannot pass their turn — they must move if a move is available.
Win condition: A player wins when their 3 pieces occupy any complete row, column, or diagonal.
Win examples for Player 1 (●):
Row: Column: Diagonal:
● ● ● ● . . ● . .
. . . ● . . . ● .
. . . ● . . . . ●
2. Architecture
You must follow the class structure shown in this UML diagram:
SpiderGame ──────────────── GamePanel (extends JPanel)
│ 1 1 │
│ has │ observes
│ ↓
├──── Board ◇────────── Cell (×9)
│ 1 0..1
│ has │
│ │ may contain
│ ↓
└──── Player (×2) ────── Piece (×3 per player)
1 owns 3
| Class | Role | Key fields |
|---|---|---|
SpiderGame |
Controller — starts the game, manages turns | Board board, Player[] players, int currentPlayer |
Board |
Model — the 3×3 grid | Cell[][] cells |
Cell |
One cell of the grid | int row, int col, Piece piece (nullable) |
Piece |
A game piece | Player owner, Cell cell (current position) |
Player |
A player | String name, Color color, Piece[] pieces |
GamePanel |
View — draws the board, captures mouse clicks | extends JPanel |
Rule:
Board,Cell,Piece, andPlayermust contain no Swing code (no imports fromjavax.swingorjava.awt). All rendering lives inGamePanel.
3. Implementation Guide
3.1 Start with the model (no Swing)
Implement and test Board, Cell, Piece, and Player without any UI first.
You can write a simple main() to verify the logic in the console.
Suggested order:
1. Player — constructor, name, color
2. Piece — constructor, current cell reference
3. Cell — constructor, isEmpty(), place(Piece), remove()
4. Board — constructor (create 9 Cell objects), getCell(row, col), checkWin(Player)
Win detection — suggested approach:
public boolean checkWin(Player p) {
// check 3 rows, 3 columns, 2 diagonals
// a line is a win if all 3 cells are occupied by p's pieces
}
3.2 Build on your TD5 GridPanel
Your GamePanel extends the GridPanel from TD5. You replace the simple int[][] board with a reference to the real Board object.
In paintComponent():
- Call super.paintComponent(g) to clear the background
- Draw the grid lines (reuse your TD5 code)
- For each cell: if it contains a piece, draw a filled circle in the piece owner's color
- Highlight the selected cell (the piece the current player wants to move)
- Display the current player's name at the top or bottom of the panel
3.3 Game flow in SpiderGame
SpiderGame constructor:
create Board
create Player 1 (blue) and Player 2 (red), each with 3 Piece objects
create GamePanel
build JFrame around GamePanel
add JLabel to show whose turn it is
set currentPlayer = 0 (Player 1 starts)
phase = PLACEMENT
On cell click (passed from GamePanel to SpiderGame):
if phase == PLACEMENT:
if cell is empty:
place current player's next unplaced piece
switch player
check if all 6 pieces placed → phase = MOVEMENT
else (MOVEMENT):
if no piece selected yet:
if clicked cell has current player's piece → select it
else:
if clicked cell is empty and adjacent to selected:
move selected piece there, deselect
check win → if won, show dialog and offer restart
switch player
else:
deselect (cancel the move)
repaint GamePanel
3.4 End of game
When a win is detected:
int choice = JOptionPane.showConfirmDialog(
gamePanel,
players[currentPlayer].getName() + " wins!\nPlay again?",
"Game Over",
JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION)
resetGame();
else
System.exit(0);
4. Minimum Requirements
To obtain a passing grade, the following must work:
- [ ] The window opens with a 3×3 grid and two player labels
- [ ] Phase 1: clicking places pieces alternately, blue and red
- [ ] Phase 2: clicking a piece selects it (highlighted), then clicking an adjacent empty cell moves it
- [ ] Win detection works for all 8 winning lines
- [ ] A dialog announces the winner and offers a rematch
5. Deliverable
Submit on Moodle before the deadline a single zip containing: - Your complete Maven project (all source files) - A PDF report (1–2 pages) containing: - Your final UML class diagram with attributes and methods - A brief description of any design choices or difficulties encountered
Zip naming: BE1_NomPrenom1_NomPrenom2.zip
Tips
- Start with Phase 1 only — make piece placement work perfectly before tackling movement.
- Test win detection with all 8 cases before connecting the UI.
Cell.isEmpty()is used in many places — make sure it is correct.- If a player has no valid moves in Phase 2, you can simply show a message and skip their turn (this situation is rare but possible).