-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthelloDatabase.py
More file actions
155 lines (123 loc) · 4.46 KB
/
OthelloDatabase.py
File metadata and controls
155 lines (123 loc) · 4.46 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import mysql.connector
import base64
import datetime
LOOKUPSHIFT = 200156682785938776
pas = ~ # password for MySQL
# Open the connection to the SQL server
connection = mysql.connector.connect(
host="localhost",
user="root",
password=p,
database="othello"
)
# Function to convert a board state that has been stored to the 0,1,2 - Blank,White,Black representation
def toTernary(n):
n = int(n)
n += LOOKUPSHIFT # Correct by the look up shift value
if n == 0:
return 0
else:
result = ""
while n > 0:
n,remainder = divmod(n,3)
result += str(remainder)
# Pad the result with 0s to ensure the string is 64 characters long and follows the standard board format
return result[::-1].zfill(64)
# Function to convert a board state from the 0,1,2 representation to decimal
def toDecimal(n):
n = str(n)[::-1]
decimal = 0
for power in range(64):
decimal += int(n[power]) * (3**power)
return decimal - LOOKUPSHIFT # Shifts by the look up shift value to reduce storage size
# Takes in all the board states from a match and converts it into data to be stored in the table
def boardsToData(boards):
data = ""
for item in boards:
data += str(toDecimal(item))
data += 'n'
return data
# Takes in the data from the table and converts it into all the board states for processing
def dataToBoards(data):
boards = []
#print(data)
boards = data.split("n")
boards.remove('')
for index in range(len(boards)):
boards[index] = str(toTernary(boards[index]))
return boards
# Creates the SQL database
def create_database(connection,query):
cursor = connection.cursor()
try:
cursor.execute(query)
#print("Database success")
except Exception as e:
print(e)
# Executes a generic SQL query passed to the function
def execute_query(connection,query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
#print("Query Success")
except Exception as e:
errorFile = open("errorLog.txt","w+")
errorFile.write(e)
errorFile.close()
print(e)
# Exectures a SQL query which has a return value and returns that result
def execute_read_query(connection,query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Exception as e:
errorFile = open("errorLog.txt","w+")
errorFile.write(e)
errorFile.close()
print(e)
# Gets relevant information from the table to display to the User a list of previous matches
def get_vals():
query = """SELECT matchID,date,aiLevel,Result FROM matches"""
result = execute_read_query(connection,query)
return result
# Gets the match data for a match with a given key and returns the converted result
def get_data(key):
query = """
SELECT matchData
FROM matches
WHERE matchID = {matchKey}""".format(matchKey=key)
result = execute_read_query(connection,query)[0][0]
result = dataToBoards(result)
return result
# Converts the board states to data to be stored and stores it in the table
def insert_data(matchData, mode):
lastBoard = matchData[-1]
if lastBoard.count("2") != lastBoard.count("1"):
winner = "Black" if lastBoard.count("2")>lastBoard.count("1") else "White"
else:
winner = "Draw"
data = boardsToData(matchData)
insert_vals = """
INSERT INTO
`matches` (`date`,`aiLevel`,`result`,`matchData`)
VALUES
('{date}','{gameMode}','{win}','{matchInfo}');
""".format(date=datetime.datetime.now().ctime(),gameMode=mode,win=winner,matchInfo=data)
execute_query(connection,insert_vals)
create_database_query = "CREATE DATABASE IF NOT EXISTS othello"
create_database(connection,create_database_query) # Creating the database if it does not already exist in the SQL
create_table = """
CREATE TABLE IF NOT EXISTS matches (
matchID INT AUTO_INCREMENT,
date TEXT,
aiLevel TEXT,
result TEXT,
matchData TEXT,
PRIMARY KEY (matchID)
) ENGINE = InnoDB
"""
execute_query(connection,create_table) # Creating the table if it does not already exist with matchID as the primary key