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
Name four reasons for keeping your money in a financial institution.
nevsk [136]
1) safety
2) you can collect interest
3) helps you save your money instead of just spending all the time
4) you can gain exponential growth which then can contribute to you.gaining more retirement money when u get older
4 0
3 years ago
Read 2 more answers
Which describes the third step in visual character development
allsm [11]

I would say 3-D model!!!! not 100% sure but this sounds most correct!

5 0
2 years ago
Read 2 more answers
Joe, a user, reports to the help desk that he can no longer access any documents on his PC. He states that he saw a window appea
posledela

Answer:

The answer is most likely B) Trojan.

Explanation:

8 0
3 years ago
Privacy laws in other countries are an important concern when performing cloud forensics and investigations. You've been assigne
Klio2033 [76]

Answer:

See the explanation for the answer.

Explanation:

Australian regulations makes extremely difficult for the enterprises to move organizations sensitive data to the cloud which is storing outside the Australian network. These are all managed by the Office of Australian Information Commissioner(OAIC) which provides oversight on the data privacy regulations designed to govern the dissemination of the sensitive information.

One rule they applied under this is The Australian National Privacy Act 1988 which tells how the organizations collect, use, secure, store the information. The National Privacy Principles in the Act tells how organizations should use the people's personal information. The NPP has a rule that An organization must take the responsibility to hold the information without misuse or modified by unauthorized access. They require enterprises to put security service level agreements with the cloud service providers that define audit rights, data location, access rights when there is cross border disclosure of information.

In later time they introduced a new principle called The Privacy Amendment Act 2012. This principle gives set of new rules along with the changes in the many rules in the previous act and also this is having a set of new principles those are called Australian Privacy Principles (APP).

In this there is principle for cross border disclosure of personal information which is APP8. This rule regulates the disclosure or transfer of personal information by an agency or company to a different location outside the country.

Before disclosure the information outside they have to take responsible steps that the company outside the Australia must not breach the APP 's.

3 0
3 years ago
Some problems are better solved by a computer and some are better solved by humans.
aalyn [17]

Answer:

I say when u don't know the answer completly.

3 0
2 years ago
Other questions:
  • Antivirus software installed to scan and monitor malware activities on a server or workstation would be identified as a ________
    8·1 answer
  • #Electrical Engineering
    13·1 answer
  • Its Inventiveness, uncertainty and futuristic ideas typically deals with science technology, what is it?
    7·2 answers
  • The Research and Development Process has five steps what are they ?
    7·1 answer
  • Amazon Web Services and Microsoft Azure are some of the most widely used _______.
    7·1 answer
  • Write a Python program string_functions.py that defines several functions. Each function re-implements Python's built-in string
    9·1 answer
  • Question 7 (True/False Worth 3 points)
    8·2 answers
  • Que relacion tiene Las palabras: fermentacion-vino y clonacion- dolly​
    9·1 answer
  • 6.What does transgenic mean?​
    12·2 answers
  • Write the function definition for a function called list_total that accepts a list of integers
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!