Question

In: Computer Science

PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...

PYTHON

(Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win.

Solutions

Expert Solution

def print_board(board):

print “The board look like this:\n”

                for i in range(3):

                                print “ “,

                             for j in range(3):

                                              if board[i*3+j]==1:

                                                         print ‘X’,

                                           elif board[i*3+j]==0:

                                                        print ‘O’,

elif board[i*3+j] != -1:

print board[i*3+j]-1,

else:

print ' ',

if j != 2:

print " | ",

print   

if i != 2:

print "----------------"

else:

print

def print_instruction():

print_board([2,3,4,5,6,7,8,9,10])   

def get_input(turn):

valid = False

while not valid:

try:

user = raw_input("where would you like to place" + turn +" (1-9)? ")

user = int(user)

if user >=1 and user <= 9:

return user-1

else:

print "That is not a valid move! Please try again \n"

print_instruction()

except Exception as e:   

print user + " is not a valid move! Please try again \n"

def check_win(board):

win_cond = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))   

for each in win_cond:

try:

if board[each[0]-1] == board[each[1]-1] and board[each[1]-1] == board[each[2]-1]:

except:

pass

return -1

def quit_game(board,msg):

print_board(board)

print msg

quit()

def main():

# Start Game

# Change turns

# Checks for winner

# Quits and redo board

print_instruction()   

board = []

for i in range(9):

board.append(-1)

win = False

move = 0

while not win:

# Print board

print_board(board)

print "Turn number" + str(move+1)

if move %2 == 0:

turn = 'X'

else:

turn = 'O'

# Get player input

user = get_input(turn)

while board[user] != -1:

print "Invalid move! Cell already taken. Please try again \n"

user = get_input(turn)

board[user] = 1 if turn == 'X' else 0

# Continue move and check if end of game

move += 1

if move > 4:   

winner = check_win(board)

if winner != -1:

out = "The winner is"

out += "X" if winner == 1 else "O"

out += ""

quit_game(board,out)

elif move == 9:

quit_game(board,"No winner")

if__name__=="__main__":

main()


Related Solutions

In a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
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 the row...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT