import datetime

import map_loader


class Game(object):
    def __init__(self, name=name, players=players, map=None):
        """ Initialize a new game. """

        self.name = name,
        self.players = players,  # List of player usernames
        self.status = 'Waiting',
        self.raw_state = self.generate_clean_state(),  # JSON object
        self.created = datetime.datetime.now(),

        map = 'default' if map is None else map
        self.map = map.read_map_file(map)

    def generate_clean_state(self):
        """ Generates a blank game state JSON object. """
        return '{}'

    def load_from_state(self):
        """ Load game attributes from raw game state. """
        pass

    def serialize(self):
        """ Turn game into a serialized game state for storage. """
        pass

    def update(self):
        """ Execute a round. """
        pass


import datetime
import json

import map_loader


class GAME_STATUS(object):
    """ Game status constants. """
    lobby = 'waiting for players'
    waiting = 'waiting for moves'
    playing = 'playing'
    cancelled = 'cancelled'
    complete = 'complete'


class Game(object):
    def __init__(self, name=name, players=players, map='default'):
        """ Initialize a new game. """
        self.name = name,
        self.players = players,  # List of player usernames
        self.status = GAME_STATUS.lobby,
        self.created = datetime.datetime.now(),

        # These attributes are persisted in the raw_state, not DB properties
        self.map = map.read_map_file(map)
        self.current_turn = 0
        self.max_turns = 0

        self.raw_state = self.serialize(),  # JSON state (a DB property)

    def load_state_from_json(self):
        """ Load game attributes from raw game state. """
        state = json.loads(self.raw_state)
        self.map = state['map']
        self.current_turn, self.max_turns = state['turn']

    def serialize_state(self):
        """ Turn game state into a serialized game state for storage. """
        state = {
            'map': self.map,
            'turn': [self.current_turn, self.max_turns],
        }
        return json.dumps(state)

    def update(self):
        """ Execute a round. """
        self.current_turn += 1
        if self.current_turn == self.max_turns:
            self.status = GAME_STATUS.complete


Add some state related methods to Game
Add some state related methods to Game

  import datetime
+ import json
  
  import map_loader
  
  
+ class GAME_STATUS(object):
+     """ Game status constants. """
+     lobby = 'waiting for players'
+     waiting = 'waiting for moves'
+     playing = 'playing'
+     cancelled = 'cancelled'
+     complete = 'complete'
+ 
+ 
  class Game(object):
-     def __init__(self, name=name, players=players, map=None):
+     def __init__(self, name=name, players=players, map='default'):
          """ Initialize a new game. """
- 
          self.name = name,
          self.players = players,  # List of player usernames
+         self.status = GAME_STATUS.lobby,
-         self.status = 'Waiting',
-         self.raw_state = self.generate_clean_state(),  # JSON object
          self.created = datetime.datetime.now(),
  
-         map = 'default' if map is None else map
+         # These attributes are persisted in the raw_state, not DB properties
          self.map = map.read_map_file(map)
+         self.current_turn = 0
+         self.max_turns = 0
  
+         self.raw_state = self.serialize(),  # JSON state (a DB property)
-     def generate_clean_state(self):
-         """ Generates a blank game state JSON object. """
-         return '{}'
  
-     def load_from_state(self):
+     def load_state_from_json(self):
          """ Load game attributes from raw game state. """
-         pass
+         state = json.loads(self.raw_state)
+         self.map = state['map']
+         self.current_turn, self.max_turns = state['turn']
  
-     def serialize(self):
+     def serialize_state(self):
-         """ Turn game into a serialized game state for storage. """
+         """ Turn game state into a serialized game state for storage. """
+         state = {
+             'map': self.map,
+             'turn': [self.current_turn, self.max_turns],
-         pass
+         }
+         return json.dumps(state)
  
      def update(self):
          """ Execute a round. """
-         pass
+         self.current_turn += 1
+         if self.current_turn == self.max_turns:
+             self.status = GAME_STATUS.complete
  
  
Add some state related methods to Game
## Code Before:
import datetime

import map_loader


class Game(object):
    def __init__(self, name=name, players=players, map=None):
        """ Initialize a new game. """

        self.name = name,
        self.players = players,  # List of player usernames
        self.status = 'Waiting',
        self.raw_state = self.generate_clean_state(),  # JSON object
        self.created = datetime.datetime.now(),

        map = 'default' if map is None else map
        self.map = map.read_map_file(map)

    def generate_clean_state(self):
        """ Generates a blank game state JSON object. """
        return '{}'

    def load_from_state(self):
        """ Load game attributes from raw game state. """
        pass

    def serialize(self):
        """ Turn game into a serialized game state for storage. """
        pass

    def update(self):
        """ Execute a round. """
        pass


## Instruction:
Add some state related methods to Game
## Code After:
import datetime
import json

import map_loader


class GAME_STATUS(object):
    """ Game status constants. """
    lobby = 'waiting for players'
    waiting = 'waiting for moves'
    playing = 'playing'
    cancelled = 'cancelled'
    complete = 'complete'


class Game(object):
    def __init__(self, name=name, players=players, map='default'):
        """ Initialize a new game. """
        self.name = name,
        self.players = players,  # List of player usernames
        self.status = GAME_STATUS.lobby,
        self.created = datetime.datetime.now(),

        # These attributes are persisted in the raw_state, not DB properties
        self.map = map.read_map_file(map)
        self.current_turn = 0
        self.max_turns = 0

        self.raw_state = self.serialize(),  # JSON state (a DB property)

    def load_state_from_json(self):
        """ Load game attributes from raw game state. """
        state = json.loads(self.raw_state)
        self.map = state['map']
        self.current_turn, self.max_turns = state['turn']

    def serialize_state(self):
        """ Turn game state into a serialized game state for storage. """
        state = {
            'map': self.map,
            'turn': [self.current_turn, self.max_turns],
        }
        return json.dumps(state)

    def update(self):
        """ Execute a round. """
        self.current_turn += 1
        if self.current_turn == self.max_turns:
            self.status = GAME_STATUS.complete

