Assignment description is here! Requires Pygame ๐ Tested with Python 2.7.6 Check out this Python tutorial!
-
Install Python 2.7 and Pygame with Super Install on Linux, you may be asked to accept, press Y
bash super-install.sh
The following are the files required to build your Tic-tac-toe player. Minimax and Alpha-Beta are the incomplete ones.
- common.py with constants and exceptions
- util.py with util functions
- interface.py with drawing calls
- tictactoe.py contains the game logic
- players contains all player implementations
- players/player_human.py asks human for help
- players/player_random.py picks any clear cell
- players/player_minimax.py uses minimax algorithm โ
- players/player_alphabeta.py uses alpha-beta pruning, this one is a bonus โ
- test_tictactoe.py contains the tests for code evaluation
Player โญ is the first to play, followed by โ, but your players can behave differently from each other using different arguments:
Argument | Player Type |
---|---|
human | Human ๐ป |
random | Random |
minimax | Minimax ๐ฝ๐ผ |
alphabeta | Alpha-Beta ๐ |
Use --quiet
flag to disable verbose output of movements during the game.
- Execute interface with default players
python interface.py [--quiet] [playerO playerX]
- Execute terminal with default players
python tictactoe.py [--quiet] [playerO playerX]
- Execute tests
python test_tictactoe.py
Questions are in the readme.txt to be sent together with the code.
Players require a minimal interface to play the game. They receive their symbol (O or X) in the initialization. Just remember to use self.symbol
to compare with the other cells in the board to see how you are going. The hard part is to select the best cell in the board, get_next_move(self, board)
is the method that must return a valid cell during the player turn. Cells must be an integer between 0 and 8. Let us take our friend Bob as an example. Somebody told him to start in the center, but he never got the rest of the message to understand how to continue the game:
from player import Player
class BobPlayer(Player):
def __init__(self, symbol):
super(BobPlayer, self).__init__(symbol)
def get_next_move(self, board):
return 4
The board is represented by a 1D list, filled with None
, 'X'
or 'O'
. If we consider Bob as the X player the board would be modified like this:
# 0 1 2
# 3 4 5
# 6 7 8
from common import *
player = Player_Bob('X')
board = [ None, None, None ,
None, None, None ,
None, None, None ]
move = player.get_next_move(board)
# check_valid_move
board[move] = X
- Use the methods from util.py to help the development:
find_empty_cells(board)
helps you see where is available to play.
board = [ None, 'X', 'O' , 'O', 'X', 'O' , None, None, None ] find_empty_cells(board) # Returns [ 0, 6, 7, 8 ]
find_winner(board)
helps you check if there is a winner. It returns a tuple, with the winner (None
for none) and the list of the winner movement.
board = [ None, 'X', 'O' , 'O', 'X', 'O' , None, 'X', None ] winner, move_sequence = find_winner(board) print winner # Prints 'X' print move_sequence # Prints [1, 4, 7]
- Do not try to mark several cells at once, the game provides a copy of the board for you to observe and only the position returned will make any difference.
- Start with a successor method that generates (yields?) different scenarios for you.
- Take a look at our random player or the questions if you get stuck.
- Printing is a slow process. Use it just for development/debug, and remove it before handing the assignment.
- Python creates a lot of .pyc files if not called with -B flag,
python -B script.py arguments