/
minigame.py
executable file
·135 lines (119 loc) · 4.14 KB
/
minigame.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
#! /usr/bin/python
#!-*-coding: utf8-*-
import random
import os
import time
import copy
class Square:
"""This is the abstraction of one Square"""
def __init__(self, x, y, attrib):
self.x = x
self.y = y
self.attrib = attrib
self.rep = '\033[4%sm%s \033[0m' % (attrib, attrib)
self.have_vecinos = True
self.is_child = False
class Screen:
"""The Screen Array of the game"""
def __init__(self, x_size, y_size, tokens):
self.x_size = x_size
self.y_size = y_size
self.tokens = tokens
self.childs = []
self.squares = []
self.populate()
self.childs.append(self.squares[0][0])
self.childs[0].rep = '# '
self.childs[0].is_child = True
self.attrib = self.childs[0].attrib
def get_near_to(self, child):
vecinos = []
if child.x - 1 >= 0:
vecinos.append(self.get_square(child.x - 1, child.y))
if child.y - 1 >= 0:
vecinos.append(self.get_square(child.x, child.y - 1))
if child.x + 1 < self.x_size:
vecinos.append(self.get_square(child.x +1, child.y))
if child.y + 1 < self.y_size:
vecinos.append(self.get_square(child.x, child.y + 1))
vecinos = [vecino for vecino in vecinos if(not vecino.is_child)]
if not vecinos:
child.have_vecinos = False
return vecinos
def get_square(self, x, y):
return self.squares[y][x]
def populate(self):
for y in xrange(0, self.y_size):
self.squares.append([self.random_square(x, y) for x in xrange(0, self.x_size)])
def random_square(self, x, y):
attrib = random.choice(self.tokens)
square = Square(x, y, attrib)
return square
def check_arround(self, child):
"""We check the attrib of the near squares"""
vecinos = self.get_near_to(child)
if vecinos:
for vecino in vecinos:
if (vecino.attrib == self.attrib):
vecino.rep = child.rep
self.childs.append(vecino)
vecino.is_child = True
def __repr__(self):
output = ''
for row in self.squares:
output += ''.join(['%s' % str(a.rep) for a in row])
output += '\n'
return output
def update(self, attrib):
self.attrib = attrib[0]
for child in self.childs:
if child.have_vecinos:
self.check_arround(child)
class Solver(Screen):
"""Toma un Puzzle y lo resuelve de manera optima"""
def __init__(self, puzzle):
self.squares = puzzle.squares
self.tokens = puzzle.tokens
self.childs = []
self.x_size = puzzle.x_size
self.y_size = puzzle.y_size
self.attrib = puzzle.attrib
def solve(self):
"""Resuelvo un puzzle, para saber cuantos movimientos son necesarios"""
for a in self.tokens:
pass
def check_move(token):
"""Chequea cuantos cuadraditos agrega una eleccion de token"""
hijos = copy.copy(self.childs)
def my_check_arround(self, child, childlist):
"""We check the attrib of the near squares"""
vecinos = self.get_near_to(child)
if vecinos:
for vecino in vecinos:
if (vecino.attrib == self.attrib):
vecino.rep = child.rep
childlist.append(vecino)
vecino.is_child = True
if __name__ == '__main__':
os.system('clear')
tokens = [str(d) for d in xrange(1, 9)]
screen = Screen(20, 20, tokens)
print screen
screen.update(screen.childs[0].attrib) #dirty Hack
limit = screen.x_size + screen.y_size
win = False
for b in xrange(1, limit + 1):
time.sleep(0.5)
a = raw_input('_#: ')
# a = random.choice(tokens)
screen.update(a)
os.system('clear')
print screen
print "%4d moves of %4d" % (b, limit)
if len(screen.childs) == screen.x_size*screen.y_size:
win = True
break
if win:
print "You Win"
else:
print "You loose"