def __init__(self, algorithm, playerColor, dificulty): self.humanPlayer = HumanPlayer(playerColor) self.aiPlayer = AiPlayer(playerColor) if playerColor == "white": print("You are white") print("Enemy player is black") self.humanPlayer.lastRow = 7 self.aiPlayer.lastRow = 0 elif playerColor == "black": print("You are black") print("Enemy player is white") self.humanPlayer.lastRow = 0 self.aiPlayer.lastRow = 7 if dificulty == "low": self.depth = 2 elif dificulty == "medium": self.depth = 4 elif dificulty == "hard": self.depth = 6 self.board = Board() self.gameWinner = None for counter in self.aiPlayer.currentCounters: self.board.PutCounterOnBoardInPosition(counter.positions[0], counter.positions[1], counter.character) for counter in self.humanPlayer.currentCounters: self.board.PutCounterOnBoardInPosition(counter.positions[0], counter.positions[1], counter.character) self._restart = False
def setPlayer(self, playerNumber, playerType): if playerNumber == 1: if playerType == "Human": self.player1 = HumanPlayer() elif playerType == "BotRand": self.player1 = BotPlayer("rand") elif playerType == "BotMaterial": self.player1 = BotPlayer("material") elif playerType == "BotMinimax": self.player1 = BotPlayer("minimax") elif playerType == "BotAlphaBeta": self.player1 = BotPlayer("alphabeta") self.player1.set_color(chess.WHITE) else: if playerType == "Human": self.player2 = HumanPlayer() elif playerType == "BotRand": self.player2 = BotPlayer("rand") elif playerType == "BotMaterial": self.player2 = BotPlayer("material") elif playerType == "BotMinimax": self.player2 = BotPlayer("minimax") elif playerType == "BotAlphaBeta": self.player2 = BotPlayer("alphabeta") self.player2.set_color(chess.BLACK) return True
class SelectTeam: def __init__(self): self.player1 = None self.player2 = None self.game_status = GameStatus() # The argument passed to HumanPlayer() and SimpleComputerPlayer says who will start (1=start, 2=chooses second) # The player who starts is set to be: to_move. The player who is not starting is et to be: other def select_starter(self): while self.game_status.to_move is None: answer = input("Do you want to start? (Y/N)") if answer == "Y" or answer == "y": self.player1 = HumanPlayer(1) self.player2 = SimpleComputerPlayer(2) self.game_status.to_move = self.player1 self.game_status.other = self.player2 if answer == "N" or answer == "n": self.player1 = HumanPlayer(2) self.player2 = SimpleComputerPlayer(1) self.game_status.to_move = self.player2 self.game_status.other = self.player1 def select_move(self): self.game_status.to_move.make_move(self.game_status) def finished(self): return self.game_status.finished() def print_result(self): if self.player1.score() > self.player2.score(): print(self.player1.name + " WON! ") elif self.player1.score() < self.player2.score(): print(self.player2.name + " WON! ") else: print("DRAW") print(self.player1.name, " chose: ", self.player1.chosen_to_string()) print("Score: ", self.player1.score()) print(self.player2.name, " chose: ") print(self.player2.chosen_to_string()) print("Score: ", self.player2.score()) def print_status(self): if self.game_status.to_move == self.player1: print("To move: " + self.game_status.to_move.name) print("Selected: " + self.game_status.to_move.chosen_to_string()) print("Score: ", self.game_status.to_move.score()) print("") print("Other: " + self.game_status.other.name) print("Selected: " + self.game_status.other.chosen_to_string()) print("Score: ", self.game_status.other.score()) print("") print(self.game_status.remaining_to_string())
class SelectTeam: def __init__(self): self.player1 = None self.player2 = None self.game_status = GameStatus() def select_starter(self): while self.game_status.to_move is None: answer = input("Do you want to start? (Y/N)") if answer == "Y" or answer == "y": self.player1 = HumanPlayer(1) self.player2 = SimpleComputerPlayer(2) self.game_status.to_move = self.player1 self.game_status.other = self.player2 if answer == "N" or answer == "n": self.player1 = HumanPlayer(2) self.player2 = SimpleComputerPlayer(1) self.game_status.to_move = self.player2 self.game_status.other = self.other def select_move(self): self.game_status.to_move.make_move(self.game_status) def finished(self): return self.game_status.finished() def print_result(self): if self.player1.score() > self.player2.score(): print(self.player1.name + " WON! ") elif self.player1.score() < self.player2.score(): print(self.player2.name + " WON! ") else: print("DRAW") print(self.player1.name, " chose: ", self.player1.chosen_to_string()) print("Score: ", self.player1.score()) print(self.player2.name, " chose: ") print(self.player2.chosen_to_string()) print("Score: ", self.player2.score()) def print_status(self): if self.game_status.to_move == self.player1: print("To move: " + self.game_status.to_move.name) print("Selected: " + self.game_status.to_move.chosen_to_string()) print("Score: ", self.game_status.to_move.score()) print("") print("To move: " + self.game_status.other.name) print("Selected: " + self.game_status.other.chosen_to_string()) print("Score: ", self.game_status.other.score()) print("") print(self.game_status.remaining_to_string())
def select_starter(self): while self.game_status.to_move is None: answer = input("Do you want to start? (Y/N)") if answer == "Y" or answer == "y": self.player1 = HumanPlayer(1) self.player2 = SimpleComputerPlayer(2) self.game_status.to_move = self.player1 self.game_status.other = self.player2 if answer == "N" or answer == "n": self.player1 = HumanPlayer(2) self.player2 = SimpleComputerPlayer(1) self.game_status.to_move = self.player2 self.game_status.other = self.player1
class PlayerTests(unittest.TestCase): def setUp(self): self.player_1 = False self.player_2 = False self.deck = False def test_human_player_is_not_bot(self): self.player_1 = HumanPlayer() self.player_2 = Bot self.assertFalse(isinstance(self.player_1, self.player_2)) def test_two_diff_not_equal(self): self.player_1 = HumanPlayer("Jeffery") self.player_2 = HumanPlayer("Robert") self.assertNotEqual(self.player_1, self.player_2) def test_two_diff_same_name_not_equal(self): self.player_1 = HumanPlayer("Jeffery") self.player_2 = HumanPlayer("Jeffery") self.assertNotEqual(self.player_1, self.player_2) def test_draw_hand(self): self.deck = Deck() self.deck.initialize() self.player_1 = HumanPlayer() # Make sure the deck is 52 cards (one full deck) self.assertIs(self.deck.currentAmount(), 52) # Player draws a full hand from the deck self.player_1.drawHand(self.deck) # Make sure after drawing that the deck takes 7 # cards away from its full total self.assertIs(self.deck.currentAmount(), 45) # Assert that the player actually has a hand self.assertIsNot(self.player_1.getHand(), []) # At least for Go Fish, the hand should be 7 # cards big when the player is starting out self.assertIs(self.player_1.countHand(), 7) def tearDown(self): self.player_1 = False self.player_2 = False self.deck = False
def spawnExtraPlayers(playerType, amount, names=None): # Returns: Array with amount of players specified. Returns object if amount == 1 if isinstance(playerType, Bot): # Spawn bots if amount == 1: return Bot() else: return [Bot() for i in range(amount)] else: # Spawn humans if amount == 1: return HumanPlayer().randomName() else: return [HumanPlayer().randomName() for i in range(amount)]
def test_request_user_colour(test_human_game, monkeypatch): human_player = HumanPlayer(0) computer_player = ComputerPlayer(1) monkeypatch.setattr('builtins.input', lambda x: 'b') test_human_game.request_user_colour() assert test_human_game.player1 == human_player and test_human_game.player2 == computer_player human_player = HumanPlayer(1) computer_player = ComputerPlayer(0) monkeypatch.setattr('builtins.input', lambda x: 'w') test_human_game.request_user_colour() assert test_human_game.player1 == human_player and test_human_game.player2 == computer_player
def test_bearoff(): board = Board() board.board = [[0, 2, 2, 3, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]] player = HumanPlayer(0) dice = Dice() return RuleBook(board, player, dice)
def __init__(self,name,options,server=False,id=str(uuid4())): self.name = name self.names = [name] pygame.init() self.windowsize = (1024,768) self.menuback = pygame.image.load("./cards/titlescreen.gif").convert() self.screen = pygame.display.set_mode(self.windowsize) self.g = CanastaRound() self.p = HumanPlayer() self.factory = ReconnectingPBClientFactory() self.port = 7171 self.server = server self.id = UUID(id) self.clock = pygame.time.Clock() self.rejected = False self.connected = False self.controller = False self.options = options self.positions = [None]*4 self.pause = False self.starting = False self.start_game = False self.start_match = False self.cancel = False self.started = False self.initialized = False self.new_players = True self.shut_down = False self.shutting_down = False self.CommandQueue = [] self.desktop = Desktop()
def test_type_difference(self): self.player_1 = HumanPlayer() self.player_2 = Bot() self.assertFalse( type(self.player_2) == 'HumanPlayer.HumanPlayer', "Player 1 type %s, Player 2 type %s")
def run_test_1(): print('------- Scenario 1: Equivance of Minimax and AlphaBeta --------') #-- Scenario 1: Test to make sure MiniMaxAI and AlphaBetaAI are giving the same results #-- Here are the various players. player_human = HumanPlayer() player_mini = MinimaxAI(3, color=True) player_alphaBeta = AlphaBetaAI(3, color=True) #-- Set up the board in a the following fashion. Remember our AIs are WHITE in these scenarios. game = ChessGame(player_human, player_mini) game.board.clear_board() game.board.set_piece_at(piece=chess.Piece(4, False), square=16) game.board.set_piece_at(piece=chess.Piece(2, False), square=8) game.board.set_piece_at(piece=chess.Piece(3, False), square=10) game.board.set_piece_at(piece=chess.Piece(3, True), square=1) #-- Display the board and possible moves. print(game) print("Possible Moves:") for move in game.board.pseudo_legal_moves: print(move) print('---------------------------') #-- Look at the choice for MinimaxAI: print('Chosen Move:', player_mini.choose_move(game.board)) print('---------------------------') #-- Look at the choice for AlphaBetaAI: print('Chosen Move:', player_alphaBeta.choose_move(game.board)) print('---------------------------')
def run_test_2(): print('------- Scenario 2: Take the Win! --------') #-- Scenario 2: Test to make dure MiniMax and AlphaBetaAI take the win for a more complicated scenario (4 more checkmate) in a predefined position. #-- Here are the various players. player_human = HumanPlayer() player_mini = MinimaxAI(3, color=True) player_alphaBeta = AlphaBetaAI(3, color=True) game = ChessGame(player_human, player_mini) game.board.reset() game.board.push(chess.Move(12,28)) game.board.push(chess.Move(52, 36)) game.board.push(chess.Move(5, 26)) game.board.push(chess.Move(57, 42)) game.board.push(chess.Move(3, 39)) game.board.push(chess.Move(62, 45)) #-- Display Board print(game) print('---------------------------') #-- Look at choice for MiniMaxAI print('Chosen Move:', player_mini.choose_move(game.board)) print('---------------------------') #-- Look at choice for AlphaBetaAI print('Chosen Move:', player_alphaBeta.choose_move(game.board)) print('---------------------------')
def run(config=None): if config == None: config = load_config(file_name=root_data_file + 'resnet_6_6_4.model', only_load_param=True) try: board = Board(width=config.board_width, height=config.board_height, n_in_row=config.n_in_row) game = Game(board) # --------------- human VS AI ---------------- best_policy = PolicyValueNet( config.board_width, config.board_height, Network=config.network, net_params=config.policy_param ) # setup which Network to use based on the net_params mcts_player = AlphaZeroPlayer( best_policy.predict, c_puct=config.c_puct, nplays=100, add_noise=True) # set larger nplays for better performance # uncomment the following line to play with pure MCTS # mcts_player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct) # human player, input your move in the format: 2,3 human = HumanPlayer() # set who_first=0 for human first game.start_game(human, mcts_player, who_first=1, is_shown=1) except KeyboardInterrupt: print('\n\rquit')
def main(args): assert args.c <= args.columns and args.c <= args.rows print('----- Parámetros -----') print('columnas: \t', args.columns) print('filas: \t\t', args.rows) print('c: \t\t', args.c) print('p: \t\t', args.p) print('----------------------') if args.blue_player is None: blue_player = HumanPlayer(BLUE) else: executable = args.blue_player.pop(0) blue_player = PlayerCommunicator(executable, args.blue_player, BLUE, RED) if args.red_player is None: red_player = HumanPlayer(RED) else: executable = args.red_player.pop(0) red_player = PlayerCommunicator(executable, args.red_player, RED, BLUE) ref = Referee(args.columns, args.rows, args.c, args.p, blue_player, red_player, show_ui=args.ui) iteration = 0 resultados = {RED: 0, BLUE: 0, TIE: 0} while args.iterations is None or args.iterations > iteration: iteration += 1 if args.first == BLUE: ganador = ref.runPlay(blue_player) elif args.first == RED: ganador = ref.runPlay(red_player) else: ganador = ref.runPlay(choice([blue_player, red_player])) resultados[ganador] += 1 print(resultados) ref.exit()
def addAllPlayers(self, player_n, bot_n): player_bucket = [] player_names = self.askPlayerNames(player_n) for i in range(player_n): if player_names: player_bucket.append(HumanPlayer(player_names[i])) else: player_bucket.append(HumanPlayer()) for i in range(bot_n): player_bucket.append(Bot().randomName()) if not self.test: shuffle(player_bucket) for p in player_bucket: self.addPlayer(p)
def play_random_game(_): players = [ HumanPlayer("Hans"), RandomPlayer("Opponent1"), RandomPlayer("Teammate"), RandomPlayer("Opponent2") ] game = Game(players) return game.play_game(choice(players)).total_points
def __init__(self, playername): # Create a list of players, one human and four computer self._playerList = [HumanPlayer(playername, self)] for i in range(1, 5): self._playerList.append(ComputerPlayer("Computer " + str(i), self)) # Get the first player firstPlayer = self._getFirstPlayer(self._playerList) print(firstPlayer.getName() + " gets to go first!") # Start the game self.newRound(firstPlayer)
def __init__(self): self.boardGui = GameGui() #self.placementGui = BoardGui() #self.placementGui = PlacementGui() self.player2 = ComputerPlayer(2) #self.human = ComputerPlayer("Player 1") self.player1 = HumanPlayer(1, self.boardGui) self.shipLengths = [2, 3, 3, 4, 5] self.numberOfShips = len(self.shipLengths) self.run()
def start_game(self): """We start the game with setup: first the two players place their ships. Then, while there is no winner yet, we get the move from the player and then get the move from the computer, repeatedly. Once there is a winner, the game is over and the winner's name is announced.""" self.player = HumanPlayer(self.user_player_name, controller=self) self.computer = ComputerPlayer(controller=self) self.is_human_turn = True self.winner = None self.submarine_name = None self.orientation = '>' # self.game_state = StringProperty('setup') self.user_submarines_positioned = 0 self.submarine_name = None self.orientation = '>' self.user_submarines_positioned = 0 self.screen_manager.current = 'game' self.game_state = 'setup'
def choose_player(kind, my_mark, their_mark): if kind == "h": return HumanPlayer(my_mark) elif kind == "c": # Currently available difficulties are: diff_low = 1 diff_high = 2 difficulty = read_int( "How clever do you want the computer to be? " "(1 and 2 i.e. stupid & slightly less so are the only current options)" ) while not diff_low <= difficulty <= diff_high: difficulty = read_int( "That is not a valid option. Please enter a number between %d and %d" % (diff_low, diff_high)) return ComputerPlayer(my_mark, their_mark, difficulty) else: print("Error with option, returning human player as default") return HumanPlayer(my_mark)
def test_draw_hand(self): self.deck = Deck() self.deck.initialize() self.player_1 = HumanPlayer() # Make sure the deck is 52 cards (one full deck) self.assertIs(self.deck.currentAmount(), 52) # Player draws a full hand from the deck self.player_1.drawHand(self.deck) # Make sure after drawing that the deck takes 7 # cards away from its full total self.assertIs(self.deck.currentAmount(), 45) # Assert that the player actually has a hand self.assertIsNot(self.player_1.getHand(), []) # At least for Go Fish, the hand should be 7 # cards big when the player is starting out self.assertIs(self.player_1.countHand(), 7)
def __init__(self, player1IsHuman=True, player2IsHuman=True, numRows=6, numCols=7, winLength=4, displayTurns=False): if (player1IsHuman): self.player1 = HumanPlayer() else: self.player1 = MachinePlayer() if (player2IsHuman): self.player2 = HumanPlayer() else: self.player2 = MachinePlayer() if (player1IsHuman or player2IsHuman): self.displayTurns = True else: self.displayTurns = displayTurns self.gameBoard = Board(numRows, numCols, winLength) self.currentTurn = 1
def run_example_game(): """ Runs a complete example game. """ players = [HumanPlayer(hand_size, "Human"), DefensivePlayer(hand_size, "Player")] env = DurakEnv(players, True) done = False state = env.reset() env.render() while not done: act = (env.get_turn_player()).get_action(state, env.to_attack()) state, _, done = env.step(act) env.render() env.end_gui()
def test_decision_phase_human_to_human_pass(self): test_card = Card("2", "Hearts") extraPlayer = spawnExtraPlayers(HumanPlayer(), 1) f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#2.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, extraPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) f.close() sys.stdin = f1 self.assertIs(self.humanPlayer.getChosenPlayer(), extraPlayer) self.assertEqual(self.humanPlayer.getChosenCard(), test_card)
def playGameAgainstHuman(): finalPlayer = FinalPlayer( dominion, featureExtractor.newestFeatureExtractor, explorationProb=0, usingCachedWeights=True, cacheStringKey=defaultCacheStringKey, actionPlayer=ExpectimaxActionPhasePlayer(dominion)) cachedWeights = Counter() cacheWeightsBackpropagate.bpsetWeightsFromCache(dominion.startKingdom, cachedWeights, finalPlayer.cacheStringKey, 1) finalPlayer.weights = cachedWeights if cachedWeights == Counter(): print "####NO WEIGHTS FOUND####" humanPlayer = HumanPlayer(dominion) players = [] players.append(finalPlayer) players.append(humanPlayer) simulateDominion(dominion, players, numGames=1, maxTurns=100, verbose=True)
def test_can_bear_off(test_bearoff, test_bar, test_rulebook): assert test_bearoff.can_bear_off() # can bear off from points indicated by the dice test_bearoff.board.board = [[0, 2, 2, 3, 2, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]] assert not test_bearoff.can_bear_off() # one checker is not in home board test_bearoff.dice.set_dice(5, 6) test_bearoff.board.board = [[3, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0], [0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]] assert test_bearoff.can_bear_off() # no checkers in points 5 and 6 but can still bear off from highest point where there are checkers test_bearoff.dice.set_dice(1, 3) test_bearoff.board.board = [[0, 2, 0, 3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0], [0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]] assert not test_bearoff.can_bear_off() # there are checkers in higher numbered points which must be moved before bearing-off test_bearoff.player = HumanPlayer(1) assert not test_bearoff.can_bear_off()
def run(config=None): if config == None: config = load_config(file_name=root_data_file + 'resnet_6_6_4.model', only_load_param=True) try: board = Board(width=config.board_width, height=config.board_height, n_in_row=config.n_in_row) #--------------------1.set player:alphazero VS human---------------------# best_policy = PolicyValueNet( config.board_width, config.board_height, Network=config.network, net_params=config.policy_param ) # setup which Network to use based on the net_params player1 = AlphaZeroPlayer( best_policy.predict, c_puct=config.c_puct, nplays=1000) #set larger nplays for better performance # uncomment the following line to play with pure MCTS #player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct) player2 = HumanPlayer() # --------------------2.set order---------------------# who_first = 0 # 0 means player1 first, otherwise player2 first # --------------------3.start game--------------------# game = Game(board, is_visualize=True) t = threading.Thread(target=game.start_game, args=(player1, player2, who_first)) t.start() game.show() except: print('\n\rquit')
self.turn.change_turn(False) print "{} ne peut pas jouer de position legale.".format( player.name) return False # Game continues def game_loop(self): """Boucle principale de jeu.""" over = False while not over: player = self.turn.get_player() over = self.game(player) return over if __name__ == "__main__": player_X = HumanPlayer("X", "Marshall Bruce Mathers III") player_O = HumanPlayer("O", "Lesane Parish Crooks") Score = Score(player_X, player_O) print "\nTest de Score.update_score() :" # Test en changeant le score de X à 42 et celui de O à 65 Score.update_score(42, 65) if Score.black_score == 42 and Score.white_score == 65: test = "OK" else: test = "NOK" print "\nScore.update_score({},{}) => Score X : {} // Score O : {} \nResultat attendu = Score X : {} // Score O : {}".format( 42, 65, Score.black_score, Score.white_score, 42, 65) print " ---> test {}".format(test)
from SmartPlayer import SmartPlayer from RandomPlayer import RandomPlayer from GreedyPlayer import GreedyPlayer from Othello import Othello from Player import playGame from HumanPlayer import HumanPlayer from pybrain.tools.customxml.networkreader import NetworkReader from TacticalPlayer import TacticalPlayer import random #nn = NetworkReader.readFrom("othelloNetwork.xml") #opponentPlayer = SmartPlayer(nn,8) #change this to change the opponent to be testing against opponentPlayer = TacticalPlayer() humanPlayer = HumanPlayer() othello = Othello() othello.resetGame() humanPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])) opponentPlayer.newGame(othello,humanPlayer.enemy) playGame(othello,humanPlayer,opponentPlayer) if othello.getWinner() == humanPlayer.color: print "You won!" elif othello.getWinner() == opponentPlayer.color: print "You lost!" else: print "Tie game! :("
def getHumanVsHumanOpponents(): return (HumanPlayer(1, gameBoard), HumanPlayer(2, gameBoard))
###################### # # Your Ai Bot # # player = MinesweeperAi() # # Random Ai # # player = RandomBot() # # Default Humain Player # player = HumanPlayer() # Instanciate Minesweeper Board minesweeper = Board(size, nMines) ############## # Start Game # ############## first_turn = True print("Starting Game") minesweeper.reveal() print("========================") minesweeper.show() print("========================")
# pip3 install python-chess # Code for testing the algorithm, and playing the AI. # To make a move, type from-square and to-square in console, no space. ex: e2e4 could be an opening move for white. import chess from RandomAI import RandomAI from HumanPlayer import HumanPlayer from MinimaxAI import MinimaxAI from AlphaBetaAI import AlphaBetaAI from ChessGame import ChessGame from IterativeAI import IterativeAI import sys player1 = HumanPlayer() player2 = AlphaBetaAI() # player2 = MinimaxAI() # player2 = IterativeAI(AlphaBetaAI(), 20) # player2 = IterativeAI(MinimaxAI(), 20) game = ChessGame(player1, player2) # function that significantly simplifies the start board def open_board(game): board = game.board board.clear() white_king = chess.Piece(6, True) black_king = chess.Piece(6, False) board.set_piece_at(chess.parse_square("a1"), white_king)
class CanastaClient(pb.Referenceable): """ Handles all user input and graphical display. Connects to a server to make plays. All games, including the one-player game, use the client. """ def __init__(self,name,options,server=False,id=str(uuid4())): self.name = name self.names = [name] pygame.init() self.windowsize = (1024,768) self.menuback = pygame.image.load("./cards/titlescreen.gif").convert() self.screen = pygame.display.set_mode(self.windowsize) self.g = CanastaRound() self.p = HumanPlayer() self.factory = ReconnectingPBClientFactory() self.port = 7171 self.server = server self.id = UUID(id) self.clock = pygame.time.Clock() self.rejected = False self.connected = False self.controller = False self.options = options self.positions = [None]*4 self.pause = False self.starting = False self.start_game = False self.start_match = False self.cancel = False self.started = False self.initialized = False self.new_players = True self.shut_down = False self.shutting_down = False self.CommandQueue = [] self.desktop = Desktop() def callDebug(self,obj): if DEBUGGER: obj.callRemote("debug") def remote_debug(self): self.g.roundOver=True def Connect(self,obj): if DEBUGGER: print "Connection established with server" obj.callRemote("joinServer",self,self.name,str(self.id),VERSION).addCallback(self.isController) def Disconnect(self,obj): if DEBUGGER: print "Hanging up connection" obj.callRemote("hangUp",str(self.id)).addCallback(self.isDisconnected) def isDisconnected(self,obj): if DEBUGGER: print "Closed connection with server:",obj self.shut_down = True def failConnect(self,obj): if self.cancel: return "cancel" else: if DEBUGGER: print "Failed to connect, retrying..." sleep(1) reactor.connectTCP("localhost",self.port, self.factory) self.factory.getRootObject().addCallbacks(self.Connect,self.failConnect) def isController(self,obj): if isinstance(obj,str): print obj self.rejected = True reactor.stop() else: self.connected = True self.controller = obj def remote_lookAlive(self): return True def getNames(self,obj): obj.callRemote("Names").addCallback(self.gotNames) def remote_updateNames(self,namelist): self.names = namelist if self.initialized: print "got a new name",namelist if self.initialized: self.g.playernames.append(namelist[-1]) self.new_players = True def remote_removeName(self,name): if DEBUGGER: print "removing " + name + " from name list" if self.initialized: self.g.playernames.remove(name) self.names.remove(name) try: self.positions.remove(name) except: pass self.new_players = True def gotNames(self,obj): self.names = obj def startServer(self,obj): self.oneRef = obj self.oneRef.callRemote("Start",str(self.id),self.player_positions,self.options).addCallbacks(self.didStart,self.failStart) def didStart(self,obj): if DEBUGGER: print "Game started on the server" self.start_match = True if not self.server: self.factory.getRootObject().addCallbacks(self.blockClients) def failStart(self,obj): if DEBUGGER: print "Failed to start game on the server, retrying..." sleep(1) self.factory.getRootObject().addCallbacks(self.startServer,self.failStart) def blockClients(self,obj): obj.callRemote("blockConnections") def notClosed(self,obj): if DEBUGGER: print "Server did not close correctly:",obj self.shut_down = True def isClosed(self,obj): if DEBUGGER: print "Server closed correctly:",obj self.shut_down = True def stopServer(self,obj): self.shutting_down = True self.oneRef = obj self.oneRef.callRemote("Shutdown",str(self.id)).addCallbacks(self.isClosed,self.notClosed) def reportReady(self,obj): self.started=False self.initialized=False self.oneRef = obj if self.g.human in [0,1,2,3]: self.oneRef.callRemote("isReady",str(self.id)) def SendCommand(self,obj): self.oneRef = obj self.oneRef.callRemote("takeCanastaCommand",str(self.id),self.lastplay) def clearCommand(self,obj): self.lastplay=CanastaCommand(NO_PLAY,[],[]) def SendChat(self,obj): if DEBUGGER: print "sending chat" if len(self.chatwin.chattxt.text)>0: self.lastchat = CanastaCommand(CHAT,[self.chatwin.chattxt.text,self.g.myPos]) self.chatwin.chattxt.text = "" self.oneRef = obj self.oneRef.callRemote("takeChat",str(self.id),self.lastchat) def remote_initGame(self,players,human,options): if DEBUGGER: print "client game initialized" if options.animation == None: options.animation = self.options.animation self.g.gameStart(players,human,options) try: self.chatwin.close() except: pass self.genChat(self.g.CHATX[0],self.g.CHATX[1]) self.g.initCanasta(nextround=False) self.chatwin.enabled = True self.screen = pygame.display.set_mode(self.windowsize,RESIZABLE) def remote_resetRound(self): self.started = False self.initialized = False def remote_resetGame(self): self.starting = False self.start_game = False self.start_match = False self.cancel = False self.started = False self.initialized = False self.new_players = True self.shut_down = False self.shutting_down = False self.CommandQueue = [] def remote_newGame(self): if DEBUGGER: print "client game reset" self.g.newGame() def remote_initRound(self): try: self.desktop.query.close() except: pass if DEBUGGER: print "client round initialized" self.g.initCanasta() try: self.chatwin.close() except: pass self.genChat(self.g.curchatx,self.g.CHATX[1]) self.chatwin.enabled = True self.started = True def remote_readInit(self,status): if not self.initialized: if DEBUGGER: print "client got status" self.g.readInit(status) self.initialized = True def remote_readCommand(self,command): """ Read a command from the server. Chats are executed immediately, everything else is queued for execution later. """ if command.action==CHAT: self.g.execCode(command) return else: self.CommandQueue.append(command) retcode = True return [self.g.lastCommand,retcode] def execCommand(self,command): """ Execute a command on the local game object. Uses items that have previously been queued from the server. """ if self.g.turn==self.g.myPos: invis = False else: invis = True self.g.execCode(command,invisible=invis) retcode = self.g.lastReturn if DEBUGGER: print "result was",self.g.lastReturn if self.g.lastReturn: if self.g.roundOver: if DEBUGGER: print "ROUND OVER *** client updating score" team1round = self.g.cardPoints(1) + self.g.specialPoints(1,params=False) - self.g.handPoints(1) team2round = self.g.cardPoints(2) + self.g.specialPoints(2,params=False) - self.g.handPoints(2) self.g.team1score += team1round self.g.team2score += team2round self.genEndRound() self.clearCommand(None) if DEBUGGER & (not retcode): print "WARNING: command failed" return [self.g.lastCommand,retcode] def remote_lostPlayer(self,name): def ComputerOnClick(button): self.result = True self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) def ResetOnClick(button): self.result = False self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) self.result = None try: self.desktop.query.close() except: pass defaultStyle.init(gui) endStyle = {'font-color': (255,255,255), 'font': font.Font(None,20), 'autosize': True, "antialias": True,'border-width': False, 'border-color': (0,0,0), 'wordwrap': False} self.desktop.query = Window(position = (250,180), size = (500,200), parent = self.desktop, text = "Lost Player", closeable = False, shadeable = False) labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (30,50),size = (100,0), parent = self.desktop.query, text = str(name) + " has disconnected", style = endStyle) Label(position = (30,75),size = (100,0), parent = self.desktop.query, text = "Do you want to replace them with a computer player?", style = endStyle) Label(position = (30,100),size = (100,0), parent = self.desktop.query, text = "Or end this game and start a new one?", style = endStyle) Label(position = (30,125),size = (100,0), parent = self.desktop.query, text = "If you replace them, you can add them back in if they reconnect", style = endStyle) Computer_button = Button(position = (30,170), size = (175,0), parent = self.desktop.query, text = "Replace with computer") Computer_button.onClick = ComputerOnClick Reset_button = Button(position = (270,170), size = (175,0), parent = self.desktop.query, text = "Start over with a new game") Reset_button.onClick = ResetOnClick while self.result == None: self.defaultInput() self.DrawQuery() return self.result def remote_waitPlayer(self,name): defaultStyle.init(gui) endStyle = {'font-color': (255,255,255), 'font': font.Font(None,20), 'autosize': True, "antialias": True,'border-width': False, 'border-color': (0,0,0), 'wordwrap': False} self.desktop.query = Window(position = (250,180), size = (500,200), parent = self.desktop, text = "Lost Player", closeable = False, shadeable = False) labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (30,50),size = (100,0), parent = self.desktop.query, text = str(name) + " has disconnected", style = endStyle) Label(position = (30,100),size = (100,0), parent = self.desktop.query, text = "Waiting for the game host to replace them or restart the game", style = endStyle) self.pause = True return def askReset(self): def YesOnClick(button): self.pause = False def1 = self.factory.getRootObject() def1.addCallback(self.gameReset) self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) def NoOnClick(button): self.pause = False self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) defaultStyle.init(gui) endStyle = {'font-color': (255,255,255), 'font': font.Font(None,24), 'autosize': True, "antialias": True,'border-width': False, 'border-color': (0,0,0), 'wordwrap': False} self.desktop.query = Window(position = (350,250), size = (300,200), parent = self.desktop, text = "Reset", closeable = False, shadeable = False) labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (100,50),size = (100,0), parent = self.desktop.query, text = "Reset the game", style = endStyle) Label(position = (100,100),size = (100,0), parent = self.desktop.query, text = "Are you sure?", style = endStyle) Yes_button = Button(position = (50,150), size = (40,0), parent = self.desktop.query, text = "Yes") No_button = Button(position = (200,150), size = (40,0), parent = self.desktop.query, text = "No") Yes_button.onClick = YesOnClick No_button.onClick = NoOnClick self.pause = True return def gameReset(self,obj): obj.callRemote("Reset",str(self.id)) def remote_unPause(self): try: self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) except: pass self.pause = False def remote_goOut(self): """ Called remotely by the server when the client's partner has asked to go out. Waits for user input. """ self.done = False def OKOnClick(button): self.done = True self.response = True def cancelOnClick(button): self.done = True self.response = False defaultStyle.init(gui) defaultStyle.init(gui) desktop_main = Desktop() desktop = Window(position = (300,220), size = (400,200), parent = desktop_main, text = "Go Out", closeable = False, shadeable = False) desktop.onClose = cancelOnClick labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (100,100),size = (200,0), parent = desktop, text = 'Your partner asked: "May I go out?"', style = labelStyleCopy) OK_button = Button(position = (100,140), size = (50,0), parent = desktop, text = "Yes") cancel_button = Button(position = (200,140), size = (50,0), parent = desktop, text = "No") OK_button.onClick = OKOnClick cancel_button.onClick = cancelOnClick while not self.done: #Handle Input Events for event in gui.setEvents(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return self.DrawGame(flip=False) desktop_main.update() desktop_main.draw() pygame.display.flip() return self.response def remote_endGame(self): """ Called by the server on all non-controlling clients after the controller sends the kill signal. Should send a confirmation and then kill the reactor in the next input loop. """ if DEBUGGER: print "quitting client" pygame.quit() self.shut_down = True def overWindow(self): result = False try: temp = self.desktop.assign assign = True assign_pos = self.desktop.assign.position assign_size = self.desktop.assign.size except: assign = False try: temp = self.desktop.query query = True query_pos = self.desktop.query.position query_size = self.desktop.query.size except: query = False try: temp = self.chatwin chat = True chat_pos = self.chatwin.position chat_size = self.chatwin.size except: chat = False if gui.events != None: for event in gui.events: if event.type in [MOUSEBUTTONUP,MOUSEBUTTONDOWN,MOUSEMOTION]: if assign: if (event.pos[0] > assign_pos[0]) & (event.pos[0]<assign_pos[0]+assign_size[0]) & (event.pos[1] > assign_pos[1]) & (event.pos[1]<assign_pos[1]+assign_size[1]): result = True if query: if (event.pos[0] > query_pos[0]) & (event.pos[0]<query_pos[0]+query_size[0]) & (event.pos[1] > query_pos[1]) & (event.pos[1]<query_pos[1]+query_size[1]): result = True if chat: if (event.pos[0] > chat_pos[0]) & (event.pos[0]<chat_pos[0]+chat_size[0]) & (event.pos[1] > chat_pos[1]) & (event.pos[1]<chat_pos[1]+chat_size[1]): result = True return result def defaultInput(self,chat=True): if self.chatwin.chattxt.hasFocus: self.g.enterchat = True else: self.g.enterchat = False play = self.p.getPlay(self.g,gui.events) if play.action == QUIT_GAME: def1 = self.factory.getRootObject() if self.controller: def1.addCallbacks(self.stopServer) else: def1.addCallbacks(self.Disconnect) play = CanastaCommand(NO_PLAY,[],[]) elif (play.action == CHAT) & chat: self.lastchat = play def1 = self.factory.getRootObject() def1.addCallback(self.SendChat) play = CanastaCommand(NO_PLAY,[],[]) elif play.action == RESIZE: self.screen = pygame.display.set_mode(play.arglist[0],RESIZABLE) self.windowsize = play.arglist[0] self.chatwin.close() self.genChat(self.g.curchatx,self.g.CHATX[1]) play = CanastaCommand(NO_PLAY,[],[]) if self.g.animating: self.g.animate() else: try: self.execCommand(self.CommandQueue.pop(0)) except: pass return play def getInput(self): """ The main user input loop. Runs concurrently with Twisted's main reactor, so that the user can enter commands whenever they want. Game commands are sent to the server for execution. If valid, the server will call back to execute them locally. Chats are sent to the server through the special back channel that allows them to be distributed even if it's someone else's turn. Cards can be moved or selected at any time, but commands will not be executed unless it's the client's turn. For efficiency, the client is coded to only submit commands on its turn. For stability, however, the server is programmed to check for the turn before executing any non-chat command it receives. """ self.clock.tick(40) if self.overWindow(): self.p.over_window = True else: self.p.over_window = False if self.cancel: reactor.stop() if self.shut_down: try: pygame.quit() except: pass try: reactor.stop() except: pass elif self.pause: self.defaultInput() self.DrawQuery() elif self.shutting_down: pass elif self.started & self.initialized: if self.chatwin.chattxt.hasFocus: self.g.enterchat = True else: self.g.enterchat = False play = self.defaultInput() if (self.g.roundOver) | (self.p.viewhelp==1): if self.p.viewhelp==1: try: if self.desktop.query.wintype != "help": try: self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) self.genHelp() except: self.genHelp() except: self.genHelp() self.DrawQuery() else: self.DrawGame() if play.action == RESET: if self.controller: self.askReset() elif play.action != NO_PLAY: if self.g.turn==self.g.myPos: self.lastplay = play def1 = self.factory.getRootObject() def1.addCallback(self.SendCommand) if self.p.viewhelp==0: try: if self.desktop.query.wintype == "help": self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) except: pass elif (not self.start_match) & self.controller & (not self.server): for event in pygame.event.get(): if event.type == QUIT: self.cancel = True elif event.type == KEYDOWN and event.key == K_ESCAPE: self.cancel = True if self.connected: self.DrawWait("Waiting for game to start...") else: self.DrawWait("Connecting to the game server...") if not self.starting: if DEBUGGER: print "Starting the server" self.factory.getRootObject().addCallbacks(self.startServer,self.failStart) self.starting = True elif (not self.server) | self.start_match: if self.connected: if self.chatwin.chattxt.hasFocus: self.g.enterchat = True else: self.g.enterchat = False self.DrawWait("Waiting for game to start...") play = self.p.getPlay(self.g,gui.events) if play.action == QUIT_GAME: def1 = self.factory.getRootObject() if self.controller: def1.addCallbacks(self.stopServer) else: def1.addCallbacks(self.Disconnect) elif play.action == CHAT: self.lastchat = play def1 = self.factory.getRootObject() def1.addCallback(self.SendChat) else: for event in pygame.event.get(): if event.type == QUIT: self.shut_down = True elif event.type == KEYDOWN and event.key == K_ESCAPE: self.shut_down = True self.DrawWait("Connecting to the game server...") elif not self.connected: self.DrawWait("Setting up the game server...") else: if not self.start_game: if self.chatwin.chattxt.hasFocus: self.g.enterchat = True else: self.g.enterchat = False play = self.p.getPlay(self.g,gui.events) #Handle Input Events for event in gui.events: if event.type == KEYDOWN and event.key == 13: if not self.g.enterchat: self.start_game = True else: pass if play.action == QUIT_GAME: def1 = self.factory.getRootObject() def1.addCallbacks(self.stopServer) elif play.action == CHAT: self.lastchat = play def1 = self.factory.getRootObject() def1.addCallback(self.SendChat) elif not self.starting: playernames=[] for index, p in enumerate(self.positions): playernames.append(p) self.player_positions = playernames self.factory.getRootObject().addCallback(self.startServer) self.starting = True self.DrawAssign() def readyStartGame(self): self.start_game = True ##################################### #Drawing routines ##################################### def DrawAssign(self): def OKOnClick(button): self.start_game = True self.desktop.assign.close() self.desktop.assign.position = (0,0) self.desktop.assign.size = (0,0) def assignPlayer(arg): which = self.glist.index(arg.name) last = self.gr[which] if DEBUGGER: print arg.name,self.glist,which,last if arg.value==None: return loc = posnames.index(arg.text) #Assign an observer. Allows starting a game with no human players only if the debugger is on. if (loc == 4) & ((positions != [None]*4) | DEBUGGER): try: self.gr[which].value = False except: pass for i in range(len(positions)): if positions[i]==arg.name: positions[i]=None self.gr[which] = arg #Reject the assign if it conflicts with another player's assignment or leaves no human players with the debugger off. elif (positions[loc]!=None) | ((loc==4) & (positions==[None]*4) & (not DEBUGGER)): print "BZZT!" arg.value = False self.gr[which].value = True #Otherwise, assign the position else: try: self.gr[which].value = False except: pass for i in range(len(positions)): if positions[i]==arg.name: positions[i]=None positions[loc] = arg.name self.gr[which] = arg if DEBUGGER: print positions self.positions = positions playernames=self.names if self.new_players: try: self.desktop.assign.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) except: pass optionsurf = pygame.image.load("./art/optionbox.png").convert_alpha() self.desktop_main = Desktop() self.desktop.assign = Window(position = (100,100), size = (800,600), parent = self.desktop, text = "Assign players", closeable = False, shadeable = False) self.gr = [] self.glist = [] defaultStyle.init(gui) labelStyleCopy = gui.defaultLabelStyle.copy() labelStyleCopy['wordwrap'] = True if self.positions == [None]*4: default = True else: default = False positions = self.positions posnames = ["Bottom","Left","Top","Right","Observer"] for index, p in enumerate(self.names): if index<4: def_pos = posnames[index] else: def_pos = None if index==0: label1 = Label(position = (125,125),size = (200,0), parent = self.desktop.assign, text = "Players will appear here when they connect to your game.", style = labelStyleCopy) label2 = Label(position = (125,145),size = (200,0), parent = self.desktop.assign, text = "Assign them to a player position and press the start button when ready.", style = labelStyleCopy) label3 = Label(position = (125,165),size = (200,0), parent = self.desktop.assign, text = "Unfilled positions will be filled with computer players.", style = labelStyleCopy) label3 = Label(position = (125,185),size = (200,0), parent = self.desktop.assign, text = "Observers can watch the game and use the chat window, and may be included in the next round.", style = labelStyleCopy) label_name = Label(position = (125,220 + index*25),size = (200,0), parent = self.desktop.assign, text = p, style = labelStyleCopy) if (index<4) & default: positions[index] = p self.gr.append(None) self.glist.append(p) for index2, pos in enumerate(posnames): o = CheckBox(position = (200+index2*75,220+index*25), parent = self.desktop.assign, text = pos, style = gui.createOptionBoxStyle(gui.defaultFont, optionsurf, 12, (255,255,255), (100,100,100), autosize = True)) o.name = p print p o.index = index2 try: if positions[index2]==p: o.value = True self.gr[index] = o except: pass if (index2==4) & (self.gr[index]==None): o.value = True o.onValueChanged = assignPlayer OK_button = Button(position = (125,400), size = (50,0), parent = self.desktop.assign, text = "Start") OK_button.onClick = OKOnClick self.positions = positions self.new_players = False self.DrawGame() def DrawWait(self,message): defaultStyle.init(gui) desktop_main = Desktop() desktop = Window(position = (300,220), size = (400,200), parent = desktop_main, text = "Waiting", closeable = False, shadeable = False) labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (100,75),size = (50,0), parent = desktop, text = message, style = labelStyleCopy) if not self.connected: font = pygame.font.Font("FreeSans.ttf", 30) self.titletext = font.render("Play against the computer",1,(255,255,255)) self.titletext2 = font.render("Start a network game",1,(255,255,255)) self.titletext3 = font.render("Join a network game",1,(255,255,255)) self.titletext4 = font.render("Options",1,(255,255,255)) self.titlepos = self.titletext.get_rect() self.titlepos.centerx = 512 self.titlepos.centery = 350 self.titlepos2 = self.titletext2.get_rect() self.titlepos2.centerx = 512 self.titlepos2.centery = 450 self.titlepos3 = self.titletext3.get_rect() self.titlepos3.centerx = 512 self.titlepos3.centery = 550 self.titlepos4 = self.titletext4.get_rect() self.titlepos4.centerx = 512 self.titlepos4.centery = 650 self.screen.fill((0,0,255)) #YOUR RENDERING HERE!!! self.screen.blit(self.menuback, (0,0)) self.screen.blit(self.titletext,self.titlepos) self.screen.blit(self.titletext2,self.titlepos2) self.screen.blit(self.titletext3,self.titlepos3) self.screen.blit(self.titletext4,self.titlepos4) else: self.DrawGame(flip=False) #Last thing to draw, desktop try: desktop_main.update() desktop_main.draw() except: pass #Flips! pygame.display.flip() def DrawGame(self,flip=True): screen = self.screen g = self.g p = self.p playernames = self.g.playernames # DRAWING screen.fill((0x00, 0xb0, 0x00)) #Stage area pygame.draw.rect(screen,(0,0,0),(g.curstagexy[0]-5,g.curstagexy[1]-5,g.curstagexy[2]+10,g.curstagexy[3]+10)) pygame.draw.rect(screen,(0,255,0),g.curstagexy) sr = screen.get_rect() centerx = sr.centerx centery = sr.centery #Turn arrows arrow_length = 30 headw = 10 headl = 15 if g.turn==0: fl = [[(centerx,centery+20),(centerx,centery+20+arrow_length)], [(centerx-headw,centery+20+arrow_length-headl),(centerx,centery+20+arrow_length)], [(centerx+headw,centery+20+arrow_length-headl),(centerx,centery+20+arrow_length)]] elif g.turn==1: fl = [[(centerx-90,centery-50),(centerx-90-arrow_length,centery-50)], [(centerx-90-arrow_length+headl,centery-50-headw),(centerx-90-arrow_length,centery-50)], [(centerx-90-arrow_length+headl,centery-50+headw),(centerx-90-arrow_length,centery-50)]] elif g.turn==2: fl = [[(centerx,centery-90),(centerx,centery-90-arrow_length)], [(centerx-headw,centery-90-arrow_length+headl),(centerx,centery-90-arrow_length)], [(centerx+headw,centery-90-arrow_length+headl),(centerx,centery-90-arrow_length)]] elif g.turn==3: fl = [[(centerx+100,centery-50),(centerx+100+arrow_length,centery-50)], [(centerx+100+arrow_length-headl,centery-headw-50),(centerx+100+arrow_length,centery-50)], [(centerx+100+arrow_length-headl,centery+headw-50),(centerx+100+arrow_length,centery-50)]] for points in fl: pygame.draw.aaline(screen,(200,0,0), points[0], points[1]) if g.roundOver: for c in g.cardGroup.cards: if (c.side==0) & c.location in range(3,401): c.flip() pygame.draw.rect(screen,(0,0,0),(g.curlocxy[1][0]+2,g.curlocxy[1][1]+2,70,90),2) g.cardGroup.draw(screen) if g.selectionRect.width > 0 and g.selectionRect.height > 0: pygame.draw.rect(screen,(0xff,0xff,0x00),g.selectionRect,3) if g.curState().turnState == PRE_DRAW: state_text = "Draw or pick up" else: state_text = "Meld or discard" #Score area pygame.draw.rect(screen,(0,0,0),(g.curscorex-5,5,280,60)) pygame.draw.rect(screen,(0,0,255),(g.curscorex,10,270,50)) font = pygame.font.Font("freesansbold.ttf", 14) font2 = pygame.font.Font("FreeSans.ttf", 14) roundtext = font.render("%s%s" % ("ROUND ",g.round),1,(255,255,255)) team1text = font.render("%s%s" % ("Team 1: ",g.team1score),1,(255,255,255)) team2text = font.render("%s%s" % ("Team 2: ",g.team2score),1,(255,255,255)) curteamtext = font2.render("%s%s%s" % (playernames[g.turn],": ",state_text),1,(255,255,255)) roundpos = roundtext.get_rect() roundpos.centerx = g.curscorex + 40 roundpos.centery = 23 team1pos = roundtext.get_rect() team1pos.centerx = g.curscorex + 110 team1pos.centery = 23 team2pos = roundtext.get_rect() team2pos.centerx = g.curscorex + 210 team2pos.centery = 23 curteampos = roundtext.get_rect() curteampos.centerx = g.curscorex + 40 curteampos.centery = 43 screen.blit(roundtext,roundpos) screen.blit(team1text,team1pos) screen.blit(team2text,team2pos) screen.blit(curteamtext,curteampos) #Chat window self.chatwin.chatdisplay.text = "" if not self.chatwin.shaded: for i in range(-1,-8,-1): try: text = self.g.chatlist[i] count = 0 rendered = gui.wrapText(text + "\n" + self.chatwin.chatdisplay.text,self.chatwin.chatdisplay.style['font'],self.chatwin.chatdisplay.size[0]) for char in rendered: if char=="\n": count += 1 if count<9: self.chatwin.chatdisplay.text = self.g.chatlist[i] + "\n" +self.chatwin.chatdisplay.text except: pass gui.setEvents() try: self.desktop.update() except: pass self.desktop.draw() if flip: pygame.display.flip() def genEndRound(self): self.next_round = False def ContinueOnClick(button): self.desktop.query.close() self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) def1 = self.factory.getRootObject() def1.addCallback(self.reportReady) team1round = self.g.cardPoints(1) + self.g.specialPoints(1,params=False) - self.g.handPoints(1) team2round = self.g.cardPoints(2) + self.g.specialPoints(2,params=False) - self.g.handPoints(2) font2 = pygame.font.Font("FreeSans.ttf", 20) team1specials = self.g.specialPoints(1,params=True) team2specials = self.g.specialPoints(2,params=True) if team1specials[2]==8: team1specials[2]=4 if team2specials[2]==8: team2specials[2]=4 if team1specials[3]==1: team1out = "Went out first" elif team1specials[3]==2: team1out = "Went out concealed" else: team1out = "" if team2specials[3]==1: team2out = "Went out first" elif team2specials[3]==2: team2out = "Went out concealed" else: team2out = "" endtext1 = ["Team 1:", str(team1round)+ " points", str(self.g.cardPoints(1)) + " face value", "-" + str(self.g.handPoints(1)) + " points in hand", str(team1specials[0])+" red canastas", str(team1specials[1])+" black canastas", str(team1specials[2])+" red threes", str(team1specials[4])+" wild card canastas", team1out] endtext2 = ["Team 2:", str(team2round)+ " points", str(self.g.cardPoints(2)) + " face value", "-" + str(self.g.handPoints(2)) + " points in hand", str(team2specials[0])+" red canastas", str(team2specials[1])+" black canastas", str(team2specials[2])+" red threes", str(team2specials[4])+" wild card canastas", team2out] if (self.g.team1score>=5000) & (self.g.team1score>self.g.team2score): endtext1.append("Team 1 is the winner!") if (self.g.team2score>=5000) & (self.g.team2score>self.g.team1score): endtext2.append("Team 2 is the winner!") defaultStyle.init(gui) endStyle = {'font-color': (255,255,255), 'font': font.Font(None,24), 'autosize': True, "antialias": True,'border-width': False, 'border-color': (0,0,0), 'wordwrap': False} self.desktop.query = Window(position = (250,180), size = (500,400), parent = self.desktop, text = "Round Over", closeable = False, shadeable = False) labelStyleCopy = gui.defaultLabelStyle.copy() Label(position = (200,40),size = (100,0), parent = self.desktop.query, text = "Round " + str(self.g.round) + " over", style = endStyle) for pos, t in enumerate(endtext1): Label(position = (50,80+25*pos),size = (200,0), parent = self.desktop.query, text = t, style = endStyle) for pos, t in enumerate(endtext2): Label(position = (300,80+25*pos),size = (200,0), parent = self.desktop.query, text = t, style = endStyle) Cont_button = Button(position = (200,350), size = (70,0), parent = self.desktop.query, text = "Continue") Cont_button.onClick = ContinueOnClick def genHelp(self): def helpClose(button): self.p.viewhelp = 0 self.desktop.query.wintype = None self.desktop.query.position = (0,0) self.desktop.query.size = (0,0) text = [ "Canasty v0.1", "-----------------------", "F1 - Display this help text.", "ESC - Quit.", "Click the scoreboard to re-sort your cards", "Click the pile to draw a card", "Click the discard pile to pick it up", "(first stage or select cards", "that you need to meld)", "Drag a card to the pile to discard it", "Select cards and right-click to meld", "(or drag them onto an existing meld)", "Drag melds to the stage area to stage them", "Left-click the stage to meld it", "(right-click to clear it)", "-----------------------", "See manual for alternate keyboard controls"] defaultStyle.init(gui) helpStyle = {'font-color': (255,255,255), 'font': font.Font(None,24), 'autosize': True, "antialias": True,'border-width': False, 'border-color': (0,0,0), 'wordwrap': False} self.desktop.query = Window(position = (300,120), size = (400,500), parent = self.desktop, text = "Help", closeable = True, shadeable = False) self.desktop.query.onClose = helpClose self.desktop.query.wintype = "help" labelStyleCopy = gui.defaultLabelStyle.copy() for pos, t in enumerate(text): Label(position = (30,35+25*pos),size = (200,0), parent = self.desktop.query, text = t, style = helpStyle) def genChat(self,x,y): defaultStyle.init(gui) self.chatwin = Window(position = (x,y), size = (280,130), parent = self.desktop, text = "", closeable = False, shadeable = True) labelStyleCopy = gui.defaultLabelStyle.copy() labelStyleCopy['autosize'] = False labelStyleCopy['wordwrap'] = True labelStyleCopy['font'] = pygame.font.Font("FreeSans.ttf", 12) textboxStyleCopy = gui.defaultTextBoxStyle.copy() textboxStyleCopy['border-width'] = 1 textboxStyleCopy['font'] = pygame.font.Font("FreeSans.ttf", 12) self.chatwin.chatdisplay = Label(position = (5,5),size = (270,105), parent = self.chatwin, text = "", style = labelStyleCopy) self.chatwin.chattxt = TextBox(position = (5,107), size = (270, 0), parent =self.chatwin, text = "", style = textboxStyleCopy) self.chatwin.enabled = True def DrawQuery(self): self.DrawGame(flip=False) #Flips! pygame.display.flip()