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
What are the parts of word?
jolli1 [7]

Answer:

Explanation:

ASIA includes 50 countries, and it is the most populated continent, the 60% of the total population of the Earth live here.

AFRICA comprises 54 countries. It is the hottest continent and home of the world's largest desert, the Sahara, occupying the 25% of the total area of Africa.

NORTH AMERICA includes 23 countries led by the USA as the largest economy in the world.

SOUTH AMERICA comprises 12 countries. Here is located the largest forest, the Amazon rainforest, which covers 30% of the South America total area.

ANTARCTICA is the coldest continent in the world, completely covered with ice. There are no permanent inhabitants, except of scientists maintaining research stations in Antarctica.

EUROPE comprises 51 countries. It is the most developed economically continent with the European Union as the biggest economic and political union in the world.

AUSTRALIA includes 14 countries. It is the least populated continent after Antarctica, only 0.2% of the total Earth population live here.

7 0
3 years ago
Why do you think viruses are called “viruses”? What do they have in common with the living viruses that cause diseases in humans
Vinvika [58]

Answer:

Computer viruses are referred to living viruses as they both have similar effects.

Explanation:

While living viruses invade cells and cause harmful effects on the body, computer viruses basically do the same thing. Computer viruses are a malicious code that inserts itself into a computer. It's designed to spread to one computer to another, just like a living virus.

Similarities:

  • Spreads from one host to another.
  • They both alter/harm the host.
  • Both can replicate itself.

Differences:

  • A computer virus is manmade, while living viruses aren't.
  • A computer virus needs to be removed manually through an app or software while living viruses are almost curable medicine.
  • A computer virus effects machines and software while living viruses only infect living organisms.
5 0
3 years ago
Read 2 more answers
I need this answer right away!
Degger [83]

Answer:

Foundation.

Bootstrap. 2.1 Resources.

Metro UI.

jQuery Mobile.

Sencha Touch.

Ionic.

HTML KickStart.

Semantic UI

Explanation:

5 0
3 years ago
Spanning Tree Protocol (STP) is a loop-prevention mechanism used in modern networks. STP is, however, vulnerable to misconfigura
maks197457 [2]

Answer:

Any of the following:

1. Root Guard

2. BPDU Guard

3. BPDU Filtering

Explanation:

Given that STP an acronym of Spanning Tree Protocol, and its major purpose is to stop the bridge loops and the broadcast radiation that emanates from its topology.

Cisco carried out three instruments or tools to protect the STP topology. These protection tools accompany the STP, and they are the following:

1. Root Guard

2. BPDU Guard

3. BPDU Filtering

3 0
3 years ago
To explain acceptable computer use to their employees, students, or other users, many organizations and educational institutions
docker41 [41]

Answer:

Codes of conduct

Explanation:

Codes of conduct are a set of rules or norms established by an organization for all employees, students or users, to ensure individual responsibilities and proper practices. The code of conduct can cover overall behaviour of individuals in an organization, but a specific code of conduct can be developed for proper computer use in order to establish what is appropriate and available to use in the organization´s computers, and also to restrict or avoid non related content.

7 0
3 years ago
Read 2 more answers
Other questions:
  • If memory accesses take 100 times more than register accesses, then a LOAD instruction will take ___ machine cycles than an ADD
    7·1 answer
  • Which is the output of the formula =AND(12&gt;6;6&gt;3;3&gt;9)
    6·1 answer
  • What is the main difference between EDBAC and EDNIAC......​
    13·2 answers
  • Modern car odometers can record up to a million miles driven. What happens to the odometer reading when a car drives beyond its
    8·1 answer
  • At which layer of the osi model is the entire message referred to as the payload
    15·1 answer
  • When a fighter pilot crosses into the airspace of another country without permission it can be considered an act of aggression o
    8·1 answer
  • Building relationships during your career exploration is called A. clustering. B. futurecasting. C. matchmaking, D. networking.
    9·2 answers
  • https://brainly.com/question/10197478#:~:text=Option%20A%20is%20the%20correct,in%20return%20of%20their%20money.
    13·1 answer
  • (PLEASE HELP!! I'll award brainiest!!)
    6·2 answers
  • A cell's address, its position in the workbook, is referred to as a ________________ when it is used in a formula.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!