Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions kalebs-game/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'view'
require_relative 'model'

class GameController
include GameView

def run!
dice = DiceList.new

Print::title_screen

loop do
Print::menu
case Print::fetch_user_input
when "R"
dice.roll!
dice.each { |die| Print::print_die(die.value) }
Print::did_i_win(dice.yahtzee?)
# when "X"
# dice.reset!
when "r"
dice.win!
dice.each { |die| Print::print_die(die.value) }
Print::did_i_win(dice.yahtzee?)
when "Q"
puts "You chose to exit"
exit
else
Print::error_message
end
end
end
end

GameController.new.run!
58 changes: 58 additions & 0 deletions kalebs-game/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Die
attr_reader :value

def initialize
@value = rand(6) + 1
end
end

class DiceList
include Enumerable
attr_reader :id, :dice, :history

def initialize
@id = 0
@dice = []
@history = History.new
end

def each(&block)
@dice.each(&block)
end

def roll!
reset!
5.times { @dice.push(Die.new); }
end

def reset!
@history.add_roll(self)
@dice = []
@id += 1
end

def yahtzee?
@dice.uniq.length == 1
end

def win!
reset!
die = Die.new
5.times { @dice.push(die) }
end

end

# Not used currently, can be implemented to see past rolls.
class History
attr_reader :history

def initialize
@history = {}
end

def add_roll(dice_list)
@history[dice_list.id] = dice_list.dice
end

end
50 changes: 50 additions & 0 deletions kalebs-game/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module GameView

module Print

class << self

def error_message
puts "I don't understand that command"
end

def title_screen
title = <<TITLE
Welcome to Yahtzee!
TITLE
puts title
end

def menu
menu = <<EOS
Press R to roll the dice!
Press Q to quit.
EOS
puts menu
end

def print_die(value)
puts " ---"; puts "| #{value} |"; puts " ---"
puts "\n"
end

def fetch_user_input(question=nil)
puts question if question
print "> "
gets.chomp
end

def did_i_win(value)
case value
when true
puts "Congrats, you got Yahtzee! Keep playing if you want."
else
puts "Keep playing..."
end
end

end

end

end