Question

In: Computer Science

Python Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their...

Python

Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer.

Use 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 player moves, place an 'O' in the specified square. Each move must be to an empty square. After each move, determine whether the game has been won and whether it’s a draw.

Solutions

Expert Solution

import random

try:

raw_input()

except NameError:

raw_input = input

WIN_COMBINATIONS = [(1, 2, 3),

(4, 5, 6),

(7, 8, 9),

(1, 4, 7),

(2, 5, 8),

(3, 6, 9),

(1, 5, 9),

(3, 5, 7)]

def display_board(board):

print(''' | |

{} | {} | {}

| |

-----------

| |

{} | {} | {}

| |

-----------

| |

{} | {} | {}

| |'''.format(*board[1:10]))

def player_input():

marker = ' '

while not (marker == 'X' or marker == 'O'):

marker = raw_input('Player 1, Choose O or X to play!').upper()

if marker == 'X':

return {'Player 1': 'X', 'Player 2': 'O'}

else:

return {'Player 2': 'X', 'Player 1': 'O'}

def win_check (board):

return any(board[a] != ' ' and board[a] == board[b] == board[c] for a, b, c in WIN_COMBINATIONS)

def choose_first(players):

random_player = 'Player {}'.format(random.randint(1, 2))

return random_player, players[random_player]

def full_check (board):

return all(b != ' ' for b in board)

def player_choice(board):

while True:

try:

position = int(raw_input('Choose number input 1-9'))

if position in range(1, 9) and board[position] == ' ':

return position

except ValueError:

pass

def replay():

return raw_input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')

def ttt():

board = [' ' for _ in range(10)]

players = player_input()

name, player_marker = choose_first(players)

print('{} with marker {} will go first.'.format(name, player_marker))

while True:

position = player_choice(board)

board[position] = player_marker

display_board(board)

if win_check(board):

print('Congratulations {}! You have won the game!'.format(name))

break

if full_check(board):

print('Congratulations {} and {}! You have a tie!'.format(players.keys()))

break

name = 'Player 1' if name == 'Player 2' else 'Player 2'

player_marker = players[name]

print(name, player_marker)

if __name__ == '__main__':

print('Welcome to Tic Tac Toe Game!')

while True:

ttt()

if not replay():

break


Related Solutions

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 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 (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...
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow...
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow each player to take sequential turns and to interactively enter their choice of position at the command line on each turn. For each turn, the script should output the set-up of the board. Note, you don’t have to use “x” and “o” as the symbol of each player. Note, the main body of this code (not including functions) should be able to be written...
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...
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...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT