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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 07:20, Hcalhoun21
Write a pseudocode solution for each of these problems. 1. design a while loop that lets that user enter a number. the number should be multiplied by 10, and the result stored in a variable named product. the loop should iterate as long as product contains a value less than 100. 2. design a do-while loop that asks the user to enter two numbers. the numbers should be added and the sum displayed. the loop should ask the user whether he or she wishes to perform the operation again. if so, the loop should repeat; otherwise it should terminate. 3. design a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 100. 4. design a nested loop that displays 10 rows of # characters. there should be 15 # characters in each row. 5. convert this for loop to a while loop. declare integer count for count = 1 to 50 display count end for 6. find the error in the following pseudocode. declare boolean finished = false declare integer value, cube while not finished display “enter a value to be cubed.” input value; set cube = value ^ 3 display value, “ cubed is “, cube end while
Answers: 2
image
Computers and Technology, 22.06.2019 08:00, dramaqueenactr2040
Two technicians are discussing the common u-joint. technician a says its input and output speeds should be equal. technician b says that it normally has two yokes. which technician is correct?
Answers: 1
image
Computers and Technology, 22.06.2019 09:50, laurentsofia09
Assume that you have an sorted array of records. assume that the length of the array (n) is known. give two different methods to search for a specific value in this array. you can use english or pseudo-code for your algorithm. what is the time complexity for each algorithm and why?
Answers: 1
image
Computers and Technology, 22.06.2019 15:30, tfornwalt4390
Melissa needs to add a topic to an email that she will send to her teacher. choose the name of the field where she should type her topic.
Answers: 2
You know the right answer?
6.48 programming project 1: encode/decode tic -tac-toe For this problem we will decode an integer...

Questions in other subjects:

Konu
Mathematics, 04.08.2021 03:40
Konu
Mathematics, 04.08.2021 03:40