BestTechie Forums: Python Help - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Python Help


#1 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 17 September 2008 - 03:50 PM

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 python
import os, sys

   
	
#text and copywrtie information with rules
def 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 game
		
	
if __name__ =="__main__":
	main()


#2 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 20 September 2008 - 01:59 AM

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 python
import os, sys, random, time
   
#text and copywrtie information with rules
def 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 screen
def Clear_screen():
	name = os.name
	if name == 'nt':
		os.system("cls")
	else:
		os.system("clear")
#find the key in a dictionary from a value
def 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 0


class 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 loop
def 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 game
   
	
if __name__ =="__main__":
	main()


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users