python - Im getting an error in the shell whenever enter is pressed when I'm changing the board size -
have @ changeboardsize() it'll part of while loop have change i'm not sure change or change to. appreciate done in anyway. ignore because trying character count can post this.
import random def setupgameboard(board, boardsize, playerinitials, computerinitials): row in range(1, boardsize + 1): column in range(1, boardsize + 1): if (row == (boardsize + 1) // 2 , column == (boardsize + 1) // 2 + 1) or (column == (boardsize + 1) // 2 , row == (boardsize + 1) // 2 + 1): board[row][column] = computerinitials elif (row == (boardsize + 1) // 2 + 1 , column == (boardsize + 1) // 2 + 1) or (column == (boardsize + 1) // 2 , row == (boardsize + 1) // 2): board[row][column] = playerinitials else: board[row][column] = " " def changeinitials(playername): print("enter initials for", playername, "'s piece") playerinitials = input() playerinitials = playerinitials.upper() print("enter initials computer's piece") computerinitials = input() computerinitials = computerinitials.upper() return computerinitials, playerinitials def changeboardsize(): boardsize = int(input("enter board size (between 4 , 9): ")) while not(boardsize >= 4 , boardsize <= 9): boardsize = int(input("enter board size (between 4 , 9): ")) if boardsize != [4, 5, 6, 7, 8, 9]: changeboardsize() elif boardsize == " ": changeboardsize() return boardsize def gethumanplayermove(playername): print(playername, "enter coodinates of square want place piece: ", end="") coordinates = input() if coordinates.isdigit(): movevalid = true return int(coordinates) def getcomputerplayermove(boardsize): return random.randint(1, boardsize) * 10 + random.randint(1, boardsize) def gameover(board, boardsize): row in range(1 , boardsize + 1): column in range(1, boardsize + 1): if board[row][column] == " ": return false return true def getplayersname(): playername = input("what name? ") return playername def checkifmoveisvalid(board, move): row = move % 10 column = move // 10 moveisvalid = false if board[row][column] == " ": moveisvalid = true return moveisvalid def getplayerscore(board, boardsize, piece): score = 0 row in range(1, boardsize + 1): column in range(1, boardsize + 1): if board[row][column] == piece: score = score + 1 return score def checkiftherearepiecestoflip(board, boardsize, startrow, startcolumn, rowdirection, columndirection): rowcount = startrow + rowdirection columncount = startcolumn + columndirection flipstillpossible = true flipfound = false opponentpiecefound = false while rowcount <= boardsize , rowcount >= 1 , columncount >= 1 , columncount <= boardsize , flipstillpossible , not flipfound: if board[rowcount][columncount] == " ": flipstillpossible = false elif board[rowcount][columncount] != board[startrow][startcolumn]: opponentpiecefound = true elif board[rowcount][columncount] == board[startrow][startcolumn] , not opponentpiecefound: flipstillpossible = false else: flipfound = true rowcount = rowcount + rowdirection columncount = columncount + columndirection return flipfound def flipopponentpiecesinonedirection(board, boardsize, startrow, startcolumn, rowdirection, columndirection): flipfound = checkiftherearepiecestoflip(board, boardsize, startrow, startcolumn, rowdirection, columndirection) if flipfound: rowcount = startrow + rowdirection columncount = startcolumn + columndirection while board[rowcount][columncount] != " " , board[rowcount][columncount] != board[startrow][startcolumn]: if board[rowcount][columncount] == "h": board[rowcount][columncount] = "c" else: board[rowcount][columncount] = "h" rowcount = rowcount + rowdirection columncount = columncount + columndirection def makemove(board, boardsize, move, humanplayersturn): row = move % 10 column = move // 10 if humanplayersturn: board[row][column] = "h" else: board[row][column] = "c" flipopponentpiecesinonedirection(board, boardsize, row, column, 1, 0) flipopponentpiecesinonedirection(board, boardsize, row, column, -1, 0) flipopponentpiecesinonedirection(board, boardsize, row, column, 0, 1) flipopponentpiecesinonedirection(board, boardsize, row, column, 0, -1) def printline(boardsize): print(" ", end="") count in range(1, boardsize * 2): print("_", end="") print() def displaygameboard(board, boardsize): print() print(" ", end="") column in range(1, boardsize + 1): print(" ", end="") print(column, end="") print() printline(boardsize) row in range(1, boardsize + 1): print(row, end="") print(" ", end="") column in range(1, boardsize + 1): print("|", end="") print(board[row][column], end="") print("|") printline(boardsize) print() def displaymenu(): print("(p)lay game") print("(e)nter name") print("(c)hange board size") print("(i)initials change") print("(a)lter piece name") print("(q)uit") print() def getmenuchoice(playername): print(playername, "enter letter of chosen option: ", end="") choice = input() return choice def createboard(): board = [] count in range(boardsize + 1): board.append([]) count2 in range(boardsize + 1): board[count].append("") return board def playgame(playername, boardsize, playerinitials, computerinitials): board = createboard() setupgameboard(board, boardsize, playerinitials, computerinitials) humanplayersturn = false while not gameover(board, boardsize): humanplayersturn = not humanplayersturn displaygameboard(board, boardsize) moveisvalid = false while not moveisvalid: if humanplayersturn: move = gethumanplayermove(playername) else: move = getcomputerplayermove(boardsize) moveisvalid = checkifmoveisvalid(board, move) if not humanplayersturn: print("press enter key , computer make move") input() makemove(board, boardsize, move, humanplayersturn) displaygameboard(board, boardsize) humanplayerscore = getplayerscore(board, boardsize, "h") computerplayerscore = getplayerscore(board, boardsize, "c") if humanplayerscore > computerplayerscore: print("well done", playername, ", have won game!") elif humanplayerscore == computerplayerscore: print("that draw!") else: print("the computer has won game!") print() random.seed() boardsize = 6 playername = "" choice = "" playerinitials = 'h' computerinitials = 'c' while choice.lower() != "q": #.lower() makes allows uppercase input displaymenu() choice = getmenuchoice(playername) if choice.lower() == "p": playgame(playername, boardsize, computerinitials, playerinitials) elif choice.lower() == "e": playername = getplayersname() elif choice.lower() == 'a': changepiecename() elif choice.lower() == 'i': changeinitials(playername) elif choice.lower() == "c": boardsize = changeboardsize()
there problem because don't handle entering characters other numbers fails on line 39
return int(coordinates) # if coordinates not str convertable int fails
so need like
def gethumanplayermove(playername): print(playername, "enter coodinates of square want place piece: ", end="") coordinates = input() if coordinates.isdigit(): movevalid = true try: return int(coordinates) except valueerror: print("please enter number") print(playername, "enter coodinates of square want place piece: ", end="") return gethumanplayermove(playername)
or create while
loop did below. so
while not isinstance(coordinates, int): ...
the function this
def gethumanplayermove(playername): movevalid = false while not movevalid: print(playername, "enter coodinates of square want place piece: ", end="") coordinates = input() if coordinates.isdigit(): movevalid = true return int(coordinates)
Comments
Post a Comment