1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Leya [2.2K]
2 years ago
10

Write Tic tac toe program in python for beginners pls

Computers and Technology
1 answer:
Travka [436]2 years ago
4 0

Answer:

#Implementation of Two Player Tic-Tac-Toe game in Python.

''' We will make the board using dictionary  

   in which keys will be the location(i.e : top-left,mid-right,etc.)

   and initialliy it's values will be empty space and then after every move  

   we will change the value according to player's choice of move. '''

theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,

           '4': ' ' , '5': ' ' , '6': ' ' ,

           '1': ' ' , '2': ' ' , '3': ' ' }

board_keys = []

for key in theBoard:

   board_keys.append(key)

''' We will have to print the updated board after every move in the game and  

   thus we will make a function in which we'll define the printBoard function

   so that we can easily print the board everytime by calling this function. '''

def printBoard(board):

   print(board['7'] + '|' + board['8'] + '|' + board['9'])

   print('-+-+-')

   print(board['4'] + '|' + board['5'] + '|' + board['6'])

   print('-+-+-')

   print(board['1'] + '|' + board['2'] + '|' + board['3'])

# Now we'll write the main function which has all the gameplay functionality.

def game():

   turn = 'X'

   count = 0

   for i in range(10):

       printBoard(theBoard)

       print("It's your turn," + turn + ".Move to which place?")

       move = input()        

       if theBoard[move] == ' ':

           theBoard[move] = turn

           count += 1

       else:

           print("That place is already filled.\nMove to which place?")

           continue

       # Now we will check if player X or O has won,for every move after 5 moves.  

       if count >= 5:

           if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")                

               break

           elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break

           elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break

           elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break

           elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break

           elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break  

           elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break

           elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal

               printBoard(theBoard)

               print("\nGame Over.\n")                

               print(" **** " +turn + " won. ****")

               break  

       # If neither X nor O wins and the board is full, we'll declare the result as 'tie'.

       if count == 9:

           print("\nGame Over.\n")                

           print("It's a Tie!!")

       # Now we have to change the player after every move.

       if turn =='X':

           turn = 'O'

       else:

           turn = 'X'        

   

   # Now we will ask if player wants to restart the game or not.

   restart = input("Do want to play Again?(y/n)")

   if restart == "y" or restart == "Y":  

       for key in board_keys:

           theBoard[key] = " "

       game()

if __name__ == "__main__":

   game()

<em>HOPE THIS HELPS :)</em>

You might be interested in
Write a python program to calculate the length of any string recursively​
Solnce55 [7]

Answer:

b

Explanation:

6 0
2 years ago
Insecurely attached infants who are left my their mothers in an unfamiliar setting often will
Temka [501]

Insecurely attached infants who are left my their mothers in an unfamiliar setting often will Hold fast in their mothers in their return.

A. Hold fast in their mothers in their return

<u>Explanation:</u>

It is in the conscience of the infants to have the company of their parents no matter the place or time and do not generally like to be left alone. Moreover, the questions says, insecurely attached infants which further add up to this behavior.

The infant would not explore the surroundings due to lack of confidence in an unfamiliar setting. They would rather be uncomfortable and most probably weep the time until their mother arrives and hold fast on to them on their return.  

8 0
3 years ago
It is a function that removes an existing file from the server.
kondaur [170]

Answer: A) remove()

Explanation:

 The remove() function removes an existing file from the server but it does not affect the existing directory and file. We can also ease and remove files in the file handling by using the remove() function. And it is built-in function that removes any type of the data from the function by taking values in the parameter whose values are equal with the passing value in the parameter.

7 0
3 years ago
And, Or, Not are examples of:
yanalaym [24]
Boolean operators it is
all the best
5 0
2 years ago
Write a console application that takes an integer input from the user and calculates the factorial of it. Note: factorial of Exa
Stells [14]

Answer:

The program in Python is as follows:

n = int(input("Integer: "))

product = 1

for i in range(1,n+1):

   product*=i

   if(i!=n):

       print(str(i)+" *",end =" ")

   else:

       print(i,end =" ")

print(" = ",product)

Explanation:

This prompts the user for integer input

n = int(input("Integer: "))

This initializes the product to 1

product = 1

This iterates through n

for i in range(1,n+1):

This multiplies each digit from 1 to n

   product*=i

This generates the output string

<em>    if(i!=n):</em>

<em>        print(str(i)+" *",end =" ")</em>

<em>    else:</em>

<em>        print(i,end =" ")</em>

This prints the calculated product (i.e. factorial)

print(" = ",product)

4 0
2 years ago
Other questions:
  • you are a software engineering consultant and have been called in by the vice president of finance of a corporation that manufac
    10·1 answer
  • What is Least effective at preventing a computer virus
    10·1 answer
  • Name the function in Python that prompts user to enter values as per the data type specified.
    5·1 answer
  • When internet techonology was developed in the 1970s by the department of defense?
    10·1 answer
  • What is the difference between line art and continuous tone copy?
    14·1 answer
  • Write a function that, given an array A of N integers, of which represents loads caused by successive processes, the function sh
    15·1 answer
  • If you notice files being transferred to or from your computer a. Simply close the window c. Tell the lab instructor b. Open a n
    11·2 answers
  • Rule- based systems are subset of expert systems true or false?
    7·1 answer
  • Ranboo back story? (not homework i just wanna find some ppl who are interested in the same things) ft. the reddie meme
    9·2 answers
  • The __________ operator increases the value of the variable by 1 after the original value is used in the expression in which the
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!