write a program for python 3
write the functions that could be used in an implementation of the
game tic-tac-toe. Below are the definitions of the different
functions. The code to test each function is currently in the
main() of the lab10.py file.
• A function to build the board. This method should create a list
of the numbers 1 – 9 and return that list. build_board () ->
list
• A void function to display the board. (see the sample displays in
Part II)
display_board( list ) -> void
• A Boolean method to determine if a spot is a legal spot on the
board.
is_legal ( board, spot ) -> bool
• A void method to fill a spot on the board. This method will need
to have the board, the position to be filled and the character to
place in that position. Make sure that the spot is legal using the
function above before you fill the spot.
fill_spot ( board, spot, char ) -> void
• A Boolean method to determine if the game has been won.
is_game_won ( board ) -> bool
• A Boolean method to determine if the game is over. This should
call the previously mentioned method plus check to make sure there
are more plays allowed on the board.
is_game_over ( board, num_plays ) -> bool
The following is the additional function required for the
assignment.
• A function play_game()to play the game. This function should
continue as long as the game is not over. Once the game is over,
display the message “Player 1 wins!”, “Player 2 wins”, or “Tie” as
is appropriate.
Once your basic functions are working: Rewrite
play_game(), your game should do the following:
• If a player plays ‘o’ or ‘x’ in spot which is already played,
show message “Please check the valid spot’ at the bottom of the
window, until the player click the empty (valid) spot.
(Fig 4 shows that player 2 try to play ‘o’ on position 5 or 1 or
7)
• When game is over, show message about the winner or ‘nobody
wins’. Part IV (Optional) Repeat the game When the game finishes,
allow user to choose whether to play again.
'''
Python version : 3.6
Python program to allow the user to play the game of tic tac
toe
'''
# function that create a list of the numbers 1 – 9 and return
that list
def build_board():
board = []
for i in range(1,10):
board.append(i)
return board
# function to display the board
def display_board(list):
board = ""
for i in range(0,len(list),3):
for j in range(3):
board +=
str(list[i+j])+"| "
board += "\n"
print(board)
# function to return if the spot is legal or
not
def is_legal( board, spot ):
if(spot >= 1 and spot <= 9 and (board[spot-1] ==
spot)):
return True
return False
# function to fill the spot with the given
char
def fill_spot( board, spot, char ):
board[spot-1] = char
# function to check and return if the game is won by wither of
the two players or not
def is_game_won( board ) :
# check row-wise
for i in range(0,len(board),3):
if( board[i] == board[i+1] ==
board[i+2]):
if(board[i] ==
'x' or board[i] == 'o'):
return True
#check column-wise
for i in range(0,3):
if(board[i] == board[i+3] ==
board[i+6]):
if board[i] ==
'x' or board[i] == 'o':
return True
# check main diagonal
if(board[0] == board[4] == board[8]):
if board[4] == 'o' or board[i] ==
'x' :
return
True
# check other diagonal
if(board[2] == board[4] == board[6]):
if board[4] == 'o' or board[4] ==
'x':
return True
return False
# function to return if the game is over or
not
def is_game_over( board, num_plays ) :
if(is_game_won(board) or num_plays ==
len(board)):
return True
return False
# function to play game
def play_game():
choice = 'y'
# loop continues till the user wants
while choice.lower() == 'y':
player = 'x'
num_plays = 0
board = build_board()
display_board(board)
# loop continues till this game is
not over
while not is_game_over(board,
num_plays):
num_plays +=
1
print('Player %s
turn : '%player)
spot =
int(input('Enter a spot(1-9) : '))
while(not
is_legal(board,spot)):
print("Please check the valid spot at the bottom
of the window")
spot = int(input('Enter a spot(1-9) : '))
fill_spot(board,spot,player)
display_board(board)
if
is_game_won(board):
break
if player ==
'x':
player = 'o'
else:
player = 'x'
if(is_game_won(board)):
if player ==
'x':
print('Player 1 wins!')
else:
print('Player 2 wins!')
else:
print('Tie')
choice = input('Do you want to
continue ? (y/n) ')
#call play_game function
play_game()
#end of program
Code Screenshot:
Output:
write a program for python 3 write the functions that could be used in an implementation...
Introduction Write a MIPS program to allow a user to play a simple variation of the game BINGO. Your program should display the following game board and the player wants to choose a location to mark BINGO A position can be marked by entering its column letter (eit B', 'I', 'N' 'G', or CO and row number (either 1, 2, 3, 4, or 5). An 'X' should mark the positions already marked. An underscore, e C 1 should represent unmarked...
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....
(Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...
Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...
(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...
Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...