Help with my game please? So far there are three situations that can occur in th
ID: 3680952 • Letter: H
Question
Help with my game please?
So far there are three situations that can occur in the game, winning the coin toss (going first, second) and losing the toss (going second).
Out of these three situations only going first works correctly. Player can win, computer can win or a cat game can happen.
When the computer goes first for the other two situation my code lags a turn behind. If someone wins it wont show up until another set of turns pass. The code looks mirrored so I;m not sure why its waiting an extra turn.
I've provided my python file and a notepad copy of it just in case there are issues opening it. Ignore the automatic and 2 player modes, I plan on adding them once i get 1 player mode set.
Here is the code:
#Declarations
import random
import sys
#Game Board
gameBoard= [0,1,2,3,4,5,6,7,8,9]
def displayBoard ():
print ' ', gameBoard[1], '|', gameBoard[2], '|', gameBoard[3]
print ' --------------'
print ' ', gameBoard[4], '|', gameBoard[5], '|', gameBoard[6]
print ' --------------'
print ' ', gameBoard[7], '|', gameBoard[8], '|', gameBoard[9], ' '
#Turns
def gameTurn (player, piece1, piece2):
turn = False
if player is 'Computer':
while turn == False:
spot = random.randint (1,9)
spot = int(spot)
if gameBoard[spot] != piece1 and gameBoard[spot] != piece2:
gameBoard[spot] = piece2
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
turn = True
else:
spot = raw_input('Select a spot ')
while int(spot) not in [1,2,3,4,5,6,7,8,9]:
print ('Please enter a value 1 through 9 ')
spot2 = raw_input('Select a spot ')
spot = spot2
while turn == False:
spot = int(spot)
if gameBoard[spot] != piece1 and gameBoard[spot] != piece2:
gameBoard[spot] = piece1
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
turn = True
else:
print ('Select a new spot, this one is taken')
spot = raw_input('Select a spot ')
#Win Conditions
def checkPieces (piece, spot1, spot2, spot3):
checkPiece = False
if gameBoard[spot1] == piece and gameBoard[spot2] == piece and gameBoard[spot3] == piece:
checkPiece = True
return checkPiece
else:
return checkPiece
def gameStatus (piece):
checkRows = False
if checkPieces(piece, 1, 2, 3):
checkRows = True
return checkRows
if checkPieces(piece, 4, 5, 6):
checkRows = True
return checkRows
if checkPieces(piece, 7, 8, 9):
checkRows = True
return checkRows
if checkPieces(piece, 1, 4, 7):
checkRows = True
return checkRows
if checkPieces(piece, 2, 5, 8):
checkRows = True
return checkRows
if checkPieces(piece, 3, 6, 9):
checkRows = True
return checkRows
if checkPieces(piece, 1, 5, 9):
checkRows = True
return checkRows
if checkPieces(piece, 3, 5, 7):
checkRows = True
return checkRows
def catGame(piece1, piece2):
if gameBoard[1] == piece1 or gameBoard[1] == piece2:
if gameBoard[2] == piece1 or gameBoard[2] == piece2:
if gameBoard[3] == piece1 or gameBoard[3] == piece2:
if gameBoard[4] == piece1 or gameBoard[4] == piece2:
if gameBoard[5] == piece1 or gameBoard[5] == piece2:
if gameBoard[6] == piece1 or gameBoard[6] == piece2:
if gameBoard[7] == piece1 or gameBoard[7] == piece2:
if gameBoard[8] == piece1 or gameBoard[8] == piece2:
if gameBoard[9] == piece1 or gameBoard[9] == piece2:
if gameCondition == False:
print "The game is a tie."
sys.exit()
#Main Game
gameCondition = False
winner = ' '
mode = input('How many players would like to play? ')
if mode == 0:
#Computer vs Computer
print ('Auto Mode')
if mode == 1:
#1 Player
print ('1 Player Mode')
player1 = raw_input('Player 1, what is your name? ')
player2 = 'Computer'
piece1 = raw_input(player1 + '. Which icon would you like to use? ')
piece2 = 'O'
if piece2 == piece1:
piece2 = 'X'
coin = raw_input('heads(0) or tails(1)? ')
coin2 = random.randint (0,1)
if int(coin) == int(coin2):
print ('Result: ', coin2)
turn = raw_input('Would you like to go first or second? ')
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
if turn == 'first':
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
print (winner + ' is the Winner!')
if turn == 'second':
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
print (winner + ' is the Winner!')
if coin != coin2:
print ('Result: ', coin2)
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
print (winner + ' is the Winner!')
if mode == 2:
#2 Players
print ('2 Player Mode')
if mode not in [0,1,2]:
print (' Sorry, you have entered an incorrect response. Please try again later, Good Bye.')
Explanation / Answer
I have added comment where fix should be.
#Declarations
import random
import sys
#Game Board
gameBoard= [0,1,2,3,4,5,6,7,8,9]
def displayBoard ():
print ' ', gameBoard[1], '|', gameBoard[2], '|', gameBoard[3]
print ' --------------'
print ' ', gameBoard[4], '|', gameBoard[5], '|', gameBoard[6]
print ' --------------'
print ' ', gameBoard[7], '|', gameBoard[8], '|', gameBoard[9], ' '
#Turns
def gameTurn (player, piece1, piece2):
turn = False
if player is 'Computer':
while turn == False:
spot = random.randint (1,9)
spot = int(spot)
if gameBoard[spot] != piece1 and gameBoard[spot] != piece2:
gameBoard[spot] = piece2
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
turn = True
else:
spot = raw_input('Select a spot ')
while int(spot) not in [1,2,3,4,5,6,7,8,9]:
print ('Please enter a value 1 through 9 ')
spot2 = raw_input('Select a spot ')
spot = spot2
while turn == False:
spot = int(spot)
if gameBoard[spot] != piece1 and gameBoard[spot] != piece2:
gameBoard[spot] = piece1
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
turn = True
else:
print ('Select a new spot, this one is taken')
spot = raw_input('Select a spot ')
#Win Conditions
def checkPieces (piece, spot1, spot2, spot3):
checkPiece = False
if gameBoard[spot1] == piece and gameBoard[spot2] == piece and gameBoard[spot3] == piece:
checkPiece = True
return checkPiece
else:
return checkPiece
def gameStatus (piece):
checkRows = False
if checkPieces(piece, 1, 2, 3):
checkRows = True
return checkRows
if checkPieces(piece, 4, 5, 6):
checkRows = True
return checkRows
if checkPieces(piece, 7, 8, 9):
checkRows = True
return checkRows
if checkPieces(piece, 1, 4, 7):
checkRows = True
return checkRows
if checkPieces(piece, 2, 5, 8):
checkRows = True
return checkRows
if checkPieces(piece, 3, 6, 9):
checkRows = True
return checkRows
if checkPieces(piece, 1, 5, 9):
checkRows = True
return checkRows
if checkPieces(piece, 3, 5, 7):
checkRows = True
return checkRows
def catGame(piece1, piece2):
if gameBoard[1] == piece1 or gameBoard[1] == piece2:
if gameBoard[2] == piece1 or gameBoard[2] == piece2:
if gameBoard[3] == piece1 or gameBoard[3] == piece2:
if gameBoard[4] == piece1 or gameBoard[4] == piece2:
if gameBoard[5] == piece1 or gameBoard[5] == piece2:
if gameBoard[6] == piece1 or gameBoard[6] == piece2:
if gameBoard[7] == piece1 or gameBoard[7] == piece2:
if gameBoard[8] == piece1 or gameBoard[8] == piece2:
if gameBoard[9] == piece1 or gameBoard[9] == piece2:
if gameCondition == False:
print "The game is a tie."
sys.exit()
#Main Game
gameCondition = False
winner = ' '
mode = input('How many players would like to play? ')
if mode == 0:
#Computer vs Computer
print ('Auto Mode')
if mode == 1:
#1 Player
print ('1 Player Mode')
player1 = raw_input('Player 1, what is your name? ')
player2 = 'Computer'
piece1 = raw_input(player1 + '. Which icon would you like to use? ')
piece2 = 'O'
if piece2 == piece1:
piece2 = 'X'
coin = raw_input('heads(0) or tails(1)? ')
coin2 = random.randint (0, 1)
if int(coin) == int(coin2):
print ('Result: ', coin2)
turn = raw_input('Would you like to go first or second? ')
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
if turn == 'first':
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
print (winner + ' is the Winner!')
if turn == 'second':
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
# FIX : Check if player1 is won after this move
print (winner + ' is the Winner!')
if coin != coin2:
print ('Result: ', coin2)
print (' ' + player1 + ' = ' + piece1 + ' ' + player2 + ' = ' + piece2)
displayBoard()
while gameCondition == False:
catGame(piece1, piece2)
if gameStatus(piece1) == True:
winner = player1
gameCondition = True
print (player2 + 's turn')
gameTurn (player2, piece1, piece2)
catGame(piece1, piece2)
if gameStatus(piece2) == True:
winner = player2
gameCondition = True
print (player1 + 's turn')
gameTurn (player1, piece1, piece2)
print (winner + ' is the Winner!')
if mode == 2:
#2 Players
print ('2 Player Mode')
if mode not in [0,1,2]:
print (' Sorry, you have entered an incorrect response. Please try again later, Good Bye.')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.