#-*- coding: utf-8 -*-
import getch
"""
Класична загадка про глечики з ascii графікою
"""
def select_first_jug():
gui[16] = gui[16].replace('\033[34m******* ', '\033[33m******* ')
gui[17] = gui[17].replace('\033[34m* * ', '\033[33m* * ')
gui[17] = gui[17].replace('\033[34m***** ', '\033[33m***** ')
gui[18] = gui[18].replace('\033[34m* * ', '\033[33m* * ')
gui[18] = gui[18].replace('\033[34m** ** ', '\033[33m** ** ')
gui[19] = gui[19].replace('\033[34m* * ', '\033[33m* * ')
gui[19] = gui[19].replace('\033[34m******* ', '\033[33m******* ')
gui[20] = gui[20].replace('\033[34m* * ', '\033[33m* * ')
gui[20] = gui[20].replace('\033[34m********* ', '\033[33m********* ')
gui[21] = gui[21].replace(' \033[34m* * ', ' \033[33m* * ')
gui[21] = gui[21].replace(' \033[34m******* ', ' \033[33m******* ')
gui[22] = gui[22].replace('\033[34m***** ', '\033[33m***** ')
return 1
def no_select_first_jug():
gui[16] = gui[16].replace('\033[33m******* ', '\033[34m******* ')
gui[17] = gui[17].replace('\033[33m* * ', '\033[34m* * ')
gui[17] = gui[17].replace('\033[33m***** ', '\033[34m***** ')
gui[18] = gui[18].replace('\033[33m* * ', '\033[34m* * ')
gui[18] = gui[18].replace('\033[33m** ** ', '\033[34m** ** ')
gui[19] = gui[19].replace('\033[33m* * ', '\033[34m* * ')
gui[19] = gui[19].replace('\033[33m******* ', '\033[34m******* ')
gui[20] = gui[20].replace('\033[33m* * ', '\033[34m* * ')
gui[20] = gui[20].replace('\033[33m********* ', '\033[34m********* ')
gui[21] = gui[21].replace(' \033[33m* * ', ' \033[34m* * ')
gui[21] = gui[21].replace(' \033[33m******* ', ' \033[34m******* ')
gui[22] = gui[22].replace('\033[33m***** ', '\033[34m***** ')
return 0
def select_second_jug():
gui[18] = gui[18].replace('\033[34m***** ', '\033[33m***** ')
gui[19] = gui[19].replace('\033[34m* * ', '\033[33m* * ')
gui[19] = gui[19].replace('\033[34m***** ', '\033[33m***** ')
gui[20] = gui[20].replace('\033[34m* * ', '\033[33m* * ')
gui[20] = gui[20].replace('\033[34m******* ', '\033[33m******* ')
gui[21] = gui[21].replace('\033[34m* * ', '\033[33m* * ')
gui[21] = gui[21].replace('\033[34m***** ', '\033[33m***** ')
gui[22] = gui[22].replace('\033[34m*** ', '\033[33m*** ')
return 2
def no_select_second_jug():
gui[18] = gui[18].replace('\033[33m***** ', '\033[34m***** ')
gui[19] = gui[19].replace('\033[33m* * ', '\033[34m* * ')
gui[19] = gui[19].replace('\033[33m***** ', '\033[34m***** ')
gui[20] = gui[20].replace('\033[33m* * ', '\033[34m* * ')
gui[20] = gui[20].replace('\033[33m******* ', '\033[34m******* ')
gui[21] = gui[21].replace('\033[33m* * ', '\033[34m* * ')
gui[21] = gui[21].replace('\033[33m***** ', '\033[34m***** ')
gui[22] = gui[22].replace('\033[33m*** ', '\033[34m*** ')
return 0
def no_select_thre_jug():
gui[13] = gui[13].replace('\033[33m***********', '\033[34m***********')
gui[14] = gui[14].replace('\033[33m* *', '\033[34m* *')
gui[15] = gui[15].replace('\033[33m* *', '\033[34m* *')
gui[16] = gui[16].replace('\033[33m* *', '\033[34m* *')
gui[17] = gui[17].replace('\033[33m* *', '\033[34m* *')
gui[18] = gui[18].replace('\033[33m* *', '\033[34m* *')
gui[19] = gui[19].replace('\033[33m* *', '\033[34m* *')
gui[20] = gui[20].replace('\033[33m* *', '\033[34m* *')
gui[21] = gui[21].replace('\033[33m* *', '\033[34m* *')
gui[22] = gui[22].replace('\033[33m******* ', '\033[34m******* ')
gui[14] = gui[14].replace('\033[33m***********', '\033[34m***********')
gui[15] = gui[15].replace('\033[33m*********', '\033[34m*********')
gui[16] = gui[16].replace('\033[33m********', '\033[34m********')
gui[17] = gui[17].replace('\033[33m***********', '\033[34m***********')
gui[18] = gui[18].replace('\033[33m*************', '\033[34m*************')
gui[19] = gui[19].replace('\033[33m*************', '\033[34m*************')
gui[20] = gui[20].replace('\033[33m***********', '\033[34m***********')
gui[21] = gui[21].replace('\033[33m*********', '\033[34m*********')
return 0
def select_thre_jug():
gui[13] = gui[13].replace('\033[34m***********', '\033[33m***********')
gui[14] = gui[14].replace('\033[34m* *', '\033[33m* *')
gui[15] = gui[15].replace('\033[34m* *', '\033[33m* *')
gui[16] = gui[16].replace('\033[34m* *', '\033[33m* *')
gui[17] = gui[17].replace('\033[34m* *', '\033[33m* *')
gui[18] = gui[18].replace('\033[34m* *', '\033[33m* *')
gui[19] = gui[19].replace('\033[34m* *', '\033[33m* *')
gui[20] = gui[20].replace('\033[34m* *', '\033[33m* *')
gui[21] = gui[21].replace('\033[34m* *', '\033[33m* *')
gui[22] = gui[22].replace('\033[34m******* ', '\033[33m******* ')
gui[14] = gui[14].replace('\033[34m***********', '\033[33m***********')
gui[15] = gui[15].replace('\033[34m*********', '\033[33m*********')
gui[16] = gui[16].replace('\033[34m*********', '\033[33m*********')
gui[17] = gui[17].replace('\033[34m***********', '\033[33m***********')
gui[18] = gui[18].replace('\033[34m*************', '\033[33m*************')
gui[19] = gui[19].replace('\033[34m*************', '\033[33m*************')
gui[20] = gui[20].replace('\033[34m***********', '\033[33m***********')
gui[21] = gui[21].replace('\033[34m*********', '\033[33m*********')
return 3
gui = [ "\033[34m*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
"| \033[33mПеред вами три глечики: перший -\033[34m |",
"* \033[33mна п'ять літрів, другий на три а\033[34m *",
"| \033[33mтретій на вісім. Два перші порожні, а\033[34m |",
"* \033[33mтретій до країв налитий водою.\033[34m *",
"| \033[33mРозділіть воду так щоб в першім і третім\033[34m |",
"* \033[33mглечиках було по чотири літри води.\033[34m *",
"| \033[33mДля керування вказівником використовуємо\033[34m |",
"* \033[33mклавіші керування курсором\033[34m *",
"| \033[33mпробіл для вибору 'ctrl+c' для виходу\033[34m |",
"| |",
"* *",
"| \033[31m8 \033[34m|",
"* \033[34m*********** \033[34m*",
"| \033[34m*********** \033[34m |",
"* \033[31m0 \033[34m********* \033[34m *",
"| \033[34m******* \033[34m********* \033[34m|",
"* \033[34m* * \033[31m0 \033[34m***********\033[34m *",
"| \033[34m* * \033[34m***** \033[34m************* \033[34m |",
"* \033[34m* * \033[34m* * \033[34m************* \033[34m *",
"| \033[34m* * \033[34m* * \033[34m*********** \033[34m |",
"* \033[34m* * \033[34m* * \033[34m********* \033[34m *",
"| \033[34m***** \033[34m*** \033[34m******* \033[34m |",
"* ^ *",
"| |",
"* *",
"| |",
"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\033[0m"]
def main():
first_jug = 0 #Кількість рідини в першім глечику
second_jug = 0 #Кількість рідини в другім глечику
thre_jug = 8 #Кількість рідини в третім глечику
selecte = 0 #Вибраний глечик
while True:
for row in gui:
print(row)
if first_jug == 4 and thre_jug == 4:
print('Перемога')
break
key = getch.getch()
print(gui[23][8])
if gui[23][8] == '^':
a = 5 - first_jug#Вільне місце в глечику
if key == 'C':
gui[23] = gui[23][:20]+'^'+gui[23][20:]
gui[23] = gui[23][:8]+' '+gui[23][10:]
if key == ' ':
if selecte == 0:
selecte = select_first_jug()
elif selecte == 1:
selecte = no_select_first_jug()
elif selecte == 3:
selecte = no_select_thre_jug()
if a <= thre_jug:
first_jug = 5
thre_jug -= a
elif a > thre_jug:
first_jug += thre_jug
thre_jug = 0
elif selecte == 2:
selecte = no_select_second_jug()
if a <= second_jug:
first_jug = 5
second_jug -= a
elif a > second_jug:
first_jug += second_jug
second_jug = 0
elif gui[23][19]== '^':
a = 3 - second_jug#Вільне місце в глечику
if key == 'C':
gui[23] = gui[23][:19]+' '+gui[23][20:]
gui[23] = gui[23][:33]+'^'+gui[23][34:]
if key == 'D':
gui[23] = gui[23][:19]+' '+gui[23][20:]
gui[23] = gui[23][:8]+'^'+gui[23][9:]
if key == ' ':
if selecte == 0:
selecte = select_second_jug()
elif selecte == 2:
selecte = no_select_second_jug()
elif selecte == 3:
selecte = no_select_thre_jug()
if a <= thre_jug:
second_jug = 3
thre_jug -= a
elif a > thre_jug:
second_jug += thre_jug
thre_jug = 0
elif selecte == 1:
selecte = no_select_first_jug()
if a <= first_jug:
second_jug = 3
first_jug -= a
elif a > second_jug:
second_jug += first_jug
first_jug = 0
elif gui[23][33]=='^':
if key == 'D':
gui[23] = gui[23][:19]+'^'+gui[23][20:]
gui[23] = gui[23][:33]+' '+gui[23][34:]
if key == ' ':
if selecte == 0:
selecte = select_thre_jug()
elif selecte == 3:
selecte = no_select_thre_jug()
elif selecte == 2:
selecte = no_select_second_jug()
thre_jug += second_jug
second_jug = 0
elif selecte == 1:
selecte = no_select_first_jug()
thre_jug += first_jug
first_jug = 0
gui[15] = gui[15][:13]+str(first_jug)+gui[15][14:]
gui[17] = gui[17][:29]+str(second_jug)+gui[17][30:]
gui[12] = gui[12][:38]+str(thre_jug)+gui[12][39:]
if first_jug == 0 : gui[21] = gui[21][:11]+" "+gui[21][16:]
if first_jug >= 1 : gui[21] = gui[21][:11]+"*****"+gui[21][16:]
if first_jug >= 2 : gui[20] = gui[20][:10]+"*******"+gui[20][17:]
if first_jug < 2 : gui[20] = gui[20][:10]+" "+gui[20][17:]
if first_jug >= 3 : gui[19] = gui[19][:11]+"*****"+gui[19][16:]
if first_jug < 3 : gui[19] = gui[19][:11]+" "+gui[19][16:]
if first_jug >= 4 : gui[18] = gui[18][:12]+"* *"+gui[18][15:]
if first_jug < 4 : gui[18] = gui[18][:12]+" "+gui[18][15:]
if first_jug == 5 : gui[17] = gui[17][:12]+"***"+gui[17][15:]
if first_jug < 5 : gui[17] = gui[17][:12]+" "+gui[17][15:]
if second_jug == 0 : gui[21] = gui[21][:28]+" "+gui[21][31:]
if second_jug >= 1 : gui[21] = gui[21][:28]+"***"+gui[21][31:]
if second_jug < 2 : gui[20] = gui[20][:27]+" "+gui[20][32:]
if second_jug >= 2 : gui[20] = gui[20][:27]+"*****"+gui[20][32:]
if second_jug < 3 : gui[19] = gui[19][:28]+" "+gui[19][31:]
if second_jug == 3 : gui[19] = gui[19][:28]+"***"+gui[19][31:]
if thre_jug == 0 : gui[21] = gui[21][:45]+" "+gui[21][52:]
if thre_jug >= 1 : gui[21] = gui[21][:45]+"*******"+gui[21][52:]
if thre_jug < 2 : gui[20] = gui[20][:44]+" "+gui[20][53:]
if thre_jug >= 2 : gui[20] = gui[20][:44]+"*********"+gui[20][53:]
if thre_jug < 3 : gui[19] = gui[19][:43]+" "+gui[19][54:]
if thre_jug >= 3 : gui[19] = gui[19][:43]+"***********"+gui[19][54:]
if thre_jug < 4 : gui[18] = gui[18][:43]+" "+gui[18][54:]
if thre_jug >= 4 : gui[18] = gui[18][:43]+"***********"+gui[18][54:]
if thre_jug < 5 : gui[17] = gui[17][:44]+" "+gui[17][53:]
if thre_jug >= 5 : gui[17] = gui[17][:44]+"*********"+gui[17][53:]
if thre_jug < 6 : gui[16] = gui[16][:40]+" "+gui[16][47:]
if thre_jug >= 6 : gui[16] = gui[16][:40]+"*******"+gui[16][47:]
if thre_jug < 7 : gui[15] = gui[15][:40]+" "+gui[15][47:]
if thre_jug >= 7 : gui[15] = gui[15][:40]+"*******"+gui[15][47:]
if thre_jug < 8 : gui[14] = gui[14][:34]+" "+gui[14][43:]
if thre_jug == 8 : gui[14] = gui[14][:34]+"*********"+gui[14][43:]
print ('\033c')
if __name__=='__main__':
main()