【发布时间】:2020-11-26 14:17:22
【问题描述】:
以下是用于使用 python 制作跳棋 AI 的游戏类的代码,我不确定解决方案是什么,因为我已经尝试过推荐的方法。任何解决方案将不胜感激
import pygame
from constants import RED, WHITE, BLUE, SQUARE_SIZE
from board import Board
class Game: # game class allows you to interface with the board and the pieces. it consists of a few methods
def __init__(self, win):
self._init()
self.win = win
def update(self):
self.board.draw(self.win)
self.draw_valid_moves(self.valid_moves)
pygame.display.update()
def _init(self):
self.selected = None
self.board = Board()
self.turn = RED
self.valid_moves = {}
def winner(self):
return self.board.winner()
def reset(self): # reset method to reset
self._init()
def select(self, row, col): # if i haave selected something then this method will try and move what i have selected to the row and colimn selected. this select metho
if self.selected: # pressed piece will move to row/ column passed
result = self._move(row, col)
if not result: # if the row oclumn was valid, as i already have something slected, then it will be moved, if not i will be able to try and select a different piece
self.selected = None
self.select(row, col) # call method again
piece = self.board.get_piece(row, col)
if piece != 0 and piece.color == self.turn:
self.selected = piece
self.valid_moves = self.board.get_valid_moves(piece)
return True
return False
def _move(self, row, col):# move method
piece = self.board.get_piece(row, col)
if self.selected and piece == 0 and (row, col) in self.valid_moves:# cannot move into a postioon that has another piece checks to see if piece sleected ( only if we have selcted sometihng and what we have sleceted is not another piece then move can be moved/ can move)
self.board.move(self.selected, row, col)#movw selected peice to row and column
skipped = self.valid_moves[(row, col)]
if skipped:
self.board.remove(skipped)
self.change_turn()
else:
return False
return True
def draw_valid_moves(self, moves):#check all of the diagonals of where that checkers piece is. draw squares for all of these valid moves. to do this the row/ column need to be identiified so that a circle can be drawn for each move
for move in moves: # loops through all the keys of the dictionary which is the row/ columns indenitidied as a tuple
row, col = move # all moves are dictionary,
pygame.draw.circle(self.win, BLUE,
(col * SQUARE_SIZE + SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)
def change_turn(self): # change turn of players
self.valid_moves = {}
if self.turn == RED:
self.turn = WHITE
else:
self.turn = RED
# in order to implement the AI the game and board class are the most funcdemental aspects, this is because when implementing where the AI can actually move from a certain postion to do this a board needs to be established to score/ eveaulate the postions and know all the piece that could potenitally move
def get_board(self):
return self.board
def ai_move(self, board):
self.board = board
self.change_turn()
【问题讨论】:
标签: python pygame youtube artificial-intelligence minimax