I'm currently taking a Data Structures and Problem Solving course at my university. We have a course long project where we'll be making an AI for the game Quoridor.
I came down with a rather nasty bug, and missed out on a week of lectures and labs. During that week, we learned how to find the shortest route between two Node objects that are connected in a Graph structure.
After looking through the posted lecture notes, I can't figure out how to do it myself. I'll include all necessary code below. I'd be grateful for some help. It's all in Python 3.3
def init(logger, playerId, numWalls, playerHomes):
"""
Part 1 - 4
The engine calls this function once at the beginning of the game.
The student player module uses this function to initialize its data
structures to the initial game state.
Parameters
logger: a reference to the logger object. The player model uses
logger.write(msg) and logger.error(msg) to log diagnostic
information.
playerId: this player's number, from 1 to 4
numWalls: the number of walls each player is given initially
playerHomes: An ordered tuple of player locations
A location is a 2-element tuple of (row,column).
If any player has already been eliminated from
the game (rare), there will be a bool False in
that player's spot in the tuple instead of a
location.
returns:
a PlayerData object containing all of this player module's data
"""
gameGrid = []
for r in range(d):
for c in range(d):
gameGrid.append(Square(r,c))
for square in gameGrid:
square.findNeighbors(gameGrid)
playerData = PlayerData(logger, playerId, list(playerHomes),gameGrid)
return playerData
myNode File
'''
Created on Dec 12, 2012
@author: Robert
'''
class EmptyListNode:
__slots__ = ()
class ListNode:
__slots__ = ( "data", "next" ) # the only valid attributes for node
def __init__(self, dataVal, nextVal=EmptyListNode()):
"""Initialize the node"""
self.data = dataVal # the node's element
self.next = nextVal # the node's next
def __str__(self):
"""Return a string representation of the node"""
return "Node val: "+ str(self.data)
myStack File
'''
Created on Dec 12, 2012
@author: Robert
'''
from .myNode import *
class Stack:
__slots__ = ( "top" )
def __init__(self):
self.top = EmptyListNode() # the top node in the stack
def push(element, stack):
"""Add an element to the top of the stack"""
newnode = ListNode(element, stack.top)
stack.top = newnode
def top(stack):
"""Access the top element oi the stack without removing it"""
if emptyStack(stack):
raise IndexError("top on empty stack")
return stack.top.data
def pop(stack):
"""Remove the top element in the stack (returns None)"""
if emptyStack(stack):
raise IndexError("pop on empty stack")
stack.top = stack.top.next
def emptyStack(stack):
"""Is the stack empty?"""
return isinstance(stack.top, EmptyListNode)
myQueue File
'''
Created on Dec 12, 2012
@author: Robert
'''
from .myNode import *
class Queue:
__slots__ = ( "front", "back" )
def __init__(self):
self.front = EmptyListNode() # The front node in the queue
self.back = EmptyListNode() # The back node in the queue
def enqueue(element, queue):
"""Insert an element into the back of the queue"""
newnode = ListNode(element)
if emptyQueue(queue):
queue.front = newnode
else:
queue.back.next = newnode
queue.back = newnode
def dequeue(queue):
"""Remove the front element from the queue (returns None)"""
if emptyQueue(queue):
raise IndexError("dequeue on empty queue")
queue.front = queue.front.next
if emptyQueue(queue):
queue.back = EmptyListNode()
def front(queue):
"""Access and return the first element in the queue without removing it"""
if emptyQueue(queue):
raise IndexError("front on empty queue")
return queue.front.data
def back(queue):
"""Access and return the last element in the queue without removing it"""
if emptyQueue(queue):
raise IndexError("back on empty queue")
return queue.back.data
def emptyQueue(queue):
"""Is the queue empty?"""
return isinstance(queue.front, EmptyListNode)
getShortestPath Method
def get_shortest_path(playerData, r1, c1, r2, c2):
"""
Part 1
This function is only called in part 1 mode. The engine calls it when
a shortest path is requested by the user via the graphical interface.
Parameters
playerData: this player's data, originally built by this
module in init()
r1: row coordinate of starting position
c1: column coordinate of starting position
r2: row coordinate of destination position
c2: column coordinate of destination position
returns:
a list of coordinates that form the shortest path from the starting
position to the destination, inclusive. The format is an ordered
list of coordinate pairs (a list of lists, e.g.,
[[0,0], [0,1], [1,1]], not a list of tuples).
If there is no path, an empty list, [], should be returned.
"""
# 'prime' the dispenser with the source
gameGrid = playerData.gameGrid
destination = gameGrid[r2*9+c2]
source = gameGrid[r1*9+c1]
dispenser = Queue()
enqueue(source, dispenser)
# construct the predecessors data structure
predecessors = {}
predecessors[(r1,c1)] = None
# loop until either the destination is found or the dispenser
# is empty (no path)
while not emptyQueue(dispenser):
current = front(dispenser)
dequeue(dispenser)
if current == destination:
break
# loop over all neighbors of current
for neighbor in current.neighbors:
# process unvisited neighbors
if neighbor not in predecessors:
predecessors[neighbor] = current
enqueue(neighbor, dispenser)
shortPath = constructPath(predecessors, source, destination)
return shortPath
constructPath Method
def constructPath(predecessors, source, destination):
"""Returns the path as a list of nodes from source to destination"""
# construct the path using a stack and traversing from the destination
# node back to the source node using Node's previous
stack = Stack()
if destination in predecessors:
tmp = destination
while tmp != source:
push(tmp, stack)
tmp = predecessors[tmp]
push(source, stack)
path = []
while not emptyStack(stack):
path.append(top(stack))
pop(stack)
return path
Your indentation is broken, I can't read this. It looks like you have a bunch of global methods for dealing with Queues, which is weird, they'd be more useful as class members, not functions in the global scope (this is not C).
You also haven't described what problem you're having, so I'm not sure what I'm supposed to look for.
Rollback Post to RevisionRollBack
Never attribute to malice what can adequately be explained by incompetence.
I'm currently taking a Data Structures and Problem Solving course at my university. We have a course long project where we'll be making an AI for the game Quoridor.
I came down with a rather nasty bug, and missed out on a week of lectures and labs. During that week, we learned how to find the shortest route between two Node objects that are connected in a Graph structure.
After looking through the posted lecture notes, I can't figure out how to do it myself. I'll include all necessary code below. I'd be grateful for some help. It's all in Python 3.3
Square Object Class
Game Grid Initialization
myNode File
myStack File
myQueue File
getShortestPath Method
constructPath Method
"Programmers never repeat themselves. They loop."
You also haven't described what problem you're having, so I'm not sure what I'm supposed to look for.
We haven't really learned object oriented programming, and as such, aren't really supposed to use it. I much prefer it, and use where I can.
A lot of the stuff was from the lecture itself, source code provided by the professor.
EDIT: Indenting appears to hate me.
EDIT2: Indenting fixed, hopefully.
My issue is that I'm either not getting a path at all, or it's not returning the correct one
"Programmers never repeat themselves. They loop."