def AI_drop(self): point = None score = 0 for i in range(self._line_points): for j in range(self._line_points): if self._checkerboard[j][i] == 0: _score = self._get_point_score(Point(i, j)) if _score > score: score = _score point = Point(i, j) elif _score == score and _score > 0: r = random.randint(0, 100) if r % 2 == 0: point = Point(i, j) self._checkerboard[point.Y][point.X] = self._my.Value return point
def _get_clickpoint(click_pos): pos_x = click_pos[0] - Start_X pos_y = click_pos[1] - Start_Y if pos_x < -Inside_Width or pos_y < -Inside_Width: return None x = pos_x // SIZE y = pos_y // SIZE if pos_x % SIZE > Stone_Radius: x += 1 if pos_y % SIZE > Stone_Radius: y += 1 if x >= Line_Points or y >= Line_Points: return None return Point(x, y)
def _get_stone_color(self, point, x_offset, y_offset, next): x = point.X + x_offset y = point.Y + y_offset if 0 <= x < self._line_points and 0 <= y < self._line_points: if self._checkerboard[y][x] == self._my.Value: return 1 elif self._checkerboard[y][x] == self._opponent.Value: return 2 else: if next: return self._get_stone_color(Point(x, y), x_offset, y_offset, False) else: return 0 else: return 0
def _get_clickpoint(click_pos): pos_x = click_pos[0] - start_X pos_y = click_pos[1] - start_Y if pos_x < -inside_width or pos_y < -inside_width: return None x = pos_x // size y = pos_y // size if pos_x % size > Piece_Radius: x += 1 if pos_y % size > Piece_Radius: y += 1 if x >= num_points or y >= num_points: return None return Point(x, y)
def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('五子棋') font1 = pygame.font.SysFont('SimHei', 36) font2 = pygame.font.SysFont('SimHei', 72) fwidth, fheight = font2.size('黑方获胜') checkerboard = Checkerboard(Line_Points) cur_runner = BLACK_CHESSMAN winner = None while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_RETURN: if winner is not None: winner = None cur_runner = BLACK_CHESSMAN checkerboard = Checkerboard(Line_Points) elif event.type == MOUSEBUTTONDOWN: if winner is None: pressed_array = pygame.mouse.get_pressed() if pressed_array[0]: mouse_pos = pygame.mouse.get_pos() click_point = _get_clickpoint(mouse_pos) if click_point is not None: if checkerboard.can_drop(click_point): winner = checkerboard.drop( cur_runner, click_point) if cur_runner == BLACK_CHESSMAN: cur_runner = WHITE_CHESSMAN else: cur_runner = BLACK_CHESSMAN else: print('超出棋盘区域') # 画棋盘 _draw_checkerboard(screen) # 画棋盘上已有的棋子 for i, row in enumerate(checkerboard.checkerboard): for j, cell in enumerate(row): if cell == BLACK_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color) elif cell == WHITE_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color) _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20), BLACK_STONE_COLOR) _draw_chessman_pos( screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20 + Stone_Radius2 * 3), WHITE_STONE_COLOR) if winner: print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, winner.Name + '获胜', RED_COLOR) if cur_runner == BLACK_CHESSMAN: print_text(screen, font1, RIGHT_INFO_POS_X, Start_X, '获胜' if winner else '落子中', BLUE_COLOR) else: print_text(screen, font1, RIGHT_INFO_POS_X, Start_X + Stone_Radius2 * 3, '获胜' if winner else '落子中', BLUE_COLOR) pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('五子棋') font1 = pygame.font.SysFont('SimHei', 32) font2 = pygame.font.SysFont('SimHei', 72) fwidth, fheight = font2.size('黑方获胜') checkerboard = Checkerboard(Line_Points) cur_runner = BLACK_CHESSMAN winner = None computer = AI(Line_Points, WHITE_CHESSMAN) black_win_count = 0 white_win_count = 0 while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_RETURN: if winner is not None: winner = None cur_runner = BLACK_CHESSMAN checkerboard = Checkerboard(Line_Points) computer = AI(Line_Points, WHITE_CHESSMAN) elif event.type == MOUSEBUTTONDOWN: if winner is None: pressed_array = pygame.mouse.get_pressed() if pressed_array[0]: mouse_pos = pygame.mouse.get_pos() click_point = _get_clickpoint(mouse_pos) if click_point is not None: if checkerboard.can_drop(click_point): winner = checkerboard.drop(cur_runner, click_point) if winner is None: cur_runner = _get_next(cur_runner) computer.get_opponent_drop(click_point) AI_point = computer.AI_drop() winner = checkerboard.drop(cur_runner, AI_point) if winner is not None: white_win_count += 1 cur_runner = _get_next(cur_runner) else: black_win_count += 1 else: print('超出棋盘区域') # 画棋盘 _draw_checkerboard(screen) # 画棋盘上已有的棋子 for i, row in enumerate(checkerboard.checkerboard): for j, cell in enumerate(row): if cell == BLACK_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color) elif cell == WHITE_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color) _draw_left_info(screen, font1, cur_runner, black_win_count, white_win_count) if winner: print_text(screen, font2, (SCREEN_WIDTH - fwidth)//2, (SCREEN_HEIGHT - fheight)//2, winner.Name + '获胜', RED_COLOR) pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Gomoku') # Font and font size font1 = pygame.font.SysFont('Arial', 35) font2 = pygame.font.SysFont('Arial', 65) fwidth, fheight = font2.size('Player Wins') # Draw the board and initiate the game checkerboard = Checkerboard(num_points) cur_runner = Black_Piece winner = None computer = AI(num_points, White_Piece) # Counting progress on who is winning black_win_count = 0 white_win_count = 0 # If the game is not over, then the following iteration goes while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_RETURN: if winner is not None: winner = None cur_runner = Black_Piece checkerboard = Checkerboard(num_points) computer = AI(num_points, White_Piece) elif event.type == MOUSEBUTTONDOWN: if winner is None: pressed_array = pygame.mouse.get_pressed() if pressed_array[0]: mouse_pos = pygame.mouse.get_pos() click_point = _get_clickpoint(mouse_pos) if click_point is not None: if checkerboard.can_drop(click_point): winner = checkerboard.drop( cur_runner, click_point) if winner is None: cur_runner = _get_next(cur_runner) computer.get_opponent_drop(click_point) AI_point = computer.AI_drop() winner = checkerboard.drop( cur_runner, AI_point) if winner is not None: white_win_count += 1 cur_runner = _get_next(cur_runner) else: black_win_count += 1 else: print('Out of Bound') # Draw the grid _draw_board(screen) # Draw the existing pieces on the grid for i, row in enumerate(checkerboard.checkerboard): for j, cell in enumerate(row): if cell == Black_Piece.Value: _draw_piece(screen, Point(j, i), Black_Piece.Color) elif cell == White_Piece.Value: _draw_piece(screen, Point(j, i), White_Piece.Color) _draw_left_info(screen, font1, cur_runner, black_win_count, white_win_count) # If a winner appears, then print the following txt. if winner: print_msg(screen, font2, (screen_width - fwidth) // 2, (screen_height - fheight) // 2, winner.Name + ' Wins', Red_Color) pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Gobang') font1 = pygame.font.SysFont('SimHei', 36) font2 = pygame.font.SysFont('SimHei', 72) fwidth, fheight = font2.size('Black Win') checkerboard = Checkerboard(Line_Points) # 白棋先走 cur_runner = WHITE_CHESSMAN winner = None while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: # 回车键重新开始 if event.key == K_RETURN: winner = None cur_runner = WHITE_CHESSMAN checkerboard = Checkerboard(Line_Points) elif event.type == MOUSEBUTTONDOWN: if winner is None: # pressed_array获得一个三元组(1, 0, 0)记录按下的是左键 pressed_array = pygame.mouse.get_pressed() # print(pressed_array, "pressed_array") if pressed_array[0]: # mouse_pos表示鼠标按下时相对于窗口的位置 mouse_pos = pygame.mouse.get_pos() # print(mouse_pos, "mouse_pos") # click_point表示鼠标按下时相对棋盘的落子位置(已经转化为具体坐标位置) click_point = _get_clickpoint(mouse_pos) # print(click_point, "click_point") if click_point is not None: if checkerboard.can_drop(click_point): winner = checkerboard.drop( cur_runner, click_point) if winner == None: if cur_runner == BLACK_CHESSMAN: cur_runner = WHITE_CHESSMAN else: cur_runner = BLACK_CHESSMAN # else: # print('超出棋盘区域') # 画棋盘 _draw_checkerboard(screen) # 画棋盘上已有的棋子 for i, row in enumerate(checkerboard.checkerboard): for j, cell in enumerate(row): if cell == BLACK_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color) elif cell == WHITE_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color) # 画状态栏棋子 _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20), BLACK_STONE_COLOR) _draw_chessman_pos( screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20 + Stone_Radius2 * 3), WHITE_STONE_COLOR) if winner: print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, winner.Name + 'Win', RED_COLOR) if cur_runner == BLACK_CHESSMAN: print_text(screen, font1, RIGHT_INFO_POS_X, Start_X, 'Win' if winner else 'ing...', BLUE_COLOR) else: print_text(screen, font1, RIGHT_INFO_POS_X, Start_X + Stone_Radius2 * 3, 'Win' if winner else 'ing...', BLUE_COLOR) pygame.display.flip()