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]
3 years ago
10

Write Tic tac toe program in python for beginners pls

Computers and Technology
1 answer:
Travka [436]3 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
Click this link to view O*NET’s Work Styles section for Petroleum Engineers. Note that common work styles are listed toward the
inysia [295]

Answer:

2 (cooperation )

3 (integrity)

5 (analytical thinking)

6 (dependability)

Explanation:

Good luck! Hope this helps.

4 0
3 years ago
Read 2 more answers
the block cipher attack technique that attempts to retrieve the key by performing decryption and encryption simultaneously on a
tankabanditka [31]

The block cipher attack strategy that attempts to retrieve the key by performing decryption and encryption simultaneously on a understood plaintext-ciphertext pair exists plaintext attack.

<h3>What is plaintext attack?</h3>

The known-plaintext attack is a type of cryptanalysis attack in which the attacker has access to both the encrypted and plaintext versions of the target data. These can be used to reveal additional hidden information, including code books and secret keys. An attack using known plaintext occurs when the attacker has access to pairs of known plaintexts and the ciphertexts that go with them.

The objective is to figure out the secret key (or keys) or to create an algorithm that would enable him to decrypt any additional messages. Ordinary readable text before it is converted into ciphertext in cryptography is known as plaintext, as is readable text that has been recovered from encryption. Not all data inputted into or outputted by encryption methods is plaintext.

Hence,  The block cipher attack strategy that attempts to retrieve the key by performing decryption and encryption simultaneously on a understood plaintext-ciphertext pair exists plaintext attack.

To learn more about cryptanalysis attack refer to:

brainly.com/question/19202523

#SPJ4

3 0
1 year ago
If you want to have certain icons available regardless of what tab you're using, you should add them to the
anzhelika [568]
If you want to have certain icons available regardless of what tab your using, you should add them to the [Quick Access Toolbar].
Quick access toolbar is a way in which you can easily access the tool you want to use often.
3 0
3 years ago
Assume you're using a three button mouse. to access shortcut menus, you would
Likurg_2 [28]
<span>Normally you would click the right hand/secondary mouse button but you may configure any of the buttons to work within the Keyboard and Mouse section of System Preferences.
Hope this helps:)</span>
6 0
3 years ago
Read 2 more answers
The sequence's insert member function normally puts a new item before the current item. What should insert do if there is no cur
igomit [66]

Answer:

B) The new item is put at the beginning of the sequence

Explanation:

The stack data structure is an example of the type for insert method as the current items are replaced at the top by a new item and the previous items move down the stack. If there no current item in the stack, the new item is put in the first index (beginning point) of the stack data structure.

5 0
3 years ago
Other questions:
  • The item in this illustration that is highlighted is the _____. quick access tool bar view buttons status bar zoom control
    10·2 answers
  • The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called withi
    12·1 answer
  • When u look at a green object through red glass the object will appear
    11·2 answers
  • If x is an int, which of the following statements adds 5 to the current value of x and stores the new value back in x?
    12·1 answer
  • If you find yourself in a position where you need to restore an object or container within active directory that has been inadve
    6·1 answer
  • Juan is interested in getting a job in the technological field. He is interested in protecting data from threats, and stopping h
    9·1 answer
  • A methods and measurements analyst for Digital Devices needs to develop a time standard for the task of assembling a computer mo
    15·1 answer
  • A programmer has an idea to use just one language for the content, structure, and style of web pages. What might be a problem wi
    11·1 answer
  • Trading stock or selling stock, selling real estate for profit, and selling other assets that gain value over time.
    13·1 answer
  • It is a type of web page that allows you to share your ideas experiences and beliefs
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!