-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlooder.py
More file actions
155 lines (142 loc) · 4.62 KB
/
Copy pathFlooder.py
File metadata and controls
155 lines (142 loc) · 4.62 KB
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
import random,sys
try:
import bext
except ImportError:
print('This requires the bext module')
sys.exit()
BOARD_WIDTH = 16
BOARD_HEIGHT = 14
MOVES_PER_GAME =20
HEART = chr(9829)
DIAMOND = chr(9830)
SPADE =chr(9824)
BALL = chr(9679)
TRIANGLE = chr(9650)
BLOCK = chr(9608)
LEFTRIGHT = chr(9472)
UPDOWN = chr(9474)
DOWNRIGHT = chr(9484)
DOWNLEFT = chr(9488)
UPRIGHT = chr(9892)
UPLEFT = chr(9496)
TILE_TYPES =(0,1,2,3,4,5)
COLORS_MAP = {0:'red',1:'green',2:'blue',3:'yellow',4:'cyan',5:'purple'}
COLOR_MODE = 'color mode'
SHAPES_MAP = {0:HEARt,1:TRIANGLE,2:DIAMOND,3:BALL,4:CLUB,5:SPADE}
SHAPE_MODE = 'shape mode'
def main():
bext.bg('black')
bext.fg('white')
bext.clear()
print('Do you want to play in colorblind mode? Y/N')
response = input('> ')
if response.upper().startswith('Y'):
displayMode = SHAPE_MODE
else:
displayMode = COLOR_MODE
gameBoard = getNewBoard()
movesLeft = MOVEs_PER_GAME
while True:
displayBoard(gameBoard, displayMode)
print('Moves left:',movesLeft)
playerMove = askForPlayerMove(displayMode)
changeTile(playerMove, gameBoard,0,0)
movesLeft -= 1
if hasWond(gameBoard):
displayBoard(gameBoard, displayMode)
print('You have won!')
break
elif movesLeft == 0:
displayBoard(gameBoard,displayMode)
print('You have run out of moves!')
break
def getNewBoard():
board = {}
for x in range(BOARD_WIDTH):
for y in range(BOARD_HEIGHT):
board[(x,y)] = random.choice(TILE_TYPES)
for i in range(BOARD_WIDTH * BOARD_HEIGHT):
x = random.randint(0, BOARD_WIDTH - 2)
y = random.randint(0,BOARD_HEIGHT - 1)
return board
def displayBoard(board,displayMode):
bext.fg('white')
print(DOWNRIGHT + (LEFTRIGHT * BOARD_WIDTH) + DOWNLEFT)
for y in range(BOARD_HEIGHT):
bext.fg('white')
if y == 0:
print('> ',end='')
else:
print(UPDOWN, end='')
for x in range(BOARD_WIDTH):
bext.fg(COLORS_MAP[board[(x,y)]])
if displayMode == COLOR_MODE:
print(BLOCK,end=' ')
elif displayMode == SHAPE_MODE:
print(SHAPES_MAP[board[(x,y)]],end='')
bext.fg('white')
print(UPDOWN)
print(UPRIGHT + (LEFTRIGHT * BOARD_WIDTH) + UPLEFT)
def askForPlayerMove(displayMode):
while True:
bext.fg('white')
print('Choose one of',end='')
if displayMode == COLOR_MODE:
bext.fg('red')
print('(R)ed',end='')
bext.fg('green')
print('(G)reen',end='')
bext.fg('blue')
print('(B)lue',end='')
bext.fg('yellow')
print('(Y)ellow',end='')
bext.fg('cyan')
print('(C)yan',end='')
bext.fg('purple')
print('(P)urple',end='')
elif displayMode == SHAPE_MODE:
bext.fg('red')
print('(H)eart',end='')
bext.fg('green')
print('(T)riange',end='')
bext.fg('blue')
print('(D)iamond',end='')
bext.fg('yellow')
print('(B)all',end='')
bext.fg('cyan')
print('(C)lub',end='')
bext.fg('purple')
print('(S)pade',end='')
bext.fg('white')
print('or QUIT')
response = input('> ').upper()
if response == 'QUIT':
print('Thanks for playing!')
sys.exit()
if displayMode == COLOR_MODE and response in tuple('RGBYCP'):
return {'R':0,'G':1,'B':2,'Y':3,'C':4,'P':5}[response]
if displayMode == SHAPE_MODE and response in tuple('HTDBCS'):
return {'H':0,'T':1,'D':2,'B':3,'C':4,'S':5}[response]
def changeTile(titleType,board,x,y,charToChange=None):
if x == 0 and y == 0:
charToChange =board[(x,y)]
if titleType == charToChange:
return
board[(x,y)] = titleType
if x > 0 and board[(x - 1,y)] == charToChange:
changeTile(tileType,board,x - 1,y,charToChange)
if y > 0 and board[(x,y - 1)] == charToChange:
changeTile(tileType, board,x,y-1,charToChange)
if x < BOARD_WIDTH - 1 and board[(x+1,y)] == charToChange:
changeTitle(titleType,board,x+1,y,charToChange)
if y < BOARD_HEIGHT - 1 and board[(x,y+1)] == charToChange:
changeTile(tileType,board,x,y+1,charToChange)
def hasWon(board):
tile = board[(0,0)]
for x in range(BOARD_WIDTH):
for y in range(BOARD_HEIGHT):
if board[(x,y)] != tile:
return False
return True
if __name__ == '__main__':
main()