use python to do it
implement the tic-tac-toe game. What to submit: 1. Your source code 2. Two runs, one user wins and one user loses.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
import random
# method to check if a player has three consecutive occurrences
horizontally
def check_horizontally(ticTac, player):
# Horizontal check for win
# Loop through each row
for row
in ticTac:
# assuming user has
won
winDetected =
True
for j
in row:
if j != player:
# current value does not belong to the player, so not won
winDetected = False
break
if
winDetected:
return True
return False
# method to check if a player has three consecutive occurrences
vertically
def check_vertically(ticTac, player):
# vertical check
for j in
range(len(ticTac[0])):
winDetected =
True
for i
in range(len(ticTac)):
if ticTac[i][j] != player:
winDetected = False
break
if
winDetected:
return True
return False
# method to check if a player has three consecutive occurrences
diagonally
def check_diagonally(ticTac, player):
# Diagonal check 1 (top left to bottom
right)
winDetected = True
for i in
range(len(ticTac)):
if
ticTac[i][i] != player:
winDetected = False
break
if winDetected:
return
True
# Diagonal check 2 (top right to
bottom left)
winDetected = True
for i in
range(len(ticTac)):
if
ticTac[len(ticTac) - 1 - i][i] != player:
winDetected = False
break
if winDetected:
return True
return False
# method to check if a player has won. This will use other
methods to check if
# a player with given value has won. To see if X has won, pass tic
tac toe board
# and 'X' to the method, To see if O has won, pass tic tac toe
board
# and 'O' to the method,
def hasWon(ticTac, player):
# checking if user has won horizontally,
vertically or diagonally
if
check_horizontally(ticTac, player):
return
True
if check_vertically(ticTac,
player):
return
True
if check_diagonally(ticTac,
player):
return
True
return False
#method to make move to the board, here ticTac is the
board
#player is player's character and computer is a flag to denote
if
#this is computer's turn or not
def make_move(ticTac, player,
computer=False):
#if no more moves left, not doing
anything
if
no_moves_left(ticTac):
return
#if computer's turn, generating a
random move
if computer:
#loops until a
random empty location is found
while
True:
i=random.randint(0,len(ticTac)-1)
j=random.randint(0,len(ticTac[0])-1)
if ticTac[i][j]==' ':
#found
print('Computer chose row: {} and col:
{}'.format(i,j))
ticTac[i][j]=player
break #end of loop
else:
#loops until valid
row, column indices are entered
while
True:
print('Your turn')
i =int(input('Row index: '))
j = int(input('Column index: '))
if i<0 or i>=len(ticTac)
or j<0 or
j>=len(ticTac[0]):
#invalid
print('Invalid row/column')
continue #continuing loop
if ticTac[i][j] == ' ':
#valid
ticTac[i][j] = player
break
else:
print('That position is already occupied, try
another')
#method to check if there are no more moves left
def no_moves_left(ticTac):
for i in
range(len(ticTac)):
for j
in range(len(ticTac[0])):
if ticTac[i][j]==' ':
#empty space found
return False
return True #no empty spaces
#method to print the board
def print_board(ticTac):
for i in
ticTac:
for j
in i:
print('[{}]'.format(j),end='
')
print()
#method to play the game for once
def play():
#creating board
ticTac=[]
for i in
range(3):
ticTac.append(['
',' ',' '])
#defining characters for player and
computer
player='X'
computer='O'
#starting with random player's
turn(either computer or player)
player_turn = True
if random.randint(0,1)==0:
player_turn=False
print('Player take X and
Computer take O')
if player_turn:
print('Player
starts first')
else:
print('Computer
starts first')
#loops until no_moves_left or win
while not
no_moves_left(ticTac):
#printing
board
print_board(ticTac)
#checking if
player's turn
if
player_turn:
#making player's move
make_move(ticTac,player,computer=False)
#checking if won
if hasWon(ticTac,player):
#won
print('Congratulations, user wins')
break
else:
#computer's move
make_move(ticTac,computer,computer=True)
if hasWon(ticTac,computer):
print('Computer wins')
break
#checking
if tied
if
no_moves_left(ticTac):
print('Its a
tie!')
player_turn=not player_turn
#playing one round
play()
#output 1 Player take X and Computer take O Player starts first [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Your turn Row index: 0 Column index: 0 [X] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Computer chose row: 0 and col: 2 [X] [ ] [O] [ ] [ ] [ ] [ ] [ ] [ ] Your turn Row index: 1 Column index: 0 [X] [ ] [O] [X] [ ] [ ] [ ] [ ] [ ] Computer chose row: 2 and col: 0 [X] [ ] [O] [X] [ ] [ ] [O] [ ] [ ] Your turn Row index: 1 Column index: 1 [X] [ ] [O] [X] [X] [ ] [O] [ ] [ ] Computer chose row: 0 and col: 1 [X] [O] [O] [X] [X] [ ] [O] [ ] [ ] Your turn Row index: 1 Column index: 2 Congratulations, user wins #output 2 Player take X and Computer take O Computer starts first [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Computer chose row: 0 and col: 0 [O] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Your turn Row index: 0 Column index: 1 [O] [X] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Computer chose row: 0 and col: 2 [O] [X] [O] [ ] [ ] [ ] [ ] [ ] [ ] Your turn Row index: 1 Column index: 0 [O] [X] [O] [X] [ ] [ ] [ ] [ ] [ ] Computer chose row: 2 and col: 1 [O] [X] [O] [X] [ ] [ ] [ ] [O] [ ] Your turn Row index: 1 Column index: 1 [O] [X] [O] [X] [X] [ ] [ ] [O] [ ] Computer chose row: 1 and col: 2 [O] [X] [O] [X] [X] [O] [ ] [O] [ ] Your turn Row index: 2 Column index: 0 [O] [X] [O] [X] [X] [O] [X] [O] [ ] Computer chose row: 2 and col: 2 Computer wins
use python to do it implement the tic-tac-toe game. What to submit: 1. Your source code...
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...
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...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 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. Create...
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...
CSII Java plz GUI Implementation Implement a tic-tac-toe game via GUI. Your game must have to panels, one display the game board for the user to play and the other is a control panel showing the user with options to pay a new game or quit. At any moment, only one panel is shown taking the entire window/frame. Initially, the game boar shown first, and the control panel is shown only when the user completes a game.
1. Use Turtle Graphics to create a tic tac toe grid in Python. Write a Python program that prints a tic tac toe grid. Use Turtle Graphics to create the graphic.
In Python, Randomly select X or O to go first for a tic tac toe game.
So I have to make a tic tac toe game in C++. and the problem is asking me to design, implement and test classea to reperesnt the game board(3x3 sqaure), and the x and o markers. the problem is also asking me to provide suitable observor and mutator methods for modifying the game board and displaying game status. I have to use clases to create a game that prompts for player x and player O to place markers at specified...