Hi there!... i was seeing some codes in Python, but i can't understand it, so... i really don't want learn phyton, by that reason i want to know some things (specific things) for try convert it, now... if someone can convert it for me, i'll be sooo glad :P.

Here's the code:

# Modules
import random

# Constants

# Class
class knightTour(object):
    """ Knight's Tour """

    def __init__(self, row=8, col=8):

        self.row = row
        self.col = col
        self.knight = None
        self.top = (row * col) - 1

        self.grid = [0] * self.row
        for i in range(self.row):
            self.grid[i] = [0] * self.col

    def __str__(self):
        s = []
        X = "|> "

        for i in range(self.row):
            for j in range(self.col):
                if self.grid[i][j] == :
                    s.append("%2d " % self.grid[i][j])

        return "".join(s)

    def set(self, pos):

        self.knight = pos
        self.grid[pos[0]][pos[1]] = KNIGHT

    def fwd(self, new_pos, cont):

        self.grid[self.knight[0]][self.knight[1]] = cont
        self.grid[new_pos[0]][new_pos[1]] = KNIGHT

        self.knight = new_pos

    def bck(self, old_pos):

        self.grid[self.knight[0]][self.knight[1]] = 0
        self.grid[old_pos[0]][old_pos[1]] = KNIGHT

        self.knight = old_pos

    def successors(self, pos):

        row = pos[0]
        col = pos[1]

        moves = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1),
                 (-2, -1)]

        successors = []

        for v, w in moves:
            if (row + v >= 0 and row + v < self.row and col + w >= 0 and
                col + w < self.col):
                if self.grid[row + v][col + w] == 0:
                    successors.append((row + v, col + w))

        return successors

    def heuristic(self, successors):
        """ heuristic(self, list of tuples successors) -> list of tuples
       Warnsdorff's algorithm.

        tmp = []

        for x in successors:

        for i in range(len(tmp) - 1):
            minimo = i
            for j in range(i + 1, len(tmp)):
                if len(tmp[j]) < len(tmp[minimo]):
                    minimo = j

            tmp[i], tmp[minimo] = tmp[minimo], tmp[i]
            successors[i], successors[minimo] = successors[minimo], successors[i]

        return successors

    def knightTour(self, pos, cont=0):

        print self

        if cont == 0:

        old_pos = self.knight
        self.fwd(pos, cont)

        if cont == self.top:
            return True

            successors = self.heuristic(self.successors(pos))

            for x in successors:
                if self.knightTour(x, cont + 1):
                    return True

            print "i will not find a way."

            return False


def main():
    input = raw_input("Write the number of rows and columns (eg: 8 8)\n>> ")
    input = input.split(" ")

    row = int(input[0])
    col = int(input[1])

    pc = knightTour(row, col)

    if pc.knightTour((random.randrange(0, row), random.randrange(0, col))):
        print "A way is found!: \n"
        print pc
        print "Does not found any way"

if __name__ == "__main__":

Well, I know this code is the "Knight's tour", but i do not know how to convert it to C# or VB.NET.

(Of course, I'm a .NET programmer, not Python, and makes me dificult to make the conversion, I really want to see how it works in C# or VB.NET).

If someone can tell me to do, or convert it for me, i'll be grateful.

PS: My native language isn't english, so... if you don't understand something, please notify me, and i will remake the question ;).

Edited by Sandmann: n/a

5 Years
Discussion Span
Last Post by vegaseat

IronPython runs under .NET: http://ironpython.codeplex.com/ so you do not need convert.

If you want to ask some question ask specifically about that function, but the wiki algorithm should be easy to convert to your language and already in pseudocode more suitable for coding than code in other language:


    set P to be a random initial position on the board
    mark the board at P with the move number "1"
    for each move number from 2 to the number of squares on the board:
        let S be the set of positions accessible from the input position
        set P to be the position in S with minimum accessibility
        mark the board at P with the current move number
    return the marked board – each square will be marked with the move number on which it is visited.

Edited by pyTony: n/a


Thanks but i just want to see that python code converted to C#, just like here.

But i guess here i will not get the luck of Altarium, what a shame.

Thanks anyways.

Edited by Sandmann: n/a


If you compare Python code with C# code you find out that C# required ten times more typing than Python. It might be better to ask this question in the C# forum where people love to type.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.