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
Mkdir() is the command in Java to create a new directory.<br> a) True b) False
svp [43]

Answer:

True.

Explanation:

mkdir() method in java is a part of the file class.The mkdir() command is used to create a new directory and it is denoted by the path name that is abstract.This function mkdir() returns true if the directory is created and false if the directory is not created by this function.

Hence the answer for this question is True.

8 0
3 years ago
Juan wrote a loop to print all the prime numbers between 1 and 100. But instead of stopping at 100, it continues on and on forev
aniked [119]

Answer:

oh im cool

Explanation:

6 0
2 years ago
A ________ is a very large general-purpose computer that is capable of performing very many functions as if these are done simul
Soloha48 [4]

Answer:

Mainframe Computer is capable of doing all the functions which are listed in question.

Explanation:

A mainframe is a very large general-purpose computer (usually costing millions of dollars) that is capable of performing very many simultaneous functions, supporting very many simultaneous users, and storing huge amounts of data. A microcomputer is the type of computer you use.

3 0
3 years ago
The ability to anticipate and determine upcoming driving hazards and conditions are adversely affected by stress.
dybincka [34]
It can be stress or drinking, drugs, or anything of the sort.
6 0
3 years ago
Inc AX,2 is valid in assembly language ?
masya89 [10]

Answer:

in most so yes

Explanation:

4 0
3 years ago
Other questions:
  • Elvis has asked Bonnie out on a date to a baseball game. Elvis is a friend of Bonnie's classmate Ginger and she has met him a fe
    5·2 answers
  • 5255555555555+55555555555555/1111*99442
    14·2 answers
  • Binary code what does this mean I was sick so I don't under stand
    7·2 answers
  • Write a class named RetailItem that holds data about an item in a retail store. The class should store the following data in att
    15·1 answer
  • A simulation model includes: a. a description of the components of the system. b. a simulation clock. c. a definition of the sta
    8·1 answer
  • A(n) __________ item is a hardware or software item that is to be modified and revised throughout its life cycle
    10·1 answer
  • What are the cues that a website is safe to use for exchanging sensitive information?
    10·1 answer
  • Write a program that reads from the user any three points in two dimensional space: x1, y1, x2, y2, x3, y3. Assume these points
    13·1 answer
  • A _________ is a component commonly used in an analog pressure gauge. Use letter keys to select choices A microprocessor B press
    8·1 answer
  • When importing data using the Get External Data tools on the Data tab, what wizard is automatically
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!