def run_game(): print (welcome_message) print ("") print (instructions) choice=input("Pick H or T\n") while choice.upper() not in ['H', 'T']: choice=input("Invalid input. Please pick H or T\n") #flip a coin i=random.randint(0,1) winner='HT'[i] if winner==choice: turn='player' print ("Congratulations! You go first :)") else: turn='ai' print ("Sorry, you go second :(") #game starts game_ongoing=True game_state = game.GameState(turn) #create a new game while game_ongoing: if game_state.getTurn()=='ai': #AI's Turn ai_status=ai.play(game_state) if ai_status==game.PLAYER_LOST: print ("AI WON!") game_ongoing=False if ai_status==game.TIE: game_ongoing=False print ("TIE!") else: #Player Turn game_state.printBoard() #display board for player move=input("Please pick a tile\n") status=game.update(game_state, move) while status==game.NOT_VALID_MOVE: move=input("Invalid tile. Please pick a tile that isn't occupied\n") status=game.update(game_state, move) #picked a valid tile, game updated accordingly if status==game.PLAYER_WON: print ("CONGRATS! YOU WON!") game_ongoing=False if status==game.TIE: game_ongong=False print ("TIE!") game_state.printBoard() #display board for player
def play_game(config, size, candidate_color): game = new_game(config, size, candidate_color) board = game['board'] if candidate_color == 'b': print("You start to play !") else: print("The other side starts to play !") while True: print_board(board) move = ai.play(board, candidate_color) try: board = new_move(game, move) except GameOver as e: if e.winner == candidate_color: print('Game over: you win ! Congratulation !') elif e.winner == ' ': print('Game over: draw !') else: print('Game over: The other side wins !') return True except InvalidMoveException: return False
def playerpass(self): """ Allows a player to pass. If both players pass, ends the game """ if self.passCount == 0: self.passCount += 1 if self.turn: self.status.config(text="Black passes!") else: self.status.config(text="White passes!") self.turn = not self.turn if not self.turn and self.playComputer: #black passed, so white needs to know to go stateX, stateY = ai.play(copy.deepcopy(self.state)) if stateX == -1 and stateY == -1: self.playerpass() else: self.click(None, stateX, stateY) else: self.status.config(text="Game Over!") self.findscore()
def generate_random_game(rounds, bonus=False, verbatim=True): assert 0 < rounds <= TOTAL_TRICKS deck = [val+suit for val,suit in itertools.product(VALUES, SUITS)] random.shuffle(deck) players = [[], [], [], []] playergains = copy.deepcopy(players) global QScount global QSbombed global faroff global acceptable global perfect global tscr global avscr global starthandz global scorez for i in range(HANDSIZE): for j in range(PLAYERS): players[j].append(deck.pop()) prev_tricks = [] deck_top = [deck.pop()] if bonus: pred_scores = [] player_data = [] for i in range(PLAYERS): score = program.predict_score(players[i]) pred_scores.append(score) player_data.append(score) pred_scores = tuple(pred_scores) prev_winner = 0 QSstart = None for round_id in range(rounds): if round_id == 0: for hand in players: starthandz.append(copy.copy(hand)) for p in range(4): for card in players[p]: if card == 'QS': QSstart = p break curr_trick = [] for j in range(PLAYERS): player_id = (prev_winner + j) % PLAYERS if bonus: card, player_data[player_id] = program.play(curr_trick, players[player_id], prev_tricks, deck_top, player_data=player_data[player_id]) else: card = program.play(curr_trick, players[player_id], prev_tricks, deck_top, suppress_player_data=True) curr_trick.append(card) playergains[player_id].append(card) #print("card = {}".format(card)) players[player_id].remove(card) curr_trick = tuple(curr_trick) prev_tricks.append(curr_trick) curr_winner, _score = program.get_winner_score(curr_trick, round_id, deck_top) prev_winner = (curr_winner + prev_winner) % PLAYERS if round_id < PHASE_ONE_TRICKS: for j in range(PLAYERS): if j == prev_winner: players[j].append(deck_top[round_id]) else: players[j].append(deck.pop()) if deck: deck_top.append(deck.pop()) scores = program.score_game(prev_tricks, deck_top) if verbatim: if bonus: #print("PREDICTED SCORES: {}".format(pred_scores)) #print("ORIGINAL SCORES: {}".format(scores)) gains = [] rev_scores = [] for i in range(PLAYERS): diff = scores[i] - pred_scores[i] gains.append(diff) diff = abs(diff) if diff == 0: rev_scores.append(scores[i] - 10) perfect += 1 elif 1 <= diff <= 2: rev_scores.append(scores[i] - 2) acceptable += 1 elif 3 <= diff <= 4: rev_scores.append(scores[i]) acceptable += 1 elif 5 <= diff <= 6: rev_scores.append(scores[i] + 2) faroff += 1 elif 7 <= diff <= 8: rev_scores.append(scores[i] + 4) faroff += 1 else: rev_scores.append(scores[i] + 10) faroff += 1 scores = tuple(rev_scores) #print("ADJUSTED SCORES: {}".format(scores)) #print("DIFFERENCE SCORES: {}".format(tuple(gains))) else: #print("SCORES: {}".format(scores)) for scr in scores: scorez.append(scr) temp = 0 for score in scores: tscr += score temp += score avscr = (avscr + temp/4)/2 if not QSstart: QScount += 1 for p in range(4): for card in playergains[p]: if card == 'QS': if p != QSstart: QSbombed += 1 break
def click(self, event=None, cx=None, cy=None): """ Handles normal play Verifies that play is allowable Makes any possible captures Checks for Ko rule. This is not done in ValidPlay(), as we've got to make captures first, and there's no real reason to do that twice. Finish by redrawing board """ self.status.config(text="") if event == None: stateX = cx stateY = cy else: #figure out what square we clicked on x = (event.x + self.wf) - event.x % self.wf y = (event.y + self.hf) - event.y % self.hf stateX = event.x / self.wf stateY = event.y / self.hf possible, message = validPlay(copy.deepcopy(self.state), self.turn, stateX, stateY) self.status.config(text=message) if not possible: return False backup = copy.deepcopy(self.state) self.state[stateX][stateY] = 'b' if self.turn else 'w' #check for captures #store existing prisoners in case we need to roll back preb = self.bPrisoners prew = self.wPrisoners newState, possible, captures = captureHelper(copy.deepcopy(self.state), stateX, stateY, self.turn) if possible: self.state = copy.deepcopy(newState) if self.turn: self.bPrisoners += captures else: self.wPrisoners += captures self.score.config(text="B: %d W: %d" \ % (self.bPrisoners, self.wPrisoners)) #Check for ko if self.state == \ self.stateHistory[len(self.stateHistory) - 2]['state']: self.state = copy.deepcopy(backup) self.bPrisoners = preb self.wPrisoners = prew self.score.config(text="B: %d W: %d" \ % (self.bPrisoners, self.wPrisoners)) self.status.config(text="That would be a violation of Ko") return False self.turn = not self.turn self.lastX = stateX self.lastY = stateY self.stateHistory.append({'state': copy.deepcopy(self.state), 'pass': self.passCount, 'wPrisoners': self.wPrisoners, 'bPrisoners': self.bPrisoners, 'turn': self.turn, 'lastX': stateX, 'lastY': stateY}) #reset passcount - they'll need to pass twice in a row in order to end self.passCount = 0 #redraw board self.resize() root.update() #if it's whites turn, then do ai stuff if not self.turn and self.playComputer: stateX, stateY = ai.play(copy.deepcopy(self.state)) if stateX == -1 and stateY == -1: self.playerpass() else: self.click(None, stateX, stateY)