Where is the 2d array or list in this python program?? #########################
ID: 3777386 • Letter: W
Question
Where is the 2d array or list in this python program??
#######################################################################
# Program Filename: assign9.py
# Author: Matthew Tong
# Date: 11/27/16
# Description: Tic Tac toe
# Input: Players choice on characters and location on the board
# Output: Characters on the board, player's choices, total wins
#######################################################################
import sys
#######################################################################
# Function: initialize_board
# Description: Method to initialize the baord to spaces
# Parameters: none
# Return values: This returns the board
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def initialize_board():
board = []
for i in range(3):
board.append([])
for j in range(3):
board[i].append(' ') # adds its parameter as a single element to the list
return board
#######################################################################
# Function: determine_player_choice
# Description: Method to pick the user choices
# Parameters: none
# Return values: The return value of from each usersw
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def determine_player_choice():
ch1 = input('Player 1: What character do you want? ')
ch2 = input('Player 2: What character do you want? ')
return ch1, ch2
#######################################################################
# Function: fill_board
# Description: Method to ifll the board with user choice
# Parameters: The player's characters
# Return values: The location: x and y on the board
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def fill_board(board, ch1, ch2):
while not is_full(board):
print_board(board)
while True:
x, y = input('Player 1: Where would you like to put your ' + ch1 + '? ').split(' ')
if board[int(x)][int(y)] == ' ':
break
else:
sys.stdout.write('Invalid choice. Try again!! ') # print out whatever string you give it. What the print function really does; it adds a carraige return to the end of the string you're printing
board[int(x)][int(y)] = ch1
print_board(board)
if check_for_winner(board, ch1) or check_for_winner(board, ch2):
return
while True:
x, y = input('Player 2: Where would you like to put your ' + ch2 + '? ').split(' ')
if board[int(x)][int(y)] == ' ':
break
else:
sys.stdout.write('Invalid choice. Try again!! ')
board[int(x)][int(y)] = ch2
if check_for_winner(board, ch1) or check_for_winner(board, ch2):
return
#######################################################################
# Function: print_board
# Description: Method to print the tic tac board
# Parameters: none
# Return values: Prints the board out
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def print_board(board):
for i, lst in enumerate(board): #starts counting at board but if you give it a second integer argument, itll start from that board
for j, ch in enumerate(lst):
sys.stdout.write(ch)
if j != len(lst) - 1:
sys.stdout.write(' | ')
if i != len(board) - 1:
sys.stdout.write(' ----------')
sys.stdout.write(' ')
sys.stdout.write(' ')
#######################################################################
# Function: is_full
# Description: Method to check the winner of the board
# Parameters: none
# Return values: the winnder of the baord
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def is_full(board):
for lst in board:
for ch in lst:
if ch == ' ':
return False
return True
#######################################################################
# Function: check_for_winner
# Description: Method to check the winner of the board
# Parameters: player's input for the location: x and y
# Return values: User inputs their locations, place on the board
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def check_for_winner(board, ch):
return (board[0][0] == ch and board[0][1] == ch and board[0][2] == ch) or
(board[1][0] == ch and board[1][1] == ch and board[1][2] == ch) or
(board[2][0] == ch and board[2][1] == ch and board[2][2] == ch) or
(board[0][0] == ch and board[1][0] == ch and board[2][0] == ch) or
(board[0][1] == ch and board[1][1] == ch and board[2][1] == ch) or
(board[0][2] == ch and board[1][2] == ch and board[2][2] == ch) or
(board[0][0] == ch and board[1][1] == ch and board[2][2] == ch) or
(board[0][2] == ch and board[1][1] == ch and board[2][0] == ch)
#######################################################################
# Function: print_winner_results
# Description: Method to print the result
# Parameters: Each matche to see who wins or ties
# Return values: Total wins for each players and ties
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def print_winner_results(total, p1_wins, p2_wins):
print('Total Wins:')
print('Player 1: ' + str(p1_wins))
print('Player 2: ' + str(p2_wins))
print('Ties: ' + str(total - p1_wins - p2_wins))
#######################################################################
# Function: main
# Description: Main method to invoke other method and play the game
# Parameters: After the users input their characters and see who wins the match.
# Return values: The winner.
# Pre-Conditions:
# Post-Conditions:
#######################################################################
def main():
total = 0
p1_wins = 0
p2_wins = 0
while True:
board = initialize_board()
ch1, ch2 = determine_player_choice()
fill_board(board, ch1, ch2)
total += 1
if check_for_winner(board, ch1):
p1_wins += 1
print('Congratulations Player 1, you are a winner!!!')
elif check_for_winner(board, ch2):
p2_wins += 1
print('Congratulations Player 2, you are a winner!!!')
ch = input('Do you want to play again (y-yes, n-no): ')
if ch == 'n' or ch == 'N':
break
print_winner_results(total, p1_wins, p2_wins)
main()
Explanation / Answer
def initialize_board():
board = []
for i in range(3):
board.append([])
for j in range(3):
board[i].append(' ')
return board
Here board is list
===============================================
def fill_board(board, ch1, ch2):
while not is_full(board):
print_board(board)
while True:
x, y = input('Player 1: Where would you like to put your ' + ch1 + '? ').split(' ')
if board[int(x)][int(y)] == ' ':
break
else:
sys.stdout.write('Invalid choice. Try again!! ') # print out whatever string you give it. What the print function really does; it adds a carraige return to the end of the string you're printing
board[int(x)][int(y)] = ch1
print_board(board)
if check_for_winner(board, ch1) or check_for_winner(board, ch2):
return
while True:
x, y = input('Player 2: Where would you like to put your ' + ch2 + '? ').split(' ')
if board[int(x)][int(y)] == ' ':
break
else:
sys.stdout.write('Invalid choice. Try again!! ')
board[int(x)][int(y)] = ch2
if check_for_winner(board, ch1) or check_for_winner(board, ch2):
return
===========================================================
In above function board is 2D array
==========================================================
def is_full(board):
for lst in board:
for ch in lst:
if ch == ' ':
return False
return True
In above function board is matrix
===================================================================
def check_for_winner(board, ch):
return (board[0][0] == ch and board[0][1] == ch and board[0][2] == ch) or
(board[1][0] == ch and board[1][1] == ch and board[1][2] == ch) or
(board[2][0] == ch and board[2][1] == ch and board[2][2] == ch) or
(board[0][0] == ch and board[1][0] == ch and board[2][0] == ch) or
(board[0][1] == ch and board[1][1] == ch and board[2][1] == ch) or
(board[0][2] == ch and board[1][2] == ch and board[2][2] == ch) or
(board[0][0] == ch and board[1][1] == ch and board[2][2] == ch) or
(board[0][2] == ch and board[1][1] == ch and board[2][0] == ch)
In above function board is 2D Array.As itt has 2 dimensions
===================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.