Problem 626085 · hard · Level 06 Heuristics & Optimization

Four in a Row: Beat the House

minimax · alpha-beta pruning · evaluation function · game bots

Write a bot for four in a row on a small board: 6 columns and 5 rows. Players take turns dropping a stone into a column; it falls to the lowest free cell. Four of your stones in a line (across, down or diagonal) wins. A full board is a draw.

move(board, me) gets the board and your stone ("X" or "O", and "X" moves first) and returns the column (0 to 5) to play. board is a list of 5 strings, the top row first, with "." for an empty cell. An illegal move (a full column, or not a number from 0 to 5) loses the game.

The helpers empty_board(), drop(board, col, stone) and winner(board) are available in your code. The tests run play_matches(move, games, seed): your bot plays the house bot, which wins when it can, blocks your immediate wins, avoids moves that let you win at once, and otherwise picks one of the central columns at random. The two bots take turns to start.

How this problem is scored

A result passes if your bot takes at least half of the points (a win is one point, a draw half a point). Its quality is the share of points you took, from 50 to 100.

Examples

Input:  play_matches(move, 6, 1)
Output: {"wins": ..., "draws": ..., "losses": ..., "games": 6}
        passes with at least 3 points

Constraints

  • Each move must be quick: a test plays up to 8 games, and a test must finish within about a second in your browser. Search a few moves ahead, not to the end of the game.

Goals

  • Search a game tree to a fixed depth
  • Score unfinished positions with an evaluation function
  • Prune branches that cannot change the decision
Starting Python…