Question
PYTHON CODE Write a class 'Board' (in Python), representing the board 3x3 in tic-tac-toe, in which each field can be empty or occupied by the
PYTHON CODE
Write a class 'Board' (in Python), representing the board 3x3 in tic-tac-toe, in which each field can be empty or occupied by the symbol of one of the players (O or X).
Class should include:
1. __init__(self) - creates an initial empty board
2. move(self,x,y) - makes the active player move (i.e. X if there are fewer X on the board than a O or O otherwise; move consists of placing the active player symbol on the board field in column X and row y)
(i) if the field is already taken, the method throws an exception
(ii) if the move was successful for the active player, the method returns the player's symbol or None otherwise
(iii) if the move is made on a board where one of the players has already won, the method throws an exception
3. reset(self) - removes all symbols from the board (condition at the beginning of the game )
4. __str__(self) - returns a string representing the board
Test:
board = Board( ) board .move ( 2 , 2) board .move ( 2 , 1) board .move ( 1 , 2) board .move ( 3 , 1)
res = board .move ( 3 , 2) assert res == O
# board .move (2 , 2) #EXPECT board.reset( )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started