/
main.py
236 lines (221 loc) · 6.27 KB
/
main.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
import random, os.path, inquirer, os
#Our stuff
import core_solver
import game
import menu
from menu import GameMenu
import utils
from utils import Block
#import basic pygame modules
import pygame
from pygame.locals import *
import sys
from PIL import Image
from resizeimage import resizeimage
#see if we can load more than standard BMP
if not pygame.image.get_extended():
raise SystemExit("Sorry, extended image module required")
def terminal_menu(squareside):
####################
#Select heuristic
questions = [
inquirer.List('heuristic',
message="\033[92mSelect heuristic",
choices=['\033[91mManhattan', '\033[91mEuclidian', '\033[91mChebyshev'],
),
]
heuristic = inquirer.prompt(questions)
heuristic = heuristic["heuristic"]
if heuristic == '\033[91mEuclidian':
heuristic = "Euclidian"
elif heuristic == '\033[91mManhattan':
heuristic = "Manhattan"
else:
heuristic = "Chebyshev"
#####################
#####################
#Select algo if squareside <= 3
algo = 'IDAstar'
if squareside <= 3:
questions = [
inquirer.List('algo',
message="\033[92mSelect algorithm",
choices=['\033[91mAstar', '\033[91mIDAstar'],
),
]
algo = inquirer.prompt(questions)
algo = algo["algo"]
if algo == '\033[91mAstar':
algo = 'Astar'
else:
algo = 'IDAstar'
#####################
return heuristic, algo
def file_read(file):
linePos = 0
filegameboard = []
rfile = file.readlines()
if len(rfile) == 0:
print('\033[91mError value doesn\'t exist\033[0m')
exit()
numberelem = 0
numberCol = 0
squareside = 0
for line in rfile:
numberRaw = 0
line = line.replace('\t', ' ')
line = line.replace(' ', ',')
if ('#' in line):
line = line.split('#', 1)[0]
if (line == ''):
continue
if (line == ''):
print('\033[91mError empty line\033[0m')
exit()
if (line != '\n'):
line = line.replace('\n', '')
if linePos == 0:
if not line.isdigit():
print('\033[91mError incorrect value\033[0m')
exit()
else:
squareside = int (line)
else:
if (line == ''):
print('\033[91mError empty line\033[0m')
exit()
if (line != '\n'):
line = line.replace('\n', '')
line = line.replace('\t', ' ')
lineElem = line.split(',')
for elem in lineElem:
if elem.isdigit():
numberelem += 1
numberRaw += 1
filegameboard.append(int(elem))
elif (elem == '#'):
break
elif (elem == ''):
break
else:
print('\033[91mError incorrect size of board\033[0m')
exit()
if (linePos != 0 and numberRaw != squareside):
print('\033[91mError incorrect size of board\033[0m')
exit()
numberCol += 1
linePos += 1
if (numberCol -1!= squareside):
print('\033[91mError incorrect size of board\033[0m'+str(numberCol))
exit()
# test_squareroot_value = numberelem ** 0.5
# decimal = str(test_squareroot_value - int(test_squareroot_value))[1:]
# if decimal != '.0':
# print('\033[91m 4Error incorrect size of board\033[0m')
# exit()
for i in range(numberelem):
if i not in filegameboard:
print('\033[91mError value out of range\033[0m')
exit()
return filegameboard, int(squareside)
def graphical_interface_mode():
while 42:
#####################
#Select interface Yes, No
questions = [
inquirer.List('interface',
message="\033[92mDo you want a Graphical interface for Npuzzle:",
choices=['\033[91mYes', '\033[91mNo'],
),
]
interface = inquirer.prompt(questions)
interface = interface["interface"]
#####################
if interface == "\033[91mYes":
interface = 1
break
else:
interface = 0
break
return interface
def game_setting_menu(interface, input_board, squareside):
fenetre = None
if interface != 1:
if (input_board == []):
print("\033[90m")
squareside = input('\033[92mChoose size for Npuzzle: \n\033[91m')
#####################
#Ce check est obligatoire sinon tu peux balance une string ....
if squareside.isdigit() == False or int(squareside) < 2:
print("\033[91mBad value select int > 1\033[0m")
exit()
elif int(squareside) > 100:
print("\033[91mDo you really wanna wait forever ??\033[0m")
exit()
#####################
squareside = int(squareside)
print("\033[90m")
heuristic, algo = terminal_menu(squareside)
gamemode = 'ia'
# Comment this else if you don't want to go in the menu
#
else:
fenetre = pygame.display.set_mode((1000, 800), 0, 32)
menu_items = ('IA', 'Solo', 'Quit')
pygame.display.set_caption('Game Menu')
gm = GameMenu(fenetre, menu_items, input_board)
# return the choice enter in the menu
heuristic, algo, squareside, gamemode = gm.run()
return heuristic, algo, squareside, gamemode, fenetre
def main(argv):
squareside = 0
filegameboard = []
input_board = []
fenetre = None
file_from_input = 0
if argv != None:
try:
fd = open(argv, 'r')
except FileNotFoundError:
print("\033[91mFile doesn't exist\033[0m")
exit()
filegameboard, squareside = file_read(open(argv, 'r'))
input_board = core_solver.set_board(filegameboard, squareside)
file_from_input = 1
interface = graphical_interface_mode()
try:
Image.open("/Users/gkuma/Downloads/photo_200_.jpg")
except IOError:
game_size = 2
while (game_size < 6):
size = game_size*100
try:
img = Image.open("/Users/gkuma/Downloads/photo.jpg").resize((size,size))
except FileNotFoundError:
print("\033[91mImage doesn't exist, Please drop an image named 'photo.jpg' there:\nPath:/Users/gkuma/Downloads/photo.jpg \033[0m")
exit()
out = open("/Users/gkuma/Downloads/photo_"+str(size)+"_"+".jpg", "w")
img.save(out, "JPEG")
game_size += 1
main_menu_loop = 1
while main_menu_loop == 1:
heuristic, algo, squareside, gamemode, fenetre = game_setting_menu(interface, input_board, squareside)
ia_final_move, allcase = core_solver.call_core(squareside, heuristic, algo, gamemode, input_board)
if input_board:
allcase = filegameboard
if interface == 1:
main_menu_loop = game.call_game(ia_final_move, squareside, allcase, gamemode, fenetre)
if file_from_input:
filegameboard, squareside = file_read(open(argv, 'r'))
input_board = core_solver.set_board(filegameboard, squareside)
ia_final_move = []
allcase = []
if __name__ == '__main__':
if len(sys.argv) == 1:
main(None)
elif len(sys.argv) == 2:
main(sys.argv[1])
else:
print('\033[91mBad number of argument !')
exit()