Created
November 4, 2015 13:42
-
-
Save swoopej/3e61f6c1ae88319dd5d6 to your computer and use it in GitHub Desktop.
Using a Game Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
MAX_X = 3 | |
MAX_Y = 3 | |
DIRECTIONS = [] | |
def errors | |
@errors | |
end | |
def initialize | |
@x = 0 | |
@y = 0 | |
@errors = nil | |
end | |
def current_position | |
[@x, @y] | |
end | |
def move_left | |
if @x == -MAX_X | |
@errors = "eek too far left!" | |
else | |
@x -= 1 | |
end | |
end | |
def move_right | |
if @x == MAX_X | |
@errors = "eek too far right!" | |
else | |
@x += 1 | |
end | |
end | |
def move_up | |
if @y == MAX_Y | |
@errors = "eek too far up!" | |
else | |
@y += 1 | |
end | |
end | |
def move_down | |
if @y == -MAX_Y | |
@errors = "eek too far up!" | |
else | |
@y -= 1 | |
end | |
end | |
def reset | |
@x = 0 | |
@y = 0 | |
@errors = nil | |
end | |
end | |
class Game | |
def initialize | |
@player = Player.new | |
end | |
def play | |
while true | |
puts "You're currently at #{@player.current_position}." | |
puts "How would you like to move?" | |
move = gets.chomp | |
@player.send(move) | |
puts @player.errors if @player.errors | |
end | |
end | |
end | |
game = Game.new | |
game.play | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment