GithubHelp home page GithubHelp logo

thug-lorz's Introduction

GitHub Logo

Thug-Lorz

Drug Wars clone written in Ruby for the command-line console

This video game was inspired by my curiosity to learn the Ruby programming language.

To run the program, in the Ruby console, type: ruby game.rb

The implementation relies heavily upon a menu system, which was developed from scratch. I've never created a command-line console menu system before so I put some thought into creating something robust yet minimalistic. By capitalizing upon the powerful lambda expression this vital component was easily composed into something that can be used for any command-line interface from very simple menus to extremely complex nested menu structures. This system incorporates user input validation and custom event handling for precise management of program flow with minimal involvement of menu related details. Here's a quick blurb on how to use the Menu class to quickly and easily create a simple non-nested menu in a command-line driven program:

  1. Create a main menu

    main_menu = Menu.new(“Main”)

  2. Set content

    main_menu.set_content(lambda {puts ”Press A to do this, press B to do that, or Q to Quit”})

  3. Assign options

    main_menu.add_option(“A”, true, “Option A”, [{rule: lambda {true}, on_valid: lambda{puts “You pressed A”}}])

    main_menu.add_option(“B”, true, “Option B”, [{rule: lambda {true}, on_valid: lambda{puts “You pressed B”}}])

    main_menu.add_option(“Q”, true, “Quit”, [{rule: lambda {true}, on_valid: lambda{Menu.get_menu(“Main”).exit_menu}}])

  4. Display the main menu

    main_menu.display

To create a nested menu system just define another menu, like the Main Menu is defined, before the option is set and then call its display method inside its on_valid lambda expression. To use the built in user input prompt and validation system, just call Menu.handle_input_prompt and provide the prompt string that displays and the various options to handle all the cases in which the input should be validated.

For example, this is how an array of options can be defined for the handle_input_prompt options parameter:

options = [{conditions: [{ rule: lambda {Menu.prompt_input.upcase == "Y"}, on_fail: lambda {}} ], handler: lambda {Menu.get_menu("Main").exit_menu}, reprompt_on_success: false, reprompt_on_failure: false, reprompt_while: lambda {false} }, {conditions: [{ rule: lambda {Menu.prompt_input.upcase == "N"}, on_fail: lambda {}} ], handler: lambda {}, reprompt_on_success: false, reprompt_on_failure: false, reprompt_while: lambda {false}} ])

Each option is a hash consisting of an array of conditions for that option, an option handler method, two booleans which specify whether the prompt should repeat in either outcome of the evaluated conditions, and another method that determines if the prompt should repeat with a much more robust specification using a lambda expression for virtually unlimited control.

Example usage of the above options:

main_menu.add_option("q", false, "Quit", [{ rule: lambda {true}, on_invalid: lambda{} }], lambda { Menu.handle_input_prompt("Are you sure you want to quit? (y/n) ", options) })

As shown, the Menu.handle_input_prompt method is called inside the add_option event handler for the 'q' keypress event.

The Thug Lorz game heavily depends upon the structure this menu system provides, which made the development of the game quick and easy. Refer to the game's source code for examples in how the menu system can be used for nested menus.

thug-lorz's People

Contributors

scriptgeek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.