-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmastermind.rb
132 lines (111 loc) · 3.88 KB
/
mastermind.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
require_relative 'gameboard.rb'
require_relative 'colorcode.rb'
require_relative 'hint.rb'
require_relative 'computer.rb'
class Mastermind
attr_accessor :gameboard, :win, :turns, :color_spectrum, :player_mode, :computer
def initialize
@gameboard = GameBoard.new()
@win = false
@turns = 1
@color_spectrum = ["red", "green", "yellow", "blue", "black", "magenta", "cyan", "white"]
@player_mode = true
@computer = Computer.new()
end
def play
instructions
prompt_switch_mode
set_solution if @player_mode == false
start
end
def instructions
puts " __ __ _ _ _
| \\/ | __ _ ___| |_ ___ _ _ _ __ (_) _ _ __| |
| |\\/| |/ _` |(_-<| _|/ -_)| '_|| ' \\ | || ' \\ / _` |
|_| |_|\\__,_|/__/ \\__|\\___||_| |_|_|_||_||_||_|\\__,_|"
puts "_________________________________________________________\n\n"
puts "Guess the code to win!"
puts "The computer will select a code of four different colors in a specific order!"
puts "You have twelve guesses."
@gameboard.display
puts "_________________________________________________________\n\n"
puts "The left side is where your guesses will go."
puts "The right side will tell you how accurate you are: "
puts "green for every color in the correct position"
puts "red for every color in an incorrect position!"
puts "\nGood luck!"
puts "_________________________________________________________\n\n"
end
def prompt_switch_mode
puts "Do you want to play or do you want the computer to play?"
print "Say \"computer\" or \"me\".\n> "
mode = gets.chomp
until mode == "computer" || mode == "me"
print "\nI didn't quite understand that. Say \"computer\" or \"me.\"\n> "
mode = gets.chomp
end
puts ""
@player_mode = false if mode == "computer"
end
def set_solution
puts "What would you like your solution to be?"
puts "\nType four colors separated by spaces."
print "Your choices are: red, green, yellow, blue, black, magenta, cyan, and white.\n> "
solution = get_player_guess
@gameboard.solution = ColorCode.new(solution[0], solution[1], solution[2], solution[3])
end
def start
make_guesses
turns > 12 ? lose : win
end
def make_guesses
while @win == false && @turns < 13
prompt_guess
guess = @player_mode ? get_player_guess : get_computer_guess
add_guess(guess)
@win = true if gameboard.guesses[12 - @turns].colors == gameboard.solution.colors
@turns += 1 if @win == false
end
end
def prompt_guess
puts @player_mode ? "What is your ##{@turns} guess?" : "\nComputer, make your ##{@turns} guess."
puts "Type four colors separated by spaces."
print "Your choices are: red, green, yellow, blue, black, magenta, cyan, and white.\n> "
end
def get_player_guess
1.times do
guess = gets.chomp.downcase.split(" ")
if guess.length != 4
print "\nYou entered the wrong amount of colors! Try again:\n> "
redo
end
if !@color_spectrum.include?(guess[0]) || !@color_spectrum.include?(guess[1]) || !@color_spectrum.include?(guess[2]) || !@color_spectrum.include?(guess[3])
print "\nYou can only enter the colors specified! Try again:\n> "
redo
end
if @player_mode == false
if guess.uniq.length !=4
print "\nYou must have different colors for the solution. Try again:\n> "
redo
end
end
return guess
end
end
def get_computer_guess
@computer.guess(@gameboard.hints, @turns)
end
def add_guess(guess)
gameboard.guesses[12 - @turns] = ColorCode.new(guess[0], guess[1], guess[2], guess[3])
gameboard.refresh(12 - @turns)
end
def lose
puts @player_mode ? "You have failed to crack the puzzle! It's okay; you're just not smart." : "No... I have failed."
puts "The solution was #{@gameboard.solution.colors}."
end
def win
puts @player_mode ? "\nYou have solved the code! Amazing!" : "\nI have solved the code, you measly human. Next I will destroy you!"
end
end
game = Mastermind.new()
game.play