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 kind of operating system is MS-DOS?
ELEN [110]

MS-DOS is a command-line operating system.

Therefore, the best answer is Command-line.

8 0
3 years ago
Read 2 more answers
Describe psychographic differences among the past five generations of Americans that you learned about in this course. What type
Hunter-Best [27]

i would love to help i just dont understand

7 0
2 years ago
There are two main types of hard drive available to a computer. State what they are and describe their use.
Gala2k [10]

Answer:

Hard disk drives (HDD), which use one or more rotating discs and rely on magnetic storage, and solid-state drives (SSD), which have no moving mechanical parts, but use flash memory like the kind found in USB flash drives.

Explanation:

6 0
2 years ago
Write a program that first calls a function to calculate the square roots of all numbers from 1 to 1,000,000 and write them to a
svlad2 [7]

Answer:

The csharp program to compute square roots of numbers and write to a file is as follows.

class Program  

{  

static long max = 1000000;  

static void writeRoots()  

{  

StreamWriter writer1 = new StreamWriter("roots.txt");  

double root;  

for (int n=1; n<=max; n++)  

{  

root = Math.Sqrt(n);  

writer1.WriteLine(root);  

}  

}  

static void Main(string[] args)  

{  

writeRoots();  

}  

}

The program to compute squares of numbers and write to a file is as follows.

class Program  

{  

static long max = 1000000;  

static void writeSquares()  

{  

StreamWriter writer2 = new StreamWriter("squares.txt");  

long root;  

for (int n = 1; n <= max; n++)  

{  

root = n*n;  

writer2.WriteLine(root);  

}  

}  

static void Main(string[] args)  

{  

writeSquares();  

}  

}  

Explanation:

1. An integer variable, max, is declared to hold the maximum value to compute square root and square.

2. Inside writeRoots() method, an object of StreamWriter() class is created which takes the name of the file as parameter.

StreamWriter writer1 = new StreamWriter("roots.txt");

3. Inside a for loop, the square roots of numbers from 1 to the value of variable, max, is computed and written to the file, roots.txt.

4. Similarly, inside the method, writeSquares(), an object of StreamWriter() class is created which takes the name of the file as parameter.

StreamWriter writer2 = new StreamWriter("squares.txt");

5. Inside a for loop, the square of numbers from 1 to the value of the variables, max, is computed and written to the file, squares.txt.

6. Both the methods are declared static.

7. Inside main(), the method writeRoots() and writeSquares() are called in their respective programs.

8. Both the programs are written using csharp in visual studio. The program can be tested for any operation on the numbers.

9. If the file by the given name does not exists, StreamWriter creates a new file having the given name and then writes to the file using WriteLine() method.

10. The csharp language is purely object-oriented language and hence all the code is written inside a class.

3 0
3 years ago
1. an image can be stored either in a vector graphic file or in a bitmap file true or false
lions [1.4K]

Answer:

1 true 2 true 3 false 4 true

Explanation:

7 0
2 years ago
Other questions:
  • Which presentation software element can you use to create a model diagram with two concentric circles inside a triangle?
    10·2 answers
  • How many seconds are required to make a left turn and join traffic?​
    11·2 answers
  • Why might you use the "search network campaigns with display opt-in" campaign type?
    11·2 answers
  • F a domain consists of dcs that are running verions of windows server earlier than windows server 2008, what replication method
    10·1 answer
  • While you are working on your computer, it shuts down unexpectedly, and you detect a burning smell. When you remove the case cov
    9·1 answer
  • What term refers to the text label that describes each data series?
    9·1 answer
  • How to use repl.it css
    7·1 answer
  • Select the correct answer.
    13·1 answer
  • Write the correct statements for the above logic and syntax errors in program below.
    12·1 answer
  • Who is the king of computers?
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!