-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
145 lines (126 loc) · 4.17 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
# Mastermind
# Codemaker: create 4 color (6^4 = 1296) combination (color can be repeated)
# Codebreaker: guess the secret code within 12 turns
require_relative 'player'
require_relative 'computer'
require_relative 'board'
require_relative 'feedback'
require_relative 'colorize'
class Mastermind
attr_accessor :board, :turns, :code_colors
include Colorization
def initialize
@board = Board.new
@turns = 1
@code_colors = Board.code_colors
end
def play
### Initialize the game
introduction
define_role
set_secret_code
turn until over?
result
end
def introduction
puts ' --------------------------------------------------------------------'
puts ' ╔╦╗┌─┐┌─┐┌┬┐┌─┐┬─┐┌┬┐┬┌┐┌┌┬┐'
puts ' ║║║├─┤└─┐ │ ├┤ ├┬┘│││││││ ││'
puts ' ╩ ╩┴ ┴└─┘ ┴ └─┘┴└─┴ ┴┴┘└┘─┴┘'
puts ' --------------------------------------------------------------------'
sleep(2)
explain_instruction
explain_feedback
@board.display_board
end
def explain_instruction
puts "\n Play as the Codebreaker to guess the code, or"
puts ' play as the Codemaker to create an unbreakable code!'
puts "\n Both Player and Computer must guess the secret code within 12 turns to win."
puts ' - Left side of the board displays the guesses to the code'
puts ' - Right side will display the feedback to the guesses'
end
def explain_feedback
puts "\n Feedback peg (*) reference: "
puts " #{colorize_string('*', 'red')} Red: a guess is in the correct position and correct color"
puts " #{colorize_string('*', 'white')} White: a guess is the correct color BUT in the wrong position"
puts " #{colorize_string('*', 'black')} Black: a guess is neither correct in color / position"
end
def define_role
# defines the player's name and role
puts "\n --------------------------------------------------------------------"
puts "\n Would you like to play as Codebreaker or Codemaker?"
puts ' - Codebreaker: guess the secret code computer came up with'
puts ' - Codemaker: come up with a secret code and computer will try to guess it'
print "\n Type [1] for Codebreaker or [2] for Codemaker: "
input = gets.chomp.to_i
until input == 1 || input == 2
print " Sorry, didn't catch that. \nType [1] for Codebreaker or [2] for Codemaker: "
input = gets.chomp.to_i
end
assign_role(input)
puts "\n Great, you will be #{@player.role.capitalize} for this game!"
end
def assign_role(input)
if input == 1
@player = Player.new('codebreaker')
elsif input == 2
@player = Player.new('codemaker')
end
end
def set_secret_code
if @player.role != 'codemaker'
Computer.generate_code
@board.solution = Computer.secret_code
# Debug purpose only
# puts " Secret code is #{colorize_choices(board.solution)}"
else
@player.create_code
@board.solution = @player.code
puts " Secret code is #{colorize_choices(board.solution)}"
end
end
def turn
puts "\n --------------------------------------------------------------------"
puts "\n **Turn #{@turns}**"
choice = ask_guess
puts "\n Code has been entered: #{colorize_choices(choice)}"
update_guess(choice)
@turns += 1
end
def ask_guess
if @player.role == 'codebreaker'
@player.prompt_guess
guess = @player.code
else
Computer.guess_code(board.feedback, @turns)
guess = Computer.code
end
guess
end
def update_guess(choice)
board.guesses[@turns - 1] = Code.new(choice[0], choice[1], choice[2], choice[3])
board.update_board(@turns - 1)
end
def won?
if @turns < 2
false
else
board.guesses[@turns - 2].colors == board.solution
end
end
def over?
won? || @turns > 12
end
def result
if won?
puts "\n Codebreaker has cracked the secret code!"
elsif @turns > 12
puts "\n Codebreaker was unable to crack the code."
puts " The secret code was: #{colorize_choices(board.solution)}"
puts nil
end
end
end
game = Mastermind.new
game.play