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
oksian1 [2.3K]
3 years ago
14

Write code to play a Tic-tac-toe tournament. Tic-tac toe is a game for two players who take turns marking the spaces with Xs and

Os in a 3x3 grid. The purpose of the game is to place three of your marks in a horizontal, vertical or diagonal.
Computers and Technology
1 answer:
-Dominant- [34]3 years ago
7 0

Answer:

Explanation:

The following code is written in Python and is a full Two player tic tac toe game on a 3x3 grid that is represented by numbers per square.

# Making all of the main methods of the game

board = [0,1,2,

        3,4,5,

        6,7,8]

win_con = [[0,1,2],[3,4,5],[6,7,8],

           [0,3,6],[1,4,7],[2,5,8],

           [0,4,8],[2,4,6]] # possible 3-in-a-rows

def show():

   print(board[0],'|',board[1],'|',board[2])

   print('----------')

   print(board[3],'|',board[4],'|',board[5])

   print('----------')

   print(board[6],'|',board[7],'|',board[8])

def x_move(i):

   if board[i] == 'X' or board[i] == 'O':

       return print('Already taken!')

   else:

       del board[i]

       board.insert(i,'X')

def o_move(i):

   if board[i] == 'X' or board[i] == 'O':

       return print('Already taken!')

   else:

       del board[i]

       board.insert(i,'O')  

 

# Creating the main loop of the game

while True:

   turn_num = 1

   board = [0,1,2,3,4,5,6,7,8]

   print('Welcome to Tic-Tac-Toe!')

   print('AI not implemented yet.')

   while True:

       for list in win_con: #check for victor

           xnum = 0

           onum = 0

           for num in list:

               if board[num] == 'X':

                   xnum += 1

               elif board[num] == 'O':

                   onum += 1

               else:

                   pass

           if xnum == 3 or onum == 3:

               break

       if xnum == 3 or onum == 3: # break loops

           break

       if turn_num > 9: # Check if there are any more moves available

           break

       show()

       if turn_num % 2 == 1:

           print('X\'s turn.')

       else:

           print('O\'s turn.')

       move = int(input('Choose a space. '))

       if turn_num % 2 == 1:

           x_move(move)

       else:

           o_move(move)

       turn_num += 1

   if xnum == 3:  #If game ends

       print('X Won!')

   elif onum == 3:

       print('O Won!')

   else:

       print('Draw!')

   play_again = input('Play again? Y or N ')

   if play_again == 'Y' or play_again == 'y':

       continue

   else:

       break

You might be interested in
What area displays the title of the document
stich3 [128]

Answer:

The Title Bar

Explanation:

It is the part of the Software that not only tells you the name/title of the software but also the file name.

8 0
4 years ago
Read 2 more answers
Write a program to move the Turtle based on the user’s request. Display a menu with options for the user to choose. Use the foll
Strike441 [17]

Answer:

#import turtle

import turtle

 

# set screen

Screen = turtle.Turtle()

 

# decide colors

cir= ['red','green','blue','yellow','purple']

 

# decide pensize

turtle.pensize(4)

 

# Draw star pattern

turtle.penup()

turtle.setpos(-90,30)

turtle.pendown()

for i in range(5):

   turtle.pencolor(cir[i])

   turtle.forward(200)

   turtle.right(144)

 

turtle.penup()

turtle.setpos(80,-140)

turtle.pendown()

 

# choose pen color

turtle.pencolor("Black")

# importing turtle module

import turtle

 

# number of sides

n = 10

 

# creating instance of turtle

pen = turtle.Turtle()

 

# loop to draw a side

for i in range(n):

   # drawing side of  

   # length i*10

   pen.forward(i * 10)

 

   # changing direction of pen  

   # by 144 degree in clockwise

   pen.right(144)

 

# closing the instance

turtle.done()

turtle.done()

Explanation:

7 0
2 years ago
Which character is used to begin a comment?
Aloiza [94]
# is used to begin a comment.
8 0
3 years ago
Need help with understanding Project Reactor<br><br> https://projectreactor.io/
ddd [48]

Answer:

IT IS LIKE A ONILE AND THE PERFECT

Explanation:

THIS IS YOUR ANSWER

I HAVE EXPLAINED YOU

8 0
3 years ago
Is a colon (:) and semicolon (;) important in CSS declaration?
Mariulka [41]

Answer:

YES

Explanation:

this is very important

8 0
3 years ago
Other questions:
  • It is used to replace numeric number of a website​
    7·1 answer
  • What is the first step you should take when you want to open a savings account?
    9·1 answer
  • How can styles be used in Word? Check all that apply. to standardize the font size of a title in a Word document to standardize
    9·2 answers
  • An administrator has added a firewall within an Azure virtual network. What do we know for sure about the firewall?
    8·1 answer
  • During the _______ steps of the information processing cycle, data is manipulated, calculated, or organized to create useful inf
    9·1 answer
  • Send the memes whoever is the best will get that crown thing lol
    10·2 answers
  • Www.microsoft.com is an example of this (two words) (last letter is e) and has to be (10 letters)
    10·1 answer
  • What do customers use to access the internet, usually for a monthly fee?
    6·1 answer
  • As computer words get larger and larger, there is a law of diminishing returns: the speed of execution of real application progr
    8·1 answer
  • Vocational counselors group career and occupation specialties into <br> career clusters.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!