def getAIChoice(): #This function is used to change certain in game features ai_choice = entryMove.get() if ai_choice == "B": #The user can change the colour of piece that the AI opponent plays as chess.ai_colour_flag = True #The ai colour flag in chess.py is raised and a message is outputted to confirm the colour change brint("AI colour changed") elif ai_choice == "W": #This feature could be used as a form of a hint system chess.ai_colour_flag = False #If the user does not know what to play they can make the AI play for them brint("AI colour changed") elif ai_choice == "0" or ai_choice == "1" or ai_choice == "2" or ai_choice == "3": #The user can also change the ai difficulty level whilst playing chess.difficulty_level = int(ai_choice) d_string = "AI difficulty level: " + str(ai_choice) #A message is outputted to provide feedback to the user brint(d_string) else: #If a valid input is not given then some instructions are brinted to help the user brint("\nEnter W/B to change the AI colour\n") brint("\nEnter a number from 0 to 3 to change AI difficulty\n")
def getAIChoice(): ai_choice = entryMove.get() if ai_choice == "0" or ai_choice == "1" or ai_choice == "2" or ai_choice == "3": #The user is able to change difficulty in this mode, but not colour chess.difficulty_level = int(ai_choice) #If the user were to change the colour it would have no effect since it is AI vs AI d_string = "AI difficulty level: " + str(ai_choice) brint(d_string) else: brint("\nEnter a number from 0 to 3 to change AI difficulty\n")
def cpuWindow(event=None): #This function creates the AI sub window clearWindow() #This window is the same as the human sub window except for the entry widget which is actually used here labelCPU = tk.Label(mainFrame, text="Versus Computer") labelCPU.config(font=("Comic Sans MS", 11, "bold")) labelCPU.pack() entryMove = tk.Entry(mainFrame) entryMove.pack() entryMove.focus_set() def getAIChoice(): #This function is used to change certain in game features ai_choice = entryMove.get() if ai_choice == "B": #The user can change the colour of piece that the AI opponent plays as chess.ai_colour_flag = True #The ai colour flag in chess.py is raised and a message is outputted to confirm the colour change brint("AI colour changed") elif ai_choice == "W": #This feature could be used as a form of a hint system chess.ai_colour_flag = False #If the user does not know what to play they can make the AI play for them brint("AI colour changed") elif ai_choice == "0" or ai_choice == "1" or ai_choice == "2" or ai_choice == "3": #The user can also change the ai difficulty level whilst playing chess.difficulty_level = int(ai_choice) d_string = "AI difficulty level: " + str(ai_choice) #A message is outputted to provide feedback to the user brint(d_string) else: #If a valid input is not given then some instructions are brinted to help the user brint("\nEnter W/B to change the AI colour\n") brint("\nEnter a number from 0 to 3 to change AI difficulty\n") buttonEnter = tk.Button(mainFrame, text="Enter", command=getAIChoice) buttonEnter.pack() buttonMenu = tk.Button(text="Menu", command=lambda: chess.Menu()) buttonMenu.pack(side="right") buttonResign = tk.Button(text="Resign", command=lambda: chess.Resign()) buttonResign.pack(side="right") buttonDraw = tk.Button(text="Draw", command=lambda: chess.offerDraw()) buttonDraw.pack(side="right") buttonTakeback = tk.Button(text="Takeback", command=lambda: chess.offerTakeback()) buttonTakeback.pack(side="right") initTerminalChess() initTkinterBoard() chess.ai_flag = True #The ai flag is raised so that the Move function in chess.py allows the AI to play every other turn buttonBack = tk.Button(mainFrame, command=selectOpponentWindow, text="Back") buttonBack.pack(side="bottom") brint("Click the 'Menu' button to view the menu options") brint("Click the board to make the AI move") brint("Click instructions to make them disappear!")
def getUsername(): #This time the function gets the username and adds it to the usernames file if it does not already exist username = entryUsername.get() if username == "": #Same error handling as before, if the entry box is blank then exit the function brint("\nPlease enter a username\n") return None with open("usernames.txt") as f: u_data = f.read() u_split = u_data.split("\n") #The data is read from the file and split by line for name in u_split: if name == username: #If a duplicate is found then the username is not added brint("\nThis user already exists.\n") return None u_data += username + "\n" #Otherwise the username is added with a new line character and a message is brinted to provide feedback to the user f = open("usernames.txt", "w") f.write(u_data) f.close() brint("\nUsername added.\n")
def getUsername(): #This function gets the username and logs the user in with it username = entryUsername.get() if username == "": #If the user leaves the entry blank when the button is pressed then the function ends early and the user can try again brint("\nPlease enter a username\n") return None with open("usernames.txt") as f: #If the username is valid then the usernames file is read u_data = f.read() #The username data is then split by line u_split = u_data.split("\n") valid_flag = False for name in u_split: if name == username: #If the username that the user entered is found in the file then the valid flag is raised valid_flag = True if valid_flag == False: #If the username was not found then the user is not logged in brint("\nPlease enter a valid username.\n") else: #Otherwise the user is logged in chess.nameUser = username #The variable nameUser (a global variable from the chess.py) is assigned the username, thus loggin the user in login_string = "\nLogged in as " + username + ".\n" #A message is brinted to tell the user that the log in was successful brint(login_string)
def demoWindow(event=None): #This function creates the Demo sub window clearWindow() #This is the same as the cpu window except colour cannot be changed labelCPU = tk.Label(mainFrame, text="AI vs AI Demo") labelCPU.config(font=("Comic Sans MS", 11, "bold")) labelCPU.pack() entryMove = tk.Entry(mainFrame) entryMove.pack() entryMove.focus_set() def getAIChoice(): ai_choice = entryMove.get() if ai_choice == "0" or ai_choice == "1" or ai_choice == "2" or ai_choice == "3": #The user is able to change difficulty in this mode, but not colour chess.difficulty_level = int(ai_choice) #If the user were to change the colour it would have no effect since it is AI vs AI d_string = "AI difficulty level: " + str(ai_choice) brint(d_string) else: brint("\nEnter a number from 0 to 3 to change AI difficulty\n") buttonEnter = tk.Button(mainFrame, text="Enter", command=getAIChoice) buttonEnter.pack() buttonMenu = tk.Button(text="Menu", command=lambda: chess.Menu()) buttonMenu.pack(side="right") buttonResign = tk.Button(text="Resign", command=lambda: chess.Resign()) buttonResign.pack(side="right") buttonDraw = tk.Button(text="Draw", command=lambda: chess.offerDraw()) buttonDraw.pack(side="right") buttonTakeback = tk.Button(text="Takeback", command=lambda: chess.offerTakeback()) buttonTakeback.pack(side="right") initTerminalChess() initTkinterBoard() chess.ai_duel_flag = True #Here a difficult flag is raised to indicate that the user wants to play in the demo mode buttonBack = tk.Button(mainFrame, command=selectOpponentWindow, text="Back") buttonBack.pack(side="bottom") brint("Click the 'Menu' button to view the menu options") brint("Click the board to make the AI move") brint("Click instructions to make them disappear!")
def pvpWindow(event=None): #This function creates the human sub window clearWindow() #The window is cleared and the title is packed labelPlayer = tk.Label(mainFrame, text="Versus Player") labelPlayer.config(font=("Comic Sans MS", 11, "bold")) labelPlayer.pack() #The entry box does not do anything, but it is here so that the windows have a consistent look to them entryMove = tk.Entry(mainFrame) entryMove.pack() entryMove.focus_set() def getEntry(): return entryMove.get() #In the other modes this button runs more useful functions buttonEnter = tk.Button(mainFrame, text="Enter", command=getEntry) buttonEnter.pack() #These buttons are always available to be pressed by the user buttonMenu = tk.Button(text="Menu", command=lambda: chess.Menu()) buttonMenu.pack(side="right") #The Menu button brints the menu and opens a pop-up for enput, the menu contains more functions that can be run buttonResign = tk.Button(text="Resign", command=lambda: chess.Resign()) buttonResign.pack(side="right") #These buttons are used to resign, to offer a draw, or to offer a takeback respectively buttonDraw = tk.Button(text="Draw", command=lambda: chess.offerDraw()) buttonDraw.pack(side="right") #By packing the buttons to the right, they appear to the right of the chessboard, rather than underneath buttonTakeback = tk.Button(text="Takeback", command=lambda: chess.offerTakeback()) buttonTakeback.pack(side="right") #Then the logical and physical chess boards are initialised initTerminalChess() initTkinterBoard() #There is also a back button which returns the user to the previous window buttonBack = tk.Button(mainFrame, command=selectOpponentWindow, text='Back') buttonBack.pack(side="bottom") #Here are some instructions to help the user get familiar with the software brint("Click the 'Menu' button to view the menu options") brint("Click the board to make the AI move") brint("Click instructions to make them disappear!")