SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RoR Noob game design help

    Im jumping in at the deep end with Ruby on Rails and looking for some design advice,Im trying to write a simple game where a player will be given a scenario and then a few options to choose from.

    Based on whatever option they choose they will be presented with a new scenario and a different set of options and so on.

    I want to give points for correct choices and detract points for wrong choices.

    For example

    Bare with me for being long-winded!

    Scenario 1: You are stuck in a sweet shop starving of hunger.

    Choices: Do you
    A:Eat all the sweets around you.
    B:Starve

    So I was thinking of having a table called Story which would have fields as below.

    Fields |
    Scenario number |1
    Scenario detail |You are stuck in a sweet shop starving of hunger.
    Correct A |Eat all the sweets around you.
    Correct B |
    Wrong A |Starve
    Wrong B |
    Wrong C |

    If the user then selects the option that maps to a the Correct A colum the next row in Story is displayed i.e Scenario 2.

    If selection = Correct user will get 3 points.
    If selection = Wrong user will get -2 points.

    I know this sort of functionality could be achieved in simple HTML, using a link to progres to a different page, but I think this would be a framework for a more managable and scalable game.

    So my question to you is, am I over complicating things and are there better solutions?

    Any advice greatly appreciated!

  2. #2
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A little bump.
    Please help if you know any examples or tutorials on below?
    Essentially what I want to do is a multiple choice game, where one answer will progress to another page listing a question and answer.Im getting stuck in the DB schema setup.

    Thanks

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks to me that your fundamental unit is the "Scenario" and therefore your model would be Scenario and your table scenarios. The table would simply have these fields:

    scenarios
    id: integer
    detail: string or text depending how big you want to allow

    You then have a second set of entities called answers. Answer model and answers table with these fields:

    answers
    id: integer
    scenario_id: integer
    detail: string or text depending how big you want to allow
    correct: bollean
    score: integer

    You'd then have the following model code:
    Code:
    class Scenario < Base
      has_many :answers
    end
    
    class Answer < Base
      belongs_to :scenario
    end
    
    #display answers for first scenario
    scenario = Scenario.find(:first)
    for answer in scenario.answers
      puts answer.detail
    end
    The advantage of this arrangement is that you aren't limited to the number or type of answers by the table design. You could have 2 correct answers and 3 incorrect one, or 14 wrong answers and 1 right answer. Also you are not replicating the answer parameters across multiple table fields.

  4. #4
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To continue onReggieB work: in the answer model you will have the Bool attribute "good_answer" and "next scenario_id".

    you should also add the relationship "has_one :scenario" and have Scenario belongs_to :answer.

    so when the user pick an answer, you check if it is the correct one, adjust the score then render the scenario_id to move to the desired page and repeate.

    I hope I was clear enough..
    Ruby on Rails plugin directory and content: The Rail Station
    Cinemaphotograper visit: Canon 5D Tips

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a million guys, I couldnt see the wood for the trees.
    Thats a great starting point.

    Cheers again

  6. #6
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for your input but Ive run into trouble trying the solution you suggested.
    I created 2 tables

    Scenario:

    ID
    detail

    Answers:

    ID
    scenario_id
    detail
    correct
    score

    and made the following code changes

    scenario.rb

    class Scenario < ActiveRecord::Base
    has_many :answers
    end
    #display answers for first scenario
    scenario = Scenario.findlast)
    for answer in scenario.answers
    puts answer.detail
    end

    answer.rb

    class Answer < ActiveRecord::Base
    belongs_to :scenario
    has_one :scenario
    end


    When I go to localhost:3000/scenarios/new
    I can submit new scenarios by entering an ID and Detail.


    When I go to localhost:3000/answers/new
    I can submit new answers populating the fields
    ID
    Scenario
    Detail
    Correct
    Score

    Did I insert your code correctly?

    Should I have a an Scenario_ID field in Scenarios to map to Scenarios_ID in answers?

    The answers for a specified scenario dont appear anywhere.

    Also if a user click on a correct answer, what is the best way to progress them to the next set of scenario + answer, perhaps another field in scenarios that stores next_scenario, that is called when answer = true?

    Again, I hope that is somewhat clear and I really really appreciate your help, this is trickier than I anticipated!
    Cheers

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Megapixelicious View Post
    To continue onReggieB work: in the answer model you will have the Bool attribute "good_answer" and "next scenario_id".
    I wouldn't do that. The next scenario should not be determined from the last answer. What happens if you add another answer, or delete the last one. You'd have to redetermine the last answer again. Instead, use a property of the scenairos to determine their order. Order by id, created_at, or add another field called something like "position" and order by that.

    Also what does "good_answer" do that correct doesn't?

    Quote Originally Posted by Megapixelicious View Post
    you should also add the relationship "has_one :scenario" and have Scenario belongs_to :answer.
    That doesn't achieve anything. "belongs_to :scenario" in the Answer class code already generates a method call scenario that will return the scenario. Also "has_many :answers" generates a method called answers returning an array of all the answer instances that match this scenario.

    Sorry to be so negative but I just don't agree with your additions.

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ID should be id lower case. If you use migrations to create the tables, this field is made for you automatically.

    The bit of code after "#display answers for first scenario" was added to show you how the code worked and should not be included in your model's code. answer.scenario_id mapps to scenario.id.

    A simple way to handle what you do is to create @scenarios in your controller and then create drop-down selection of the scenarios in your form. Something like this should work:

    Controller - in methods using the form (e.g. new, edit)
    Code:
    @scenarios = Scenario.find(:all)
    In the form
    Code:
    <&#37;= select(:answer, :scenario_id, @scenarios.collect{|s| [s.detail, s.id]}) -%>

  9. #9
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry mate for being so slow, maybe im not cut out for this stuff

    (Ive had to edit the below a bit due to rule about not posting email/URLs!)
    Is this what you mean about creating a drop down:- I create an action in the scenarios_controller .rb
    e.g.

    def select_scenario
    'at'scenarios = Scenario .findall)
    end

    and then create a view select_scenario. html.erb
    with code:
    <%= selectanswer, :scenario_id,'at'scenarios. collect{|s| [s.detail, s.id]}) -%>

    and the model files remain as below?

    scenario.rb

    class Scenario < ActiveRecord::Base
    has_many :answers
    end


    answer.rb

    class Answer < ActiveRecord::Base
    belongs_to :scenario
    has_one :scenario
    end

    What do I do with the code you suggested before, do I create a new action in scenario_controller.rb for it?
    #display answers for first scenario
    scenario = Scenario.findfirst)
    for answer in scenario.answers
    puts answer.detail
    end


    Really sorry for being so annoying

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Restless Rails (I'm not a great fan of Rest) you can do this:

    Scenario model:
    Code:
    class Scenario < Base
      has_many :answers
    end
    Answer model:
    Code:
    class Answer < Base
      belongs_to :scenario
    end
    Answer controller
    Code:
    AnswerController < ApplicationController
      def list
        @answers = Answer.find(:all)
      end  
    
      def show
        @answer = Answer.find(params[:id])
      end
    
      def new
        @answer = Answer.new
        @scenarios = Scenario.find(:all)
      end
    
      def create
        @answer = Answer.new(params[:answer])
        if @answer.save
           flash[:notice] = "New answer created"
           redirect_to :action => 'show', :id => @answer
        else
           @scenarios = Scenario.find(:all)
           render :action => 'new'
        end
      end
    end
    New template (new.html.erb):
    Code:
    <&#37; form_tag(:action => 'create') do %>
      <%= render :partial => 'form' %>
      <%= submit_tag "Create" %>
    <% end %>
    Form partial (_form.html.erb):
    Code:
    <p><label for="answer_detail">Details: </label><br />
    <%= text_field 'answer', 'detail'  -%></p>
    
    <p><label for="answer_scenario_id">Select screnario:</label><br />
    <%= select(:answer, :scenario_id, @scenarios.collect{|s| [s.detail, s.id]}) -%></p>
    
    <p><label for="answer_value">Value: </label><br />
    <%= text_field 'answer', 'value'  -%></p>
    
    <p><label for="answer_correct">Answer is correct: </label>
    <%= check_box("answer", "correct") -%></p>
    This does mean you'll need to create the scenario first! This is also very much the starting point. You'll need to add validation to your models and probably code to catch problems, but hopefully it does show how the select code can be used.

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    This does mean you'll need to create the scenario first!
    Do you have any scenarios? To keep the code straight-forward, I didn't add check in the code that there were scenarios. You'll get an error such as the one you've listed if you don't add a scenario first.

    Also the variable @scenarios needs to match @scenarios in the controller. If one is @scenario and the other is @scenarios, it won't work.

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh! and have a look at this guide to using [code] tags.

  13. #13
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again ReggieB.

    One question,do I need to and where, initialise the @ scenario variable in _form. html. erb.

    I get the error below, Ive tried a few things but to no avail
    Code:
     Showing app/views/answers/_form. html. erb where line #5 raised:
    
    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil. collect
    
    Extracted source (around line #5):
    
    2: <%= text_field 'answer', 'detail'  -%></p>
    3: 
    4: <p><label for="answer_scenario_id">Select screnario:</label><br />
    5: <%= select(:answer, :scenario_id, @ scenarios. collect{|s| [s.detail, s.scenario_id, s.id]}) -%></p>
    6: 
    7: <p><label for="answer_value">Value: </label><br />
    8: <%= text_field 'answer', 'value'  -%></p>
    Cheers,

  14. #14
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry mate,I was away in the wilderness for a few days.

    Yes, I have a scenario in the DB, the scenarios schema is just id:integer and detail:string.

    Ive started again and am encountering new "You have a nil object when you didn't expect it!
    " error in index. html. erb.

    Ive tried changing 'answer .scenario .id' to 'answers .scenario .id etc' to match my DB table but still get the same error.

    Code:
    <h1>Listing answers</h1>
    
    <table>
      <tr>
        <th>Scenario</th>
        <th>Detail</th>
        <th>Correct</th>
        <th>Score</th>
      </tr>
    
    <% for id in @ answers %>
      <tr>
        <td><%=h answer. scenario_id %></td>
        <td><%=h answer. detail %></td>
        <td><%=h answer. correct %></td>
        <td><%=h answer. score %></td>
        <td><%= link_to 'Show', answers %></td>
        <td><%= link_to 'Edit', edit_answer_path(answers) %></td>
        <td><%= link_to 'Destroy', answers, :confirm => 'Are you sure?', :method => :delete %></td>
      </tr>
    <% end %>
    </table>
    
    <br />
    
    <%= link_to 'New answer', new_answer_path %>
    Getting disheartened here!

    Appreciate you help loads.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •