repl.it
@Shoroh/

Guess the Door Game

Ruby

No description

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'singleton'

# The Door class
class Door
  DOORS_POSITIONS = (1..7).to_a.freeze

  attr_reader :position

  def self.doors
    @doors ||= DOORS_POSITIONS.map { |d| new(d) }
  end

  def initialize(position)
    @position = position
  end
end

# The Cat class
class Cat
  include Singleton

  AVAILABLE_MOVES = %i[left right].freeze

  def door
    @door ||= Door.doors.sample
  end
  alias set_door door

  def move
    do_move(random_valid_move)
  end

  private

  def do_move(move)
    case move
    when :left then @door = Door.doors[(door.position - 1) - 1]
    when :right then @door = Door.doors[(door.position + 1) - 1]
    end
  end

  def random_valid_move
    move = nil
    until move_valid?(move) do
      move = AVAILABLE_MOVES[rand(2)]
    end
    move
  end

  def move_valid?(move)
    case move
    when :left then door.position > 1
    when :right then door.position < 7
    else false
    end
  end
end

TITLE = '  Guess The Door Game (c) Shelestov Aleksandr  '.freeze
TITLE_SIZE = TITLE.size.freeze

cat = Cat.instance
cat.set_door
tries = 0
debug_mode = false

puts '-' * TITLE_SIZE
puts TITLE
puts '-' * TITLE_SIZE
puts "\n"

puts ' _______ ' * 7
puts ' |     | ' * 7

doors = (1..7).each_with_object('') do |door_number, memo|
  memo << " |  #{door_number}  | "
end
puts doors
puts ' |     | ' * 7
puts ' |_____| ' * 7

puts "

  There are 7 doors and one cat.
  Can you guess what the door does hide the cat?

  The cat has been placed randomly behind one of the doors.
  Every day you can open just one door to catch the Cat.

  If you missed the door, the cat randomly move to the
  other nearest door.
  For example, if the cat was behind the 5th door,
  it can move left to the 4th one, or to the right to the
  6th one.

  Of course the Cat can't go to the right if it's already
  reached the 7th door.
  Opposite, it can't go to the left, when the previous door
  was 1th.

  So, you should find a way to find a cat with less tries.
  Let's go!

  "

loop do
  puts '-' * TITLE_SIZE
  print 'input the number of the door (EXIT to quit): '

  tries += 1
  input = gets.strip
  break if input =~ /exit|quit|escape|\q|:q/i
  if input !~ /^[1-7]{1}$/
    puts 'Please put the number between 1 and 7!'
    next
  end

  puts '-' * TITLE_SIZE

  if cat.door.position == input.to_i
    puts "
      Congratulations!
      You found the cat behind the door ##{input}.
      It took #{tries} tries. So, your result is #{tries}!
    "
    break
  else
    puts 'You missed!'
    puts "The cat was behind the #{cat.door.position} door!" if debug_mode
    cat.move
  end
end

ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
?