subject

6.48 programming project 1: encode/decode tic -tac-toe For this problem we will decode an integer representation of a tic-tac-toe game board and determine if there is a winner. The 9 element board is defined as:
We can store this board as a list boardList=[b0,b1,b2,b3,...,b8].

Each board location b0..b8 can take on values in 0,1,2:

0 : indicates board location is empty, marked by a '-'
1 : indicates board location belongs to 'X'
2 : indicates board location belongs to '0'
For example, the board

would be represented by boardList=[0, 1, 0, 0, 1, 0, 2, 1, 2].

We can encode a particular tic-tac-toe configuration by assigning an integer representation to the corresponding boardList, treating it as a 9 digit base 3 number:

For the example above ( boardList=[0, 1, 0, 0, 1, 0, 2, 1, 2]) we find that nBoard=2291. Note that in the exponent, we subtract 8-i in order to make element 0 of boardList be the most significant digit. To decode this integer representation of the tic-tac-toe board, we reverse the process (code shown in the template).

For this problem, you should input an integer value specifying the state of the board. Decode this integer to find the board it represents, and print that board out. Then, check if either player has a won.

For example if the input is 1815, the corresponding board list would be : [0, 0, 2, 1, 1, 1, 0, 2, 0], and the board would be:

- - O
X X X
- O -
You need to write (at least) two functions:

(1) decodeBoard takes an integer as input and returns the corresponding board.

(2) checkWinner takes a board (list) as input and returns either '-' (no winner), 'X' (X wins), or 'O' (O wins).

The declarations for both functions are given in the template.

You may use more functions if you find that helpful for your implementation, but only these two will be explicitly checked.

def findWinner(boardList):
# return 'X' if X wins, 'O' if O wins and '-' if no one wins
pass

def decodeBoard(nBoard):
# nBoard is an integer representing a 9 digit base 3 number
# 0 means '-' (no player in that spot), 1 means 'X' (X in that spot), 2 means 'O' (O in that spot)
# boardList[0]=location (0,0) on board and is most significant digit, boardList[8]=location(2,2) on board
# and is least significant digit

boardList=[]
nRemainder=nBoard # divide and subtract
for digit in range(TODO): # todo :: modify the RANGE so it goes from 8 to 0 (inclusive)
x = nRemainder / (3**digit)
x = math. floor(x)
nRemainder = nRemainder - ( x * (3**digit) )
# todo : add digit x to the boardList
# ...
return boardList

if __name__=="__main__":
# optional - add any test code here inside this block. we do this so that when the zyBooks tests includes
# your files to call your functions directly that the testing code in this block is not invoked
pass

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 05:20
Which operating system is a version of linux?
Answers: 1
question
Computers and Technology, 23.06.2019 22:30
How many points do i need before i can send a chat
Answers: 1
question
Computers and Technology, 24.06.2019 03:30
Which explains extrinsic motivation? a)motivation in which there is a reward b)motivation that is personally satisfying c)motivation that is personally meaningful d)motivation in which the subject is interesting
Answers: 1
question
Computers and Technology, 25.06.2019 00:00
Rom is designed for computer instructions temporary storage of data processing data
Answers: 1
You know the right answer?
6.48 programming project 1: encode/decode tic -tac-toe For this problem we will decode an integer...
Questions
Questions on the website: 13722363