/
Game.py
944 lines (827 loc) · 39.1 KB
/
Game.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# -*- coding: utf-8 -*-
"""All game stuff for LearnMath."""
from random import randint, choice
import LM_Utils
from Rule import RuleException, Rule
OPERATION_SUBTRACTION = "-"
OPERATION_ADDITION = "+"
OPERATION_MULTIPLY = "*"
OPERATION_DIVISION = "/"
RANGE_0_10 = {'from': 0, 'to': 10}
RANGE_0_20 = {'from': 0, 'to': 20}
RANGE_0_30 = {'from': 0, 'to': 30}
RANGE_0_40 = {'from': 0, 'to': 40}
RANGE_0_50 = {'from': 0, 'to': 50}
RANGE_0_100 = {'from': 0, 'to': 100}
# class Configuration
class ConfigEquation(object):
def __init__(self, operator, range_dict, without_zero=True,
custom_rules=None):
"""
:param operator: one from "+" | "-" | "*"
:param range_dict: dictionary with range
{"from":min_value, "to":max_value}
:param without_zero: set True to not random zero to equation elements
| czy losować 0 do działań?
:param custom_rules: custom rules for equation elements L1, L2, W.
Array of tuples (element, operand, value)
| reguły użytkownika dla L1, L2 i W.
"""
self.operator = operator
self.range_dict = range_dict
self.without_zero = without_zero
self.custom_rules = custom_rules
class ConfigAddition(ConfigEquation):
def __init__(self, range_dict, without_zero=True,
summand_1_condition=">=", summand_1_value=0,
summand_2_condition=">=", summand_2_value=0,
result_condition=None, result_value=None, custom_rules=None):
"""
:param range_dict: zakres
:param without_zero: czy losować 0 do działań?
:param summand_1_condition: składnik_1 - warunek = ["<" | ">", "="]
:param summand_1_value: składnik_1 - wartość
:param summand_2_condition: składnik_2 - warunek = ["<" | ">", "="]
:param summand_2_value: składnik_2 - wartość
:param result_condition: wynik - warunek = ["<" | ">", "="]
:param result_value: wynik - wartość
:param custom_rules: reguły uzytkownika na L1, L2 i W.
Array of tuples (element, operand, value)
"""
ConfigEquation.__init__(self, "+", range_dict, without_zero,
custom_rules)
self.summand_1_condition = summand_1_condition
self.summand_1_value = summand_1_value
self.summand_2_condition = summand_2_condition
self.summand_2_value = summand_2_value
self.result_condition = result_condition
self.result_value = result_value
class ConfigSubtraction(ConfigEquation):
def __init__(self, range_dict, without_zero=True,
minuend_condition=">=", minuend_value=0,
subtrahend_condition=">=", subtrahend_value=0,
minuend_ge_subtrahend=True, result_condition=None,
result_value=None, custom_rules=None):
"""
:param range_dict: zakres
:param without_zero: czy losować 0 dla odjemnej?
:param minuend_condition: odjemna - warunek = ["<" | ">", "="]
:param minuend_value: odjemna - wartość
:param subtrahend_condition: odjemnik - warunek = ["<" | ">", "="
:param subtrahend_value: odjemnik - wartość
:param minuend_ge_subtrahend: odjemna większa bądź równa niż odjemnik,
domyślnie True
:param result_condition: wynik - warunek = ["<" | ">", "="]
:param result_value: wynik - wartość
:param custom_rules: reguły uzytkownika na L1, L2 i W.
Array of tuples (element, operand, value)
:return:
"""
ConfigEquation.__init__(self, "-", range_dict, without_zero,
custom_rules)
self.minuend_condition = minuend_condition
self.minuend_value = minuend_value
self.subtrahend_condition = subtrahend_condition
self.subtrahend_value = subtrahend_value
self.minuend_ge_subtrahend = minuend_ge_subtrahend
self.result_condition = result_condition
self.result_value = result_value
class ConfigMultiply(ConfigEquation):
def __init__(self, range_dict, without_zero=True, without_one=False,
element_1_condition=">=", element_1_value=0,
element_2_condition=">=", element_2_value=1,
result_condition=None, result_value=None, custom_rules=None):
"""
:param range_dict: zakres (dictioniary = {"from":x, "to":y},
x,y - integers)
:param without_zero: czy losować 0 do działań?
:param element_1_condition: składnik 1 - warunek = ["<" | ">", "="]
:param element_1_value: składnik 1 - wartość dla wyrażenia warunkowego
:param element_2_condition: składnik 2 - warunek = ["<" | ">", "="]
:param element_2_value: składnik 2 - wartość dla wyrażenia warunkowego
:param result_condition: wynik - warunek = ["<" | ">", "="]
:param result_value: wynik - wartość dla wyrażenia warunkowego
:param custom_rules: reguły użytkownika na L1, L2 i W.
Array of tuples (element, operand, value)
:return:
"""
ConfigEquation.__init__(self, "*", range_dict, without_zero,
custom_rules)
self.without_one = without_one
self.element_1_condition = element_1_condition
self.element_1_value = element_1_value
self.element_2_condition = element_2_condition
self.element_2_value = element_2_value
self.result_condition = result_condition
self.result_value = result_value
class Answers(object):
@staticmethod
def get_random_comment_for_answer(is_answer_correct, last_answer):
correct_answers = ["Brawo!", "Świetnie!", "Super!", "Wspaniale!",
"Bardzo dobrze!", "Tak!", "Dobrze!",
"Bezbłędna odpowiedź!", "Pięknie!", ":-)"]
bad_answers = ["Nie...", "Błąd...", "Pomyłka...", "Zły wynik...",
"Niestety nie...", "Oj, nie...", "Niepoprawnie...",
"Błędna odpowiedź...", "Ups...", "Jak to pliczyłeś?",
":-("]
if is_answer_correct:
number = randint(0, len(correct_answers) - 1)
# random = correct_answers[number]
while last_answer == correct_answers[number]:
number = randint(0, len(correct_answers) - 1)
comment = correct_answers[number]
else:
number = randint(0, len(bad_answers) - 1)
# random = bad_answers[number]
while last_answer == bad_answers[number]:
number = randint(0, len(bad_answers) - 1)
comment = bad_answers[number]
return comment
class Equation(object):
def __init__(self, number_1, operator, number_2):
self.number_1 = number_1
self.operator = operator
self.number_2 = number_2
self.user_answer = None
def __str__(self):
str_val = str(self.number_1) + " " + self.operator + " " + \
str(self.number_2) + " = " + str(self.user_answer)
return str_val
@staticmethod
def get_hint_for_equation(equation):
hint = ""
if equation.operator == OPERATION_ADDITION:
hint = Equation.get_hint_for_addition(equation)
elif equation.operator == OPERATION_SUBTRACTION:
hint = Equation.get_hint_for_subtraction(equation)
elif equation.operator == OPERATION_MULTIPLY:
hint = Equation.get_hint_for_multiply(equation)
elif equation.operator == OPERATION_DIVISION:
print("get_hint_for_division() - not handled yet")
return hint
@staticmethod
def get_hint_for_multiply(equation):
hint = "Brak podpowiedzi, sorry..."
hint_const = str(equation.number_1) + " * " + \
str(equation.number_2) + " = "
if equation.number_1 == 0 or equation.number_2 == 0:
hint = hint_const
elif equation.number_1 == 1 or equation.number_2 == 1:
hint = hint_const
else:
exp = ""
for i in range(equation.number_1):
exp = exp + str(equation.number_2) + " + "
exp_ok = exp[:len(exp) - 3]
hint_exp = exp_ok + " = "
hint = hint_const + hint_exp
return hint
@staticmethod
def get_hint_for_subtraction(equation):
hint = "Brak podpowiedzi, sorry..."
hint_const = str(equation.number_1) + " - " + \
str(equation.number_2) + " = "
if (equation.number_1 > 10) and (equation.number_2 < 10) and \
(equation.number_1 - equation.number_2 < 10): #
# np. 13-5, 16 - 8, itp
# number_1 - number_2 = number_1 - to_ten - rest
to_ten = equation.number_1 - 10
rest = equation.number_2 - to_ten
hint = str(equation.number_1) + " - " + str(equation.number_2) + \
" = " + str(equation.number_1) + " - " + str(to_ten) + \
" - " + str(rest) + " = ..."
# print("hint: ", hint)
n1_ile10 = int(equation.number_1 / 10)
n1_ile1 = equation.number_1 - (n1_ile10 * 10)
n2_ile10 = int(equation.number_2 / 10)
n2_ile1 = equation.number_2 - (n2_ile10 * 10)
if n1_ile1 - n2_ile1 < 0:
# jest pożyczka z dziesiątek
n1_ile10_po_pozyczce = n1_ile10 - 1
if n1_ile10_po_pozyczce == 0:
# równanie typu: 12 - 8
hint_exp = ""
n1_ile_do_10 = equation.number_1 - 10
hint_exp = str(equation.number_1) + \
" - " + str(n1_ile_do_10) + \
" - " + str(equation.number_2 - n1_ile_do_10) + \
" = "
hint = hint_const + hint_exp
return hint
else: # 44 - 8, 44 - 18, 91 - 82, ...
# coś bardziej skomplikowanego
# 67 - 39 = 67 - 7 - 32 = 60 - 32 =
hint_exp = ""
hint_exp = hint_exp + str(equation.number_1) + \
" - " + str(n1_ile1) + \
" - " + str(equation.number_2 - n1_ile1) + \
" = "
hint = hint_const + hint_exp
return hint
else:
# nie ma pożyczki z dziesiątek
# 15 - 5, 17 - 5, 28 - 3
if n1_ile1 - n2_ile1 == 0:
# np.: 15 - 5, 27 - 7
hint = hint_const
elif equation.number_2 == 10:
hint = hint_const
else:
if n2_ile10 >= 1:
# np. 67 - 23 = 60 - 7 - 16 =
hint_exp = ""
hint_exp = hint_exp + str(equation.number_1) + \
" - " + str(n1_ile1) + \
" - " + str(equation.number_2 - n1_ile1) + \
" = "
hint = hint_const + hint_exp
pass
else:
# np. 28 - 3
hint_exp = ""
hint_exp = str(n1_ile10 * 10) + \
" + " + str(n1_ile1) + \
" - " + str(equation.number_2) + \
" = "
hint = hint_const + hint_exp
return hint
@staticmethod
def get_hint_for_addition(equation):
hint = "Brak podpowiedzi, sorry..."
hint_const = str(equation.number_1) + \
" + " + str(equation.number_2) + \
" = "
n1_ile10 = int(equation.number_1 / 10)
n1_ile1 = equation.number_1 - (n1_ile10 * 10)
n2_ile10 = int(equation.number_2 / 10)
n2_ile1 = equation.number_2 - (n2_ile10 * 10)
if n1_ile1 + n2_ile1 <= 9:
# print("przy dodawaniu liczb nie ma przeniesienia na dziesiątki")
# przy dodawaniu liczb nie ma przeniesienia na dziesiątki
# np.: 13 + 13, 11 + 18, 34 + 25, 66 + 11, itp.
# hint_exp =
# dziesiatki_n1 + dziesiatki_n2 + jednosci_n1 + jednosci_n2 +
# " = "
hint_exp = ""
hint_exp = hint_exp + str(n1_ile10 * 10) + \
" + " if n1_ile10 > 0 else hint_exp
hint_exp = hint_exp + str(n2_ile10 * 10) + \
" + " if n2_ile10 > 0 else hint_exp
hint_exp = hint_exp + str(n1_ile1) + " + " + str(n2_ile1) + " = "
hint = hint_const + hint_exp
else:
# print("przy dodawaniu liczb jest przeniesienie na dziesiątki")
# przy dodawaniu liczb jest przeniesienie na dziesiątki
# np.: 13 + 18, 34 + 29, itp.
n1_ile_do_10 = 10 - n1_ile1
n2_reszta_1 = n2_ile1 - n1_ile_do_10
# hint_exp =
# dziesiatki_n1 + dziesiatki_n2 + jednosci_n1 + n1_ile_do_10 +
# n2_reszta_1 + " = "
if n1_ile10 > 0: # spr dla 7 + 13
hint_exp = ""
hint_exp = hint_exp + str(n1_ile10 * 10) + " + " \
if n1_ile10 > 0 else hint_exp
hint_exp = hint_exp + str(n1_ile1) + " + " \
if n1_ile1 > 0 else hint_exp
hint_exp = hint_exp + str(n1_ile_do_10) + " + " \
if n1_ile_do_10 > 0 else hint_exp
hint_exp = hint_exp + str(n2_ile10 * 10) + " + " \
if n2_ile10 > 0 else hint_exp
hint_exp = hint_exp + str(n2_reszta_1) + " = " \
if n2_reszta_1 > 0 else hint_exp
if hint_exp[len(hint_exp) - 2] == "+":
hint_exp = hint_exp[:len(hint_exp) - 2] + "= "
# hint_exp =
# str(n1_ile10 * 10) + " + " + str(n2_ile10 * 10) +
# " " + str(n1_ile1) + " + " + str(n1_ile_do_10) +
# " + " + str(n2_reszta_1) + " = "
else:
hint_exp = ""
hint_exp = hint_exp + str(n1_ile1) + " + " \
if n1_ile1 > 0 else hint_exp
hint_exp = hint_exp + str(n1_ile_do_10) + " + " \
if n1_ile_do_10 > 0 else hint_exp
hint_exp = hint_exp + str(n2_ile10 * 10) + " + " \
if n2_ile10 > 0 else hint_exp
hint_exp = hint_exp + str(n2_reszta_1) + " = " \
if n2_reszta_1 > 0 else hint_exp
if hint_exp[len(hint_exp) - 2] == "+":
hint_exp = hint_exp[:len(hint_exp) - 2] + "= "
# hint_exp = str(n1_ile1) + " + " + str(n2_ile10 * 10) +
# " + " + str(n1_ile_do_10) + " + " +
# str(n2_reszta_1) + " = "
hint = hint_const + hint_exp
return hint
@staticmethod
def is_subtraction_result_met(n1, n2, result_condition, result_value):
"""
Returns True if conditon is met for:
n1 - n2 result_condition result_value
:rtype : bool
"""
if result_condition is None and result_value is None:
return True
elif result_condition is not None and result_value is not None:
if result_condition == "=" or result_condition == "==":
return n1 - n2 == result_value
elif result_condition == "<":
return n1 - n2 < result_value
elif result_condition == "<=":
return n1 - n2 <= result_value
elif result_condition == ">":
return n1 - n2 > result_value
elif result_condition == ">=":
return n1 - n2 >= result_value
else:
return False
@staticmethod
def is_addition_result_met(n1, n2, result_condition, result_value):
"""
Returns True if conditon is met for:
n1 + n2 result_condition result_value
:rtype : bool
"""
if result_condition is None and result_value is None:
return True
elif result_condition is not None and result_value is not None:
if result_condition == "=" or result_condition == "==":
return n1 + n2 == result_value
elif result_condition == "<":
return n1 + n2 < result_value
elif result_condition == "<=":
return n1 + n2 <= result_value
elif result_condition == ">":
return n1 + n2 > result_value
elif result_condition == ">=":
return n1 + n2 >= result_value
else:
return False
@staticmethod
def is_multiply_result_met(n1, n2, result_condition, result_value):
"""
Returns True if conditon is met for:
n1 * n2 result_condition result_value
:rtype : bool
"""
if result_condition is None and result_value is None:
return True
elif result_condition is not None and result_value is not None:
if result_condition == "=" or result_condition == "==":
return n1 * n2 == result_value
elif result_condition == "<":
return n1 * n2 < result_value
elif result_condition == "<=":
return n1 * n2 <= result_value
elif result_condition == ">":
return n1 * n2 > result_value
elif result_condition == ">=":
return n1 * n2 >= result_value
else:
return False
@staticmethod
def prepare_equation_addition(game_config):
if game_config.custom_rules is None:
return Equation.prepare_equation_addition_simple_config(game_config)
else:
eq_add = Equation.prepare_equation_addition_config_custom_rules(
game_config)
return eq_add
@staticmethod
def prepare_equation_addition_config_custom_rules(game_config):
c_rules = game_config.custom_rules
condition_end = False
conditions_for_rules = []
min_value_l1 = None
max_value_l1 = None
min_value_l1, max_value_l1 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_1, c_rules)
min_value_l2 = None
max_value_l2 = None
min_value_l2, max_value_l2 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_2, c_rules)
min_value_w = None
max_value_w = None
min_value_w, max_value_w = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_W, c_rules)
while not condition_end:
conditions_for_rules = []
for i in c_rules:
conditions_for_rules.append(False)
try:
s_rule_wt_min_max = "Rules for %1 don't " \
"determine it's min and " \
"max value."
if min_value_l1 is None or max_value_l1 is None:
raise RuleException(s_rule_wt_min_max.replace("%1", "L1"))
n1 = randint(min_value_l1, max_value_l1)
# print("n1 = ", n1)
if min_value_l2 is None or max_value_l2 is None:
raise RuleException(s_rule_wt_min_max.replace("%1", "L2"))
n2 = randint(min_value_l2, max_value_l2)
# print("n2 = ", n2)
idx = 0
Equation.validate_rules_for_w(c_rules)
for rule in c_rules:
if rule[0] == Rule.RULE_ELEMENT_1 \
or rule[0] == Rule.RULE_ELEMENT_2:
conditions_for_rules[idx] = True
elif rule[0] == Rule.RULE_ELEMENT_W:
if Equation.is_addition_result_met(n1, n2,
rule[1], rule[2]):
conditions_for_rules[idx] = True
idx += 1
condition_user_rules = True
for idx in range(len(conditions_for_rules)):
condition_user_rules = condition_user_rules \
and conditions_for_rules[idx]
condition_end = condition_user_rules
except ValueError as ve:
# Temporary !!!!!!!!!!!!!!!!!!!!!!!!!!!!
if ve.__str__().find("empty range for randrange()") >= 0:
# there is invalid range for L1 or L2, eg.: [50, 20]
raise RuleException("Empty set evauluated for given rules")
except Exception as exc:
# print(exc.__str__())
raise exc
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_ADDITION, n2)
return p_equation
@staticmethod
def prepare_equation_addition_simple_config(game_config):
number_1 = game_config.summand_1_value # składnik_1
number_1_condition = game_config.summand_1_condition
number_2 = game_config.summand_2_value # składnik_2
number_2_condition = game_config.summand_2_condition
number_range_dict = game_config.range_dict # range
number_range_min = number_range_dict["from"]
number_range_max = number_range_dict["to"]
condition_end = False
while not condition_end:
condition_addition_result_met = True
condition_without_zero = True
# print("n1")
crnd_int = LM_Utils.random_int_w_condition
n1 = crnd_int(number_range_min, number_range_max,
number_1_condition, number_1)
# print("n2")
n2 = crnd_int(number_range_min, number_range_max,
number_2_condition, number_2)
if (game_config.result_condition is not None) \
and (game_config.result_value is not None):
# gdy podano warunek na wynik
if not Equation.is_addition_result_met(n1, n2,
game_config.result_condition,
game_config.result_value):
condition_addition_result_met = False
if game_config.without_zero:
if n1 == 0 or n2 == 0:
condition_without_zero = False
condition_end = condition_addition_result_met \
and condition_without_zero
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_ADDITION, n2)
return p_equation
@staticmethod
def prepare_equation_subtraction(game_config):
if game_config.custom_rules is None:
return Equation.prepare_equation_subtraction_simple_config(game_config)
else:
eqt = Equation.prepare_equation_subtraction_config_custom_rules(
game_config)
return eqt
@staticmethod
def prepare_equation_subtraction_config_custom_rules(game_config):
c_rules = game_config.custom_rules
condition_end = False
conditions_for_rules = []
min_value_l1 = None
max_value_l1 = None
min_value_l1, max_value_l1 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_1, c_rules)
min_value_l2 = None
max_value_l2 = None
min_value_l2, max_value_l2 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_2, c_rules)
min_value_w = None
max_value_w = None
min_value_w, max_value_w = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_W, c_rules)
while not condition_end:
conditions_for_rules = []
for i in c_rules:
conditions_for_rules.append(False)
try:
s_rule_min_max = "Rules for %1 don't determine it's min " \
"and max value."
if min_value_l1 is None or max_value_l1 is None:
raise RuleException(s_rule_min_max.replace("%1", "L1"))
n1 = randint(min_value_l1, max_value_l1)
# print("n1 = ", n1)
if min_value_l2 is None or max_value_l2 is None:
raise RuleException(s_rule_min_max.replace("%1", "L2"))
n2 = randint(min_value_l2, max_value_l2)
# print("n2 = ", n2)
idx = 0
Equation.validate_rules_for_w(c_rules)
for rule in c_rules:
if rule[0] == Rule.RULE_ELEMENT_1 \
or rule[0] == Rule.RULE_ELEMENT_2:
conditions_for_rules[idx] = True
elif rule[0] == Rule.RULE_ELEMENT_W:
if Equation.is_subtraction_result_met(n1, n2,
rule[1], rule[2]):
conditions_for_rules[idx] = True
idx += 1
condition_user_rules = True
for idx in range(len(conditions_for_rules)):
condition_user_rules = \
condition_user_rules and conditions_for_rules[idx]
condition_end = condition_user_rules
except ValueError as ve:
# Temporary !!!!!!!!!!!!!!!!!!!!!!!!!!!!
if ve.__str__().find("empty range for randrange()") >= 0:
# there is invalid range for L1 or L2, eg.: [50, 20]
raise RuleException("Empty set evauluated for given rules")
except Exception as exc:
# print(exc.__str__())
raise exc
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_SUBTRACTION, n2)
return p_equation
@staticmethod
def prepare_equation_subtraction_simple_config(game_config):
number_1 = game_config.minuend_value # odjemna
number_1_condition = game_config.minuend_condition
number_2 = game_config.subtrahend_value # odjemnik
number_2_condition = game_config.subtrahend_condition
number_range_dict = game_config.range_dict # range
number_range_min = number_range_dict["from"]
number_range_max = number_range_dict["to"]
condition_end = False
while not condition_end:
condition_minuend_ge_subtrahend = True
condition_subtraction_result_met = True
condition_without_zero = True
# print("n1")
n1 = LM_Utils.random_int_w_condition(number_range_min,
number_range_max,
number_1_condition,
number_1)
# print("n2")
n2 = LM_Utils.random_int_w_condition(number_range_min,
number_range_max,
number_2_condition,
number_2)
if game_config.minuend_ge_subtrahend:
# gdy odjemna ma być większa bądź równa niż odjemnik
if not n1 >= n2:
condition_minuend_ge_subtrahend = False
if (game_config.result_condition is not None) \
and (game_config.result_value is not None):
# gdy podano warunek na wynik
if not Equation.is_subtraction_result_met(n1, n2,
game_config.result_condition,
game_config.result_value):
condition_subtraction_result_met = False
if game_config.without_zero:
if n2 == 0:
condition_without_zero = False
condition_end = condition_minuend_ge_subtrahend \
and condition_subtraction_result_met \
and condition_without_zero
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_SUBTRACTION, n2)
return p_equation
@staticmethod
def get_min_max_values(str_element, custom_rules):
"""
Returns min_value, max_value for str_element basing on custom_rules
:param str_element: one from ["L1|"L2"|"W"]
:param custom_rules: array of tuples (element, operand, value).
Value is int.
:return: min_value, max_value for str_element basing on custom_rules
"""
el_min = None
el_max = None
for rule in custom_rules:
if rule[0] == str_element:
# print(str_element + " rule found: " + rule[0] + " " + rule[1]
# + " " + str(rule[2]))
r_op = rule[1]
r_val = rule[2]
if r_op == "==" or r_op == "=":
el_max = r_val
el_min = r_val
elif r_op == "<=":
el_max = r_val
elif r_op == "<":
el_max = r_val - 1
elif r_op == ">=":
el_min = r_val
elif r_op == ">":
el_min = r_val + 1
return el_min, el_max
@staticmethod
def validate_rules_for_w(custom_rules):
"""
Raises RuleException when rules for W are not valid.
:param custom_rules:
:return:
"""
op_eq = 0
op_less = 0
op_greater = 0
for rule in custom_rules:
if rule[0] == Rule.RULE_ELEMENT_W:
if rule[1] == "==" or rule[1] == "=":
op_eq += 1
elif rule[1] == "<" or rule[1] == "<=":
op_less += 1
elif rule[1] == ">" or rule[1] == ">=":
op_greater += 1
if op_eq > 1:
raise RuleException("Rules for W contain multiple rules with "
"operator = or ==.\n "
"It's not allowed - fix the rules.")
if op_eq > 0 and (op_less + op_greater) > 0:
# gdy jest operator =
# i przynajmniej jeden z operatorów >=, >, <, <=
raise RuleException("Rules for W contain operator == and "
"another operator.\n "
"It's not allowed - fix the rules.")
if op_less > 1:
raise RuleException("Rules for W contain multiple rules with "
"operator < or <=.\n "
"It's not allowed - fix the rules.")
if op_greater > 1:
raise RuleException("Rules for W contain multiple rules with "
"operator > or >=.\n "
"It's not allowed - fix the rules.")
if (op_less == 1 and op_greater == 0) \
or (op_less == 0 and op_greater == 1):
raise RuleException("Rules for W don't contain rules to determine "
"min and max value for W.\n "
"It's not allowed - fix the rules.")
min_w, max_w = Equation.get_min_max_values("W", custom_rules)
if min_w is not None and max_w is not None:
if min_w > max_w:
raise RuleException("Rules for W contain rules which determine"
" an empty set for W values.\n "
"It's not allowed - fix the rules.")
@staticmethod
def prepare_equation_multiply(game_config):
if game_config.custom_rules is None:
return Equation.prepare_equation_multiply_simple_config(game_config)
else:
eqt = Equation.prepare_equation_multiply_config_custom_rules(
game_config)
return eqt
@staticmethod
def prepare_equation_multiply_config_custom_rules(game_config):
c_rules = game_config.custom_rules
condition_end = False
conditions_for_rules = []
min_value_l1 = None
max_value_l1 = None
min_value_l1, max_value_l1 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_1, c_rules)
min_value_l2 = None
max_value_l2 = None
min_value_l2, max_value_l2 = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_2, c_rules)
min_value_w = None
max_value_w = None
min_value_w, max_value_w = \
Equation.get_min_max_values(Rule.RULE_ELEMENT_W, c_rules)
while not condition_end:
conditions_for_rules = []
for i in c_rules:
conditions_for_rules.append(False)
try:
s_rule_min_max = "Rules for %1 don't determine it's min " \
"and max value."
if min_value_l1 is None or max_value_l1 is None:
raise RuleException(s_rule_min_max.replace("%1", "L1"))
n1 = randint(min_value_l1, max_value_l1)
# print("n1 = ", n1)
if min_value_l2 is None or max_value_l2 is None:
raise RuleException(s_rule_min_max.replace("%1", "L2"))
n2 = randint(min_value_l2, max_value_l2)
# print("n2 = ", n2)
idx = 0
Equation.validate_rules_for_w(c_rules)
for rule in c_rules:
if rule[0] == Rule.RULE_ELEMENT_1 \
or rule[0] == Rule.RULE_ELEMENT_2:
conditions_for_rules[idx] = True
elif rule[0] == Rule.RULE_ELEMENT_W:
if Equation.is_multiply_result_met(n1, n2, rule[1],
rule[2]):
conditions_for_rules[idx] = True
idx += 1
condition_user_rules = True
for idx in range(len(conditions_for_rules)):
condition_user_rules = condition_user_rules \
and conditions_for_rules[idx]
condition_end = condition_user_rules
except ValueError as verr:
# Temporary !!!!!!!!!!!!!!!!!!!!!!!!!!!!
if verr.__str__().find("empty range for randrange()") >= 0:
# there is invalid range for L1 or L2, eg.: [50, 20]
raise RuleException("Empty set evauluated for given rules")
except Exception as exc:
# print(exc.__str__())
raise exc
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_MULTIPLY, n2)
return p_equation
@staticmethod
def prepare_equation_multiply_simple_config(game_config):
number_1 = game_config.element_1_value
number_1_condition = game_config.element_1_condition
number_2 = game_config.element_2_value
number_2_condition = game_config.element_2_condition
number_range_dict = game_config.range_dict # range
number_range_min = number_range_dict["from"]
number_range_max = number_range_dict["to"]
condition_end = False
while not condition_end:
# condition_minuend_ge_subtrahend = True
condition_multiply_result_met = True
condition_without_zero = True
condition_without_one = True
# print("n1")
n1 = LM_Utils.random_int_w_condition(number_range_min,
number_range_max,
number_1_condition,
number_1)
# print("n2")
n2 = LM_Utils.random_int_w_condition(number_range_min,
number_range_max,
number_2_condition,
number_2)
# if game_config.minuend_ge_subtrahend:
# # gdy odjemna ma być większa bądź równa niż odjemnik
# if not (n1 >= n2):
# condition_minuend_ge_subtrahend = False
if (game_config.result_condition is not None) \
and (game_config.result_value is not None):
# gdy podano warunek na wynik
if not Equation.is_multiply_result_met(n1, n2,
game_config.result_condition,
game_config.result_value):
condition_multiply_result_met = False
if game_config.without_zero:
if n1 == 0 or n2 == 0:
condition_without_zero = False
if game_config.without_one:
if n1 == 1 or n2 == 1:
condition_without_one = False
condition_end = condition_multiply_result_met \
and condition_without_zero \
and condition_without_one
# print("n1 = ", n1, " and n2 = ", n2)
# print(game_config.__str__())
p_equation = Equation(n1, OPERATION_MULTIPLY, n2)
return p_equation
@staticmethod
def prepare_equation_basing_on_single_operator(g_config):
n_equation = None
if g_config.operator == OPERATION_ADDITION:
n_equation = Equation.prepare_equation_addition(g_config)
elif g_config.operator == OPERATION_SUBTRACTION:
n_equation = Equation.prepare_equation_subtraction(g_config)
elif g_config.operator == OPERATION_MULTIPLY:
n_equation = Equation.prepare_equation_multiply(g_config)
elif g_config.operator == OPERATION_DIVISION:
print("prepareEquationDivision() - not handled yet")
return n_equation
@staticmethod
def prepare_equation_basing_on_multiple_operator(g_config_list):
n_equation = None
g_config = choice(g_config_list)
# r = randint(0, len(g_config_list-1))
# g_config = g_config_list[r]
if g_config.operator == OPERATION_ADDITION:
n_equation = Equation.prepare_equation_addition(g_config)
elif g_config.operator == OPERATION_SUBTRACTION:
n_equation = Equation.prepare_equation_subtraction(g_config)
elif g_config.operator == OPERATION_MULTIPLY:
print("prepare_equation_multiply() - not handled yet")
elif g_config.operator == OPERATION_DIVISION:
print("prepareEquationDivision() - not handled yet")
return n_equation
def check_user_result(self, user_result):
""" Check user answer against correct result.
Return True if user_result is correct answer, False otherwise.
"""
self.user_answer = user_result
correct = eval(str(self.number_1) + self.operator +
str(self.number_2))
return correct == user_result