def __init__(self, size=10): self.size = size board_1 = self.random_board() board_2 = self.random_board() board_2.hid = True self.user = User(board_1, board_2) self.ai = AI(board_2, board_1)
def _init_players(self, ai_plays: bool) -> Tuple[Player, Player]: choice = input("Do you want to start? Y/n [any key]. ") self.player1 = Human() if choice.lower() == 'y' \ or not ai_plays else AI() self.player2 = Human() if choice.lower() != 'y' \ or not ai_plays else AI() return (self.player1, self.player2) if choice.lower() == 'y' \ else (self.player2, self.player1)
def main(): game = Game() player1 = AI("daubajee", "X") player2 = AI("Chaku", "O") game.setPlayer1(player1) game.setPlayer2(player2) game.setFirstPlayer(player1) game.run()
def new_game_init(self): print "New Game initializing" self.game = Game() self.player1 = AI("artificial agent", "X", self) self.player2 = AI("artificial agent 2", "O", self) self.game.setPlayer1(self.player1) self.game.setPlayer2(self.player2) self.game.setFirstMover(self.player1) self.startGame = True self.playerHasMoved = False self.playerMove = None print "New Game started"
def game_loop(): player = Player() ai = AI() # MAIN LOOP playing = True while playing: player_turn = True while player_turn: print(f"""You: Castle: {player.castle} Food: {player.food} Stone: {player.stone} Villagers: {player.villagers} Warriors: {player.warriors} """) print(f"""Computer: Castle: {ai.castle} Food: {ai.food} Stone: {ai.stone} Villagers: {ai.villagers} Warriors: {ai.warriors} """) action = 0 action = int(input("1) Build\n2) Gather\n3) Recruit\n4) Attack\n")) if action == 1: player_turn = player.build() elif action == 2: player_turn = player.gather() elif action == 3: player_turn = player.recruit() elif action == 4: player_turn = player.attack(ai) else: playerTurn = True ai.make_move(player) if ai.castle <= 0: playing = False print("You Win!\n") elif player.castle <= 0: playing = False print("You Lose!") else: os.system('clear')
def play_ai(): table = Table() left_player = AI('left_pad') right_player = Model('right_pad') game = Game(table, left_player, right_player, max_score=100) game.play() print('winner:', game.left_score, game.right_score)
def main(): ge = [(g, e) for g in (.999, .95, .9, .7, .5) for e in (.999, .9, .95, .9, .7, .5)] for g, e in ge: t = time.time() table = Table() ql = QEpsilonGreedyPlayer(gamma=g, epsilon=e, position='left_pad') ai = AI() # qr = QEpsilonGreedyPlayer(gamma=g, epsilon=e, position='right_pad') max_score = 10 max_sets = 10000 stat_sets = [10, 30, 100, 300, 1000] display_sets = list(range(1000, max_sets + 1, 1000)) print('train: gamma=%f, epsilon=%f' % (g, e)) T = Train(table, ql, ai, max_score, max_sets, stat_sets, display_sets) T.train() d = time.time() - t m, s = divmod(d, 60) print('time elapsed: %s min %s sec' % (int(m), int(s))) play = input('play sample game? (y/n) ') if play in ('', 'y', 'Y'): T.play_game() cont = input('train next model? (y/n) ') if not cont in ('', 'y', 'Y'): break
def main(): g = 1 m = 50000 for lambda_ in (.5, .2, .9): for e in (.5, .9, .1): t = time.time() table = Table() ql = QLambda(gamma=g, epsilon=e, lambda_=lambda_, max_episodes=m, position='left_pad') ai = AI() # qr = QEpsilonGreedyPlayer(gamma=g, epsilon=e, position='right_pad') max_score = 10 max_sets = m // 10 stat_sets = [10, 30, 100, 300, 1000] display_sets = list(range(1000, max_sets + 1, 1000)) print('train: gamma=%f, epsilon=%f, lambda_=%f' % (g, e, lambda_)) T = Train(table, ql, ai, max_score, max_sets, stat_sets, display_sets) T.train() d = time.time() - t min, sec = divmod(d, 60) print('time elapsed: %s min %s sec' % (int(min), int(sec))) play = input('play sample game? (y/n) ') if play in ('', 'y', 'Y'): T.play_game() cont = input('train next model? (y/n) ') if not cont in ('', 'y', 'Y'): break
def main(argv): game = Game(Board(argv[0])) if (len(argv) == 1): player = Human(game) else: # try: # player = AI(game, argv[1:]) # except: # print("Invalid search parameters, please try again") # exit() player = AI(game, argv[1:]) player.play()
def main(): width, height = -1, -1 bad = lambda x: x != 3 and x != 4 while bad(width) or bad(height): height, width = [ int(x) for x in raw_input( "Enter height and width of game board: ").split() ] gb = GameBoard(height, width) p1 = Player() p2 = AI() ttoe = TicTacToe(gb, p1, p2) ttoe.play()
class Game: def __init__(self, size=10): self.size = size board_1 = self.random_board() board_2 = self.random_board() board_2.hid = True self.user = User(board_1, board_2) self.ai = AI(board_2, board_1) def create_ships(self): ships = (4, 3, 3, 2, 2, 2, 1, 1, 1, 1) board = Field(size=self.size) a = 0 for i in ships: while True: a += 1 if a > 2000: return None ship = Ship(Dot(randint(0, self.size), randint(0, self.size)), randint(1, 2), i) try: board.add_ship(ship) break except CannotPlaceShip: pass board.check() return board def random_board(self): board = None while board is None: board = self.create_ships() return board @staticmethod def logo(): print("___________________") print("| SEA BUTTLE |") print("|#################|") print("| Use 1 to 10 |") print("|_____to shoot____|") def loop(self): turns = 0 while True: print("User:"******"#" * 22) print("AI:") self.ai.board.show() if turns % 2 == 0: print("#" * 23) print("Make a turn") repeat = self.user.move() else: print("#" * 23) print("AI makes a turn") repeat = self.ai.move() if repeat: turns -= 1 if self.ai.board.alive == 0: print("#" * 23) print("You win!") break elif self.user.board.alive == 0: print("-" * 23) print("AI wins!") break turns += 1 def start(self): self.logo() self.loop()
def __init__(self): first = 'first' second = 'second' self.players = {first: Human(first), second: AI(second)} GameState.initPlayers(self.players)
#!/usr/bin/env python3 # Name: Shweta Jones(shsujone) # Date: June 7, 2020 #Main game code from board import Board from player import Player, AI, SmartAI print("Welcome to TIC-TAC-TOE Game!") while True: board = Board() #runs Board class, initializes values player1 = AI("Bob", "X", board) #provides player 1 information player2 = SmartAI("Alice", "O", board) turn = True board.show() #prints board which is empty while True: if turn: player1.choose( ) #gets input and updates board based on user input for player 1 turn = False else: player2.choose( ) #gets input and updates board based on user input for player 2 turn = True if board.isdone(): #when the board is break board.show() #print the board if board.get_winner() == player1.get_sign( ): #when the winner is the sign of player 1, winner displayed board.show() print(f"{player1.get_name()} is a winner!")
return num def check_directs(self,color,ind): directions=[(0,1),(1,-1),(1,0),(1,1)] max_x=1 for d in directions: x=self.check_direct(color,ind,d,1) if x==5: return 5 if x>max_x: max_x=x return max_x if __name__=='__main__': c=chess(15) w=AI(15,COLOR_WHITE,1000) b=AI(15,COLOR_BLACK,1000) for i in range(100): print("-------------------------------") b.go(c.board) tmp_b=b.candidate_list[-1] c.go(tmp_b,COLOR_BLACK) if c.win!=0: break w.go(c.board) tmp_w=w.candidate_list[-1] c.go(tmp_w,COLOR_WHITE) if c.win!=0: break c.show() #c.show()