Commit 0606414f by Mai Hoang Thai Ha

add exercise

parent 9eabb055
Pipeline #1183 failed with stages
in 0 seconds
......@@ -27,7 +27,7 @@ def bear_room
puts "How are you going to move the bear?"
bear_moved = false
while true
while bear_moved == false
print "> "
choice = $stdin.gets.chomp
......@@ -88,4 +88,4 @@ def start
end
# start
gold_room
bear_room
\ No newline at end of file
......@@ -180,7 +180,8 @@ class EscapePod < Scene
puts "but you don't have time to look. There's 5 pods, which one"
puts "do you take?"
good_pod = rand(1..5)
good_pod = rand(1..5) #random
# puts good_pod
print "[pod #]> "
guess = $stdin.gets.chomp.to_i
......@@ -236,6 +237,6 @@ class Map
end
end
a_map = Map.new('central_corridor')
a_map = Map.new('escape_pod')
a_game = Engine.new(a_map)
a_game.play()
\ No newline at end of file
class Scene
def enter()
puts "This scene is not yet configured. Subclass it and impletent enter()" #something
exit(1)
end
end
class Engine
def initialize(scene_map)
@scene_map = scene_map
end
def play()
current_scene = @scene_map.opening_scene()
last_scene = @scene_map.next_scene('finished')
while current_scene != last_scene
next_scene_name = current_scene.enter()
current_scene = @scene_map.next_scene(next_scene_name)
end
current_scene.enter()
end
end
class Death < Scene
@@quips = [
"You died. You kinda suck at this.",
"Your mom would be proud...if she were smarter.",
"Such a luser.",
"I have a small puppy that's better at this."
]
def enter()
puts @@quips[rand(0..(@@quips.length - 1))]
exit(1)
end
end
class WarmUp < Scene
def enter()
puts "Ban da lac vao Vung Dat Chet"
puts "Chi co mot cach de song sot!"
puts "Vuot qua nhung thu thach de den con tau. Ban se can no de tro ve"
puts "Co mot co hoi de den con tau som hon neu ban la nguoi may man"
puts "Hay chon mot con so may man cho minh (1-100)"
lucky_number = rand(1..100)
print "Nhap so > "
guess = $stdin.gets.chomp.to_i
if guess != lucky_number
puts "That tiec %s khong phai la so may man" %guess
puts "nhap 'e' de tiep tuc hanh trinh"
while true
puts "> "
next_ch = $stdin.gets.chomp
if next_ch == "e"
puts "Di tiep"
return 'finished'
else
puts "vui long nhap lai"
end
end
else
puts "Chuc mung!!! %s chinh la so may man" %guess
puts "Ban se duoc di chuyen den tau ma khong can phai hoan thanh thu thach"
puts "Hay su dung no de tro ve"
return 'finished'
end
end
end
class Finished
def enter()
puts "You won! Good job."
exit(0)
end
end
class Map
@@scenes = {
'warm_up' => WarmUp.new(),
'finished' => Finished.new()
}
def initialize(start_scene)
@start_scene = start_scene
end
def next_scene(scene_name)
val = @@scenes[scene_name]
return val
end
def opening_scene()
return next_scene(@start_scene)
end
end
a_map = Map.new('warm_up')
a_game = Engine.new(a_map)
a_game.play
\ No newline at end of file
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
Gem::Specification.new do |spec|
spec.name = "NAME"
spec.version = '1.0'
spec.authors = ["Thai Ha"]
spec.email = ["hamht@zigexn.vn"]
spec.summary = %q{Short summary of your project}
spec.description = %q{Longer description of your project.}
spec.homepage = "http://domainforproject.com/"
spec.license = "MIT"
spec.files = ['lib/NAME.rb']
spec.executables = ['bin/NAME']
spec.test_files = ['tests/test_NAME.rb']
spec.require_paths = ["lib"]
end
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "tests"
t.test_files = FileList['tests/test*.rb']
t.verbose = true
end
require "./lib/NAME.rb"
require "test/unit"
class TestNAME < Test::Unit::TestCase
def test_sample
assert_equal(4, 2+2)
end
end
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "tests"
t.test_files = FileList['tests/test*.rb']
t.verbose = true
end
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
Gem::Specification.new do |spec|
spec.name = "NAME"
spec.version = '1.0'
spec.authors = ["Thai Ha"]
spec.email = ["hamht@zigexn.vn"]
spec.summary = %q{Short summary of your project}
spec.description = %q{Longer description of your project.}
spec.homepage = "http://domainforproject.com/"
spec.license = "MIT"
spec.files = ['lib/NAME.rb']
spec.executables = ['bin/NAME']
spec.test_files = ['tests/test_NAME.rb']
spec.require_paths = ["lib"]
end
class Room
def initialize(name, description)
@name = name
@description = description
@paths = {}
end
# these make it easy for you to access the variables
attr_reader :name
attr_reader :paths
attr_reader :description
def go(direction)
return @paths[direction]
end
def add_paths(paths)
@paths.update(paths)
end
end
require "ex47/game.rb"
require "test/unit"
class TestGame < Test::Unit::TestCase
def test_room()
gold = Room.new("GoldRoom",
"""This room has gold in it you can grab. There's a
door to the north.""")
assert_equal("GoldRoom", gold.name)
assert_equal({}, gold.paths)
end
def test_room_paths()
center = Room.new("Center", "Test room in the center.")
north = Room.new("North", "Test room in the north.")
south = Room.new("South", "Test room in the south.")
center.add_paths({'north'=> north, 'south'=> south})
assert_equal(north, center.go('north'))
assert_equal(south, center.go('south'))
end
def test_map()
start = Room.new("Start", "You can go west and down a hole.")
west = Room.new("Trees", "There are trees here, you can go east.")
down = Room.new("Dungeon", "It's dark down here, you can go up.")
start.add_paths({'west'=> west, 'down'=> down})
west.add_paths({'east'=> start})
down.add_paths({'up'=> start})
assert_equal(west, start.go('west'))
assert_equal(start, start.go('west').go('east'))
assert_equal(start, start.go('down').go('up'))
end
end
ruby-training @ af69e4e6
Subproject commit af69e4e6b7e55bdfc85555c131cab828bf7775b9
def enter()
puts "Ban da lac vao Vung Dat Chet"
puts "Chi co mot cach de song sot!"
puts "Vuot qua nhung thu thach de den con tau. Ban se can no de tro ve"
puts "Co mot co hoi de den con tau som hon neu ban la nguoi may man"
puts "Hay chon mot con so may man cho minh (1-100)"
lucky_number = rand(1..100)
puts lucky_number
print "Nhap so > "
guess = $stdin.gets.chomp.to_i
if guess != lucky_number
puts "That tiec %s khong phai la so may man" %guess
puts "nhap 'e' de tiep tuc hanh trinh"
while true
print "> "
next_ch = $stdin.gets.chomp.to_s
if next_ch == "e"
puts "Di tiep"
# return ###
else
puts "vui long nhap lai"
end
end
else
puts "Chuc mung!!! %s chinh la so may man" %guess1
puts "Ban se duoc di chuyen den tau ma khong can phai hoan thanh thu thach"
puts "Hay su dung no de tro ve"
return 'finished'
end
end
def hai
puts 'day la hai '
exit(0)
end
enter
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment