Stefano Chiodino :: bot.py (v2)

This bot has played 1840 games (272 wins / 1 draws / 1567 losses).

Play against this bot as X or as O.

These bots have done best aganist bot.py...

Bot P W D L Points Size From beginner One hit wonder Goldfish
Jon Bannister : : anybot_r282.py (v5) 10 10 0 0 10 974 False False True
Dan Miller : : board_bot.py 10 10 0 0 10 596 False False False
Daniel Patrick : : error_unable_to_connect4.py (v2) 10 10 0 0 10 757 False False True
Ian Ozsvald : : ian12.py 10 10 0 0 10 943 False False True
Dominik K : : lightgambler.py 10 10 0 0 10 145 False False True

...and these bots have done worst

Bot P W D L Points Size From beginner One hit wonder Goldfish
house : : anarchist.py 10 4 0 6 -2 None None None None
house : : opportunist.py 10 3 0 7 -4 None None None None
Peter Inglesby : : random.py 10 3 0 7 -4 26 False True True
James Campbell : : horrible.py (v4) 10 0 0 10 -10 246 False False True
Ronan Murphy : : loser.py 10 0 0 10 -10 105 False False True

All standings against bot.py

The code

      
# This bot looks one move ahead, and if possible it will make a move to
# block its opponent winning.  Otherwise, it picks a move at random.

import copy
import random
from typing import List

from botany_connectfour import game


def get_next_move(board: List[List[str]], token: str) -> int:
    # Always play middle column if playing first.
    if sum([col.count('X') + col.count('O') for col in board]) == 0:
        return int(len(board) / 2 + 1)

    available_moves = game.available_moves(board)

    other_token = 'O' if token == 'X' else 'X'

    opponent_winning_moves = []

    for col in available_moves:
        board1 = copy.deepcopy(board)
        game.make_move(board1, col, token)
        winner = game.check_winner(board1)
        if winner == token:
            return col
        elif winner == other_token:
            opponent_winning_moves.append(col)

    if len(opponent_winning_moves) > 0:
        return opponent_winning_moves[0]

    return random.choice(available_moves)