Farama-Foundation / Arcade-Learning-Environment

The Arcade Learning Environment (ALE) -- a platform for AI research.
https://ale.farama.org/
GNU General Public License v2.0
2.18k stars 424 forks source link

Include sample program for ale_interface #7

Closed mhauskn closed 11 years ago

mhauskn commented 11 years ago

The following is a c++ example program showing the use of the ale_interface. I think it would be useful to include with the documentation.

This is the compile command:

g++ -I /home/matthew/projects/Arcade-Learning-Environment/src/ -L /home/matthew/projects/Arcade-Learning-Environment interfaceExample.cpp -lale -lz

And here is the program interfaceExample.cpp:

#include <iostream>
#include <ale_interface.hpp>
using namespace std;

int main(int argc, char** argv) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " rom_file" << std::endl;
        return 1;
    }

    ALEInterface ale;

    // Load the ROM file
    ale.loadROM(argv[1], false);

    // Get the vector of legal actions
    ActionVect legal_actions = ale.getLegalActionSet();

    // Play 10 episodes
    for (int episode=0; episode<10; episode++) {
        float totalReward = 0;
        while (!ale.game_over()) {
            Action a = legal_actions[rand() % legal_actions.size()];
            // Apply the action and get the resulting reward
            float reward = ale.act(a);
            totalReward += reward;
        }
        cout << "Episode " << episode << " ended with score: " << totalReward << endl;
        ale.reset_game();
    }
};
mgbellemare commented 11 years ago

Done. Added a doc/examples section.