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, 22.06.2019 02:40
Respecting individual differencespre-test active2time remaining48: 47nina is exploring her gender identity and sexual orientationwhich best describes how the role of the media might influence nina during this time in her life? the media would her decide because television shows are always unbiased about gender issues and do notstereotypethe media could make things difficult for her because television sometimes portrays rigid ideas about gender roles.all forms of media will her decide because the media always portrays rigid ideas about gender roles.all forms of media will make things easy for her because the media always portrays open and fair ideas about gender roles.
Answers: 1
question
Computers and Technology, 23.06.2019 21:40
Simon says is a memory game where "simon" outputs a sequence of 10 characters (r, g, b, y) and the user must repeat the sequence. create a for loop that compares the two strings. for each match, add one point to user_score. upon a mismatch, end the game. sample output with inputs: 'rrgbryybgy' 'rrgbbrybgy'
Answers: 3
question
Computers and Technology, 24.06.2019 11:00
The program below has been generalized to read a user's input value for hourlywage. run the program. notice the user's input value of 10 is used. modify that input value, and run again. generalize the program to get user input values for workhoursperweek and workweeksperyear (change those variables' initializations to 0). run the program. monthsperyear will never change, so define that variable as final. use the standard for naming final variables. ex: final int max_length
Answers: 2
question
Computers and Technology, 24.06.2019 15:30
Python. primary u.s. interstate highways are numbered 1-99. odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. thus, the 405 services the 5, and the 290 services the 90. given a highway number, indicate whether it is a primary or auxiliary highway. if auxiliary, indicate what primary highway it serves. also indicate if the (primary) highway runs north/south or east/west.
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
question
Biology, 24.08.2019 14:10
question
Mathematics, 24.08.2019 14:10
question
Mathematics, 24.08.2019 14:10
question
Mathematics, 24.08.2019 14:10
Questions on the website: 13722361