Skip to content

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Tic-Tac-Toe minimax starter

A small Java Swing game for a Coding Club exercise. You play X and go first. The computer plays O. Click New Game to start over.

Run it

Use JDK 17 or later. In IntelliJ IDEA, open the root pom.xml and run the main method in TicTacToe.java. You can also run these commands from the project directory:

mvn package
java -jar target/tic-tac-toe.jar

Maven is optional. With just the JDK:

javac -d out src/main/java/club/coding/minimax/Board.java src/main/java/club/coding/minimax/Minimax.java src/main/java/club/coding/minimax/TicTacToe.java
java -cp out club.coding.minimax.TicTacToe

Your task

Open Minimax.java and fill in the TODO in minimax(Board board, char turn). The starter already scores finished positions: an O win is +1, an X win is -1, and a draw is 0. It temporarily scores every unfinished position as 0, so the game is playable immediately: the computer takes an immediate win if available and otherwise picks the first open square.

The function returns a score. The provided chooseMove method uses those scores to pick a square for O.

Helper What it gives you
board.legalMoves() The playable square numbers, ordered left to right and top to bottom.
board.play(square, turn) A new board with that move applied. The original board is unchanged.
Board.other(turn) The other player, for the recursive call.

Square numbers:

0 1 2
3 4 5
6 7 8

For each legal move, score the resulting board with the other player to move. O chooses the maximum score; X chooses the minimum. Rebuild or rerun the program after editing.

Try these after the edit

  • Take a win: OO. / XX. / X.. with O to play. Square 2 wins immediately.
  • Block a win: XX. / .O. / ... with O to play. Square 2 prevents an immediate X win.
  • Try to beat it: if both players choose optimally from an empty board, the result is a draw.

The game rules live in Board.java, and Swing only handles the buttons and game display. Neither file needs to change for the exercise.

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages