def main() : myBoard = Board() myBoard.draw() humanPlayer = input( "Pick your token: " ).upper() while humanPlayer not in availableTokens : print( "Please enter a valid token." ) humanPlayer = input( "Pick your token: " ).upper() human = Player( humanPlayer ) computer = Player( "O" ) if humanPlayer != "O" else Player( "X" ) while myBoard.availableMoves() : humanMove = int( input( "Enter your move: " ) ) while not myBoard.isValidMove( humanMove ) : print( "Please enter a valid move." ) humanMove = int( input( "Enter your move: " ) ) myBoard.makeMove( human.getToken(), humanMove ) myBoard.draw() if myBoard.isWinner( human.getToken(), humanMove ) : print( "{} wins!!!".format( human.getToken() ) ) break if myBoard.availableMoves() : computerMove = myBoard.findBestMove( computer.getToken() ) myBoard.makeMove( computer.getToken(), computerMove ) myBoard.draw() if myBoard.isWinner(computer.getToken(), computerMove) : print("{} wins!!!".format(computer.getToken())) break
def second_move(self): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[2], "b6") assert player2.place_piece(player2.pieces[2], "f6")
def main(): board = ExtremeBoard() #set up boards, players players = [Player(1), Player(2)] while board.extreme_board_playable: #play until someone has won print('building board...') board.RenderExtremeBoard() print('player 1 turn...') board.ChooseBoard(players[0]) print('checking for winner...') if board.CheckIfXBWon(): break board.RenderExtremeBoard() board.ChooseBoard(players[1]) if board.CheckIfXBWon(): break #TODO: just make this a little more spiffy, maybe an if statement for alt. message if there's no winner...and set up the asterisks to adjust??? idk if board.extreme_board_won_by == '[X]': winner = '1' else: winner = '2' print('*******************************') print('\tPlayer ' + winner + ' won!') print('*******************************')
def first_move(self): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[2], "a7") assert player2.place_piece(player2.pieces[2], "g7")
def place_all_pieces(): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[1], "a7") player2.place_piece(player2.pieces[1], "g7") player1.place_piece(player1.pieces[2], "a4") player2.place_piece(player2.pieces[2], "g4") player1.place_piece(player1.pieces[3], "d1") player2.place_piece(player2.pieces[3], "d5") player1.place_piece(player1.pieces[4], "b6") player2.place_piece(player2.pieces[4], "f6 ") player1.place_piece(player1.pieces[5], "c5") player2.place_piece(player2.pieces[5], "b4") player1.place_piece(player1.pieces[6], "f4") player2.place_piece(player2.pieces[6], "e3") player1.place_piece(player1.pieces[7], "d6") player2.place_piece(player2.pieces[7], "c4") player1.place_piece(player1.pieces[8], "e4") player2.place_piece(player2.pieces[8], "d2") player1.place_piece(player1.pieces[0], "d3") player2.place_piece(player2.pieces[0], "d6") assert True
def phase2_remove_test(): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[1], "a7") player2.place_piece(player2.pieces[1], "g7") player1.place_piece(player1.pieces[2], "a4") player2.place_piece(player2.pieces[2], "g4") player1.place_piece(player1.pieces[3], "d1") player2.place_piece(player2.pieces[3], "d5") player1.place_piece(player1.pieces[4], "b6") player2.place_piece(player2.pieces[4], "f6 ") player1.place_piece(player1.pieces[5], "c5") player2.place_piece(player2.pieces[5], "b4") player1.place_piece(player1.pieces[6], "f4") player2.place_piece(player2.pieces[6], "e3") player1.place_piece(player1.pieces[7], "d6") player2.place_piece(player2.pieces[7], "c4") player1.place_piece(player1.pieces[8], "e4") player2.place_piece(player2.pieces[8], "d2") player1.place_piece(player1.pieces[0], "d3") player2.place_piece(player2.pieces[0], "d6") player1.move_piece(player1.pieces[3], "a1") player2.remove_piece(player2, player1.pieces[3]) assert player2.get_phase() == 2
def third_move(self): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[2], "c5") assert player2.place_piece(player2.pieces[2], "e5")
def test_check_win_a(): p1 = Player("X") p2 = Player("O") board = Board(p1, p2) board.add_move(3, p2) board.add_move(3, p1) board.add_move(2, p2) board.add_move(4, p1) assert board.check_winner()[0] == False
def test_check_win_b(): p1 = Player("X") p2 = Player("O") board = Board(p1, p2) board.board = [['X', ' ', ' ', ' ', ' ', ' ', ' '], ['X', ' ', ' ', ' ', ' ', ' ', ' '], ['X', ' ', ' ', ' ', ' ', ' ', ' '], ['O', ' ', ' ', 'O', ' ', ' ', ' '], ['X', ' ', ' ', 'O', ' ', ' ', ' '], ['X', 'O', ' ', 'O', ' ', ' ', ' ']] assert board.check_winner()[0] == False
def phase1_remove_test(): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[1], "a7") player2.place_piece(player2.pieces[1], "g7") player1.place_piece(player1.pieces[2], "a4") player2.place_piece(player2.pieces[2], "g4") player1.place_piece(player1.pieces[3], "a1") player2.place_piece(player2.pieces[3], "d1") player2.remove_piece(player2, player1.pieces[3]) assert player2.get_phase() == 1
def __init__(self, count_of_players): self.game_board = Board() self.count_of_players = count_of_players self.player_list = [] self.player_color_list = [RED, GREEN, BLUE] for i in range(self.count_of_players): name = input("Enter Name for Player 1 : ") self.player_list.append(Player(name, self.player_color_list[i]))
def piece_gone(): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) player1.place_piece(player1.pieces[1], "a7") player2.place_piece(player2.pieces[1], "g7") player1.place_piece(player1.pieces[2], "a4") player2.place_piece(player2.pieces[2], "g4") player1.place_piece(player1.pieces[3], "a1") player2.place_piece(player2.pieces[3], "d1") player2.remove_piece(player2, player1.pieces[3]) piece_removed = True for item in player2.get_placed_pieces(): if item == player2.pieces[3]: piece_removed = False assert piece_removed
def _on_move(self, ws, data): try: x, y, player, ai = data['x'], data['y'], data['player'], data['ai'] except KeyError: IO.write_error('error in the incoming request') return if ai: self.game.board.move_player(Player.OPPONENT, x, y) self.game.brain.turn() else: self.game.board.move_player(Player(player), x, y)
def gameFrame(window, stack): clerkDialogManager = DialogManager('redneck') diag = DialogFrame(stack, window, clerkDialogManager, playerImage='media/avatars/protag.png', npcImage='media/avatars/notnigel.png') b = Board((20,20)) for x in range(0,20): for y in range(0,20): b.spaces[x][y] = Tile('media/tiles/basic.png', []) p = Player(20, 10, None, None, None, 4) p.enableRender() b.placeEntity(p, 3, 4) b.placeEntity(Clerk(20, 10, None, diag), 5,5) return GameplayFrame(stack, window, b, p)
def gameFrame(window, stack): # b = Board(None, filename='media/maps/test') b = Board((20, 20)) for x in range(0, 20): for y in range(0, 20): b.spaces[x][y] = Tile('media/tileproto1.png', []) p = Player(20, 10, None, None, None, 4) p.makeRenderable(window) b.placeEntity(p, 3, 4) b.placeEntity(Clerk(20, 10, None, 'redneck'), 5, 5) return GameplayFrame(stack, window, b, p)
def pieces_display(): board = Board() player1 = Player("Player 1", board) player2 = Player("player 2", board) assert player1.pieces
def second_node(self): board = Board() player1 = Player("Player 1", board) assert player1.place_piece(player1.pieces[0], "f4")
def third_node(self): board = Board() player1 = Player("Player 1", board) assert player1.place_piece(player1.pieces[1], "c5")
def main(word, board, hint, type): pygame.display.set_caption("Word Cluttered") background = pygame.Surface(screen1.get_size()) screen1.blit(background, (0, 0)) pygame.display.flip() global guess guess = [] circle = Letters("", board, 0, 0, 0, 0, "letters", type, True) Letters.__init__(circle, word, board, 0, 0, 0, 0, "letters", type, True) allSprites = pygame.sprite.Group(circle) keepGoing = True print(str(circle)) screen1.fill((206, 185, 177)) allSprites.clear(screen1, background) tempX = 0 tempY = 0 used = [True for x in range(len(board)**2)] affected = [] circleList = colorboard(board, word, affected, type, True) clock = pygame.time.Clock() soloCount = 0 totalCount = len(word) popWords = [] msg = "" while keepGoing: global score if type == "multi": global milliseconds global seconds global minutes if milliseconds > 1000: seconds += 1 milliseconds -= 1000 if seconds > 60: minutes += 1 seconds -= 60 if minutes > 1: screen.Multi.timesUp(score) timer(minutes, seconds) milliseconds += clock.tick_busy_loop(60) totalScore() theme(hint) image = pygame.image.load("images/mm.png").convert_alpha() rect = image.get_rect() image = pygame.transform.scale(image, (175, 50)) rect.center = [180, 50] screen1.blit(image, rect) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False pressed1, pressed2, pressed3 = pygame.mouse.get_pressed() currentLetters = -1 if pressed1: x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] # inspired from https://stackoverflow.com/questions/43641282/how-would-i-go-about-making-clickable- # text-in-pygame tx = width//2 - (len(hint)//2 * 20) if x in range(tx, tx+100) and y in range(30, 60): define(hint) pass if x < 175 and y < 37: screen.Multi.mainMenu((175, 50), 180, 50) for circle in circleList: currentLetters += 1 circleX, circleY = circle.position() x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] r = 40 if circleX + r >= x >= circleX - r and circleY + r >= y >= circleY - r and used[currentLetters]: if len(guess) == 0 and circle.letter != 0: guess.append(circle.letter) prevX = circle.index()[0] prevY = circle.index()[1] used[currentLetters] = False affected.append(currentLetters) elif len(guess) > 0 and legal(circle.index()[0], circle.index()[1], prevX, prevY) and circle.letter != 0: tempX = prevX # for when the user deletes their answer choice tempY = prevY guess.append(circle.letter) prevX = circle.index()[0] prevY = circle.index()[1] used[currentLetters] = False affected.append(currentLetters) if type == "solo": soloAnswer(guess, word, popWords) else: answer(guess, word) colorboard(board, word, affected, type) global numHints if event.type == pygame.KEYDOWN and pygame.key.name(event.key) == 'space': print("space") numHints = 0 guess = [] used = [True for x in range(len(board)**2)] for i in range(len(popWords)): word.insert(0, popWords[i]) score -= 1 popWords = [] affected = [] soloCount = 0 colorboard(board, word, affected, type) soloAnswer(guess, word, popWords) if event.type == pygame.KEYDOWN and pygame.key.name(event.key) == 'h' and numHints < 5: i = 0 next = "" size = 30 for j in range(len(word[i])): if j < len(guess) and guess[j] == (word[i])[j]: next = (word[i])[j+1] elif j < len(guess) and guess[j] != (word[i])[j]: next = "Wrong Guess" if next == "": next = word[i][0] myfont = pygame.font.SysFont("serif", 30) pygame.draw.rect(screen1, (206, 185, 177), (500, 40, 140, 80)) label = myfont.render("Hint: ", 1, (0, 0, 0)) screen1.blit(label, (520, 40)) if next == "Wrong Guess": myfont = pygame.font.SysFont("serif", 25) label2 = myfont.render("Wrong", 1, (0, 0, 0)) label3 = myfont.render("Guess", 1, (0, 0, 0)) screen1.blit(label2, (520, 60)) screen1.blit(label3, (520, 80)) else: myfont = pygame.font.SysFont("serif", 40) label2 = myfont.render(next.upper(), 1, (0, 0, 0)) screen1.blit(label2, (530, 65)) numHints += 1 if len(guess) > 0 and event.type == pygame.KEYDOWN and pygame.key.name(event.key) == 'backspace': prevX = tempX prevY = tempY index = affected.pop() used[index] = True guess.pop() if type == "solo": soloAnswer(guess, word, popWords) else: answer(guess, word) colorboard(board, word, affected, type) if type == "solo": for i in range(len(word)): if yesWon(guess, word[i]): popWords.append(word.pop(0)) guess = [] soloCount += 1 score += 1 break if soloCount == totalCount: screen1.fill((0, 0, 0)) pygame.display.update() print("winner! Score: ", score) sologame() elif won(guess, word, type): print("winner! Score: ", score) screen1.fill((0, 0, 0)) pygame.display.update() guess = [] score += 1 msg = "point" from board import Player Player(word, hint, msg) if type == "multi": import board board.start(False) else: sologame() pygame.display.flip() clock.tick(200) pygame.mouse.set_visible(True) pygame.quit()
def paint(self): self.updateCam() self.window.blit(self.buffer, (self.cameraX, self.cameraY)) if __name__ == '__main__': pygame.init() fpsClock = pygame.time.Clock() window = pygame.display.set_mode((600, 600)) pygame.display.set_caption('72 --- gameTest') b = Board((20, 20)) for x in range(0, 20): for y in range(0, 20): b.spaces[x][y] = Tile('media/tileproto1.png', []) p = Player(20, 10, None, None, None, 4) b.placeEntity(p, 3, 4) b.placeEntity(Enemy(20, 10, None, None, None, 10), 5, 5) g = GameplayFrame(None, window, b, p) while True: window.fill(pygame.Color(255, 255, 255)) if not runStack([g]): break pygame.display.update() fpsClock.tick(30)
from board import Board from board import Player import ai_player computer_player = Player("X") human_player = Player("O") current_board = Board(computer_player, human_player) print(current_board) while True: current_board.add_move(input("Select move"), human_player) print(current_board) if current_board.check_winner()[0]: print(f"Player {current_board.check_winner()[1]} is the winner") break if len(current_board.get_moves()) == 0: print("Game is a draw") current_board.add_move( ai_player.get_best_move(current_board, computer_player, human_player), computer_player) print(current_board) if current_board.check_winner()[0]: print(f"Player {current_board.check_winner()[1]} is the winner") break if len(current_board.get_moves()) == 0: print("Game is a draw")
def first_node(self): board = Board() player1 = Player("Player 1", board) assert player1.place_piece(player1.pieces[2], "a7")
#Oceania earth["westaustralia"].add_conns("newguinea", "eastaustralia", "madagascar", "indonesia") earth["newguinea"].add_conns("indonesia", "eastaustralia") #Africa earth["madagascar"].add_conns("southafrica", "eastafrica") earth["congo"].add_conns("southafrica", "eastafrica", "northafrica") earth["egypt"].add_conns("eastafrica", "northafrica", "south") earth["northafrica"].add_conns("west", "brasil", "eastafrica") earth["southafrica"].add_conns("eastafrica") #South America earth["brasil"].add_conns("venezuela", "argentina", "peru") earth["peru"].add_conns("venezuela", "argentina") #global earth.purify_transitions() earth.activate_transitions() earth.setmap("worldmap") earth.name = "earth" return earth ## Players & Countries earth = create_earth() blue = Country("blue", (10, 0, 150)) red = Country("red", (150, 10, 0)) players = [Player("Bluka", [blue]), Player("Redna", [red])]
def start_game(): clock = pygame.time.Clock() board = Board() turns = 0 MAX_TURNS = 500 play_ai = None choice = Choice("Human", "Computer") choice.fade_in_sec = 6.5 screen = pygame.display.set_mode(WIN_SIZE) while play_ai is None: events = pygame.event.get() mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) for e in events: if e.type == pygame.QUIT: return 0 if e.type == pygame.MOUSEBUTTONDOWN: if play_ai is None: picked = choice.is_clicked(mouse_pos) if picked == 1: play_ai = False player1 = Player("Player 1", 1, board) player2 = Player("Player 2", 2, board) elif picked == 2: play_ai = True player1 = Player("Player 1", 1, board) player2 = AI_Player("Computer", 2, board, player1) elif pygame.Rect(0,0,10,10).collidepoint(mouse_pos): play_ai = "yes" player1 = AI_Player("C3PO", 1, board, None) player2 = AI_Player("R2D2", 2, board, player1) player1.opponent = player2 choice.draw(screen) pygame.display.flip() clock.tick(60) gui = Gui(board, player1, player2) player_toggle = itertools.cycle([player1, player2]) current_player = next(player_toggle) selected_piece = None prev_selected_piece = None prev_selected_piece_pos = None can_remove_piece = False game_won_by = None while True: if isinstance(current_player, AI_Player) and not game_won_by: if current_player.get_phase() == Phase.FLYING: AI_Player.DEPTH = 2 move = current_player.get_best_move() log.info("Move score: %d", move.score) if move.phase == Phase.PLACING: piece = list(current_player.pieces.values())[0] if current_player.place_piece(piece, move.dest): node = gui.find_node(board.board[move.dest]) gui.find_piece(piece).move(*node.xy) log.info("Placed %s", piece) else: log.critical("AI BROKE. COULD NOT PLACE.") else: piece = board.board[move.src].piece if current_player.move_piece(piece, move.dest): node = gui.find_node(board.board[move.dest]) gui.find_piece(piece).move(*node.xy) log.info("Moved %s piece from %s", piece, move.src) else: log.critical("AI BROKE. COULD NOT MOVE.") if move.remove: other_player = next(player_toggle) piece = board.board[move.remove].piece if other_player.remove_piece(piece): for player in gui.players.values(): if player.player is piece.player: player.remove_piece(piece.id) log.info("Computer removed %s piece", piece) else: log.critical("AI BROKE. COULD NOT REMOVE.") next(player_toggle) # skip the next cycle to return to normal turns += 1 current_player = next(player_toggle) events = pygame.event.get() mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) mouse_btns = pygame.mouse.get_pressed() for e in events: if e.type == pygame.QUIT: return 0 if e.type == pygame.MOUSEBUTTONDOWN and mouse_btns == (1,0,0): selected_piece = gui.get_piece(mouse_pos) if selected_piece: log.info("%s piece picked up", selected_piece) prev_selected_piece_pos = selected_piece.xy if e.type == pygame.MOUSEBUTTONUP and (mouse_btns == (0,0,0) or mouse_btns == (0,0,1)): log.info("%s piece dropped", selected_piece) prev_selected_piece = selected_piece selected_piece = None if game_won_by: pass elif selected_piece and not can_remove_piece: selected_piece.move(*mouse_pos) elif prev_selected_piece: action_succesful = False node = gui.get_node(mouse_pos) if can_remove_piece: other_player = next(player_toggle) if other_player.remove_piece(prev_selected_piece.piece): for player in gui.players.values(): if player.player is prev_selected_piece.piece.player: player.remove_piece(prev_selected_piece.piece.id) log.info("Remove %s piece", prev_selected_piece.piece) action_succesful = True can_remove_piece = False next(player_toggle) # skip the next cycle to return to normal elif node: log.info("Over %s node", node) if current_player.get_phase() == Phase.PLACING: if current_player.place_piece(prev_selected_piece.piece, node.node.name): prev_selected_piece.move(*node.xy) action_succesful = True else: if node.node in prev_selected_piece.piece.node.neighbors() or current_player.can_fly(): if current_player.move_piece(prev_selected_piece.piece, node.node.name): prev_selected_piece.move(*node.xy) action_succesful = True else: log.info("Could move %s piece to %s node. Not a neighbor", prev_selected_piece, node) if not action_succesful: prev_selected_piece.move(*prev_selected_piece_pos) else: if prev_selected_piece.piece in current_player.get_mills(): can_remove_piece = True else: current_player = next(player_toggle) prev_selected_piece = None if turns >= MAX_TURNS or \ not player1.can_move() and not player2.can_move() and \ not player1.pieces and len(player1.get_placed_pieces()) < MIN_NUM_PIECES and \ not player2.pieces and len(player2.get_placed_pieces()) < MIN_NUM_PIECES: game_won_by = "TIE! Everyone" elif not player1.can_move() or not player1.pieces and len(player1.get_placed_pieces()) < MIN_NUM_PIECES: game_won_by = player2 elif not player2.can_move() or not player2.pieces and len(player2.get_placed_pieces()) < MIN_NUM_PIECES: game_won_by = player1 gui.draw_board() if game_won_by: gui.game_message("%s WON!" % (game_won_by)) elif not can_remove_piece: gui.game_message("%s turn" % (current_player)) else: gui.game_message("%s remove piece from other player." % (current_player)) choice = Choice("Quit", "Play new game") while game_won_by is not None: events = pygame.event.get() mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) for e in events: if e.type == pygame.QUIT: return 0 if e.type == pygame.MOUSEBUTTONDOWN: picked = choice.is_clicked(mouse_pos) if picked == 1: return 0 elif picked == 2: return 1 gui.draw_board() gui.game_message("%s WON!" % (game_won_by)) gui.draw_pieces() choice.draw(screen) pygame.display.flip() clock.tick(60) gui.draw_pieces() gui.debug_message(str(mouse_pos), gui.tell(mouse_pos)) pygame.display.flip() clock.tick(60)