def play_game(gameName, p1Name, p2Name, maxExpansions, p1alphabeta, \ p2alphabeta): """Plays a game. "gameName" is the name of the Python module, in the working directory or on the path, containing the GameState subclass we want and a make_state() function that returns an opening state object. "p1Name" is the name of the first player's module, which is in the "players/[gameName]" subdirectory. This module must contain a player class derived from GamePlayer and a make_player() function that returns an instance of this class. e.g., the simple TicTacToe agent lives in the players/tictactoe/tictactoe_simple.py file, which contains the TicTacToePlayer class defining the agent. The value of this argument is "tictactoe_simple". "p2Name" is the name of the second player's module, which works exactly the same as the first player's (and can in fact be the same module) "maxExpansions" is the maximum number of game-tree expansions to allow each player as they search during one turn. "p*alphabeta" is a boolean indicating whether alpha-beta is used or not for player1 and player 2, respectively.""" wd = os.getcwd() # Load game, player modules gameMod = load_module(gameName.lower(), None, wd) p1Mod = load_module(p1Name, os.path.join(PLAYER_PATH, gameName.lower()), wd) p2Mod = load_module(p2Name, os.path.join(PLAYER_PATH, gameName.lower()), wd) if gameMod == None or p1Mod == None or p2Mod == None: sys.exit(2) # Load game, player classes state = call_name(gameMod, "make_state") if state == None: sys.exit(2) gameIDs = state.get_players() p1 = call_name(p1Mod, "make_player", gameIDs[0]) p2 = call_name(p2Mod, "make_player", gameIDs[1]) if p1 == None or p2 == None: sys.exit(2) fn1 = game_controller.ALPHA_BETA if p1alphabeta \ else game_controller.MINIMAX fn2 = game_controller.ALPHA_BETA if p2alphabeta \ else game_controller.MINIMAX p1 = game_controller.PlayerInfo(p1,p1Name,fn1) p2 = game_controller.PlayerInfo(p2,p2Name,fn2) # Create a game controller try: gm = game_controller.GameController(state, [p1,p2], maxExpansions, wd) except game_controller.PlayerException, e: print "Player ID not covered!" print e sys.exit(3)
def main(): print('---------------------------------------------') print('Welcome to street craps!') print() print(""" Rules: If you roll 7 or 11 on your first roll, you win. If you roll 2, 3, or 12 on your first role, you lose. If you roll anything else, that's your 'point', and you keep rolling until you either roll your point again (win) or roll a 7 (lose) """) gc = game_controller.GameController() while True: if (not gc.play()): break
async def _game_loop(randezvous): logger = logging.getLogger("game") game = None while True: message = await randezvous.game_queue.get() if message == _START_GAME: logger.info(f"Starting the game between %s and %s!" % (randezvous.player1.name, randezvous.player2.name)) game = game_controller.GameController(_NUM_ROWS, _NUM_COLS, randezvous.player1.name, randezvous.player2.name) views = game.initial_views() await randezvous.player1.enqueue_message( views[randezvous.player1.name]) await randezvous.player2.enqueue_message( views[randezvous.player2.name]) continue (player, move) = message if not game: await player.enqueue_message( "The game has not started yet. Still waiting for the second player...\n" ) continue logger.info("handling \"%s\" from %s", move, player.name) try: views = game.play(player.name, move) if views.get(randezvous.player1.name): await randezvous.player1.enqueue_message( views[randezvous.player1.name]) if views.get(randezvous.player2.name): await randezvous.player2.enqueue_message( views[randezvous.player2.name]) if views[game_controller.GAME_OVER_KEY]: randezvous.reset_game() return except Exception as e: logger.error("%s's %s failed with: %s", player.name, move, e)
def go_to_cygnus(): game = game_controller.GameController() # イージーシグナスへ移動 game.active_game_window() time.sleep(0.1) game.send_key('T') time.sleep(0.3) img = game.take_png_screenshot() center_point = game.template_match("Screenshot_152.png", img) if center_point is None: return game.send_click(center_point) time.sleep(0.1) game.send_click(center_point) time.sleep(0.1) game.send_click(center_point) time.sleep(0.1) game.send_click([center_point[0] - 185, center_point[1]]) time.sleep(0.1) game.send_click([center_point[0] - 25, center_point[1] + 265]) time.sleep(3) # グループ申請 game.send_key('7') time.sleep(0.1) group_ss = game.take_png_screenshot() find_group_point = game.template_match("Screenshot_157.png", group_ss) if find_group_point is None: return game.send_click(find_group_point) time.sleep(0.2) group_list_ss = game.take_png_screenshot() my_group_point = game.template_match("Screenshot_159.png", group_list_ss) if my_group_point is None: return game.send_click([my_group_point[0] + 160, my_group_point[1]]) time.sleep(0.3) game.send_key(win32con.VK_RETURN)
import game_controller as gc import time if __name__ == '__main__': game = gc.GameController() while True: print('========init game========') game.init_game() # 初始化游戏 print('========bet time========') time.sleep(game.bet_time) # 下注时间 print('========start game========') game.start_game() # 开始游戏 print('========reward time========') game.reward() # 分配奖励 print('========save game========') game.next_iteration() # 保存游戏状态 print('========cold time========') time.sleep(game.cold_time) # 冷却时间 print('========clear game========') game.clear_game() # 重置游戏相关数据
def play_tournament(gameName, exclusions, maxExpansions, quiet): """Runs a tournament between all the game players it can find for the indicated game. "gameName" is as for play_game() above. "exclusions" is a list of strings indicating player modules to leave out of the tournament (useful for excluding human-interaction modules). "maxExpansions" is as for play_game() above. "quiet" indicates that the program should refrain from outputting each and every game state as games are played, if True.""" wd = os.getcwd() # Load game module gameMod = load_module(gameName.lower(), None, wd) if gameMod == None: sys.exit(2) # Instantiate game class & get player IDs state = call_name(gameMod, "make_state") if state == None: sys.exit(2) playerIDs = state.get_players() # Get a list of player modules and try to load them playerNames = os.listdir(os.path.join(PLAYER_PATH, gameName.lower())) playerNames = [x[:-3] for x in playerNames if x.endswith('.py')] playerNames = [x for x in playerNames if x not in exclusions] playerMods = [load_module(x, \ os.path.join(PLAYER_PATH, gameName.lower()), wd) \ for x in playerNames] if None in playerMods: sys.exit(2) # Instantiate a player-1 instance and a player-2 instance of every player players = [(call_name(x, "make_player", playerIDs[0]), \ call_name(x, "make_player", playerIDs[1])) \ for i,x in enumerate(playerMods)] # Cut down the name, module lists to successfully-instantiated players playerNames = [x for i,x in enumerate(playerNames) \ if players[i][0] != None and players[i][1] != None] playerMods = [x for i,x in enumerate(playerMods) \ if players[i][0] != None and players[i][1] != None] players = [x for x in players if x[0] != None and x[1] != None] players = [(game_controller.PlayerInfo(ps[0],name,game_controller.TOURN), \ game_controller.PlayerInfo(ps[1],name,game_controller.TOURN)) \ for (ps,name) in zip(players,playerNames)] print players assert(len(players) > 1) # Player scores are all 0 to begin playerScores = [0 for x in players] # Create a game controller try: gm = game_controller.GameController(state, \ [players[0][0],players[1][1]], \ maxExpansions, wd) except game_controller.PlayerException, e: print "Player ID not covered!" print e sys.exit(3)
import sys import pygame import game_controller pygame.init() game_controller = game_controller.GameController() clock = pygame.time.Clock() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: game_controller.handle_mouse_button_down(event.pos, event.button) elif event.type == pygame.MOUSEBUTTONUP: game_controller.handle_mouse_button_up(event.pos, event.button) game_controller.check_state() time = clock.tick(15) pygame.display.update()