def __init__(self): super().__init__() self._board = Board() self._game_state = "UNFINISHED" self._players = (Player("b"), Player("w")) self._active = 0
def seed_db(): #from models.Book import Book from models.Board import Board from models.User import User from main import bcrypt from faker import Faker import random faker = Faker() users = [] for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(20): # book = Book() # book.title = faker.catch_phrase() # book.user_id = random.choice(users).id # db.session.add(book) board = Board() board.title = faker.catch_phrase() board.user_id = random.choice(users).id db.session.add(board) db.session.commit() print("Tables seeded")
def post(self, user, body): board = Board(body["title"]) user.boards.append(board) db.session.add(user) db.session.commit() boards = user.get_boards() return boards
def main(): board = Board(price=250, color=Color(128, 128, 128), weight=25.5, producer="Economix", material=Material.METAL, surface="chalky", size_of_surface=Size(150, 7.5, 100), type_of_frame="aluminium") calculator = Calculator(price=300, color=Color(128, 0, 128), weight=0.2, producer="Intel", material=Material.PLASTIC, type="engineering", bit_size=32, corpus_size=Size(10.0, 5.0, 0.5)) stapler = Stapler(price=25.5, color=Color(256, 128, 256), weight=1.2, producer="Buromax", material=Material.METAL_PLUS_PLASTIC, staples_size=0.7, power=25) stationery_knife = StationeryKnife(price=55.0, color=Color(128, 128, 128), weight=0.5, producer="ABC", material=Material.METAL, width_of_blade=10.8) office_appliances = list() office_appliances.append(board) office_appliances.append(calculator) office_appliances.append(stapler) office_appliances.append(stationery_knife) manager = OfficeAppliancesManager(office_appliances) print("List :\n") print_list(office_appliances) color = Color(red=128, green=128, blue=128) print("Selection by color" + str(color)) print_list(manager.find_by_color(color)) print("Sort by ascending by price\n") manager.sort_by_price(office_appliances, SortType.ASCENDING) print_list(office_appliances) print("Sort by descending by price\n") manager.sort_by_price(office_appliances, SortType.DESCENDING) print_list(office_appliances) print("Sort by ascending by weight\n") manager.sort_by_weight(office_appliances, SortType.ASCENDING) print_list(office_appliances) print("Sort descending by weight\n") manager.sort_by_weight(office_appliances, SortType.DESCENDING) print_list(office_appliances)
def board_create(): #Create a new board board_fields = board_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") new_board = Board() new_board.title = board_fields["title"] user.boards.append(new_board) db.session.commit() return jsonify(board_schema.dump(new_board))
class Game: def __init__(self): self.board = Board() self.player1 = Player('y') self.player2 = Player('x') def start_game(self): while self.board.x_keys_qtd > 0 and self.board.y_keys_qtd > 0: player = self.player2 if self.board.round % 2 == 0 else self.player1 self.board.print_info(player) [piece_pos_x, piece_pos_y, pos_x, pos_y] = map( int, input("Insert position of piece as: piece_pos_x, piece_pos_y, pos_to_x, pos_to_y\n").split(" ") ) self.board.walk_keys_to_pos(piece_pos_x, piece_pos_y, pos_x, pos_y, player)
def get(): """ :return: """ args = request.args bid = int(args.get('id')) b = Board.find_one(id=bid) # log('get board', b.json()) return json_response(b.json())
class Game(QObject): """ A game of Gess. Maintains two Players and a Board. """ def __init__(self): super().__init__() self._board = Board() self._game_state = "UNFINISHED" self._players = (Player("b"), Player("w")) self._active = 0 @Property(list) def board(self): return self._board @Property(str) def game_state(self): return self._game_state @game_state.setter def game_state(self, game_state): self._game_state = game_state @Property(object) def active_player(self): return self._players[self._active] @Property(object) def inactive_player(self): return self._players[self._active ^ 1] def make_move(self, source, target): """ Moves the piece and updates the state of the game. """ delta = self.get_delta(source, target) if not self.is_legal_direction(source, delta): raise IllegalMove("This is not a legal direction.") if not self.is_legal_distance(source, delta): raise IllegalMove("This is not a legal distance.") self._board.move_piece(source, target) self._board.clear_gutter() # Check if move leaves the active player without rings try: self.update_rings() except IllegalMove as e: # Replace pieces before propagating the error for piece in (source, target): self._board.place_piece(piece) raise e self._board.selected = None self.check_win_condition() self.switch_turn() return source, target @staticmethod def is_legal_direction(piece, delta): """ A piece is a 3x3 square in the form {(row, col): stone}, delta is a tuple indicating (row_change, col_change) of a move. """ # Check if horizontal, vertical, or diagonal if not (delta[0] == 0 or delta[1] == 0 or abs(delta[0]) == abs(delta[1])): return False # Check if no move direction if delta[0] == 0 and delta[1] == 0: return False # View of board is inversion of 2D-board list; index 4 is center square stones = list(piece.values()) unit_delta = [1 if x > 0 else -1 if x < 0 else 0 for x in delta] if stones[4 + unit_delta[0] * 3 + unit_delta[1]] == "": return False return True def is_legal_distance(self, piece, delta): """ A piece is a 3x3 square in the form {(row, col): stone}, delta is a tuple indicating (row_change, col_change) of a move. """ locations = list(piece.keys()) # The absolute value of any direction is the spaces moved; stones[4] is the center stone; if not (list(piece.values())[4] != "" or abs(delta[0]) <= 3 and abs(delta[1]) <= 3): return False # Get the series of steps taken to get to the full move, but not the final step unit_delta = [1 if x > 0 else -1 if x < 0 else 0 for x in delta] row_steps = range( unit_delta[0], delta[0], unit_delta[0]) if unit_delta[0] else [0] * abs(delta[1]) col_steps = range( unit_delta[1], delta[1], unit_delta[1]) if unit_delta[1] else [0] * abs(delta[0]) # Simulate the movement of the piece, one square at a time to check premature overlap for (row_step, col_step), location in product(zip(row_steps, col_steps), locations): row = location[0] + row_step col = location[1] + col_step # There is a stone in the movements path if (row, col ) not in locations and self.board.squares[row][col] != "": return False return True def update_rings(self): """ Updates both players rings. Raises an exception if the active player is attempting to destroy their last ring. """ rings = self._board.check_for_rings() active_player = self.active_player active_stone = active_player.stone active_rings = [ pos for pos, stone in rings.items() if stone == active_stone ] inactive_rings = [ pos for pos, stone in rings.items() if stone != active_stone ] # Prevent the active player from destroying their last rings if len(active_rings) == 0: raise IllegalMove("Unable to break last ring") active_player.rings = active_rings self.inactive_player.rings = inactive_rings def switch_turn(self): """ Switches the active player. """ self._active ^= 1 def check_win_condition(self): """ Checks if the active player has won. """ if not self.inactive_player.has_rings(): self._game_state = self.active_player.color + "_WON" @staticmethod def get_delta(origin, target): """ Returns the (row_change, col_change) between two pieces. """ origin_square = list(origin.keys())[0] target_square = list(target.keys())[0] return target_square[0] - origin_square[0], target_square[ 1] - origin_square[1]
def check(): data = request.get_json() board_id = data.get('id') b = Board.get(board_id) b.update({'content': data.get('content')}) return jsonify(status='ok')
from discord.ext import commands from models.Board import Board board = Board() class TicTacToe(commands.Cog): @commands.command(aliases=['ttt'], brief='Show TicTacToe rules.') async def tictactoe(self, ctx: commands.Context): board.reset_board() await ctx.send(embed=board.get_guide_embed()) await ctx.send(embed=board.get_board_embed()) await ctx.send(embed=board.get_turn_embed()) @commands.command(aliases=['m'], brief='Make a move in TicTacToe board.', description='Valid inputs: 1-9') async def move(self, ctx: commands.Context, *args): message = ' '.join(*args) status = board.get_move_status(message) if status != board.SUCCESS_MESSAGE: await ctx.send(status) return move = int(message) board.place(move) await ctx.send(embed=board.get_board_embed()) # Print out message and reset game if there is a winner or tie.
def __init__(self): self.board = Board() self.player1 = Player('y') self.player2 = Player('x')