-
Notifications
You must be signed in to change notification settings - Fork 0
/
readMove.py
executable file
·64 lines (46 loc) · 1.77 KB
/
readMove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
import sys
import re
import cs50
mainDatabase="pgn-database.db"
tableName="games"
columnName="PGN"
# TODO parametric
selectedRow=50000
games={}
moves={}
moveNumber = 1
def main():
games = getGamesFromDatabase()
for i in range(selectedRow):
print("reading game ", i+1)
insertMovesFromGame(i+1, games)
def getGamesFromDatabase():
global games
with open(f''+mainDatabase, "r"):
db = cs50.SQL("sqlite:///"+mainDatabase)
games = db.execute("SELECT "+columnName+" FROM "+tableName)
return games
def insertMovesFromGame(selectedRow, games):
global moveNumber
game=games[selectedRow - 1][columnName]
gameMoves=game.split(" ")
# create moves table
#with open(f"pgn-database.db", "a"):
moveDB = cs50.SQL("sqlite:///pgn-database.db")
#moveDB.execute("CREATE TABLE moves (game_id, moveID INTEGER PRIMARY KEY AUTOINCREMENT, moveNumber INT, color TEXT, move TEXT, FOREIGN KEY(game_id) REFERENCES games(GameID))")
for i in range(len(gameMoves)):
regex="\d{1,}\.(.*)"
match=re.search(regex, gameMoves[i])
if match:
# print("White move on moveNum ",moveNumber,": ", match.group(1))
moveDB.execute("INSERT INTO moves (game_id, moveNumber, color, move) VALUES(?, ?, ?, ?)", selectedRow, moveNumber, "White", match.group(1))
else:
if(gameMoves[i] == ""):
# game is finished
# print("Result: ", gameMoves[i+1])
break
# print("Black move on moveNum ",moveNumber,": ", gameMoves[i])
moveDB.execute("INSERT INTO moves (game_id, moveNumber, color, move) VALUES(?, ?, ?, ?)", selectedRow, moveNumber, "Black", gameMoves[i])
moveNumber += 1
main()