def post(self, request): """Check form data, and send the result if form is valid""" form = GameForm(request.POST) context = {'form': form} if form.is_valid(): # Initialize instance of game game = GameManager(number_of_players=form.cleaned_data.get( 'number_of_players', 0), sequence=form.cleaned_data.get('board', ''), deck=form.cleaned_data.get('deck', '')) game_result = game.result() # Save game data at database self.model.objects.create( players=form.cleaned_data.get('number_of_players', None), squares=form.cleaned_data.get('number_of_squares', None), cards=form.cleaned_data.get('number_of_cards', None), sequence=form.cleaned_data.get('board', None), deck=form.cleaned_data.get('deck', None), result=game_result) # Send result of game to client side context['result'] = game_result else: context['error'] = 'Form is not valid!' return render(request, self.template_name, context)
class Game: def __init__(self): '''Game module is top level class, handles game loop.''' self.frame_time = int(10**3 / config['game']['fps']) # in msec self.window = self.create_window() self.manager = GameManager(self.window) def run(self): '''Main loop of the game, loops as long as window not receive quit signal. ''' running = True while running: self.manager.update() self.manager.draw() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.time.delay(self.frame_time) pygame.quit() def create_window(self): '''Initializes pygame window with parameters from config file Returns: Surface: initialized pygame window ''' pygame.init() window = pygame.display.set_mode( (config['window']['width'], config['window']['height'])) pygame.display.set_caption(config['game']['name']) return window
def test_board_eval(game: GameManager): game.make_move(0) game.make_move(1) game.make_move(2) game.make_move(2) game.make_move(1) game.make_move(1) game.make_move(0) assert game.state.evaluate_board == 2 game.make_move(0) assert game.state.evaluate_board == -7
def final_room(request, room_id): """ Рендеринг комнаты финального этапа :param request: запрос :param room_id: номер конаты """ final_room = GameManager.get_final_game().get_room_by_id(int(room_id)) return render(request, 'game/room.html', {'room': final_room})
def test_game_manager(game: GameManager): game.make_move(0) game.make_move(1) game.make_move(0) game.make_move(1) game.make_move(0) assert game.make_move(1) is None assert game.make_move(0) is game.state.next_player
def test_win_by_tsumo_and_scores_calculation(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() manager.riichi_sticks = 1 winner = clients[0] manager.process_the_end_of_the_round(list(range(0, 14)), 0, winner, None, True) self.assertEqual(winner.player.scores, 26900) self.assertEqual(clients[1].player.scores, 24700) self.assertEqual(clients[2].player.scores, 24700) self.assertEqual(clients[3].player.scores, 24700)
def room(request, room_id): """ Рендеринг комнаты отборочного этапа :param request: запрос :param room_id: номер конаты """ qualifying_room = GameManager.get_qualifying_game().get_room_by_id( int(room_id)) return render(request, 'game/room.html', {'room': qualifying_room})
def test_play_round_with_retake(self): game.game_manager.shuffle_seed = lambda: 0.01 clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.set_dealer(3) manager.init_round() result = manager.play_round() self.assertEqual(manager.round_number, 1) self.assertEqual(result['is_tsumo'], False) self.assertEqual(result['is_game_end'], False) self.assertEqual(result['winner'], None) self.assertEqual(result['loser'], None)
class GameTestCase(TestCase): def setUp(self): self.case1 = GameManager(number_of_players=2, sequence='RYGPBRYGBRPOP', deck='R, B, GG, Y, P, B, P, RR') self.case2 = GameManager(number_of_players=2, sequence='RYGRYB', deck='R, YY, G, G, B') self.case3 = GameManager(number_of_players=3, sequence='QQQQQQQQQ', deck='Q, QQ, Q, Q, QQ, Q') def test_game_results_is_valid(self): self.assertEqual(self.case1.result(), 'Player 1 won after 7 cards.') def test_game_two_win_results_is_valid(self): self.assertEqual(self.case2.result(), 'Player 2 won after 4 cards.') def test_game_results_with_common_squares_valid(self): self.assertEqual(self.case3.result(), 'No player won after 6 cards.')
class FastGame(IGameType): def __init__(self, *args, **kwargs): self._game_manager = GameManager() self.room = None self.players = [] def enter(self, player_id): if player_id in self.players: print('xxoo') return player = Player(player_id) self.players.append(player) def start(self): if not self.players: return self._game_manager.start() def check(self): self._game_manager.check() def chupai(self): self._game_manager.fapai()
def test_init_scores_and_recalculate_position(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.set_dealer(3) clients[0].player.scores = 24000 clients[1].player.scores = 23000 clients[2].player.scores = 22000 clients[3].player.scores = 21000 manager.recalculate_players_position() self.assertEqual(clients[0].player.scores, 24000) self.assertEqual(clients[0].player.position, 1) self.assertEqual(clients[1].player.scores, 23000) self.assertEqual(clients[1].player.position, 2) self.assertEqual(clients[2].player.scores, 22000) self.assertEqual(clients[2].player.position, 3) self.assertEqual(clients[3].player.scores, 21000) self.assertEqual(clients[3].player.position, 4) clients[0].player.scores = 24000 clients[1].player.scores = 24000 clients[2].player.scores = 22000 clients[3].player.scores = 22000 manager.recalculate_players_position() self.assertEqual(clients[0].player.scores, 24000) self.assertEqual(clients[0].player.position, 1) self.assertEqual(clients[1].player.scores, 24000) self.assertEqual(clients[1].player.position, 2) self.assertEqual(clients[2].player.scores, 22000) self.assertEqual(clients[2].player.position, 3) self.assertEqual(clients[3].player.scores, 22000) self.assertEqual(clients[3].player.position, 4)
def game(request): """ Рендеринг первоначальной страницы с комнатами отборочного этапа :param request: запрос :return: рендер страницы """ first_free_qualifying_room = GameManager.get_qualifying_game( ).is_free_rooms() if first_free_qualifying_room is None: return render(request, 'game/game.html', {'rooms_exhausted': True}) return render(request, 'game/game.html', {'room': first_free_qualifying_room})
def setUp(self): self.case1 = GameManager(number_of_players=2, sequence='RYGPBRYGBRPOP', deck='R, B, GG, Y, P, B, P, RR') self.case2 = GameManager(number_of_players=2, sequence='RYGRYB', deck='R, YY, G, G, B') self.case3 = GameManager(number_of_players=3, sequence='QQQQQQQQQ', deck='Q, QQ, Q, Q, QQ, Q')
def test_is_game_end_by_negative_scores(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.set_dealer(0) manager.init_round() winner = clients[0] loser = clients[1] loser.player.scores = 500 result = manager.process_the_end_of_the_round(list(range(0, 14)), 0, winner, loser, False) self.assertEqual(loser.player.scores, -500) self.assertEqual(result['is_game_end'], True)
def execute(self): player1 = Player(Alliance.RED, PlayerType.HUMAN, name="Dia") player2 = Player(Alliance.YELLOW, PlayerType.COMPUTER) SelectDifficultyCommand().execute() gm = GameManager(player1, player2) care_taker = Caretaker(gm) winner = None while not gm.game_over and not gm.draw: self.__print_format_table(gm) move = gm.state.next_player.get_move if isinstance(move, str): UndoCommand(gm, care_taker).execute() care_taker.backup() winner = self.__handle_move(move, gm, care_taker) self.__print_format_game_over(gm, winner)
def test_init_round(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() self.assertEqual(len(manager.dead_wall), 14) self.assertEqual(len(manager.dora_indicators), 1) self.assertIsNotNone(manager.current_client) self.assertEqual(manager.round_number, 0) self.assertEqual(manager.honba_sticks, 0) self.assertEqual(manager.riichi_sticks, 0) self.assertEqual([i.player.scores for i in manager.clients], [25000, 25000, 25000, 25000]) for client in clients: self.assertEqual(len(client.player.tiles), 13) self.assertEqual(len(manager.tiles), 70)
def mgr(): return GameManager()
from uuid import uuid4 from flask import Flask, render_template, session from flask_socketio import SocketIO, send, emit from api import GameServerAPI from game.game_manager import GameManager zombie_app: Flask = Flask(__name__) zombie_app.config['SECRET_KEY'] = 'Ajdjeu234Jfjsd!lsd@#@33jA' socketio = SocketIO(zombie_app, cors_allowed_origins='*') api = GameServerAPI(GameManager(), session, emit, send, zombie_app.logger) socketio.on_namespace(api) @zombie_app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': socketio.run(zombie_app, debug=True, host='0.0.0.0')
def test_win_by_ron_and_scores_calculation(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() winner = clients[0] loser = clients[1] # only 1000 hand manager.process_the_end_of_the_round(list(range(0, 14)), 0, winner, loser, False) self.assertEqual(winner.player.scores, 26000) self.assertEqual(loser.player.scores, 24000) winner.player.scores = 25000 winner.player.dealer_seat = 1 loser.player.scores = 25000 manager.riichi_sticks = 2 manager.honba_sticks = 2 manager.process_the_end_of_the_round(list(range(0, 14)), 0, winner, loser, False) self.assertEqual(winner.player.scores, 28600) self.assertEqual(loser.player.scores, 23400) self.assertEqual(manager.riichi_sticks, 0) self.assertEqual(manager.honba_sticks, 0) winner.player.scores = 25000 winner.player.dealer_seat = 0 loser.player.scores = 25000 manager.honba_sticks = 2 # if dealer won we need to increment honba sticks manager.process_the_end_of_the_round(list(range(0, 14)), 0, winner, loser, False) self.assertEqual(winner.player.scores, 26600) self.assertEqual(loser.player.scores, 23400) self.assertEqual(manager.honba_sticks, 3)
def __init__(self): '''Game module is top level class, handles game loop.''' self.frame_time = int(10**3 / config['game']['fps']) # in msec self.window = self.create_window() self.manager = GameManager(self.window)
def __init__(self, *args, **kwargs): self._game_manager = GameManager() self.room = None self.players = []
#! ./venv/bin/python3 from game.game_manager import GameManager import logging if __name__ == "__main__": logger = logging.getLogger('game') logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(levelname)s:%(message)s') ch.setFormatter(formatter) logger.addHandler(ch) logger.info('Starting the application...') gm = GameManager() gm.start()
def test_retake_and_honba_increment(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() # no one in tempai, so honba stick should be added manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(manager.honba_sticks, 1) manager.honba_sticks = 0 manager.set_dealer(0) clients[0].player.in_tempai = False clients[1].player.in_tempai = True # dealer NOT in tempai, no honba manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(manager.honba_sticks, 0) clients[0].player.in_tempai = True # dealer in tempai, so honba stick should be added manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(manager.honba_sticks, 1)
def test_is_game_end_by_eight_winds(self): clients = [Client() for _ in range(0, 4)] current_dealer = 0 manager = GameManager(clients) manager.init_game() manager.set_dealer(current_dealer) manager.init_round() manager._unique_dealers = 1 for x in range(0, 7): # to avoid honba client = current_dealer == 0 and 1 or 0 result = manager.process_the_end_of_the_round( list(range(0, 14)), 0, clients[client], None, True) self.assertEqual(result['is_game_end'], False) self.assertNotEqual(manager.dealer, current_dealer) current_dealer = manager.dealer result = manager.process_the_end_of_the_round(list(range(0, 14)), 0, clients[0], None, True) self.assertEqual(result['is_game_end'], True)
def test_init_game(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() self.assertTrue(manager.dealer in [0, 1, 2, 3])
def test_change_dealer_after_end_of_the_round(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.set_dealer(0) manager.init_round() # retake. dealer is NOT in tempai, let's move a dealer position manager.process_the_end_of_the_round(list(range(0, 14)), None, None, None, False) self.assertEqual(manager.dealer, 1) # retake. dealer is in tempai, don't move a dealer position clients[1].player.in_tempai = True manager.process_the_end_of_the_round(list(range(0, 14)), 0, None, None, False) self.assertEqual(manager.dealer, 1) # dealer win by ron, don't move a dealer position manager.process_the_end_of_the_round(list(range(0, 14)), 0, None, None, False) self.assertEqual(manager.dealer, 1) # dealer win by tsumo, don't move a dealer position manager.process_the_end_of_the_round(list(range(0, 14)), 0, None, None, False) self.assertEqual(manager.dealer, 1) # NOT dealer win by ron, let's move a dealer position manager.process_the_end_of_the_round(list(range(0, 14)), 0, clients[3], clients[2], False) self.assertEqual(manager.dealer, 2) # NOT dealer win by tsumo, let's move a dealer position manager.process_the_end_of_the_round(list(range(0, 14)), 0, clients[1], None, True) self.assertEqual(manager.dealer, 3)
def test_call_riichi(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() client = clients[0] self.assertEqual(manager.riichi_sticks, 0) self.assertEqual(client.player.scores, 25000) self.assertEqual(client.player.in_riichi, False) manager.call_riichi(client) self.assertEqual(manager.riichi_sticks, 1) self.assertEqual(client.player.scores, 24000) self.assertEqual(client.player.in_riichi, True) clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() manager.call_riichi(clients[0]) self.assertEqual(clients[0].player.in_riichi, True) self.assertEqual(clients[1].player.in_riichi, False) self.assertEqual(clients[2].player.in_riichi, False) self.assertEqual(clients[3].player.in_riichi, False) for client in clients: client.player.in_riichi = False manager.call_riichi(clients[1]) self.assertEqual(clients[0].player.in_riichi, False) self.assertEqual(clients[1].player.in_riichi, True) self.assertEqual(clients[2].player.in_riichi, False) self.assertEqual(clients[3].player.in_riichi, False) for client in clients: client.player.in_riichi = False manager.call_riichi(clients[2]) self.assertEqual(clients[0].player.in_riichi, False) self.assertEqual(clients[1].player.in_riichi, False) self.assertEqual(clients[2].player.in_riichi, True) self.assertEqual(clients[3].player.in_riichi, False) for client in clients: client.player.in_riichi = False manager.call_riichi(clients[3]) self.assertEqual(clients[0].player.in_riichi, False) self.assertEqual(clients[1].player.in_riichi, False) self.assertEqual(clients[2].player.in_riichi, False) self.assertEqual(clients[3].player.in_riichi, True)
def test_scores_calculations_after_retake(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.init_game() manager.init_round() manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(clients[0].player.scores, 25000) self.assertEqual(clients[1].player.scores, 25000) self.assertEqual(clients[2].player.scores, 25000) self.assertEqual(clients[3].player.scores, 25000) clients[0].player.in_tempai = True manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(clients[0].player.scores, 28000) self.assertEqual(clients[1].player.scores, 24000) self.assertEqual(clients[2].player.scores, 24000) self.assertEqual(clients[3].player.scores, 24000) for client in clients: client.player.scores = 25000 clients[0].player.in_tempai = True clients[1].player.in_tempai = True manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(clients[0].player.scores, 26500) self.assertEqual(clients[1].player.scores, 26500) self.assertEqual(clients[2].player.scores, 23500) self.assertEqual(clients[3].player.scores, 23500) for client in clients: client.player.scores = 25000 clients[0].player.in_tempai = True clients[1].player.in_tempai = True clients[2].player.in_tempai = True manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(clients[0].player.scores, 26000) self.assertEqual(clients[1].player.scores, 26000) self.assertEqual(clients[2].player.scores, 26000) self.assertEqual(clients[3].player.scores, 22000) for client in clients: client.player.scores = 25000 clients[0].player.in_tempai = True clients[1].player.in_tempai = True clients[2].player.in_tempai = True clients[3].player.in_tempai = True manager.process_the_end_of_the_round(None, None, None, None, False) self.assertEqual(clients[0].player.scores, 25000) self.assertEqual(clients[1].player.scores, 25000) self.assertEqual(clients[2].player.scores, 25000) self.assertEqual(clients[3].player.scores, 25000)
def game(player_red, player_blue): return GameManager(player_red, player_blue)
def test_init_dealer(self): clients = [Client() for _ in range(0, 4)] manager = GameManager(clients) manager.set_dealer(0) manager.init_round() self.assertTrue(manager.clients[0].player.is_dealer) self.assertFalse(manager.clients[1].player.is_dealer) self.assertFalse(manager.clients[2].player.is_dealer) self.assertFalse(manager.clients[3].player.is_dealer) manager.set_dealer(1) manager.init_round() self.assertTrue(manager.clients[1].player.is_dealer) self.assertFalse(manager.clients[0].player.is_dealer) self.assertFalse(manager.clients[2].player.is_dealer) self.assertFalse(manager.clients[3].player.is_dealer) manager.set_dealer(2) manager.init_round() self.assertTrue(manager.clients[2].player.is_dealer) self.assertFalse(manager.clients[0].player.is_dealer) self.assertFalse(manager.clients[1].player.is_dealer) self.assertFalse(manager.clients[3].player.is_dealer) manager.set_dealer(3) manager.init_round() self.assertTrue(manager.clients[3].player.is_dealer) self.assertFalse(manager.clients[0].player.is_dealer) self.assertFalse(manager.clients[1].player.is_dealer) self.assertFalse(manager.clients[2].player.is_dealer)