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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!