Skip to content

Commit

Permalink
Merge pull request #26 from Kautenja/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
Kautenja authored Jul 22, 2018
2 parents 5bba4ba + 9009fa7 commit 0fe9e76
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 41 deletions.
1 change: 0 additions & 1 deletion nes_py/laines/include/joypad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Joypad {

unsigned get_num_buttons();
void write_buttons(int n, u8 buttons);
u8 read_state(int n);
void write_strobe(bool v);
Expand Down
6 changes: 0 additions & 6 deletions nes_py/laines/joypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ namespace Joypad {
/// Joypad strobe latch.
bool strobe;

/// Return the number of buttons on the joypad
unsigned get_num_buttons() {
// a u8 should be 1 byte, with 8-bits per byte
return sizeof(u8) * 8;
}

/**
Write a button state to the given joypad.
Expand Down
3 changes: 0 additions & 3 deletions nes_py/laines/nes_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ extern "C" {
/// The initializer to return a new NESEnv with a given path.
NESEnv* NESEnv_init(wchar_t* path){ return new NESEnv(path); }

/// The number of buttons on the NES joypad.
unsigned NESEnv_num_buttons() { return Joypad::get_num_buttons(); }

/// The width of the NES screen.
unsigned NESEnv_width() { return GUI::get_width(); }

Expand Down
13 changes: 4 additions & 9 deletions nes_py/nes_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import gym
import numpy as np
from numpy.ctypeslib import as_ctypes
from .spaces import Bitmap
from gym.spaces import Discrete


# the path to the directory this
Expand All @@ -25,9 +25,6 @@
# setup the argument and return types for NESEnv_init
_LIB.NESEnv_init.argtypes = [ctypes.c_wchar_p]
_LIB.NESEnv_init.restype = ctypes.c_void_p
# setup the argument and return types for NESEnv_num_buttons
_LIB.NESEnv_num_buttons.argtypes = None
_LIB.NESEnv_num_buttons.restype = ctypes.c_uint
# setup the argument and return types for NESEnv_width
_LIB.NESEnv_width.argtypes = None
_LIB.NESEnv_width.restype = ctypes.c_uint
Expand All @@ -54,8 +51,6 @@
_LIB.NESEnv_close.restype = None


# the number of buttons on the NES joy-pad
NUM_BUTTONS = _LIB.NESEnv_num_buttons()
# height in pixels of the NES screen
SCREEN_HEIGHT = _LIB.NESEnv_height()
# width in pixels of the NES screen
Expand Down Expand Up @@ -92,7 +87,7 @@ class NESEnv(gym.Env):
)

# action space is a bitmap of button press values for the 8 NES buttons
action_space = Bitmap(NUM_BUTTONS)
action_space = Discrete(256)

def __init__(self, rom_path, frameskip=1, max_episode_steps=float('inf')):
"""
Expand Down Expand Up @@ -221,13 +216,13 @@ def reset(self):
# reset the emulator
_LIB.NESEnv_reset(self._env)
# call the after reset callback
self._did_reset_()
self._did_reset()
# copy the screen from the emulator
self._copy_screen()
# return the screen from the emulator
return self.screen

def _did_reset_(self):
def _did_reset(self):
"""Handle any RAM hacking after a reset occurs."""
pass

Expand Down
6 changes: 0 additions & 6 deletions nes_py/spaces/__init__.py

This file was deleted.

15 changes: 0 additions & 15 deletions nes_py/spaces/bitmap.py

This file was deleted.

18 changes: 18 additions & 0 deletions nes_py/wrappers/binary_to_discrete_space_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,23 @@ def reset(self):
"""Reset the environment and return the initial observation."""
return self.env.reset()

def get_keys_to_action(self):
"""Return the dictionary of keyboard keys to actions."""
# get the old mapping of keys to actions
old_keys_to_action = self.env.unwrapped.get_keys_to_action()
# invert the keys to action mapping to lookup key combos by action
action_to_keys = {v: k for k, v in old_keys_to_action.items()}
# create a new mapping of keys to actions
keys_to_action = {}
# iterate over the actions and their byte values in this mapper
for action, byte in self._action_map.items():
# get the keys to press for the action
keys = action_to_keys[byte]
# set the keys value in the dictionary to the current discrete act
keys_to_action[keys] = action

return keys_to_action


# explicitly define the outward facing API of this module
__all__ = [BinarySpaceToDiscreteSpaceEnv.__name__]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def README():

setup(
name='nes_py',
version='0.8.8',
version='0.8.9',
description='An NES Emulator and OpenAI Gym interface',
long_description=README(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 0fe9e76

Please sign in to comment.