Exemplo n.º 1
0
    def test_col_win(self):
        connect_four = ConnectFour()
        connect_four.board = [
           [1, 2, 1, 0, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
        ]
        self.assertFalse(connect_four.col_win(PLAYER_1, 5, 0))

        connect_four.board = [
           [1, 2, 1, 1, 1, 1, 1],
           [1, 1, 2, 1, 2, 1, 1],
           [1, 2, 1, 2, 1, 2, 1],
           [1, 1, 2, 1, 2, 1, 1],
           [2, 1, 1, 1, 1, 1, 1],
           [2, 1, 2, 1, 2, 1, 1],
        ]
        self.assertTrue(connect_four.col_win(PLAYER_1, 0, 0))
        self.assertTrue(connect_four.col_win(PLAYER_1, 1, 0))
        self.assertTrue(connect_four.col_win(PLAYER_1, 2, 0))
        self.assertTrue(connect_four.col_win(PLAYER_1, 3, 0))

        self.assertTrue(connect_four.col_win(PLAYER_1, 0, 6))
        self.assertTrue(connect_four.col_win(PLAYER_1, 1, 6))
        self.assertTrue(connect_four.col_win(PLAYER_1, 2, 6))
        self.assertTrue(connect_four.col_win(PLAYER_1, 3, 6))
        self.assertTrue(connect_four.col_win(PLAYER_1, 4, 6))
        self.assertTrue(connect_four.col_win(PLAYER_1, 5, 6))
Exemplo n.º 2
0
    def test_row_win(self):
        connect_four = ConnectFour()
        connect_four.board = [
           [1, 2, 1, 0, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
        ]
        #self.assertFalse(connect_four.row_win(PLAYER_1, 5, 0))

        connect_four.board = [
           [1, 2, 1, 1, 1, 1, 2],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 1, 1, 1, 1, 1, 1],
           [2, 1, 2, 1, 2, 1, 2],
        ]
        self.assertTrue(connect_four.row_win(PLAYER_1, 0, 3))
        self.assertTrue(connect_four.row_win(PLAYER_1, 0, 2))
        self.assertTrue(connect_four.row_win(PLAYER_1, 0, 4))
        self.assertTrue(connect_four.row_win(PLAYER_1, 0, 5))

        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 0))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 1))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 2))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 3))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 4))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 5))
        self.assertTrue(connect_four.row_win(PLAYER_1, 4, 6))
Exemplo n.º 3
0
def simulateGame(player, opponent):
    # Returns fitness delta
    game = ConnectFour()
    illegal_moves = 0
    random_moves_left = 3
    while not game.isFinished():

        # TODO track stats?

        if game.isOurTurn():
            try:
                pickAndMakeMove(game, player)
            except IndexError:
                illegal_moves += 1
                if illegal_moves >= NUMBER_ILLEGAL_MOVES_ALLOWED:
                    # Penalise player
                    return -NUMBER_TO_SAMPLE
                else:
                    continue

            if game.isFinished():
                break

            if random_moves_left > 0:
                pickAndMakeMove(game, agents.RandomAgent())
                random_moves_left -= 1
            else:
                try:
                    pickAndMakeMove(game, opponent)
                except IndexError:
                    pickAndMakeMove(game, agents.RandomAgent())
        else: # Not our turn
            if random_moves_left > 0:
                pickAndMakeMove(game, agents.RandomAgent())
                random_moves_left -= 1
            else:
                try:
                    pickAndMakeMove(game, opponent)
                except IndexError:
                    pickAndMakeMove(game, agents.RandomAgent())

            if game.isFinished():
                break

            try:
                pickAndMakeMove(game, player)
            except IndexError:
                illegal_moves += 1
                if illegal_moves >= NUMBER_ILLEGAL_MOVES_ALLOWED:
                    # Penalise player
                    return -NUMBER_TO_SAMPLE
                else:
                    continue

    # Game is finished (or illegal move made)

    # TODO debug prints, or stats

    return game.score()
Exemplo n.º 4
0
    def test_horizontal_win_down(self):
        connect_four = ConnectFour()
        connect_four.board = [
           [1, 2, 1, 0, 1, 2, 1],
           [1, 2, 1, 0, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 0, 1, 2, 1],
           [1, 2, 1, 0, 1, 2, 1],
        ]
        self.assertFalse(connect_four.horizontal_win_down(PLAYER_1, 5, 0))

        connect_four.board = [
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
           [1, 2, 1, 2, 1, 2, 1],
           [2, 1, 2, 1, 2, 1, 2],
        ]
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 0, 0))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 1, 1))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 2, 2))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 3, 3))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 4, 4))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 5, 5))

        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 0, 1))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 1, 2))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 2, 3))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 3, 4))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 4, 5))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 5, 6))

        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 0, 2))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 1, 3))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 2, 4))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 3, 5))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 4, 6))

        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 0, 3))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 1, 4))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 2, 5))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 3, 6))

        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 1, 0))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 2, 1))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 3, 2))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 4, 3))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_2, 5, 4))

        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 2, 0))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 3, 1))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 4, 2))
        self.assertTrue(connect_four.horizontal_win_down(PLAYER_1, 5, 3))  
Exemplo n.º 5
0
 def test_game_draw(self):
    connect_four = ConnectFour()
    connect_four.board = [
        [1, 2, 1, 2, 1, 2, 1],
        [1, 2, 1, 2, 1, 2, 1],
        [2, 1, 2, 1, 2, 1, 2],
        [2, 1, 2, 1, 2, 1, 2],
        [1, 2, 1, 2, 1, 2, 1],
        [2, 1, 2, 1, 2, 1, 2],
    ]
    self.assertTrue(connect_four.is_game_draw())
    self.assertEqual(connect_four.check_game_status(PLAYER_1, 1, 1), GAME_STATUS_DRAW)
Exemplo n.º 6
0
 def test_game_incomplete(self):
     connect_four = ConnectFour()
     connect_four.board = [
        [1, 2, 1, 0, 1, 2, 1],
        [1, 2, 1, 2, 1, 2, 1],
        [2, 1, 2, 1, 2, 1, 2],
        [2, 1, 2, 1, 2, 1, 2],
        [1, 2, 1, 2, 1, 2, 1],
        [2, 1, 2, 1, 2, 1, 2],
     ]
     self.assertFalse(connect_four.is_game_draw())
     self.assertEqual(connect_four.check_game_status(PLAYER_1, 1, 1), GAME_INCOMPLETE)
Exemplo n.º 7
0
from __future__ import print_function
import pickle  # pip install cloudpickle
import os
import sys

from game import ConnectFour
import agents

from utilities import pickMove, pickAndMakeMove

game = ConnectFour()
if len(sys.argv) >= 2:
    print("Using opponent from {}".format(sys.argv[1]))
    with open(sys.argv[1], 'rb') as output:
        opponent = pickle.load(output)
else:
    print("Using random agent")
    opponent = agents.RandomAgent()

while not game.isFinished():
    if game.isOurTurn():
        print(game)
        possibles = game.possibleMoves()
        column = input("Which column {}? ".format(possibles))
        try:
            game.playMove(int(column))
        except Exception as e:
            print("error occurred", e)
    else:
        try:
            pickAndMakeMove(game, opponent)