In: Computer Science
In this question, you are going to implement a human vs. human version of Notakto.
Notakto is a tic-tac-toe variant. It is played across three 3 x 3 boards: Board A, board B and board C. When you start the game you should output the boards as follows.
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1:
There are two players: Player 1 and player 2. Player 1 always
starts. Both players play the same piece: X. E.g., let player 1
choose location 6 on board A, i.e., the user will enter A6. The
output of the program should be as follows (bold font represents
user input).
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1: A6
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X 7 8 6 7 8 6 7 8
Player 2:
Each player takes turn placing an X on the board in a vacant space
(a space not already occupied by an X).
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1: A6
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X 7 8 6 7 8 6 7 8
Player 2: A7
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X X 8 6 7 8 6 7 8
Player 1:
If a board has three X in a row, column, or diagonal, the board is
dead and it cannot be played anymore. It should not be displayed
anymore. E.g., in the following, board A becomes dead and is not
displayed anymore.
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1: A6
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X 7 8 6 7 8 6 7 8
Player 2: A7
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X X 8 6 7 8 6 7 8
Player 1: A8
B C
0 1 2 0 1 2
3 4 5 3 4 5
6 7 8 6 7 8
Player 2:
The game ends when all the boards contain three X in a row, column,
or diagonal, at which point the player to have made the last move
loses the game. Unlike tic-tac-toe, there will always be a player
who wins any game of Notakto.
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1: A6
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X 7 8 6 7 8 6 7 8
Player 2: A7
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
X X 8 6 7 8 6 7 8
Player 1: A8
B C
0 1 2 0 1 2
3 4 5 3 4 5
6 7 8 6 7 8
Player 2: B0
B C
X 1 2 0 1 2
3 4 5 3 4 5
6 7 8 6 7 8
Player 1: B4
B C
X 1 2 0 1 2
3 X 5 3 4 5
6 7 8 6 7 8
Player 2: C0
B C
X 1 2 X 1 2
3 X 5 3 4 5
6 7 8 6 7 8
Player 1: C4
B C
X 1 2 X 1 2
3 X 5 3 X 5
6 7 8 6 7 8
Player 2: C8
B
X 1 2
3 X 5
6 7 8
Player 1: B8
Player 2 wins game
Note that you should check for legal moves. If the users enters
something illegal you should prompt them again. Let's play a new
game to illustrate this.
A B C
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 1: C0
A B C
0 1 2 0 1 2 X 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
Player 2: B9
Invalid move, please input again
Player 2: fds
Invalid move, please input again
Player 2: C0
Invalid move, please input again
Player 2: C6
A B C
0 1 2 0 1 2 X 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 X 7 8
Player 1: C6
Invalid move, please input again
Player 1: C3
A B
0 1 2 0 1 2
3 4 5 3 4 5
6 7 8 6 7 8
Player 2: C2
Invalid move, please input again
Player 2:
Implement the game and try to pass all test cases. The list of test
cases is not complete. We may add more test cases when marking
after the deadline.
Yo
I want the answer in python
def createBoard(): #method to create empty 3x3 board of
0-8
Y = []
count = 0
for i in range(3):
temp = []
for j in range(3):
temp.append(count)
count = count + 1
Y.append(temp)
return Y
def display(b,stat): #method to display those boards
that are alive
if(stat[0]):
print("A ",end = "")
if(stat[1]):
print("B ",end = "")
if(stat[2]):
print("C ",end = "")
print("")
for i in range(3):
for j in range(3):
if(stat[j] == False):
continue
for k in range(3):
print(b[j][i][k]," ",end = "")
print("")
def checkBoard(b,s): #method to check if 3 consecutive X
are there in any board. If there is, update status and
return.
for i in range(3):
if(s[i] == False):
continue
for j in range(3):
if(b[i][j][0] == 'X' and b[i][j][2] == 'X' and b[i][j][1] == 'X'):
#checking rows
s[i] = False
for j in range(3):
if(b[i][1][j] == 'X' and b[i][2][j] == 'X' and b[i][0][j] == 'X'):
#checking columns
s[i] = False
if(b[i][0][0] == 'X' and b[i][1][1] == 'X' and b[i][2][2] == 'X'):
#checking principal diagonal
s[i] = False
if(b[i][2][0] == 'X' and b[i][1][1] == 'X' and b[i][0][2] == 'X'):
#checking other diagonal
s[i] = False
return s
def play(): #method to implement natokto
#creating 3 boards
A = createBoard()
B = createBoard()
C = createBoard()
#then adding them in one
board = []
board.append(A)
board.append(B)
board.append(C)
#status signifies alive boards
status = [True,True,True]
pnum = 0 #current player
bdict = {"A":0,"a":0,"B":1,"b":1,"C":2,"c":2} #dictionary
to identify board from input
bnum = 0 #board number
cell = 0 #cell number 0-8
while(True):
display(board,status) #display board
print("Player ",(pnum+1),": ",end = "")
inp = input()
#display error and continue if
#there are not exactly 2 characters in
input
if(len(inp) != 2):
print("Invalid move. Please try again.")
continue
#first character is not A/B/C and/or cell number is not
integer
try:
bnum = bdict[inp[0]]
cell = int(inp[1])
except (ValueError,KeyError):
print("Invalid move. Please try again.")
continue
#cell number is not in range
if(cell<0 or cell>8):
print("Invalid move. Please try again.")
continue
#finding row and column from cell number
row = cell//3
col = cell%3
#update that specific position in board
board[bnum][row][col] = 'X'
#next player
pnum = (pnum + 1)%2
#update status
status = checkBoard(board,status)
#if all boards are dead, we've reached the
end
if True not in status:
break
winner = pnum + 1
loser = (pnum + 1)%2 + 1
print("\n\nPlayer ",winner," wins! Player ",loser," loses.")
#calling play() to play notakto
play()
Code screenshot for indentation help:
Sample execution:
(I've not shown the complete execution as it'll be pretty long)
In case of any doubt, drop a comment and I'll surely get back to you.
Please give a like if you're satisfied with the answer. Thank you.