Hope a programmer can help a friend of mine! who is learning to program, this is was he posted to me if some kind member could take a look!
I've attached the guidelines and my code so far. I've added some code that is sort of correct but just needs some tweaking and a while statement adding to run the functions in the correct order. There are a few files on this project but the correct one is called "Othello.py".
Please help!
Assignment Code to look @
can some one that knows python othello check this code out!
player1 = " "
player2 = "@@"
board = []
userChoice = ""
row = ""
col = ""
player = ""
playerOpposite = ""
def newGame():
request_players_names()
header()
game_in_play()
SaveGame(board)
footer()
'game_in_play()'
pass
def mainmenu():
funcNames = [newGame, restoreGame]
userInput = ""
if userInput != "3":
userInput = (input("\n:::Welcome to O'Neillo Game:::\n==============================\n\t\t1. New Game\n\t\t2. Restore a Game\n\t\t3. Quit\n\nSelect a number from the menu : ")).strip()
if userInput in ['1', '2'] : funcNames[int(userInput) -1]()
elif userInput != '3': print("\nInput not recognised, try again!\n")
def request_players_names():
global player1, player2
player1 = players_input(1)
player2 = players_input(2)
print("\nHi " + player1 + " you are ' '\nHi " + player2 + " you are @@\n\nLet's play!")
def players_input(player):
if player == 1:
player = input("Please enter player 1's name?")
while len(player) == "":
player = input("Please enter a valid players name?")
return player
else:
player = input("Please enter player 2's name?")
while player == player1 or len(player) == 0:
player = input("Please enter players name different to player 1?")
return player
def header():
print("Number of tokens for " + player1 + " : 2")
print("Number of tokens for " + player2 + " : 2")
def restoreGame():
pass
def drawBoard():
size = 8
board =[]
counter=1
for i in range (size):
row=[]
for j in range (size):
if counter<10:
number="0"+str(counter)
else:
number = str(counter)
row.append(number)
counter+=1
board.append(row)
return board
def setOthello(board):
for i in range(len(board)):
for j in range(len(board)):
if (i==3 and j == 3):
board[i][j]=str(" ")
elif (i==3 and j == 4):
board[i][j]=str("@@")
elif (i==4 and j == 3):
board[i][j]=str("@@")
elif (i==4 and j == 4):
board[i][j]=str(" ")
return board
def printBoard(board):
for i in range(len(board)):
print(" ----"*len(board))
print("|", end=" ")
for j in range(len(board)):
print(board[i][j], "|", end = " ")
print()
print(" ----"*len(board))
board = drawBoard()
def game_in_play():
game = True
while game == True:
printBoard(board)
userChoice(board)
flip(board, row, col, player, playerOpposite)
print("Hell yeah")
def userChoice(board):
usernumber = int(input("Select your choice between 1 and 64:\n"))
while (usernumber<1 or usernumber>64):
usernumber = int(input("Outside the range!\nSelect your choice between 1 and 64"))
validateMove(board, usernumber, player2)
return board
def validNumber(board, row, col):
valid=True
if (board[row][col]==" " or board[row][col]=="@@"):
valid=False
return valid
def validateMove(board, userChoice, player):
print("Player pattern : ", player)
valid = False
row=-1
col=-1
#check whether the place has been chosen
if (board[row][col]==" " or board[row][col]=="@@"):
valid=False
if (userChoice%len(board)==0):
row = (userChoice//len(board))-1
col = (userChoice%len(board))-1
board[row][col] = player
else:
row = userChoice//len(board)
col = (userChoice%len(board))-1
board[row][col] = player
playerOpposite=""
if (player=="@@"):
playerOpposite=" "
print("this")
else:
playerOpposite="@@"
print("this b")
print("player opposite pattern : ", playerOpposite)
#west
if (board[row][col-1]==playerOpposite):
print("west")
index=(col-1)
while(index>0):
index-=1
if (board[row][index]==player):
valid=True
if(valid):
while(col>0):
col-=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#northwest
if (board[row-1][col-1]==playerOpposite):
print("northwest")
indexRow=(row-1)
indexCol=(col-1)
while(indexRow>1 or indexCol>1):
indexRow-=1
indexCol-=1
if (board[indexRow][indexCol]==player):
valid=True
if(valid):
while(row>1 or col>1):
row-=1
col-=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#north
if (board[row-1][col]==playerOpposite):
print("north")
indexRow=(row-1)
while(indexRow>0):
indexRow-=1
if (board[indexRow][col]==player):
valid=True
if(valid):
while(row>0):
row-=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#northeast
if (board[row-1][col+1]==playerOpposite):
print("northeast")
indexRow=(row-1)
indexCol=(col+1)
while(indexRow!=1 or indexCol!=len(board)-2):
indexRow-=1
indexCol+=1
if (board[indexRow][indexCol]==player):
valid=True
if(valid):
while(row!=0 or col!=len(board)-1):
row-=1
col+=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#east
if (board[row][col+1]==playerOpposite):
print("east")
indexCol=(col+1)
while(indexCol<len(board)-2):
indexCol+=1
print(row, indexCol)
if (board[row][indexCol]==player):
valid=True
if(valid):
while(col<len(board)-2):
col+=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#southeast
if (board[row+1][col+1]==playerOpposite):
print("southeast")
indexRow=(row+1)
indexCol=(col+1)
while(indexRow<len(board)-2 or indexCol<len(board)-2):
indexRow+=1
indexCol+=1
print(indexRow, indexCol)
if (board[indexRow][indexCol]==player):
valid=True
if(valid):
while(row<len(board)-2 or col<len(board)-2):
row+=1
col+=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#south
if (board[row+1][col]==playerOpposite):
print("south")
indexRow=(row+1)
while(indexRow<len(board)-1):
indexRow+=1
if (board[indexRow][col]==player):
valid=True
if(valid):
while(row<len(board)-2):
row+=1
if (board[row][col]==playerOpposite):
board[row][col]=player
#southwest
if (board[row+1][col-1]==playerOpposite):
print("southwest")
indexRow=(row+1)
indexCol=(col-1)
while(indexRow<len(board)-2 or indexCol>1):
indexRow+=1
indexCol-=1
print(indexRow, indexCol)
if (board[indexRow][indexCol]==player):
valid=True
if(valid):
while(row<len(board)-2 or col!=0):
row+=1
col+=1
if (board[row][col]==playerOpposite):
board[row][col]=player
return valid
def flip(board, row, col, player, playerOpposite):
while(row<len(board)-2 or col!=0):
row+=1
col-=1
if (board[row][col]==playerOpposite):
board[row][col]=player
def SaveGame(board):
with open("Data.dat", "w") as fp:
for row in range(len(board)):
string = ""
for col in range(len(board)):
string += board[row][col] + ","
fp.write(string)
fp.write("\n")
fp.close()
def footer():
print("")
'''
board=drawBoard()
#printBoard(board)
setOthello(board)
print()
printBoard(board)
'''
#valid = validateMove(board, 27, player2)
#print(valid)
#printBoard(board)
'''
print("\n\n")
cont = True
userMenu = int(input("Select a number from the menu : "))
while (cont):
while (userMenu<1 or userMenu>3):
userMenu = int(input("Invalid range! Select a number from the menu : "))
if (userMenu==1):
print("Playing with computer")
elif(userMenu==2):
player1Name = input("Insert the first player name : ")
player2Name = input("Insert the second player name : ")
print("Hi ", player1Name, " your are ", "'",player1, "'")
print("Hi ", player2Name, " your are ", player2)
print("\nLet's play!\n")
printBoard(board)
print()
elif(userMenu==3):
print("Bye!")
cont=False
print(menu)
userMenu = int(input("Select a number from the menu : "))
'''
mainmenu()
Here's one problem. On line five, the variable
row
is set as a string. On line 103, flip is called with
row
as one of it’s arguments.
def flip(board, row, col, player, playerOpposite):
while(row<len(board)-2 or col!=0):
In
flip ()
on line 293
row
is tested to be less than
len(board)
producing the error:
Error:
TypeError: '<' not supported between instances of 'str' and 'int'
Because you're testing the string
row
against the integer
len(board)
.
Much appreciated for any help @ all on this as he is trying very hard to get his head around this programming stuff!
just passed this info on to him.... so any more help is very much appreciated... thanks
BashBedlam - can you tell me if you have run the code and if so how far in the game did you get!
are the errors making the game not run! ?
(May-05-2021, 08:27 PM)Powell123 Wrote: [ -> ]BashBedlam - can you tell me if you have run the code and if so how far in the game did you get!
are the errors making the game not run! ?
The game can't be played in its current state. The first move in the game results in the error message BashBedlam pointed out. When that first call to the
flip()
function occurs, the variables
row, col, player, playerOppposite
all still have the empty string values they are assigned in lines 5-8.
In 5 change it to row=0, and in 6 to col=0.
Comment out the global statements
Add print(row, col) above line 296 and see what you get. That is the next logic error.
Image not available.
Also, bad form to post an image link. Please post using the BBT tags for output, error, code, etc.
I will ask him to send me the output code then i will post it using BBT tags.
(May-06-2021, 10:43 PM)jefsummers Wrote: [ -> ]Image not available.
Also, bad form to post an image link. Please post using the BBT tags for output, error, code, etc.