-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGo.py
177 lines (137 loc) · 5.83 KB
/
Go.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
import pygame
from pygame.locals import *
def player_move(turn):
if turn % 2 == 0: # turn is incremented once per turn. By evaluating odd or even we can determine whose turn it is to play.
player = 1
else:
player = 2
# the below code is temporary until I can figure out a GUI to play on
#play_pass = raw_input("Play or pass: ").lower()
#if play_pass != "pass":
return player
def make_board():
# this makes our board. each index holds a piece with 3 (so far) values. Piece value. Vertical Index. Horizontal Index.
board = [[[0, y, x] for x in range(0,19)]for y in range(0,19)]
# y is vertical (index[1]) x is horizontal (index[2]).
return board
def print_board(board):
# this function prints the board out
for row in board:
print row
def gameplay_logic(board, player):
temp_dict = {} # temporary dictionary that is used for our search algorithm. HOLD ON. WHY DO WE NEED TO USE A DICTIONARY? LIST IS OK NO?
count = 0 # counter is used to create unique dictionary keys.
if player == 1:
enemy_piece = 2
else:
enemy_piece = 1
for row in board:
for index in row:
if index[0] == enemy_piece: # if an enemy piece is found in any index.
print "ENEMY PIECE FOUND"
temp_dict[count] = [index[0], index[1], index[2]]
print temp_dict # add it to the temp_dict with the key being the current increment value of count.
count += 1 # add one to the increment value so that next time there is a new key.
search_all(count, enemy_piece, board, index[1], index[2], temp_dict) # run search_all() with the required arguments.
else:
pass
def search_all(count, enemy_piece, board, vertical_index, horizontal_index, temp_dict):
found = True
while found: # while found is true
found = False # set it to false by default
for key in range(0, count):
# searches for enemy pieces directly to the right of all items in the dictionary.
found_right_piece = search_right(enemy_piece, board, temp_dict[key][1], temp_dict[key][2])
if found_right_piece: # if the value is real, not empty
print "PIECE TO THE RIGHT OF THIS ONE"
temp_dict[count] = [found_right_piece[0], found_right_piece[1], found_right_piece[2]] # add it to our dictionary
count += 1
found = True # set found to True so the program knows to loop again to make sure everything is covered.
for key in range(0, count):
found_down_piece = search_down(enemy_piece, board, temp_dict[key][1], temp_dict[key][2])
if found_down_piece:
temp_dict[count] = [found_down_piece[0], found_down_piece[1], found_down_piece[2]]
count += 1
found = True
for key in range(0, count):
found_left_piece = search_left(enemy_piece, board, temp_dict[key][1], temp_dict[key][2])
if found_left_piece:
temp_dict[count] = [found_left_piece[0], found_left_piece[1], found_left_piece[2]]
count += 1
found = True
for key in range(0, count):
found_up_piece = search_up(enemy_piece, board, temp_dict[key][1], temp_dict[key][2])
if found_up_piece:
temp_dict[count] = [found_up_piece[0], found_up_piece[1], found_up_piece[2]]
count += 1
found = True
def search_right(enemy_piece, board, vertical_index, horizontal_index):
found_piece = board[vertical_index][horizontal_index+1]
if found_piece == enemy_piece:
return found_piece # THIS WILL BE ADDED TO OUR DICTIONARY
def search_down(enemy_piece, board, vertical_index, horizontal_index):
found_piece = board[vertical_index-1][horizontal_index]
if found_piece == enemy_piece:
return found_piece # THIS WILL BE ADDED TO OUR DICTIONARY
def search_left(enemy_piece, board, vertical_index, horizontal_index):
found_piece = board[vertical_index][horizontal_index-1]
if found_piece == enemy_piece:
return found_piece # THIS WILL BE ADDED TO OUR DICTIONARY
def search_up(enemy_piece, board, vertical_index, horizontal_index):
found_piece = board[vertical_index+1][horizontal_index]
if found_piece == enemy_piece:
return found_piece # THIS WILL BE ADDED TO OUR DICTIONARY
def mouse_processing():
board_position_x = "no click"
board_position_y = "no click"
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mouse_position = pygame.mouse.get_pos()
board_position_x = mouse_position[0] / 50
board_position_y = mouse_position[1] / 50
print "\nHORIZONTAL" + str(board_position_x)
print "VERTICAL" + str(board_position_y)
break
else:
pass
return board_position_x, board_position_y
def circle_generate(screen, position_x, position_y, player, board):
if player == 1:
colour = (0,0,0)
else:
colour = (255,255,255)
for row in board
for i in row
if i[0] = 1 or i[0] = 2:
pygame.draw.circle(screen, colour, (25 + position_x * 50, 25 + position_y * 50), 25, 0)
#if player == 1:
# colour = (0,0,0)
#else:
# colour = (255,255,255)
#pygame.draw.circle(screen, colour, (25 + position_x * 50, 25 + position_y * 50), 25, 0)
def main():
pygame.init()
screen = pygame.display.set_mode((950,950))
go_graph_board = pygame.image.load('Blank_Go_board.png')
stop_play = False
board = make_board()
turn = 0
screen.blit(go_graph_board, (0, 0))
while not stop_play:
pygame.display.update()
player = player_move(turn)
position_x, position_y = mouse_processing()
if position_x == "no click":
continue
else:
circle_generate(screen, position_x, position_y, player, board)
board[position_y][position_x][0] = player
#print "TURN: {}".format(turn)
pygame.display.update()
gameplay_logic(board, player)
print_board(board)
print "\n\n"
turn += 1
print turn
print player
main()