I built a command line game in Ruby and now I’m trying to build a GUI for the game using Shoes. I spent the morning reading about Shoes, looking at some code samples, and writing a bit of code myself. It appears (I’m new to Shoes, so this could be totally wrong) that the Shoes code (for example, a shoes.rb file that you open with the Shoes application) has to contain all the Ruby code you want to run. All of the Ruby code will exist within that file.
The way I’m firing up this game is at the command line by passing command line arguments to play.rb (a Ruby script). Either run play.rb "command line game" or play.rb "shoes game" at the command line. If you run the latter, it will make a system call to open shoes.rb with the Shoes application. Within play.rb it will pass ShoesInterface to the players, rather than CommandLineInterface. Below is an example of how a player might be asked for a move:
In the human_player.rb file:
def make_move
@interface.print("It's your turn to make a move.")
end
@interface will either be CommandLineInterface or ShoesInterface. The print method in CommandLineInterface is simply a puts statement. The print method in ShoesInterface should tell the Shoes GUI to display that text. I’m trying to re-use as much of my code as possible. Theoretically, regardless of how someone is playing the game (on the web, at the command line, through a GUI, etc.) it should use most of the same logic. The interfaces simply display messages and receive user input. They just do it in different ways.
The problem is that I don’t know how to connect my Shoes GUI to my existing Ruby code. Is anyone here proficient in Shoes? This might not be possible in Shoes…maybe I’ll have to use a different Ruby GUI, but I thought I’d ask before moving on to another one.
What you can do, is to write a Shoes Widget that will respond to method print. Below is a very crude example of such a widget which will append a message from the edit line to its arbitrary display slot. Of course you can easily modify the Widget so that you can initialize it with a target slot for display etc but the idea stays the same.
I hope that is what yo wanted.