iccaros Posted September 17, 2008 Report Share Posted September 17, 2008 what am I doing wrong??this should be a this or that answer, but it fails if either are not true??code below finish = input("Finish:") if board.arena[finish[0]] [finish[1]] != '-' or board.arena[finish[0]] [finish[1]] != 'B': print"Not a legal move"#!/usr/bin/env pythonimport os, sys#text and copywrtie information with rulesdef Question(): question = raw_input("Welcome to PyHexAPawn, would you like to read the instructions? Y/n: ") if question == 'Y' or question == 'n': if question == 'Y': OpeningText() else: print "you must enter either Y or n, nothing more please" Question()def OpeningText(): Rules_File = open('rules.txt', 'r') for line in Rules_File: print line Rules_File.close()def Clear_screen(): name = os.name if name == 'nt': os.system("cls") else: os.system("clear")class Game_Board(): def __init__(self): self.arena = [ ['B','B','B'], ['-','-','-'], ['W','W','W'] ] def Print_Board(self): print self.arena[0][0], self.arena[0][1], self.arena[0][2] print self.arena[1][0], self.arena[1][1], self.arena[1][2] print self.arena[2][0], self.arena[2][1], self.arena[2][2]def main(): #OpeningText() Question() #define board and print it Clear_screen() board = Game_Board() board.Print_Board() #this will be the main loop a return will exit while 1: while 1: #user input start = input("Start:") if board.arena[start[0]] [start[1]] != 'W': print"you must choose your pawn" else: finish = input("Finish:") if board.arena[finish[0]] [finish[1]] != '-' or board.arena[finish[0]] [finish[1]] != 'B': print"Not a legal move" else: return return #end gameif __name__ =="__main__": main() Quote Link to post Share on other sites
iccaros Posted September 20, 2008 Author Report Share Posted September 20, 2008 don't know what I did wrong, but in case people care, here is my code at its current state. this is a nice basic AI experiment. no fancy graphics, thou with pygame they would be simple to add, but for now this is an experiment in having the computer learn, the same way we do, observer and repeat along with remember your successes. this is still rough, I am working out logic...love that python shell#!/usr/bin/env pythonimport os, sys, random, time#text and copywrtie information with rulesdef Question(): question = raw_input("Welcome to PyHexAPawn, would you like to read the instructions? Y/n: ") if question == 'Y' or question == 'n': if question == 'Y': OpeningText() else: print "you must enter either Y or n, nothing more please" Question()def OpeningText(): x = 0 Rules_File = open('rules.txt', 'r') for line in Rules_File: print line, x += 1 if x == 40: raw_input("Press Enter to contenue") x = 0 Rules_File.close()#cross platform clear screendef Clear_screen(): name = os.name if name == 'nt': os.system("cls") else: os.system("clear")#find the key in a dictionary from a valuedef find_key(dic,val): return [k for k, v in dic.iteritems() if v == val] [0]def User_inputStart(board): try: start = int(raw_input("Start:")) except ValueError: print "incorrect input" start = User_inputStart(board) if board.arena[start] != 'W': print "you have to select your pawn" User_inputStart(board) return(start)def User_inputFinish(): try: finish = int(raw_input("Finish:")) except ValueError: print "incorrect input" User_inputFinish() return(finish)def Check_badMove(randMove): badmove = [9,13,19,23] #holds moves that would be illegal for check in badmove: if randMove == check: return 1 return 0class Game_Board(): def __init__(self): self.arena = [ 'B','B','B', '-','-','-', 'W','W','W' ] self.location = {0: 0,1: 1,2: 2,10: 3,11: 4,12: 5,20: 6,21: 7,22: 8} def Print_Board(self): print self.arena[0], self.arena[1], self.arena[2] print self.arena[3], self.arena[4], self.arena[5] print self.arena[6], self.arena[7], self.arena[8]class Computer(): def __init__(self): self.memory = [] self.currentMap = [] self.moves = [] self.currentmove = [] self.currentPawns = [] def Move(self,board): self.memory.append(board) #returns a list, with the first number representing true or false def Checkmemory(self,test): x = 0 for check in self.memory: if check == test: return ("true",x) x += 1 return ("false",0) def addToMoves(self,Move): self.currentmove.append(Move) def Win(self): self.moves.append(self.currentmove) self.currentmove = [] def Lose(self): x = len(self.currentmove) del self.currentmove[x-1] self.moves.append(self.currentmove) self.currentmove = []#main loopdef main(): #create game instance varables computer = Computer() computer.currentPawns = [0,1,2] #I like default values, I know so Un Python like playerStart = 0 playerFinish = 0 Question() raw_input("Press Enter to start play") #define moves #define board board = Game_Board() contenue = "yes" #this will be the main loop a return will exit while contenue == "yes": #define if the player made a fair move nomove = 0 Clear_screen() board.Print_Board() while 1: #user input check playerStart = User_inputStart(board) playerFinish = User_inputFinish() #check that move is legal if find_key(board.location,playerStart) - 10 == find_key(board.location,playerFinish):#up one if board.arena[playerFinish] != 'B': board.arena[playerStart] = '-' board.arena[playerFinish] = 'W' break if board.arena[playerFinish] == 'B': nomove = 1 break if find_key(board.location,playerStart) - 11 == find_key(board.location,playerFinish):#diagnal left if board.arena[playerFinish] != 'B': nomove = 1 break elif board.arena[playerFinish] == 'B': board.arena[playerStart] = '-' board.arena[playerFinish] = 'W' #need to remove computer player break elif find_key(board.location,playerStart) - 9 == find_key(board.location,playerFinish):#diagnal right if board.arena[playerFinish] != 'B': nomove = 1 break elif board.arena[playerFinish] == 'B': board.arena[playerStart] = '-' board.arena[playerFinish] = 'W' #need to remove computer player break if nomove == 1: print "nomove" else: #redraw board Clear_screen() board.Print_Board() time.sleep(2)#slow down move, because we know interpated languages are slow :) while 1: #computers move answer = computer.Checkmemory(board.arena) if answer[0] == "true":#found layout in memory board.arena[computer.moves[answer[1]][0]] = "-" board.arena[computer.moves[answer[1]][1]] = "B" else:#did not find in memory computer.memory.append(computer.currentMap) randStart = computer.currentPawns[random.randint(0,len(computer.currentPawns)-1) ] randMove = find_key(board.location,randStart) + random.randint(9,11) if randMove == randStart + 10: if board.arena[board.location[randMove]] == "-": board.arena[board.location[randStart]] = "-" board.arena[board.location[randMove]] = "B" tempMove = [randStart,randMove] computer.currentmove.append(tempMove) break else: if Check_badMove(randMove) != 1: if board.arena[board.location[randMove]] == "W": board.arena[board.location[randStart]] = "-" board.arena[board.location[randMove]] = "B" tempMove = [randStart,randMove] computer.currentmove.append(tempMove) break #redraw board Clear_screen() board.Print_Board() #end gameif __name__ =="__main__": main() Quote Link to post Share on other sites
tobywells Posted May 31, 2018 Report Share Posted May 31, 2018 Thank you for an update! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.