g Write a Tic-Tac-Toe game, which will be played and drawn with text characters on the console. This assignment is based on exercise 8.19, but the specifications here will be more specific.

Answer :

mateolara11

Hi, you haven't provided a programing language, therefore, we will make the game using python and you can extend it to any programming language using the explanation provided.  

Answer:

class TicTacToe():

   def __init__(self):

       self.board = '  1 2 3\nA|_|_|_|\nB|_|_|_|\nC|_|_|_|'

       self.position = [0,0,0,0,0,0,0,0,0]

       self.representation = (('A1','A2','A3','B1','B2','B3','C1','C2','C3'),\

                                                 (10,12,14,19,21,23,28,30,32))

       print(self.board)

       

   def check(self, movement):

       try:

           self.index = self.representation[0].index(movement)

           self.value = self.representation[1][self.index]

           if self.position[self.index] != 0:  

               print('Occupied Position')

               return False # No free space to play

           else:

               return True

           

       except ValueError:

           print('Invalid Input')

           return False # Input error  

       

   def printBoard(self, simbol):

       self.simbol = simbol  

       self.board = self.board[:(self.value)]+simbol+self.board[(self.value+1)::]

       print(self.board)

       self.position[self.index] = simbol

       

game = TicTacToe()

f = 0

while True:

   if f == 0:

       p1 = input("Player1: ").upper()

       status = game.check(p1)

       if status:

           game.printBoard('x')

           f = 1

           victory = input("Player one won? y/n: ").lower()

           if victory == 'y':

               print("Player one wins !!!\nEnd of he game")

               break

   elif f == 1:  

       p2 = input("Player2: ").upper()

       status = game.check(p2)

       if status:

           game.printBoard('o')

           f = 0

           victory = input("Player two won? y/n: ").lower()

           if victory == 'y':

               print("Player two wins !!!\nEnd of he game")

               break

           

   if not(0 in game.position): #no more possible movments  

       print('End of the game')

       break  

Explanation:

With the init method we define our basic text-board, a position array that stores the movements and a tuple for storing the possible movements and the places in the text-board where the x and o are going to be located. In the check method, we verify that a given movement is possible by checking if the player inputs a valid and free position, finally the print method uses 'symbol' to locate the x or o in the corresponding position. This game doesn't implement an automatic victory checking method instead the players decide whether a player won or not.  

${teks-lihat-gambar} mateolara11
${teks-lihat-gambar} mateolara11
${teks-lihat-gambar} mateolara11
${teks-lihat-gambar} mateolara11

Other Questions