Is there a way to deconstruct a string of characters into an assemblage of class objects in Python?

Viewed 65

Sorry about the title gore, I'm not sure of a good way to phrase this question, but I'll try to provide a good amount of detail. So, my program takes in a "board state" for a game called Oska. The game is fairly simple, and I want to use an AI-based program that eventually will be able to play it. I began implementing it, but I ran into a few problems that I think would be solvable if I took in the starting board state differently.

The thing about Oska is that its board is weird, and it has varying sizes, so I can't construct an array as simply as I could for, lets say, checkers. This is what the starting input might look like:

['wwww','---','--','---','bbbb']

I'd like to create objects out of these pieces, with 4 for the white pieces (in this example) and 4 for the black. Ultimately, I want to use this to create a field of potential moves. Pieces in Oska move much like they do in checkers, with the main difference being that the board is missing chunks on either side. I am not familiar with creating classes in Python, so I don't know how it would work. I know in C/C++, you can make an object and save a next object, like root->right, which I think might help in this case? I'm not worried about the intelligence portion of this program yet, because I can always assign values to the different moves; what I am worried about is finding what moves are available to make. But, all I want to focus on for now is creating a class object for the board and the pieces. I have seen suggestions to do this, but not suggestions on how to do this. What should be in the class's __init__?

If someone has seen or done something similar for checkers, that would be a huge help. Try as I might, I haven't been able to find anything that can help me.

1 Answers

class wise the syntax might look like bellow in python, for the parsing of you list of strings, you may want to use a class or function to give the values to build your Tile instances. Hope this helps.

class Tile:
def __init__(self, row, column, next=None, previous=None):
    self.row = row
    self.column = column
    if next is None:
        next = []
    self.next = next
    if previous is None:
        previous = []
    self.previous = previous
Related