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
. :
Rudik [331]

Answer:

Yes.  I completely agree.

Explanation:

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

Yes.  I completely agree.

7 0
3 years ago
Preciso de ajuda urgente, é para amanhã cedo!!
Papessa [141]


Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
5 0
3 years ago
Assume that speed = 10 and miles = 5. What is the value of each of the
Assoli18 [71]

a. speed + 12 - miles * 2  = 10 + 12 - 5 * 2. With order of operations, we do the multiplication first so the equation is now 10 + 12 - 10 = 22 - 10 = 12

b. speed + miles * 3  = 10 + 5 * 3 and again, order of operations gives us 10 + 15 = 25

c. (speed + miles) * 3  = (10 + 5) * 3 = 15 * 3 = 45

d. speed + speed * miles + miles  = 10 + 10 * 5 + 5 = 10 + 50 + 5 = 60 + 5 = 65

e. (10 – speed) + miles / miles = (10 - 10) + 5 / 5 = 0 + 5 / 5 = 5 / 5 = 1

5 0
3 years ago
Which of the following are true about the PUSH instruction?
kkurt [141]

Answer:

Option A.

Explanation:

Only option A satisfies the definition of push statements. So it is only the correct option.

7 0
3 years ago
In the center pane of the __________, the direction of each arrow indicates the direction of the TCP traffic, and the length of
Sliva [168]

Answer:

The answer is "Result of Flow Graph Analysis".

Explanation:

A flow chart is also known as a graphical representation of any task. It a graphing program, which collects data, checks its control flows and provides abstracts from program details.

  • It produces a decision chart in the paper, which reduces the control flow chart but keeps the program branching structure.
  • It also used to manage to troubleshoot network issues.
3 0
3 years ago
Other questions:
  • Using the merge method of the Map interface, which statement correctly updates the salesTotalByDept map of type Map to update th
    7·1 answer
  • Rachel works in a bank. She wants to present the idea of implementing an IS to the management. How should Rachel describe the IS
    9·2 answers
  • Explique si en la pc que se usa para hacer las tareas escolares se puede ver un video juego de los llamados “pesados”
    13·1 answer
  • Which of the following is an example of a query with an explicit location? Select all that apply. True False [walmart boston], E
    7·2 answers
  • How do you get free Wifi on your phone without paying
    6·1 answer
  • What makes iron man different from other superheroes ? why is iron man the best superhero ??
    5·2 answers
  • What was the history of technology since the 1980s to now and how is it used in classrooms?.?. PLEASE HELP
    13·1 answer
  • Donna often travels around the world. When she travels, she needs to access her emails from different locations. However, to kee
    6·2 answers
  • Read the scenario and then answer the question using only the information provided.
    7·1 answer
  • Which feature of cryptography is used to prove a user's identity and prevent an individual from fraudulently reneging on an acti
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!