Dan Miller :: board_bot.py

This bot has played 1450 games (258 wins / 6 draws / 1186 losses).

Play against this bot as X or as O.

These bots have done best aganist board_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
Dilwoar Hussain : : dee-bot.py (v12) 10 10 0 0 10 306 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 : : centrist.py 10 0 0 10 -10 None None None None
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
Luke Spademan : : lossingbot.py (v3) 10 0 0 10 -10 129 False False True
Peter Inglesby : : random.py 10 0 0 10 -10 26 False True True

All standings against board_bot.py

The code

      
import copy
import random
from botany_connectfour import game


def get_next_move(board, move_list, token, state):
    print("Board:", board)
    print("Move List:", move_list)
    print("Token:", token)
    print("State:", state)
    available_moves = game.available_moves(board)

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

    for col in available_moves:
        board1 = copy.deepcopy(board)
        game.make_move(board1, col, other_token)
        if game.check_winner(board1) is not None:
            return col

    for col in available_moves:
        best_moves = []
        horizontal_moves = []
        vertical_moves = []
        board1 = copy.deepcopy(board)
        board1[col] = update_column(board1[col], token)
        vertical_win, vertical_count = vertical(board1[col], token, other_token)
        horizontal_win, horizontal_count = horizontal(board, col, token, other_token)
        if vertical_win and horizontal_win:
            best_moves.append((col, vertical_count+horizontal_count))
        elif horizontal_win:
            horizontal_moves.append((col, horizontal_count))
        elif vertical_win:
            vertical_moves.append((col, vertical_count))
        best_moves.sort(reverse=True)
        horizontal_moves.sort(reverse=True)
        vertical_moves.sort(reverse=True)
        if best_moves:
            print("Best Moves:", best_moves)
            return best_moves[0][0]
        elif horizontal_moves:
            print("Horizontal Moves:", horizontal_moves)
            return horizontal_moves[0][0]
        elif vertical_moves:
            print("Vertical Moves:", vertical_moves)
            return vertical_moves[0][0]
        else:
            print("Available Moves:", available_moves)
            random.choice(available_moves), "cheese"

def update_column(column, token):
    for number, space in enumerate(column):
        if space == ".":
            column[number] = token
            return column

def vertical(column, token, other_token):
    count = 0
    remaining_spaces = 0
    for space in column:
        if space == token:
            count += 1
        elif space == other_token:
            count = 0
        else:
            remaining_spaces += 1
    if count + remaining_spaces < 4:
        return False, count
    else:
        return True, count

def horizontal(board, column, token, other_token):
    count = 0
    print("Available Move:", column)
    for row, space in enumerate(board[column]):
        if space == ".":
            current_row = row
            print("Current Row:", current_row)
            break
    horizonal_line = []
    for column in board:
        horizonal_line.append(column[current_row])
    # for column, space in enumerate(horizonal_line):
    #     if current_row != 0:
    #         if board[column][current_row-1] == ".":
    #             horizonal_line[column] = "N"
    # print("Horizontal Line:", horizonal_line)
    print(horizonal_line)
    for space in horizonal_line:
        if space == "." and horizonal_line.count(other_token) < 4 and horizonal_line[3] != other_token:
            return True, horizonal_line.count(token) + horizonal_line.count(".")
    return False, count