1
1
import random
2
+ import time
2
3
from typing import Tuple , List
3
4
4
5
from table import Table
@@ -40,27 +41,35 @@ def _gen_random_expression_free(self, operator=None) -> List:
40
41
return [operands [0 ], operator , operands [1 ], '=' , result ]
41
42
42
43
def _get_random_cell_for_next (self ) -> Tuple [int , int , int , int ]:
44
+ start_time = time .time ()
45
+ counter = 0
46
+ cells = self .table .find_not_empty_cells ()
47
+ if len (cells ) == 0 :
48
+ cells = [self .table .get_cell (0 , 0 )]
43
49
while True :
44
- cells = self .table .find_not_empty_cells ()
45
- if not cells :
46
- cell_operand = self .table .get_cell (0 , 0 )
47
- else :
48
- cell_operand = random .choice (cells )
49
- horizontal = random .choice ([True , False ])
50
- if horizontal :
51
- if self .table .get_cell (cell_operand .get_x () + 1 , cell_operand .get_y ()).is_not_empty ():
52
- continue
53
- if self .table .get_cell (cell_operand .get_x () - 1 , cell_operand .get_y ()).is_not_empty ():
54
- continue
55
- else :
56
- if self .table .get_cell (cell_operand .get_x (), cell_operand .get_y () + 1 ).is_not_empty ():
57
- continue
58
- if self .table .get_cell (cell_operand .get_x (), cell_operand .get_y () - 1 ).is_not_empty ():
59
- continue
60
- return cell_operand .get_x (), cell_operand .get_y (), 1 if horizontal else 0 , 0 if horizontal else 1
50
+ counter += 1
51
+ if time .time () - start_time > 0.005 :
52
+ raise Exception ('timeout: ' , time .time () - start_time , 'counter: ' , counter )
53
+ cell_operand = random .choice (cells )
54
+ directions = []
55
+ if self .table .get_cell (cell_operand .get_x () + 1 , cell_operand .get_y ()).is_not_empty () is False :
56
+ directions .append ((1 , 0 ))
57
+ if self .table .get_cell (cell_operand .get_x () - 1 , cell_operand .get_y ()).is_not_empty () is False :
58
+ directions .append ((- 1 , 0 ))
59
+ if self .table .get_cell (cell_operand .get_x (), cell_operand .get_y () + 1 ).is_not_empty () is False :
60
+ directions .append ((0 , 1 ))
61
+ if self .table .get_cell (cell_operand .get_x (), cell_operand .get_y () - 1 ).is_not_empty () is False :
62
+ directions .append ((0 , - 1 ))
63
+
64
+ if len (directions ) == 0 :
65
+ cells .remove (cell_operand )
66
+ continue
67
+ direction = random .choice (directions )
68
+ return cell_operand .get_x (), cell_operand .get_y (), direction [0 ], direction [1 ]
61
69
62
70
def generate (self ):
63
- for _ in range (2 ):
71
+ start_time = time .time ()
72
+ for _ in range (20 ):
64
73
x , y , xadd , yadd = self ._get_random_cell_for_next ()
65
74
expression = self ._gen_random_expression ()
66
75
@@ -69,6 +78,7 @@ def generate(self):
69
78
cell .set_value (expression_part )
70
79
x += xadd
71
80
y += yadd
81
+ print ('time: ' , time .time () - start_time )
72
82
73
83
def print (self ):
74
84
self .table .print ()
0 commit comments