Coder Social home page Coder Social logo

kwk-l1-rps-game-app-kwk-students-l1-phil-072318's Introduction

Creating Rock, Paper, Scissors

Instructions

Create the game model

Using learn or rspec to build the RPSGame model according to the tests. This is a challenge that will require you to utilize all you've learned about Object Oriented Ruby and Sinatra!

Create the Sinatra app

Integrate your RPSGame class with the Sinatra application in app.rb.

The application will serve 1 route: GET requests to the path /rps/:play. The value of :play will be used as the player's throw (rock, paper, or scissors).

The Sinatra application should serve an HTML page that displays the move that the player made, the move that the computer made, and whether the player won, lost, or tied.

Hints:

  • There is a custom error provided in the RPSGame model. To invoke this, use raise PlayTypeError in your initialize method, but only if the move received is invalid.

  • It is possible to pass the first tests automatically... but you can actually fail these tests later if you do not write the correct solution for raising an error on invalid moves.

  • To make this a playable game, look into using rand(3) as a way to choose the index of an array of moves. Remember how to select a specific element from an array using its index?

  • The only valid moves are "rock", "paper", and "scissors", and we need to refer to these in multiple locations on our model. Maybe we can use some sort of... variable... at the class level to store these?

  • Read the specs to see exactly what they're testing if you get stuck. There are specific variables, like @computer_play that the spec is looking for.

  • In order to call instance methods within other methods of the same class, you need to write self. before the method name.. so self.tied? for instance, would allow you to call the method tied in some other method...

  • The word self refers to a particular instance of a class, so it is sort of like saying 'for this particular instance of RPSGame, call one of the instance methods'. This allows us to build 'helper' methods - methods that do one job and can be used in other methods to keep code organized and easy to follow. You don't necessarily need this to pass the tests, but you will need it to continue building a playable game.

After the Tests

If you've passed all the tests, CONGRATULATIONS! This was a tough one! However... we're not quite finished. The tests in this lab are in place to make sure you've got a good set up. To create a playable game, you will need to do the following:

  1. Start up shotgun and navigate to the provide IP address. Add '/rps/:play' at the end and you should see our rps_game.erb file displaying "WELCOME TO ROCK PAPER SCISSORS".

  2. In app.rb, you will need to call RPSGame.new(), passing in the correct parameter from the dynamic URL. Assign this to a variable, like @game, so it reads @game = RPSGame.new(params[:play]). If the parameter is incorrect or an invalid move, you should see Sinatra displaying our custom PlayTypeError error appear. If the parameter matches "rock", "paper" or "scissors", you will see "WELCOME TO ROCK PAPER SCISSORS" again, which means that your initialize method on RPSGame has accepted the parameter as valid.

  3. @game contains the entire instance of the class RPSGame that you just initialized, meaning it can access to any attributes you will let access. You'll want to add an attribute for the winner, like @winner.. and you'll also need to write some logic in your initialize method that plays the game. You've got all the parts already built out, and you start the game with your move as the player. Write some code that uses the computer_play method, as well as the won? method. Assign @winner to something to indicate who won the game, like "player" or "computer".

  4. Back in app.rb, you'll be able to access @winner within your game instance by creating an instance variable and assigning it to @game.instance_variable_get(:@winner). @winner = @game.instance_variable_get(:@winner) will work fine, as the first variable is an instance of the GameApp class within app.rb.

  5. You will now be able to write in rps_game.erb within your 'views' folder, so add the instance variable @winner within ERB tags.

  6. Refresh the page a few times. If all is working, you should see the resulting winner displayed on the page.

Stretch Goals

  • Can you display what you and the computer both played, as well as the winner?
  • Can you build a best 2 out of 3 version of the game?

KWK-L1 ---

kwk-l1-rps-game-app-kwk-students-l1-phil-072318's People

Contributors

ahimmelstoss avatar aviflombaum avatar changamanda avatar danielseehausen avatar fislabstest avatar fs-lms-test-bot avatar ipc103 avatar irmiller22 avatar kthffmn avatar roseweixel avatar sarogers avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

anumahmad

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.