def guess_a_letter(self, request): """Allows user to guess at the secret word""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game.over == True: raise endpoints.BadRequestException("GAME HAS ENDED") if not request.guess: raise endpoints.BadRequestException( "You must guess a letter, word, or phrase") guess(game, request.guess) return game.to_form()
def test_should_allow_any_player_during_sudden_death(): game = get_game() game['next_up'] = 1 game['sudden_death'] = True outcome, game = guess(game, 2, 'z') #"wrong" player assert_that(outcome).is_not_equal_to(NOT_YOUR_TURN)
def test_should_disallow_wrong_player(): game = get_game() game['next_up'] = 1 outcome, game = guess(game, 2, 'z') assert_that(outcome).is_equal_to(NOT_YOUR_TURN) assert_that(game).has_next_up(1) # unchanged
def test_should_state_losing_reason_assassin(): game = get_game_dc() game.next_up = 2 outcome, game = guess(game.to_dict(), 2, 'b') assert_that(outcome).is_equal_to(BLACK) assert_that(game).has_lost_reason('Jane was killed by an assassin')
def test_should_state_losing_reason_out_of_time(): game = get_game_dc() game.sudden_death = True outcome, game = guess(game.to_dict(), 2, 'z') assert_that(outcome).is_equal_to(NO_MORE_TIME) assert_that(game).has_lost_reason('You ran out of time')
def test_should_not_set_next_up_on_green(): game = get_game() game['sudden_death'] = True outcome, game = guess(game, 2, random.choice(game['player1']['green'])) # player 2 finding green from player 1 assert_that(outcome).is_equal_to(GREEN) assert_that(game).has_next_up(None)
def test_should_lose_on_black(): """Black is immediate loss""" game = get_game() outcome, game = guess(game, 1, 'd') assert_that(outcome).is_equal_to(BLACK) assert_that(game['lost']).is_true()
def test_should_not_lose_on_own_black(): """Black only counts on other player""" game = get_game() outcome, game = guess(game, 2, 'n') assert_that(outcome).is_equal_to(YELLOW) assert_that(game['lost']).is_false() assert_that(game['next_up']).is_equal_to(1)
def test_should_lose_on_yellow_in_sudden_death(): """During sudden death, yellow means lose""" game = get_game() game['sudden_death'] = True outcome, game = guess(game, 1, 'z') assert_that(outcome).is_equal_to(NO_MORE_TIME) assert_that(game['lost']).is_true()
def test_should_handle_yellow(): """Yellow kills a bystander and alternates roles""" game = get_game() game['bystanders'] = 8 outcome, game = guess(game, 1, 'z') assert_that(outcome).is_equal_to(YELLOW) assert_that(game).has_lost(False) assert_that(game).has_bystanders(7)
def guess_route(game_id, game, db_game, player_token, db_attendee): content = request.get_json() index = db_attendee.index word = content['word'] result, game = guess(game, word=word, player=index) update_game_details(game, game_id) channel = get_other_player_channel(db_game, player_token) app.logger.info(f'Triggering update on socket channel {channel}') pusher_client.trigger(channel, 'game_update', {'message': f'Guessed "{word}"'}) return {'result': result, 'game': safe_game(game, game_id)}
def test_should_enter_sudden_death(): """Sudden death is triggered when last bystander is killed""" game = get_game() game['bystanders'] = 1 outcome, game = guess(game, 1, 'z') assert_that(outcome).is_equal_to(SUDDEN_DEATH) assert_that(game['lost']).is_false() assert_that(game['sudden_death']).is_true() assert_that(game['next_up']).is_none()
def application(environ, start_response): error = False if environ['REQUEST_METHOD'] != 'POST': #만약 포스트 요청이 아닐경우 response = {'code': 'error', 'msg': 'wrong HTTP method'} #에러메세지를 담은 dictionary error = True if not error: try: #environ['PATH_INFO']로부터 요청된 기능(API) 파악 path = environ['PATH_INFO'].split('/') if len(path) == 2: method = path[1] else: response = {'code': 'error', 'msg': 'wrong API path'} error = True except: response = {'code': 'error', 'msg': 'wrong API path'} error = True try: request_body_size = int(environ.get('CONTENT_LENGTH', '0')) except ValueError: request_body_size = 0 request_body = environ['wsgi.input'].read(request_body_size) d = parse_qs(request_body) if not error: #요청된 API에 따라 적절한 게임 드라이버 함수를 호출한다. if method == 'new': response = new_game(d) elif method == 'guess': response = guess(d) else: response = {'code': 'error', 'msg': 'non-existent API method'} status = '200 OK' response_body = json.dumps(response) #json 형태를 가지는 HTTP response를 출력한다. response_headers = [ ('Content-Type', 'application/json'), ('Content-Length', str(len(response_body))) ] start_response(status, response_headers) return [response_body]
def application(environ): error = False if environ["REQUEST_METHOD"] != 'POST': rsponse = {'code':'error', 'msg':'wrongHTTP method'} error = True if not error: try: path = environ['PATH_INFO'].split('/') if len(path) == 2: method = path[1] else: response = {'code':'error', 'msg':'wrong API path method'} error = True except: response = {'cod':'error', 'msg':'wrong HTTP method'} error = True try: request_body_size = int(environ.get('CONTENT_LENGTH','0')) except ValueError: request_body_size = 0 request_body = environ['wsgi.input'].read(request_body_size) d = parse_qs(request_body) if not error: if method == 'new': response = new_game(d) elif method == 'guess': response = guess(d) else: response = {'code':'error', 'msg':'non-existent API method'} status = '200 OK' response_body = [ ('Content-Type', 'application/json') ('Content-Length', str(len(response_body))) ] start_response(status, response_headers) return [response_body]
def test_guess_ok(char, wgd): assert game.guess(char, wgd)
def test_guess_fails(char, wgd): assert game.guess(char, wgd)
def test_should_allow_correct_player(): game = get_game() game['next_up'] = 1 outcome, game = guess(game, 1, 'z') assert_that(outcome).is_equal_to(YELLOW)
def test_should_allow_any_player_if_not_initialized(): game = get_game() outcome, game = guess(game, 2, 'z') assert_that(outcome).is_equal_to(YELLOW) assert_that(game).has_next_up(1) # other player is next since failed
def solution(): for i in range(100): guess(i)