Thursday, March 21, 2019

Python Code to Search Gravon Archive Stratego Files for Specific Gameboards


The Python Code below will list the games played by specific Stratego Gameboards.  Just put all the Gravon archived Stratego game files in 1 folder.  Then set the path in the Code below(line 4) to that folder.
To search for a specific gameboard, you need to use the American Stratego number system.  Change the 40 character gameboard variable to the Stratego gameboard you are searching for.  The bottom row are the 1st ten characters in the gameboard string, followed by the 2nd row, third row and top row.  The program takes a few minutes to find all the games and then writes them to the screen.  You can then watch the games on the Strados Stratego Viewer.

The code below should output this:

Player2 Won  classic-2014.10-1192.xml
Player2 Won  classic-2014.11-1195.xml
Player2 Won  classic-2014.11-2287.xml
Player1 Won  classic-2015.0-2322.xml
Player1 Won  classic-2015.1-177.xml
Player2 Won  classic-2015.1-1899.xml
Player2 Won  classic-2015.1-1947.xml
Player2 Won  classic-2015.1-2039.xml
Player2 Won  classic-2015.1-2420.xml
Player2 Won  classic-2015.1-869.xml
10
done



import re
import glob
filecount=0
path = 'C:/Users/YourNameHere/Downloads/strategogames/*.xml'files=glob.glob(path)
file4=[]

gameboard = "86BBFB88584968BB7S993997B462934715695957"
def player1convert(x1):

    x1 = x1.replace("L", "1")
    x1 = x1.replace("K", "2")
    x1 = x1.replace("J", "3")
    x1 = x1.replace("I", "4")
    x1 = x1.replace("H", "5")
    x1 = x1.replace("G", "6")
    x1 = x1.replace("F", "7")
    x1 = x1.replace("E", "8")
    x1 = x1.replace("D", "9")
    x1 = x1.replace("M", "F")
    x1 = x1.replace("C", "S")

    return x1

def player2convert(x2):

    x2 = x2.replace("X", "1")
    x2 = x2.replace("W", "2")
    x2 = x2.replace("V", "3")
    x2 = x2.replace("U", "4")
    x2 = x2.replace("T", "5")
    x2 = x2.replace("S", "6")
    x2 = x2.replace("R", "7")
    x2 = x2.replace("Q", "8")
    x2 = x2.replace("P", "9")
    x2 = x2.replace("Y", "F")
    x2 = x2.replace("O", "S")
    x2 = x2.replace("N", "B")
    return x2[::-1]

for file in files:
    f = open(file, 'r')
    filecount += 1
    for line in f:
        match = re.search('content', line)
        if match:
            p1 = re.findall('content="([A-Z]{40})', line)
            p2 = re.findall("AAAA__AA__AA([A-Z]{40})", line)
            x1=player1convert(p1[0])
            x2=player2convert(p2[0])

            for line in f:
                match1 = re.search("result", line)
                if match1:
                    line = line.strip()
                    winner = line[25]

            if x1 == gameboard:
               if winner == '1':
                   file4.append("Player1 Won  " + file[38:])
               elif winner == '2':
                   file4.append("Player1 Lost " + file[38:])
               else:
                   file4.append("Player1 Tie  "+ file[38:])
                   break                    
            if x2 == gameboard:
               if winner == '2':
                   file4.append("Player2 Won  " + file[38:])
               elif winner == '1':
                   file4.append("Player2 Lost " + file[38:])
               else:
                   file4.append("Player2 Tie  " + file[38:])


for name in file4:
    print(name)
print (len(file4))
print("done")

0 comments:

Post a Comment