Question

In: Computer Science

In this question, you are going to implement a human vs. human version of Notakto. Notakto...

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

Solutions

Expert Solution

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.


Related Solutions

You are going to set up a small shopping system, where you are going to implement...
You are going to set up a small shopping system, where you are going to implement a "shopping bag" subject to the following specifications: Each item that will be placed in the bag needs three pieces of data included: a descriptive name, an item code (2 letters followed by two numbers), and price (in US dollars). You will implement items as a separate class, where these three properties are private, and there are mutators and accessors for all three properties,...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to parent pointer instead of child pointer, ie. parent of top is null) of Kruskal's Minimum Spanning Tree with adjacency list and min-heap as the additional data structure. Note: Please implement it in C and also keep the above conditions in mind. You can take your time. Thank you.
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Implement a version of merge() that copies the second half of a[] to aux[] in decreasing...
Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order and then does the merge back to a[]. This change allows you to remove the code to test that each of the halves has been exhausted from the inner loop. Note: the resulting sort is not stable. Algorithms Fourth Edition Exercise 2.2.10
Implement a version with the outer loop, with a while loop, and the inner loop with...
Implement a version with the outer loop, with a while loop, and the inner loop with a do / while loop. Modify this program as he ask ↑↑↑ C++ // This program averages test scores. It asks the user for the 2 // number of students and the number of test scores per student. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 7 int main() 8 { 9 int numStudents, // Number of students 10 numTests; //...
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
(C++ with main to test) 2.2 Task 1 You are going to implement a variant of...
(C++ with main to test) 2.2 Task 1 You are going to implement a variant of the standard linked list, the circular list. The circular linked list is a variant of the standard linked list that wraps around so that traversals through the list will wrap around to the beginning instead of simply reaching the end. This will consist of implementing two classes: cLL and item. 2.2.1 cLL The class is described according to the simple UML diagram below: 2cLL<T>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT