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
|
from sqlite3 import *
import os
db_name = 'games.db'
if os.path.exists(os.path.join(os.getcwd(), 'games.db')): os.remove('games.db')
con = connect('games.db')
cur = con.cursor()
game_log_fld_types = {
'game_id': 'Integer',
'plr_X_name': 'Text',
'plr_X_color_int': 'Integer',
'plr_O_name': 'Text',
'plr_O_color_int': 'Integer',
'start_time': 'Real',
'start_board': 'Text',
'vic_chk': 'Integer',
'victor_name': 'Text',
'victor_color_int': 'Integer',
'three_in_a_row': 'Integer',
'draw_accept': 'Integer',
'resign': 'Integer',
'end_board': 'Text',
'turns': 'Integer',
'end_time': 'Real'
}
crt_flds = ''
for k in game_log_fld_types:
crt_flds += f'{k} {game_log_fld_types[k]}, '
crt_flds = crt_flds[:-2]
qry_crt_game_log = f'CREATE TABLE game_log ({crt_flds})'
cur.execute(qry_crt_game_log)
move_log_fld_types = {
'game_id': 'Integer',
'board': 'Text',
'turn': 'Text',
'move_num': 'Integer',
'turn_num': 'Integer',
'move': 'Text',
'timestamp': 'Real'
}
crt_flds = ''
for k in move_log_fld_types:
crt_flds += f'{k} {move_log_fld_types[k]}, '
crt_flds = crt_flds[:-2]
qry_crt_move_log = f'CREATE TABLE move_log ({crt_flds})'
cur.execute(qry_crt_move_log)
players_fld_types = {
'color_int': 'Integer',
'name': 'Text',
'is_machine': 'Integer'
}
crt_flds = ''
for k in players_fld_types:
crt_flds += f'{k} {players_fld_types[k]}, '
crt_flds = crt_flds[:-2]
qry_crt_players = f'CREATE TABLE players ({crt_flds})'
cur.execute(qry_crt_players)
plrs = [
{'color_int': 1, 'name': 'Red', 'is_machine': False},
{'color_int': 2, 'name': 'Green', 'is_machine': False},
{'color_int': 3, 'name': 'Yellow', 'is_machine': False},
{'color_int': 4, 'name': 'Blue', 'is_machine': False},
{'color_int': 5, 'name': 'Magenta', 'is_machine': False},
{'color_int': 6, 'name': 'Cyan', 'is_machine': False},
]
qry_app_players = """INSERT INTO players (color_int, name, is_machine)
VALUES (:color_int, :name, :is_machine)"""
for plr in plrs:
cur.execute(qry_app_players, plr)
machines_fld_types = {
'color_int': 'Integer',
'name': 'Text',
'is_machine': 'Integer',
'alg_1': 'Text',
'alg_2': 'Text',
'alg_3': 'Text',
'export_alg_res': 'Integer'
}
crt_flds = ''
for k in machines_fld_types:
crt_flds += f'{k} {machines_fld_types[k]}, '
crt_flds = crt_flds[:-2]
qry_crt_machines = f'CREATE TABLE machines ({crt_flds})'
cur.execute(qry_crt_machines)
machines = [
{'color_int': 1, 'name': 'Red', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
{'color_int': 2, 'name': 'Green', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
{'color_int': 3, 'name': 'Yellow', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
{'color_int': 4, 'name': 'Blue', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
{'color_int': 5, 'name': 'Magenta', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
{'color_int': 6, 'name': 'Cyan', 'is_machine': False, 'alg_1': 'no_alg', 'alg_2': 'no_alg', 'alg_3': 'no_alg', 'export_alg_res': False},
]
qry_app_machine = """INSERT INTO machines (color_int, name, is_machine, alg_1, alg_2, alg_3, export_alg_res)
VALUES (:color_int, :name, :is_machine, :alg_1, :alg_2, :alg_3, :export_alg_res)"""
for machine in machines:
cur.execute(qry_app_machine, machine)
con.commit()
con.close()
con = None
cur = None
|