def __init__(self): """Initialize Pygame""" pygame.init() # This initializes pygame and makes the submodules available. """Window Caption""" pygame.display.set_caption('Simple Game of Checkers') """Initialize game for automated play""" #self.black_player = ComputerPlayer() #self.white_player = ComputerPlayer() self.black_player = SimplePlayer() self.white_player = SimplePlayer() self.auto_game = Game(self.black_player, self.white_player) """All checkerboard squares and 24 pieces are initially added to this container when the board is drawn. Track and update all black, red checkboard spaces and checker pieces that have changed. """ self.black_spaces = pygame.sprite.RenderUpdates() self.red_spaces = pygame.sprite.RenderUpdates() self.pieces = pygame.sprite.RenderUpdates() # Both red and black pieces. self.game_on = True self.turn = 'black' self.pos_counter = collections.Counter()
def train_net_vs_simple(net_player): net_player.color = PlayerColor.BLUE players = { PlayerColor.BLUE: net_player, PlayerColor.RED: SimplePlayer(PlayerColor.RED) } global_map, players, fights, winning_player = game.init_game( map_filename, players) winner, global_map = game.run_game(False, global_map, players, fights, winning_player) owned_nodes = 0 owned_buildings = 0 owned_units = 0 for row in global_map: for loc in row: if loc and loc.unit_in_loc and loc.unit_in_loc.owner is net_player.color: owned_units += 1 if isinstance(loc, Node): if loc.owner is net_player.color: owned_nodes += 1 if not loc.building.is_empty(): owned_buildings += 1 net_player.g.fitness = owned_units + 10 * owned_nodes + 20 * owned_buildings if winner is net_player.color: net_player.g.fitness += 10000 print("Genome: ", net_player.g.key, " Fitness: ", net_player.g.fitness)
def __init__(self, session, playList, playIdx=0, playAll=False, listTitle=None, plType='local', title_inr=0, showPlaylist=True): print "YoutubePlayer:" SimplePlayer.__init__(self, session, playList, playIdx=playIdx, playAll=playAll, listTitle=listTitle, plType=plType, title_inr=title_inr, ltype='youtube', showPlaylist=showPlaylist)
def __init__(self): """Initialize Pygame""" pygame.init( ) # This initializes pygame and makes the submodules available. """Window Caption""" pygame.display.set_caption('Simple Game of Checkers') """Initialize game for automated play""" #self.black_player = ComputerPlayer() #self.white_player = ComputerPlayer() self.black_player = SimplePlayer() self.white_player = SimplePlayer() self.auto_game = Game(self.black_player, self.white_player) """All checkerboard squares and 24 pieces are initially added to this container when the board is drawn. Track and update all black, red checkboard spaces and checker pieces that have changed. """ self.black_spaces = pygame.sprite.RenderUpdates() self.red_spaces = pygame.sprite.RenderUpdates() self.pieces = pygame.sprite.RenderUpdates( ) # Both red and black pieces. self.game_on = True self.turn = 'black' self.pos_counter = collections.Counter()
def __init__(self, session, playList, playIdx=0, playAll=False, listTitle=None, plType='local', title_inr=0, showPlaylist=True, showCover=False, useResume=False): SimplePlayer.__init__(self, session, playList, playIdx=playIdx, playAll=playAll, listTitle=listTitle, plType=plType, title_inr=title_inr, ltype='youtube', showPlaylist=showPlaylist, cover=showCover, useResume=useResume, forceGST=True)
def __init__(self, session, playList): print "PlayRtmpPlayer:" SimplePlayer.__init__(self, session, playList, showPlaylist=False)
import game from util import PlayerColor from simpleplayer import SimplePlayer from smartplayer import SmartPlayer from nnplayer import NNetPlayer from humanplayer import HumanPlayerInput blue_player = NNetPlayer.create_feedforward_from_pickle( "best-vs-self-feedforward.pickle", "config.feedforward.txt", PlayerColor.BLUE) red_player = SimplePlayer(PlayerColor.RED) map_files = ["map_0.txt"] show_window = True run_count = 1 wins = {PlayerColor.BLUE: 0, PlayerColor.RED: 0} def main(): winning_players = [] players = {PlayerColor.BLUE: blue_player, PlayerColor.RED: red_player} for map_iter in map_files: global_map, players, fights, winning_player = game.init_game( map_iter, players) for _ in range(run_count): winner, global_map = game.run_game(show_window, global_map, players, fights, winning_player) winning_players.append(winner) wins[winner] += 1 print(wins) print(winning_players)
class CheckersMain(object): """This class handles the main initialization and creation of the game. A Pygame window, or screen, is described by pixel width and pixel height. It is a Pygame Surface object. Surfaces are images on the screen. """ def __init__(self): """Initialize Pygame""" pygame.init( ) # This initializes pygame and makes the submodules available. """Window Caption""" pygame.display.set_caption('Simple Game of Checkers') """Initialize game for automated play""" #self.black_player = ComputerPlayer() #self.white_player = ComputerPlayer() self.black_player = SimplePlayer() self.white_player = SimplePlayer() self.auto_game = Game(self.black_player, self.white_player) """All checkerboard squares and 24 pieces are initially added to this container when the board is drawn. Track and update all black, red checkboard spaces and checker pieces that have changed. """ self.black_spaces = pygame.sprite.RenderUpdates() self.red_spaces = pygame.sprite.RenderUpdates() self.pieces = pygame.sprite.RenderUpdates( ) # Both red and black pieces. self.game_on = True self.turn = 'black' self.pos_counter = collections.Counter() def setup_the_checkerboard(self): """Create the screen using the disply module and the width-height arguments""" self.screen = pygame.display.set_mode( (600, 600), RESIZABLE ) # The main display, or surface, of the terminal window size. self.tile_width = 75 # The size if the checkboard square. """Fill the background.""" self.background = pygame.Surface( self.screen.get_size()) # Size of screen surface self.background.fill( (255, 255, 255)) # Fill background surface. This RGB is white. self.background = self.background.convert( ) # Convert surface to make blitting faster """Set up the checkerboard.""" for row in range(8): # 0 thru 7 for col in range(8): top = self.tile_width * row # Pixel position. Row/top is the y coordinate. Start is (0,0), (0,75) etc. Down. left = self.tile_width * col # Pixel position. Column/left is the x coordinate. Start is (0,0), (75,0) if not (row % 2) and ( col % 2 ): # Initial combo is (0,1). The nested loop will layout by row. self.black_spaces.add( CheckerBoard((left, top), "black", row, col) ) # Track all black spaces by adding instances to the RenderUpdates() class. elif not (row % 2) and not (col % 2): # Initial combo is (1, 1) self.red_spaces.add( CheckerBoard((left, top), "red", row, col)) elif (row % 2) and not (col % 2): # Initial combo is (1, 0) self.black_spaces.add( CheckerBoard((left, top), "black", row, col)) elif (row % 2) and (col % 2): # Initial combo is (0, 0) self.red_spaces.add( CheckerBoard((left, top), "red", row, col)) def setup_checker_pieces(self, checkers): """Set up the checker pieces.""" #for row in range(8): # 0 thru 7 # for col in range(8): # This inner loop will go through it's iterations first. (0,0),(0,1),(0,2) etc. # if row < 3: # If y is 0, 1 or 2 # player = "red" # elif row > 4: # If y is 5, 6, or 7 # player = "black" # if row < 3 or row > 4: # top = self.tile_width*row # (0, 75, 150, 225, etc. as the x-axis.) # left = self.tile_width*col # (0, 75, 150, 225, etc. as the y-axix.) # if not(row % 2) and (col % 2): # (0, 1) # pieces.add(CheckerPieces(player,(left+(self.tile_width/2), top+(self.tile_width/2)))) # Track all checker pieces by adding instances to the RenderUpdates() class. This determines onto which squares the pieces are drawn and tracked and the center x and y. # elif (row % 2) and not(col % 2): # (1, 0) # pieces.add(CheckerPieces(player,(left+(self.tile_width/2), top+(self.tile_width/2)))) # At this point 24 checker pieces are in RenderUpdates() for checker in checkers: if checker.color == 'black': player = "black" else: player = "red" top = checker.position[ 0] * self.tile_width # (0, 75, 150, 225, etc. as the x-axis.) left = checker.position[ 1] * self.tile_width # (0, 75, 150, 225, etc. as the y-axix.) self.pieces.add( CheckerPieces(player, (left + (self.tile_width / 2), top + (self.tile_width / 2)), checker.king)) def mainloop(self): """Set up the checkerboard.""" self.setup_the_checkerboard() """Set up the checker pieces.""" self.setup_checker_pieces(self.black_player.checkers) self.setup_checker_pieces(self.white_player.checkers) """Blit the checkerboard and pieces to Surface object, i.e. the screen, so they appear.""" self.screen.blit(self.background, (0, 0)) # pygame.display.flip() # update drawing # """Manage the sprite you are controlling by putting into GroupSingle() # This allows the ability to manage a single sprite. # """ # piece_selected = pygame.sprite.GroupSingle() # GroupSingle() holds a single sprite at a time. It can be None. # space_selected = pygame.sprite.GroupSingle() # The black space where the piece has moved to. # currentpiece_position = (0,0) # Initial piece position. """Begins the event handler loop which is queue for all events""" while True: for event in pygame.event.get( ): # return events in the queue. Executes event handling code since the last time the function was called. if event.type == QUIT: # Quit if the close button is clicked. pygame.QUIT worked. QUIT is a contstant in .locals pygame.quit( ) # This is a opposite of the init() function. This is here to accomodate a bug which hands IDLE sys.exit() """Remove all events from the queue. Clear screen""" self.pieces.clear(self.screen, self.background) self.black_spaces.clear(self.screen, self.background) self.red_spaces.clear(self.screen, self.background) """Draw images using RenderUpdates() draw method""" self.black_spaces.draw( self.screen) # Draws changed sprites to the screen. self.red_spaces.draw(self.screen) self.pieces.draw(self.screen) """Refresh the screen""" # pygame.display.flip() pygame.display.update() sleep(2) if self.game_on: # Get hash value of all checkers' positions ch_black_pos = [ ch.position for ch in self.black_player.checkers ] ch_white_pos = [ ch.position for ch in self.white_player.checkers ] checkers_hash = hash(tuple(ch_black_pos + ch_white_pos)) # Game ends in draw if all checkers' positions repeat 4 times print('len(game.pos_counter) = {}'.format(len( self.pos_counter))) print('checkers_hash = {}'.format(checkers_hash)) self.pos_counter.update([checkers_hash]) print('self.pos_counter[checkers_hash] = {}'.format( self.pos_counter[checkers_hash])) if self.pos_counter[checkers_hash] >= 4: print('The game is a draw due to repeating positions') self.game_on = False if self.turn == 'black': if self.black_player.play() == 'surrender': msg = 'Black surrenders' self.game_on = False else: msg = 'Black move complete' self.turn = 'white' else: if self.white_player.play() == 'surrender': msg = 'White surrenders' self.game_on = False else: msg = 'White move complete' self.turn = 'black' print(msg) self.pieces = pygame.sprite.RenderUpdates() self.setup_checker_pieces(self.black_player.checkers) self.setup_checker_pieces(self.white_player.checkers) """Finish game""" pygame.quit()
self.white_player.color = u'white' self.white_player.checkerboard = self.chb self.white_player.checkers = self.chb.white_checkers self.pos_counter = collections.Counter() def start_game(): u""" Begin play """ pass if __name__ == u'__main__': #black_player = ComputerPlayer() logger.setLevel(logging.INFO) black_player = SimplePlayer() white_player = SimplePlayer() game = Game(black_player, white_player) game.chb.print_board() print u'Starting game\n' turn = u'black' game_on = True while game_on: # Get hash value of all checkers' positions ch_black_pos = [ch.position for ch in black_player.checkers] ch_white_pos = [ch.position for ch in white_player.checkers] checkers_hash = hash(tuple(ch_black_pos + ch_white_pos))
def __init__(self, session, playList): print "PlayHttpPlayer:" SimplePlayer.__init__(self, session, playList, showPlaylist=False, ltype='playhttp')
class CheckersMain(object): """This class handles the main initialization and creation of the game. A Pygame window, or screen, is described by pixel width and pixel height. It is a Pygame Surface object. Surfaces are images on the screen. """ def __init__(self): """Initialize Pygame""" pygame.init() # This initializes pygame and makes the submodules available. """Window Caption""" pygame.display.set_caption('Simple Game of Checkers') """Initialize game for automated play""" #self.black_player = ComputerPlayer() #self.white_player = ComputerPlayer() self.black_player = SimplePlayer() self.white_player = SimplePlayer() self.auto_game = Game(self.black_player, self.white_player) """All checkerboard squares and 24 pieces are initially added to this container when the board is drawn. Track and update all black, red checkboard spaces and checker pieces that have changed. """ self.black_spaces = pygame.sprite.RenderUpdates() self.red_spaces = pygame.sprite.RenderUpdates() self.pieces = pygame.sprite.RenderUpdates() # Both red and black pieces. self.game_on = True self.turn = 'black' self.pos_counter = collections.Counter() def setup_the_checkerboard(self): """Create the screen using the disply module and the width-height arguments""" self.screen = pygame.display.set_mode((600, 600), RESIZABLE) # The main display, or surface, of the terminal window size. self.tile_width = 75 # The size if the checkboard square. """Fill the background.""" self.background = pygame.Surface(self.screen.get_size()) # Size of screen surface self.background.fill((255,255,255)) # Fill background surface. This RGB is white. self.background = self.background.convert() # Convert surface to make blitting faster """Set up the checkerboard.""" for row in range(8): # 0 thru 7 for col in range(8): top = self.tile_width*row # Pixel position. Row/top is the y coordinate. Start is (0,0), (0,75) etc. Down. left = self.tile_width*col # Pixel position. Column/left is the x coordinate. Start is (0,0), (75,0) if not(row % 2) and (col % 2): # Initial combo is (0,1). The nested loop will layout by row. self.black_spaces.add(CheckerBoard((left,top),"black", row, col)) # Track all black spaces by adding instances to the RenderUpdates() class. elif not(row % 2) and not(col % 2): # Initial combo is (1, 1) self.red_spaces.add(CheckerBoard((left,top),"red", row, col)) elif (row % 2) and not(col % 2): # Initial combo is (1, 0) self.black_spaces.add(CheckerBoard((left,top),"black", row, col)) elif (row % 2) and (col % 2): # Initial combo is (0, 0) self.red_spaces.add(CheckerBoard((left,top),"red", row, col)) def setup_checker_pieces(self, checkers): """Set up the checker pieces.""" #for row in range(8): # 0 thru 7 # for col in range(8): # This inner loop will go through it's iterations first. (0,0),(0,1),(0,2) etc. # if row < 3: # If y is 0, 1 or 2 # player = "red" # elif row > 4: # If y is 5, 6, or 7 # player = "black" # if row < 3 or row > 4: # top = self.tile_width*row # (0, 75, 150, 225, etc. as the x-axis.) # left = self.tile_width*col # (0, 75, 150, 225, etc. as the y-axix.) # if not(row % 2) and (col % 2): # (0, 1) # pieces.add(CheckerPieces(player,(left+(self.tile_width/2), top+(self.tile_width/2)))) # Track all checker pieces by adding instances to the RenderUpdates() class. This determines onto which squares the pieces are drawn and tracked and the center x and y. # elif (row % 2) and not(col % 2): # (1, 0) # pieces.add(CheckerPieces(player,(left+(self.tile_width/2), top+(self.tile_width/2)))) # At this point 24 checker pieces are in RenderUpdates() for checker in checkers: if checker.color == 'black': player = "black" else: player = "red" top = checker.position[0] * self.tile_width # (0, 75, 150, 225, etc. as the x-axis.) left = checker.position[1] * self.tile_width # (0, 75, 150, 225, etc. as the y-axix.) self.pieces.add(CheckerPieces(player,(left+(self.tile_width/2), top+(self.tile_width/2)), checker.king)) def mainloop(self): """Set up the checkerboard.""" self.setup_the_checkerboard() """Set up the checker pieces.""" self.setup_checker_pieces(self.black_player.checkers) self.setup_checker_pieces(self.white_player.checkers) """Blit the checkerboard and pieces to Surface object, i.e. the screen, so they appear.""" self.screen.blit(self.background, (0,0)) # pygame.display.flip() # update drawing # """Manage the sprite you are controlling by putting into GroupSingle() # This allows the ability to manage a single sprite. # """ # piece_selected = pygame.sprite.GroupSingle() # GroupSingle() holds a single sprite at a time. It can be None. # space_selected = pygame.sprite.GroupSingle() # The black space where the piece has moved to. # currentpiece_position = (0,0) # Initial piece position. """Begins the event handler loop which is queue for all events""" while True: for event in pygame.event.get(): # return events in the queue. Executes event handling code since the last time the function was called. if event.type == QUIT: # Quit if the close button is clicked. pygame.QUIT worked. QUIT is a contstant in .locals pygame.quit() # This is a opposite of the init() function. This is here to accomodate a bug which hands IDLE sys.exit() """Remove all events from the queue. Clear screen""" self.pieces.clear(self.screen, self.background) self.black_spaces.clear(self.screen, self.background) self.red_spaces.clear(self.screen, self.background) """Draw images using RenderUpdates() draw method""" self.black_spaces.draw(self.screen) # Draws changed sprites to the screen. self.red_spaces.draw(self.screen) self.pieces.draw(self.screen) """Refresh the screen""" # pygame.display.flip() pygame.display.update() sleep(2) if self.game_on: # Get hash value of all checkers' positions ch_black_pos = [ch.position for ch in self.black_player.checkers] ch_white_pos = [ch.position for ch in self.white_player.checkers] checkers_hash = hash(tuple(ch_black_pos + ch_white_pos)) # Game ends in draw if all checkers' positions repeat 4 times print('len(game.pos_counter) = {}'.format(len(self.pos_counter))) print('checkers_hash = {}'.format(checkers_hash)) self.pos_counter.update([checkers_hash]) print('self.pos_counter[checkers_hash] = {}'.format(self.pos_counter[checkers_hash])) if self.pos_counter[checkers_hash] >= 4: print('The game is a draw due to repeating positions') self.game_on = False if self.turn == 'black': if self.black_player.play() == 'surrender': msg = 'Black surrenders' self.game_on = False else: msg = 'Black move complete' self.turn = 'white' else: if self.white_player.play() == 'surrender': msg = 'White surrenders' self.game_on = False else: msg = 'White move complete' self.turn = 'black' print(msg) self.pieces = pygame.sprite.RenderUpdates() self.setup_checker_pieces(self.black_player.checkers) self.setup_checker_pieces(self.white_player.checkers) """Finish game""" pygame.quit()