-
-
Save mariozig/4507554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hangman | |
attr_accessor :words | |
def initialize | |
@words = [] | |
@word = [] | |
@hint = [] | |
@guess = "" | |
@player = Player.new() | |
@computer = Computer.new() | |
@tries | |
@mode | |
end | |
#debugger | |
def read_dictionary | |
File.foreach('words.txt') do |word| | |
@words << word.chomp | |
end | |
end | |
def play | |
@mode = get_mode | |
read_dictionary | |
if @mode == 1 | |
user_play | |
elsif @mode == 2 | |
@guess = @computer.play | |
process_computer_guess(@guess) | |
# get indicies of where the guess is in the users word | |
# and puts the letter (guess) in an array computer_guess_status | |
# in the positions returned by the user. | |
# computer_guess_status is the length given by the user (the length | |
# of their word) and each element is initialized to nil. | |
# the game will be won when computer_guess_status is the users word | |
else | |
puts "You entered an invalid option." | |
end | |
end | |
def user_play | |
@word = @computer.choose_word(@words) | |
create_initial_hint | |
@tries = 0 | |
won = false | |
while !won && @tries < 10 | |
won = play_round | |
end | |
if won == false | |
puts "You lose! The word is #{@word}" | |
end | |
end | |
def get_mode | |
puts "Press 1 to guess a word, press 2 to make the computer guess." | |
gets.chomp.to_i | |
end | |
def play_round #return true if win false if not win | |
show_hint | |
process_guess(get_guess) | |
end | |
def get_guess | |
@player.guess | |
end | |
def show_hint | |
puts "Secret word: #{@hint.join}" | |
print "> " | |
end | |
def create_initial_hint | |
@hint = [] | |
@word.length.times { @hint << "_" } | |
end | |
def process_guess(guess) | |
made_correct_guess = false | |
@word.split('').each_with_index do |letter, i| | |
if guess == letter | |
@hint[i] = guess | |
made_correct_guess = true | |
end | |
end | |
@tries +=1 unless made_correct_guess | |
if @hint.join == @word | |
puts "You won. The word was #{@word}!" | |
return true | |
else | |
return false | |
end | |
end | |
def process_computer_guess(guess) | |
puts "Is #{guess} in your word?" | |
# user is expected to respond with the index or indices of | |
# where the guess is in their word (using 0 as the index) | |
# of the last letter | |
@hint = gets.chomp | |
########## works until here ############### | |
end | |
end | |
class Computer | |
def initialize | |
@words = [] | |
end | |
def read_dictionary | |
File.foreach('words.txt') do |word| | |
@words << word.chomp | |
end | |
end | |
def choose_word(words) | |
words.sample | |
end | |
def play | |
letter_frequency = Hash.new(0) | |
read_dictionary | |
puts "Pick a word, any word. How many letters does it contain?" | |
word_length = gets.chomp.to_i | |
#get best guess from possible words | |
possible_words = @words.map {|word| word.length == word_length} | |
possible_words = @words.map {|word| word.length == word_length && word[1] == @hint[1]} | |
possible_word_letters = possible_words.join.split('') | |
possible_word_letters.each do |letter| | |
letter_frequency[letter] += 1 | |
end | |
frequency_array = letter_frequency.sort_by { |letter, frequency| frequency } | |
guess = frequency_array.pop[0] | |
return guess | |
end | |
end | |
class Player | |
attr_accessor :guess | |
def initialize | |
end | |
def guess | |
@guess = gets.chomp | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Computer | |
attr_accessor :masterlist | |
def initialize | |
@masterlist = [] | |
@colors = ["R", "G", "B", "Y", "O", "P"] | |
end | |
def create_win_combo | |
@masterlist = @colors.shuffle[0..3] | |
end | |
def win?(guess) | |
@masterlist == guess | |
end | |
def matched_colors(guess) | |
index = 0 | |
while index < 4 | |
if (@masterlist[index] == guess[index]) | |
puts "#{guess[index]} is a match!" | |
end | |
index +=1 | |
end | |
end | |
def existing_colors(guess) | |
guess.each_with_index do |color, index| | |
if @masterlist.include?(color) | |
if @masterlist[index] != guess[index] | |
puts "#{color} is the right color, wrong position" | |
end | |
end | |
end | |
end | |
end | |
class User | |
def initialize | |
end | |
def guess | |
gets.upcase.chomp.split('') | |
end | |
end | |
class Mastermind | |
attr_accessor :computer_player, :player, :current_guess | |
def initialize | |
@computer_player = Computer.new() | |
@player = User.new() | |
@current_guess = [] | |
end | |
def play | |
@computer_player.create_win_combo | |
won = false | |
tries = 0 | |
while !won && tries < 11 | |
p @computer_player.masterlist | |
play_round | |
tries += 1 | |
if @computer_player.win?(@current_guess) | |
puts "You won!" | |
won = true | |
else | |
@computer_player.matched_colors(@current_guess) | |
@computer_player.existing_colors(@current_guess) | |
end | |
if tries == 11 | |
puts "Sorry, you didn't guess within 10 tries!" | |
end | |
end | |
end | |
def play_round | |
puts "Guess a sequence of 4 colors" | |
puts "(R)ed, (G)reen, (B)lue, (Y)ellow, (O)range, (P)ink" | |
@current_guess = player.guess | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment