tic tac toe - Python How to keep score in a tic tac toe game -
tic tac toe - Python How to keep score in a tic tac toe game -
i need help figuring out how calculate record wins, losses, , ties. right now, code returns string "loss" when player loses. want homecoming 1, 1 loss. can help? here code have far.
# tic tac toe import random def score(): wins = 0 losses = 0 ties = 0 def result(wins, losses, ties): if result =='win': wins += 1 if result == 'loss': losses += 1 else: ties += 1 def drawboard(board): # function prints out board passed. # "board" list of 10 strings representing board (ignore index 0) print(' | |') print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) print(' | |') print('-----------') print(' | |') print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) print(' | |') print('-----------') print(' | |') print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) print(' | |') def inputplayerletter(): # let's player type letter want be. # returns list player's letter first item, , computer's letter second. letter = '' while not (letter == 'x' or letter == 'o'): print('do want x or o?') letter = input().upper() # first element in tuple player's letter, sec computer's letter. if letter == 'x': homecoming ['x', 'o'] else: homecoming ['o', 'x'] def whogoesfirst(): # randomly take player goes first. if random.randint(0, 1) == 0: homecoming 'computer' else: homecoming 'player' def playagain(): # function returns true if player wants play again, otherwise returns false. print('do want play again? (yes or no)') homecoming input().lower().startswith('y') def makemove(board, letter, move): board[move] = letter def iswinner(bo, le): # given board , player's letter, function returns true if player has won. # utilize bo instead of board , le instead of letter don't have type much. homecoming ((bo[7] == le , bo[8] == le , bo[9] == le) or # across top (bo[4] == le , bo[5] == le , bo[6] == le) or # across middle (bo[1] == le , bo[2] == le , bo[3] == le) or # across bottom (bo[7] == le , bo[4] == le , bo[1] == le) or # downwards left side (bo[8] == le , bo[5] == le , bo[2] == le) or # downwards middle (bo[9] == le , bo[6] == le , bo[3] == le) or # downwards right side (bo[7] == le , bo[5] == le , bo[3] == le) or # diagonal (bo[9] == le , bo[5] == le , bo[1] == le)) # diagonal def getboardcopy(board): # create duplicate of board list , homecoming duplicate. dupeboard = [] in board: dupeboard.append(i) homecoming dupeboard def isspacefree(board, move): # homecoming true if passed move free on passed board. homecoming board[move] == ' ' def getplayermove(board): # allow player type in move. move = ' ' while move not in '1 2 3 4 5 6 7 8 9'.split() or not isspacefree(board, int(move)): print('what next move? (1-9)') move = input() homecoming int(move) def chooserandommovefromlist(board, moveslist): # returns valid move passed list on passed board. # returns none if there no valid move. possiblemoves = [] in moveslist: if isspacefree(board, i): possiblemoves.append(i) if len(possiblemoves) != 0: homecoming random.choice(possiblemoves) else: homecoming none def getcomputermove(board, computerletter): # given board , computer's letter, determine move , homecoming move. if computerletter == 'x': playerletter = 'o' else: playerletter = 'x' # here our algorithm our tic tac toe ai: # first, check if can win in next move in range(1, 10): re-create = getboardcopy(board) if isspacefree(copy, i): makemove(copy, computerletter, i) if iswinner(copy, computerletter): homecoming # check if player win on next move, , block them. in range(1, 10): re-create = getboardcopy(board) if isspacefree(copy, i): makemove(copy, playerletter, i) if iswinner(copy, playerletter): homecoming # seek take 1 of corners, if free. move = chooserandommovefromlist(board, [1, 3, 7, 9]) if move != none: homecoming move # seek take center, if free. if isspacefree(board, 5): homecoming 5 # move on 1 of sides. homecoming chooserandommovefromlist(board, [2, 4, 6, 8]) def isboardfull(board): # homecoming true if every space on board has been taken. otherwise homecoming false. in range(1, 10): if isspacefree(board, i): homecoming false homecoming true print('welcome tic tac toe!') while true: # reset board theboard = [' '] * 10 playerletter, computerletter = inputplayerletter() turn = whogoesfirst() print('the ' + turn + ' go first.') gameisplaying = true while gameisplaying: if turn == 'player': # player's turn. drawboard(theboard) move = getplayermove(theboard) makemove(theboard, playerletter, move) if iswinner(theboard, playerletter): drawboard(theboard) result = 'win' print('hooray! have won game!') iswinner = true gameisplaying = false else: if isboardfull(theboard): drawboard(theboard) result = 'tie' print('the game tie!') break else: turn = 'computer' else: # computer's turn. move = getcomputermove(theboard, computerletter) makemove(theboard, computerletter, move) if iswinner(theboard, computerletter): drawboard(theboard) print('the computer has beaten you! lose.') result = 'loss' iswinner = false gameisplaying = false else: if isboardfull(theboard): drawboard(theboard) result = 'tie' print('the game tie!') break else: turn = 'player' if not playagain(): print(result) break
instead of doing result = 'win'
set wins = 0
@ start , when resetting, , when player wins wins += 1
. same ties
, losses
, can extract finish record function takes wins
,losses
, ties
arguments.
python tic-tac-toe
Comments
Post a Comment