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
Verizon's implementation of a web-based digital dashboard to provide managers with real-time information such as customer compla
elena55 [62]

An example of  improved efficiency is Verizon's use of a Web-based digital dashboard to give management access to real-time data such as customer complaints.

<h3>What does the term " improved efficiency" mean?</h3>
  • Efficiency improvement refers to the improved value and/or quality that a company achieves as a result of changing a service or the manner in which a service is offered.
  • This improvement might be more expensive, but it is worth more in comparison.
  • Being effective in your daily operations can help you raise productivity, boost production output, and get rid of time-consuming administrative activities.
  • It might also imply that you don't need to rely as heavily on costly machinery, unreliable external suppliers, or even temporary workers.

To learn more about improved efficiency, refer to:

brainly.com/question/7690431

#SPJ4

5 0
2 years ago
Given a string on one line and an integer index on a second line, output the character of the string at that index.
morpeh [17]

The code is in Java.

It uses a built-in function named chartAt() to get the character at the entered index.

Recall that built-in functions are functions that are already defined in the language. We can use them by calling the function and passing the required argument. The chartAt() function takes an index and returns the character at that index.

Comments are used to explain each line of the code.

//Main.java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    //Scanner object to get input from the user

    Scanner input = new Scanner(System.in);

   

    //Declaring the variables

    String s;

    int index;

    char c;

   

    //Getting the inputs

    s = input.nextLine();

    index = input.nextInt();

   

    //Getting the character at the index using the charAt() function

    c = s.charAt(index);

   

    //Printing the character

 System.out.println(c);

}

}

You may check the following link to see another similar question:

brainly.com/question/15688375

5 0
3 years ago
A(n) ____________ is a group of similar or identical computers, connected by a high-speed network, that cooperate to provide ser
scoundrel [369]

Answer:

cluster

Explanation:

A cluster in a computer system is a collection of servers and other resources that work together to provide high reliability and, in certain situations, load balancing and parallel processing.

6 0
2 years ago
Who has gotten a random file link from someone? What file does it contain?
Bad White [126]

Answer:

those are bots , just report those

Explanation:

8 0
3 years ago
Read 2 more answers
Which type of database program is Microsoft Access 2016?
nadya68 [22]

Answer:

O relational

Explanation:

If I'm wrong I'm so so sorry! But form my research it keeps saying its relational.

If I'm right please give me brainliest I really need it to level up so please help me!

If you don't know how to give brainliest there should be a crown underneath my answer you just have to click it.

Thank you and have a wonderful night,morning,afternoon/day! :D

6 0
3 years ago
Read 2 more answers
Other questions:
  • A poem for coduction
    15·2 answers
  • If Asa changes the text to bold, he has changed the style. True False
    8·2 answers
  • Ou are driving in stop-and-go traffic during the daytime, and someone in another vehicle tells you that your brake lights are no
    10·1 answer
  • List the five human relations skills
    12·1 answer
  • Which step is first in changing the proofing language of an entire document?
    11·1 answer
  • In a penetration test, a ________ team consists of IT staff who defend against the penetration testers. They are generally aware
    14·1 answer
  • David wanted to build a Temple for God in________
    10·2 answers
  • Complete the statement by entering the appropriate term in the space below.
    13·2 answers
  • What are the 3 rules of music<br><br> ps: there is no music subject so i had to put something else
    15·2 answers
  • Which of the following can technology NOT do?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!