Question: For my assignment . I need to write a Python3 board game named hounds and hare , I also need a GUI interface for the

For my assignment . I need to write a Python3 board game named hounds and hare , I also need a GUI interface for the game .
the first picture below shows the request for the Python3 code;
Rules of the game:
Hounds and hares implementations:
1). there are two players, one representing the hounds and other represents the hares. Each player take turns to move.
The player representing the hounds can only move one hound at one time;
2). The three hounds try to immobilize the hares and the hare tries to escape to the left of all hounds;
3). The hounds can move up and down, straight forward, or diagonally forward toward the right end of the game board.
The hare can move horizontally, vertically or diagonally in any direction;
4). The hounds win if they "trap" the hare so it can no longer move;
5). The hares wins if it " escapes" to the left of all three hounds;
6). If the hounds move vertically ten times in a row, they can considered to be " stalling" and the hare wins
Python3. Request:
1. need a loopy solver
the code below is an example Python3 game named Animal chess . My hounds and hare Python3 code needs similar to this;
# API Application Programming Interface
WIN = "w"
LOSE = "l"
TIE = "t"
UNKNOWN = "u"
NONE = "n"
SIZE=9
#position = ("String elgoooGLE",0) The state of the board and the latter number that indicates whose turn it is.
#G,L,E:The first player's pieces
#g,l,e:The second player's pieces
def primitive(position):
if position[0].find('L')!=-1 and position[0].find('l')==-1 and position[1]==0:
return WIN
elif position[0].find('L')!=-1 and position[0].find('l')==-1 and position[1]==1:
return LOSE
elif position[0].find('L')==-1 and position[0].find('l')!=-1 and position[1]==0:
return LOSE
elif position[0].find('L')==-1 and position[0].find('l')!=-1 and position[1]==1:
return WIN
elif position[0].find('L')==-1 and position[0].find('l')==-1:
return NONE
elif (position[0].find('E')
return WIN
elif (position[0].find('E')
return LOSE
elif (position[0].find('e')>5 or position[0].find('l')>5 or position[0].find('g')>5) and position[0]==0:
return LOSE
elif (position[0].find('e')>5 or position[0].find('l')>5 or position[0].find('g')>5) and position[0]==1:
return WIN
else:
return UNKNOWN
def IsPrimitive(position):
if primitive(position) != UNKNOWN and primitive(position) !=NONE:
return True
else:
return False
def generate_moves(position):
"return a tuple of moves"
pos = position[0]
moves = []
if position[1]==0:
gi = position[0].find('G')
li = position[0].find('L')
ei = position[0].find('E')
g = 'G'
l = 'L'
e = 'E'
#appending every possible move of each piece
#Possible moves of giraff
if gi==0:
if pos[1] != l and pos[1] != e:
moves.append((0,1))
if pos[3] != l and pos[3] != e:
moves.append((0,3))
if gi==1:
if pos[0] != l and pos[0] != e:
moves.append((1,0))
if pos[2] != l and pos[2] != e:
moves.append((1,2))
if pos[4] != l and pos[4] != e:
moves.append((1,4))
if gi==2:
if pos[1] != l and pos[1] != e:
moves.append((2,1))
if pos[5] != l and pos[5] != e:
moves.append((2,5))
if gi==3:
if pos[0] != l and pos[0] != e:
moves.append((3,0))
if pos[4] != l and pos[4] != e:
moves.append((3,4))
if pos[6] != l and pos[6] != e:
moves.append((3,6))
if gi==4:
if pos[1] != l and pos[1] != e:
moves.append((4,1))
if pos[3] != l and pos[3] != e:
moves.append((4,3))
if pos[5] != l and pos[5] != e:
moves.append((4,5))
if pos[7] != l and pos[7] != e:
moves.append((4,7))
if gi==5:
if pos[2] != l and pos[2] != e:
moves.append((5,2))
if pos[4] != l and pos[4] != e:
moves.append((5,4))
if pos[8] != l and pos[8] != e:
moves.append((5,8))
if gi==6:
if pos[3] != l and pos[3] != e:
moves.append((6,3))
if pos[7] != l and pos[7] != e:
moves.append((6,7))
if gi==7:
if pos[4] != l and pos[4] != e:
moves.append((7,4))
if pos[6] != l and pos[6] != e:
moves.append((7,6))
if pos[8] != l and pos[8] != e:
moves.append((7,8))
if gi==8:
if pos[5] != l and pos[5] != e:
moves.append((8,5))
if pos[7] != l and pos[7] != e:
moves.append((8,7))
#Possible moves of elephant
if ei==0:
if pos[4] != l and pos[4] != g:
moves.append((0,4))
if ei==1:
if pos[3] != l and pos[3] != g:
moves.append((1,3))
if pos[5] != l and pos[5] != g:
moves.append((1,5))
if ei==2:
if pos[4] != l and pos[4] != g:
moves.append((2,4))
if ei==3:
if pos[1] != l and pos[1] != g:
moves.append((3,1))
if pos[7] != l and pos[7] != g:
moves.append((3,7))
if ei==4:
if pos[0] != l and pos[0] != g:
moves.append((4,0))
if pos[2] != l and pos[2] != g:
moves.append((4,2))
if pos[6] != l and pos[6] != g:
moves.append((4,6))
if pos[8] != l and pos[8] != g:
moves.append((4,8))
if ei==5:
if pos[1] != l and pos[1] != g:
moves.append((5,1))
if pos[7] != l and pos[7] != g:
moves.append((5,7))
if ei==6:
if pos[4] != l and pos[4] != g:
moves.append((6,4))
if ei==7:
if pos[3] != l and pos[3] != g:
moves.append((7,3))
if pos[5] != l and pos[5] != g:
moves.append((7,5))
if ei==8:
if pos[4] != l and pos[4] != g:
moves.append((8,4))
#Possible Moves of Lion
if li==0:
if pos[1] != e and pos[1] != g:
moves.append((0,1))
if pos[3] != e and pos[3] != g:
moves.append((0,3))
if pos[4] != e and pos[4] != g:
moves.append((0,4))
if li==1:
if pos[0] != e and pos[0] != g:
moves.append((1,0))
if pos[2] != e and pos[2] != g:
moves.append((1,2))
if pos[4] != e and pos[4] != g:
moves.append((1,4))
if pos[3] != e and pos[3] != g:
moves.append((1,3))
if pos[5] != e and pos[5] != g:
moves.append((1,5))
if li==2:
if pos[1] != e and pos[1] != g:
moves.append((2,1))
if pos[5] != e and pos[5] != g:
moves.append((2,5))
if pos[4] != e and pos[4] != g:
moves.append((2,4))
if li==3:
if pos[0] != g and pos[0] != e:
moves.append((3,0))
if pos[4] != g and pos[4] != e:
moves.append((3,4))
if pos[6] != g and pos[6] != e:
moves.append((3,6))
if pos[1] != e and pos[1] != g:
moves.append((3,1))
if pos[7] != e and pos[7] != g:
moves.append((3,7))
if li==4:
if pos[1] != g and pos[1] != e:
moves.append((4,1))
if pos[3] != g and pos[3] != e:
moves.append((4,3))
if pos[5] != g and pos[5] != e:
moves.append((4,5))
if pos[7] != g and pos[7] != e:
moves.append((4,7))
if pos[0] != e and pos[0] != g:
moves.append((4,0))
if pos[2] != e and pos[2] != g:
moves.append((4,2))
if pos[6] != e and pos[6] != g:
moves.append((4,6))
if pos[8] != e and pos[8] != g:
moves.append((4,8))
if li==5:
if pos[2] != g and pos[2] != e:
moves.append((5,2))
if pos[4] != g and pos[4] != e:
moves.append((5,4))
if pos[8] != g and pos[8] != e:
moves.append((5,8))
if pos[1] != e and pos[1] != g:
moves.append((5,1))
if pos[7] != e and pos[7] != g:
moves.append((5,7))
if li==6:
if pos[3] != g and pos[3] != e:
moves.append((6,3))
if pos[7] != g and pos[7] != e:
moves.append((6,7))
if pos[4] != e and pos[4] != g:
moves.append((6,4))
if li==7:
if pos[4] != g and pos[4] != e:
moves.append((7,4))
if pos[6] != g and pos[6] != e:
moves.append((7,6))
if pos[8] != g and pos[8] != e:
moves.append((7,8))
if pos[3] != e and pos[3] != g:
moves.append((7,3))
if pos[5] != e and pos[5] != g:
moves.append((7,5))
if li==8:
if pos[5] != g and pos[5] != e:
moves.append((8,5))
if pos[7] != g and pos[7] != e:
moves.append((8,7))
if pos[4] != e and pos[4] != g:
moves.append((8,4))
#end of appending every possible move of each piece
else:
gi = position[0].find('g')
li = position[0].find('l')
ei = position[0].find('e')
g = 'g'
l = 'l'
e = 'e'
#appending every possible move of each piece
#Possible moves of giraff
if gi==0:
if pos[1] != l and pos[1] != e:
moves.append((0,1))
if pos[3] != l and pos[3] != e:
moves.append((0,3))
if gi==1:
if pos[0] != l and pos[0] != e:
moves.append((1,0))
if pos[2] != l and pos[2] != e:
moves.append((1,2))
if pos[4] != l and pos[4] != e:
moves.append((1,4))
if gi==2:
if pos[1] != l and pos[1] != e:
moves.append((2,1))
if pos[5] != l and pos[5] != e:
moves.append((2,5))
if gi==3:
if pos[0] != l and pos[0] != e:
moves.append((3,0))
if pos[4] != l and pos[4] != e:
moves.append((3,4))
if pos[6] != l and pos[6] != e:
moves.append((3,6))
if gi==4:
if pos[1] != l and pos[1] != e:
moves.append((4,1))
if pos[3] != l and pos[3] != e:
moves.append((4,3))
if pos[5] != l and pos[5] != e:
moves.append((4,5))
if pos[7] != l and pos[7] != e:
moves.append((4,7))
if gi==5:
if pos[2] != l and pos[2] != e:
moves.append((5,2))
if pos[4] != l and pos[4] != e:
moves.append((5,4))
if pos[8] != l and pos[8] != e:
moves.append((5,8))
if gi==6:
if pos[3] != l and pos[3] != e:
moves.append((6,3))
if pos[7] != l and pos[7] != e:
moves.append((6,7))
if gi==7:
if pos[4] != l and pos[4] != e:
moves.append((7,4))
if pos[6] != l and pos[6] != e:
moves.append((7,6))
if pos[8] != l and pos[8] != e:
moves.append((7,8))
if gi==8:
if pos[5] != l and pos[5] != e:
moves.append((8,5))
if pos[7] != l and pos[7] != e:
moves.append((8,7))
#Possible moves of elephant
if ei==0:
if pos[4] != l and pos[4] != g:
moves.append((0,4))
if ei==1:
if pos[3] != l and pos[3] != g:
moves.append((1,3))
if pos[5] != l and pos[5] != g:
moves.append((1,5))
if ei==2:
if pos[4] != l and pos[4] != g:
moves.append((2,4))
if ei==3:
if pos[1] != l and pos[1] != g:
moves.append((3,1))
if pos[7] != l and pos[7] != g:
moves.append((3,7))
if ei==4:
if pos[0] != l and pos[0] != g:
moves.append((4,0))
if pos[2] != l and pos[2] != g:
moves.append((4,2))
if pos[6] != l and pos[6] != g:
moves.append((4,6))
if pos[8] != l and pos[8] != g:
moves.append((4,8))
if ei==5:
if pos[1] != l and pos[1] != g:
moves.append((5,1))
if pos[7] != l and pos[7] != g:
moves.append((5,7))
if ei==6:
if pos[4] != l and pos[4] != g:
moves.append((6,4))
if ei==7:
if pos[3] != l and pos[3] != g:
moves.append((7,3))
if pos[5] != l and pos[5] != g:
moves.append((7,5))
if ei==8:
if pos[4] != l and pos[4] != g:
moves.append((8,4))
#Possible Moves of Lion
if li==0:
if pos[1] != e and pos[1] != g:
moves.append((0,1))
if pos[3] != e and pos[3] != g:
moves.append((0,3))
if pos[4] != e and pos[4] != g:
moves.append((0,4))
if li==1:
if pos[0] != e and pos[0] != g:
moves.append((1,0))
if pos[2] != e and pos[2] != g:
moves.append((1,2))
if pos[4] != e and pos[4] != g:
moves.append((1,4))
if pos[3] != e and pos[3] != g:
moves.append((1,3))
if pos[5] != e and pos[5] != g:
moves.append((1,5))
if li==2:
if pos[1] != e and pos[1] != g:
moves.append((2,1))
if pos[5] != e and pos[5] != g:
moves.append((2,5))
if pos[4] != e and pos[4] != g:
moves.append((2,4))
if li==3:
if pos[0] != g and pos[0] != e:
moves.append((3,0))
if pos[4] != g and pos[4] != e:
moves.append((3,4))
if pos[6] != g and pos[6] != e:
moves.append((3,6))
if pos[1] != e and pos[1] != g:
moves.append((3,1))
if pos[7] != e and pos[7] != g:
moves.append((3,7))
if li==4:
if pos[1] != g and pos[1] != e:
moves.append((4,1))
if pos[3] != g and pos[3] != e:
moves.append((4,3))
if pos[5] != g and pos[5] != e:
moves.append((4,5))
if pos[7] != g and pos[7] != e:
moves.append((4,7))
if pos[0] != e and pos[0] != g:
moves.append((4,0))
if pos[2] != e and pos[2] != g:
moves.append((4,2))
if pos[6] != e and pos[6] != g:
moves.append((4,6))
if pos[8] != e and pos[8] != g:
moves.append((4,8))
if li==5:
if pos[2] != g and pos[2] != e:
moves.append((5,2))
if pos[4] != g and pos[4] != e:
moves.append((5,4))
if pos[8] != g and pos[8] != e:
moves.append((5,8))
if pos[1] != e and pos[1] != g:
moves.append((5,1))
if pos[7] != e and pos[7] != g:
moves.append((5,7))
if li==6:
if pos[3] != g and pos[3] != e:
moves.append((6,3))
if pos[7] != g and pos[7] != e:
moves.append((6,7))
if pos[4] != e and pos[4] != g:
moves.append((6,4))
if li==7:
if pos[4] != g and pos[4] != e:
moves.append((7,4))
if pos[6] != g and pos[6] != e:
moves.append((7,6))
if pos[8] != g and pos[8] != e:
moves.append((7,8))
if pos[3] != e and pos[3] != g:
moves.append((7,3))
if pos[5] != e and pos[5] != g:
moves.append((7,5))
if li==8:
if pos[5] != g and pos[5] != e:
moves.append((8,5))
if pos[7] != g and pos[7] != e:
moves.append((8,7))
if pos[4] != e and pos[4] != g:
moves.append((8,4))
#end of appending every possible move of each piece
return moves
def do_move(position, move):
"given position and move, do move on position"
pos=position[0]
f = move[0]
t = move[1]
ans=""
for i in range(0,SIZE):
if i==f:
a="o"
elif i==t:
a=pos[move[0]]
else:
a=pos[i]
ans+=a
if position[1]==0:
return (ans,1)
else:
return (ans,0)
### above here, animalChess
### below here, for ANY GAME
def children(position):
return [do_move(position,move) for move in generate_moves(position)]
#Key -> (Parents,Number of children,Tie Tag)
KPNT={}
Frontier=[]
VALUES = {} #position ->gameValue
NextFrontier=[]
def search_all():
start=("elgoooGLE",0)
KPNT[start]=(set([]),len(children(start)),0)
Frontier.append(start)
while len(Frontier)!=0:
for front in Frontier:
childs = children(front)
for child in childs:
if IsPrimitive(child):
NextFrontier.append(child)
VALUES[child]=primitive(child)
if child in KPNT:
KPNT[child][0].add(front)
else:
KPNT[child]=(set([front]),len(children(child)),0)
Frontier.append(child)
VALUES[child]=UNKNOWN
Frontier.remove(front)
return
def loopy_solve():
while len(NextFrontier)!=0:
for p in NextFrontier:
if VALUES[p]==LOSE:
for parent in KPNT[p][0]:
VALUES[parent]=WIN
NextFrontier.append(parent)
elif VALUES[p]==TIE:
for parent in KPNT[p][0]:
if VALUES[parent] !=UNKNOWN or VALUES[parent] !=NONE:
continue
else:
KPNT[parent]=(KPNT[parent][0],KPNT[parent][1]-1,KPNT[2])
if KPNT[parent][1]==0:
VALUES[parent]==TIE
NextFrontier.append(parent)
else:
KPNT[parent]=(KPNT[parent][0],KPNT[parent][1],1)
else:
for parent in KPNT[p][0]:
if VALUES[parent] !=UNKNOWN or VALUES[parent] !=NONE:
continue
else:
KPNT[parent]=(KPNT[parent][0],KPNT[parent][1]-1,KPNT[2])
if KPNT[parent][1]==0:
if KPNT[parent][2]==1:
VALUES[parent]=TIE
NextFrontier.append(parent)
else:
VALUES[parent]=LOSE
NextFrontier.append(parent)
NextFrontier.remove(p)
for position in VALUES:
if VALUES[position] ==UNKNOWN:
VALUES[position]=DRAW
return
search_all();
loopy_solve();
print(VALUES(("elgoooGLE",0)))
 For my assignment . I need to write a Python3 board

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!