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 | # WIN_COMBINATIONS = [(0,1,2),(4,5,6)]
'''
Enter position 2
R R __ R G G G
Enter position 4
R R G R __ G G
Enter position 5
R R G R G __ G
Enter position 3
R R G __ G R G
Enter position 1
R __ G R G R G
Enter position 0
__ R G R G R G
Enter position 2
G R __ R G R G
Enter position 4
G R G R __ R G
Enter position 6
G R G R G R __
Enter position 5
G R G R G __ R
Enter position 3
G R G __ G R R
Enter position 1
G __ G R G R R
Enter position 2
G G __ R G R R
Enter position 4
G G G R __ R R
Enter position 3
G G G __ R R R
You won
'''
def swapPositions(board, pre_position, new_pos):
b = board
# b[pre_position], b[new_pos] = b[new_pos], b[pre_position]
temp = b[pre_position]
b[pre_position] = b[new_pos]
b[new_pos] = temp
def print_board(board):
for i in board:
print(str(i), end=" ")
def check_win(board):
b = board
if b[4] == "R" and b[4] == "R" and b[4] == "R":
if b[3] == "__":
return True
return False
return False
def update_board(board, pos):
# pos is new position
pre_position = board.index('__')
print(pre_position)
if pre_position:
# swaping
swapPositions(board, pre_position, pos)
def verify_move(board, pos): #True if can move
# if pos in range(0, 7):
# return True
# else:
return True
def enter_move(board):
pos = int ( input("\nEnter position: ") )
while not verify_move(board, pos):
print ("Invalid position")
pos = int ( input("\nEnter position: ") )
return pos
def game_stuck(board): #True if no one can move
return False
def game():
board = ["R","R","R","__","G","G","G"]
moves = 0
print_board(board)
#you can define the hares positions as 1,1,1 and -1,-1,-1 or "R","R","R" and "G","G","G"
#fill the list board according with the representation you prefer
while (True):
pos = enter_move(board)
update_board(board, pos)
print_board(board)
if check_win(board):
return True, moves
if game_stuck(board):
return False, moves
moves += 1
result, moves = game()
if result:
print (f"You won with {moves} moves")
else:
print ("You are stuck")
|