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
serg [7]
3 years ago
9

In this lab, you will develop a game, "Tic Tac Toe". Assume there are two players; the computer and theuser.Initially you will

create the board, a 3x3 matrix, and fill it out with zeros.Ask user to start first, and record the user’s input as 1You can ask the user to enter the row first than the column (for ex; the user can enter 2 and 1,then the code shouldupdate the value of second row first column)The computer should make random moves, and the computer input should be recorded as 2After each move, check if someone wins, and display the boardDisplay the result (who wins and the board)Create as much helper functions as needed.Divide and ConquerSampleRun:board = [[0,0, 0],[0,0, 0],[0,0,0]]enter the row;2enter the column;1board = [[0,0, 0],[1,0, 0],[0,0,0]]the computer playsboard = [[0,2, 0],[1,0,0],[0,0,0]]CS103-Lab9Page4of6enter the row;2enter the column;2board = [[0,2, 0],[1,1, 0],[0,0,0]]the computer playsboard = [[0,2, 0],[1,1, 0],[0,0,2]]enter the row;2enter the column;3board = [[0,2, 0],[1,1,1],[0,0,2]]Game Over !!!!Winner: The User

Computers and Technology
1 answer:
Aleonysh [2.5K]3 years ago
4 0

Answer:

Code:

# importing all necessary libraries

import numpy as np

import random

# Select a random place for the player

def play(board, player):

selection = availableSpots(board)

if player==1:

row=(int)(input("Enter the row :"))

column=(int)(input("Enter the column :"))

current_loc=(row-1,column-1)

while current_loc not in selection:

print("Invalid Input. Please give again")

row=(int)(input("Enter the row :"))

column=(int)(input("Enter the column :"))

current_loc=(row-1,column-1)

else:

current_loc = random.choice(selection)

board[current_loc] = player

return board

# Check for empty places on board

def availableSpots(board):

  l = []

  for i in range(len(board)):

      for j in range(len(board)):

          if board[i][j] == 0:

              l.append((i, j))

  return(l)

# Creates an empty board

def create_board():

  return(np.array([[0, 0, 0],

                  [0, 0, 0],

                  [0, 0, 0]]))

# Checks whether the player has three

# of their marks in a horizontal row

def row_win(board, player):

  for x in range(len(board)):

      win = True

      for y in range(len(board)):

          if board[x, y] != player:

              win = False

              continue

      if win == True:

          return(win)

  return(win)

# Checks whether the player has three

# of their marks in a vertical row

def col_win(board, player):

  for x in range(len(board)):

      win = True

      for y in range(len(board)):

          if board[y][x] != player:

              win = False

              continue

      if win == True:

          return(win)

  return(win)

# Checks whether the player has three

# of their marks in a diagonal row

def diag_win(board, player):

  win = True

  for x in range(len(board)):

      if board[x, x] != player:

          win = False

  return(win)

# Evaluates whether there is

# a winner or a tie

def evaluate(board):

  winner = 0

  for player in [1, 2]:

      if (row_win(board, player) or

          col_win(board,player) or

          diag_win(board,player)):

          winner = player

  if np.all(board != 0) and winner == 0:

      winner = -1

  return winner

def printWinner(winner):

print("Game Over !!!")

if winner==1:

print("Player Wins")

elif winner==2:

print("Computer Wins")

else:

print("Nobody Wins")

# Main function to start the game

def play_game():

board, winner = create_board(), 0

print(board)

while winner == 0:

for player in [1, 2]:

board = play(board, player)

print("Board")

print(board)

winner = evaluate(board)

if winner != 0:

break

printWinner(winner)

play_game()

Explanation:

See screen shot of code and see final output

You might be interested in
_____ is the unauthorized entry into a computer system via any means
Montano1993 [528]
Hi,

The word you are looking for is "Hacking".

"Hacking is unauthorized entry into a computer system via any means."

Hope this helps.
r3t40
4 0
3 years ago
Please Help!!!<br> I keep getting this answer wrong
IgorC [24]

Answer:

I think its option a. and d.

Explanation:

option a. is right I'm sure.

option b. and c. are doubtful for me.

option d. can be true. but unlike a job letter, resumes carry a deeper view of the employee's accomplishments and qualifications.

but I still hold with option a. and b. if it's wrong please do inform, so I can refer.

5 0
3 years ago
I need Help!!<br> 4. Explain the importance of including negative space as part of a design.
vova2212 [387]
Negative space is the space between, within and surrounding an object in an image. The positive space is the focus of the image, the object itself, but the negative space is just as important. It shares edges with the positive space, defining the outline of the object and creating proportion
5 0
3 years ago
One of the first feature films to use 3D animation was called ________. Water World Futureworld Wayne’s World Cool Hand Luke
Vladimir79 [104]
It was called Futureworld

It was published in 1976, it was the first animation that using 3D computer graphics for its animated hand and face.

But they still used 2D digital composting to materialize characters over a background
8 0
3 years ago
Read 2 more answers
Which technology concept uses computer resources from multiple locations to solve a common problem?​
umka2103 [35]

Answer:

Distributed memory systems

Distributed memory systems use multiple computers to solve a common problem, with computation distributed among the connected computers (nodes) and using message-passing to communicate between the nodes.

Explanation:

5 0
3 years ago
Other questions:
  • What car dealership websites did you use to conduct your research?​
    8·1 answer
  • _____ rows indicate that there is different formatting for odd and even rows.
    14·1 answer
  • P**nhub or x-videos or other
    9·1 answer
  • How can you make a circle in JavaScript? Thank you!
    9·1 answer
  • Adele’s mother owns a Daycare and she wants to learn all about this business, to help her mom and own it one day. Which CTSO sho
    9·1 answer
  • Why is plastic durable?
    9·2 answers
  • Jody should select the
    5·1 answer
  • How would you copy all files from a remote directory into your LOCAL home folder.
    6·1 answer
  • Imagine a typical website that works as a storefront for a business, allowing customers to browse goods online, place orders, re
    13·1 answer
  • Find the total cost of a $125 coat purchased in Los Angeles County where the sales tax is 9%. Use the expression c + 0.09c
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!