Computers and Technology

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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 18:00, EinsteinBro
Kyle wants to access his school’s home page. how can he do this?
Answers: 1
image
Computers and Technology, 21.06.2019 21:30, willwhitlock803
Write code using c . (take input from user) calculate the size of a given file in kbs. in this task you will complete the function with the following prototype: float get_file_size(char * filename); the function takes the file name (address to the start of a null terminated character array) as input. the function should then open the file and find the number of bytes it contains till eof. the number of bytes divided by 1024 will give the size in kbs. if the file cannot be opened the function should return -1.
Answers: 2
image
Computers and Technology, 22.06.2019 11:00, paigesyring
How does a policy manual an organization? a. it boost productivity. b. it create awareness in employees about the organization’s values. c. it employees achieve targets. d. it safeguards the organization from liabilities.
Answers: 1
image
Computers and Technology, 22.06.2019 18:10, AdoNice
How can i delete permalinks from a word press site?
Answers: 1
Do you know the correct answer?
6.48 programming project 1: encode/decode tic -tac-toe For this problem we will decode an integer...

Questions in other subjects: