How to Use Hashes to Store methods in Ruby? - ruby

I was wondering if anyone could explain to me why I can't use my Hash($player_x_command_list) to access my player_x_move(position) method? What's the correct way of doing this?
What I am trying to do is, create a Hash that lets the user enter input and it reflecting back to my methods in my class.
I have tried various ways of altering so that the code could work correctly but nothing works.
I'm getting the error:
undefined method `player_x_move' for Game:Class (NoMethodError)
Does that mean hashes can't store methods?
Here's my code:
#Tic Tac Toe Game
#The format is the below: where index 0 represents top left and index 8 represents bottom right
#goes 0,1,2
# 3,4,5
# 6,7,8
#"e" is for empty. "x" is for Player X moves and "o" is for Player O moves
class Game
##player_x_win_count = 0
##player_o_win_count = 0
def initialize
#board = Array.new(9, "e")
#move_number = 0
end
def get_names
puts "Hi Welcome to my Tic Tac Toe Game. The board looks like this:
|TL|TM|TR|
|ML|MM|MR|
|BL|BM|BR|
Each position of the Tic Tac Toe board is represented by two letters. To \"X\" or \"O\" a position, just input the two letters in CAPS like \"MM\"
The command list is as follows:
TL = top left
TM = top mid
TR = top right
ML = mid left
MM = mid mid
MR = mid right
BL = bottom left
BM = bottom mid
BR = bottom right
board = to view the board
new game = to clean the board and create a new game (note that this command should only be used when you don't want to continue on the current game. The game automatically creates a new game if a winner, loser, or draw is declared)
"
puts "Please Enter PlayerX's name. He/she will be using X's to mark the board."
#player_x = gets.chomp
puts "Please Enter PlayerO's name. He/she will be using O's to mark the board."
#player_o = gets.chomp
self.new_round
end
$player_x_command_list = {"TL" => self.player_x_move(0), "TM" => self.player_x_move(1), "TR" => self.player_x_move(2), "ML" => self.player_x_move(3), "MM" => self.player_x_move(4),
"MR" => self.player_x_move(5), "BL" => self.player_x_move(6), "BM" => self.player_x_move(7), "BR" => self.player_x_move(8), "board" => self.board, "new game" => self.clean_board,
"win count" => self.win_count}
$player_o_command_list = {"TL" => self.player_o_move(0), "TM" => self.player_o_move(1), "TR" => self.player_o_move(2), "ML" => self.player_o_move(3), "MM" => self.player_o_move(4),
"MR" => self.player_o_move(5), "BL" => self.player_o_move(6), "BM" => self.player_o_move(7), "BR" => self.player_o_move(8), "board" => self.board, "new game" => self.clean_board,
"win count" => self.win_count}
def enter_command_player_x
puts "Please input your command, #{#player_x} aka PlayerX"
command = gets.chomp
$player_x_command_list[command]
end
def enter_command_player_o
puts "Please input your command, #{#player_o} aka PlayerY. Type \"help\" to see a full list of commands"
command = gets.chomp
$player_o_command_list[command]
end
def new_round
puts "So who wants to go first this round"
went_first_this_round = gets.chomp
if went_first_this_round == #player_x
self.enter_command_player_x
elsif went_first_this_round == #player_o
self.enter_command_player_o
else
puts "Not a valid name. Please enter one of the player's names"
end
end
def board
print "|#{#board[0]}|#{#board[1]}|#{#board[2]}|\n|#{#board[3]}|#{#board[4]}|#{#board[5]}|\n|#{#board[6]}|#{#board[7]}|#{#board[8]}|"
end
def player_x_move(position)
if #board[position] == "x" || #board[position] == "o"
return "That move was invalid as someone has already moved there. Please enter a valid move"
end
#board[position] = "x"
#move_number += 1
puts "That was move number #{#move_number} and the current board looks like: "
self.board
self.check_game
puts "Now it is #{player_o}'s turn. #{player_o} please input your next command."
self.enter_command_player_o
end
def player_o_move(position)
if #board[position] == "x" || #board[position] == "o"
return "That move was invalid as someone has already moved there. Please enter a valid move"
end
#board[position] = "o"
#move_number += 1
puts "That was move number #{#move_number} and the current board looks like: "
self.board
self.check_game
puts "Now it is #{player_x}'s turn. #{player_x} please input your next command"
self.enter_command_player_x
end
def check_game
triple_x = "xxx"
triple_o = "ooo"
if #move_number == 9
#move_number = 0
self.clean_board
return "The board is completely filled up. Looks like this is a draw. This is Game Over. Make a new game by setting any variable = to new.Game and using that variable to play"
elsif #board[0] + #board[1] + #board[2] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[3] + #board[4] + #board[5] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[6] + #board[7] + #board[8] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[0] + #board[3] + #board[6] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[1] + #board[4] + #board[7] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[2] + #board[5] + #board[8] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[0] + #board[4] + #board[8] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
elsif #board[2] + #board[4] + #board[6] == triple_x
##player_x_win_count += 1
#move_number = 0
self.clean_board
return "Player X Wins"
#now check if Player O Wins
elsif #board[0] + #board[1] + #board[2] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[3] + #board[4] + #board[5] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[6] + #board[7] + #board[8] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[0] + #board[3] + #board[6] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[1] + #board[4] + #board[7] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[2] + #board[5] + #board[8] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[0] + #board[4] + #board[8] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
elsif #board[2] + #board[4] + #board[6] == triple_o
##player_y_win_count += 1
#move_number = 0
self.clean_board
return "Player O Wins"
else
return "no one has WON YET! Continue your GAME!!"
end
end
def clean_board
#board = Array.new(9, "e")
end
def win_count
puts "So far Player X has won #{##player_x_win_count} times and Player O has won #{##player_o_win_count} times."
end
end
a = Game.new
a.get_names

You have a few things wrong here:
Your scope is wrong. When defining the hash, self is the class, not the instance of the class that you want to operate on.
When you create the hash, the value of the hash is going to be return value of the player_x_move etc method at the time it was defined.
You can simplify this a lot by using case.
def enter_command
puts "Please input your command, #{#player_x} aka PlayerX"
command = gets.chomp
case command
when "TL"
player_x_move(0)
when "TM"
player_x_move(1)
# etc
else
puts "#{command} is not a valid command."
end
end
You can also simplify your code further by creating methods that accept parameters like enter_command, player_move, and then passing the player to operate on to them. This prevents you from having to duplicate each of your methods for each player.
Something else you could consider is just looking up the index of the move based on the command given:
COMMAND_POSITIONS = %w(TL TM TR ML MM MR BL BM BR)
def enter_command(player)
puts "Please input your command, #{player}"
command = gets.chomp
case command
when *COMMAND_POSITIONS
player_move player, COMMAND_POSITIONS.index(command)
when "board"
board
when "new game"
clean_board
when "win count"
win_count
else
puts "#{command}" is not a valid command
end
end

Hashes can not store methods. Methods aren't objects, but can be converted to Procs. Method definitions return nilchanged since this writing. However, you can store a Proc or a lambda. You can also store the return (evaluation) of a method.
Here is an example of how you can store a Proc to a Hash that was derived from a method.
>> def hello(name)
>> "Hello #{name}!"
>> end
=> nil
>> my_stored_methods = {:hello => method(:hello).to_proc}
=> {:hello=>#<Proc:0x816f604 (lambda)>}
>> my_stored_methods[:hello].call("World")
=> "Hello World!"
Do not let the stroop effect of me calling the hash "my_stored_methods" lead you to believe that there is actually a real method stored there. It is lambda (a specialized Proc) stored in the hash, and used as appropriate. Indeed, had I not used .to_proc there, it would hold a Method instance.
Also, this solution does not grow with the development of the open method, if the method were to change, the Proc stored in the hash would continue to work as the method did at the point when the method was stored as a Proc.
As #AndrewMarshall reminds me, I could have left it as a Method instance. This still will not "store" the method itself, as when the method changes, the result will still be the historical behavior of the source method as it was when stored. It also provides for a stronger "Stroop effect" in that you may mistakenly think that an actual method is stored there. It simply is not.

Related

Ruby Number Guessing Game

I have tried to make a number guessing game using ruby but it seems to be looping all over again after the user gets the correct answer, here's my code and thanks in advance!
require './input_functions'
def check (rno,input)
x = 1
while (x == 1)
y = 0
if (rno > input)
puts("Try a bigger number")
input = gets.chomp.to_i
y = y + 1
x = 1
else
if (rno < input)
puts("Try a smaller number")
input = gets.chomp.to_i
y = y + 1
x = 1
else
if(rno == input)
puts("Bingo!")
x = 0
end
end
end
end
return
end
def main
rno = rand(100)
input = read_integer("Enter an integer between 0 and 100: ")
check(rno,input,y)
times = check(rno,input,y)
puts ("You have tried " + times.to_s + " times.")
end
main
The main problems are:
You call check method two times within main method, you need to remove the first one.
You need to move y = 0 out of while loop to be able to return it.
Tried to simplify your code a bit, and you could try to improve it even more.
def check(rno, input)
x = 1
y = 0
while x == 1
if rno == input
puts "Bingo!"
x = 0
else
if rno > input
puts "Try a bigger number"
else
puts "Try a smaller number"
end
input = gets.chomp.to_i
y = y + 1
end
end
return y
end
def main
rno = rand(100)
input = read_integer("Enter an integer between 0 and 100: ")
times = check(rno, input)
puts ("You have tried " + times.to_s + " times.")
end

Ruby - How do you use a while loop and conditional statement in a 2 Player Dice Game?

New to programming and trying to create a very basic two player dice throwing game in Ruby using while loops with conditional breaks.
I need it to play several rounds for each game - the first player to win two rounds wins the game (with two dice each)
So far I have come up with:
player_1 = 2 + rand(6) + rand(6) #generates number between 1-12 (simulating two dice being thrown)
player_2 = 2 + rand(6) + rand(6)
puts player_1
puts player_2
player_1_score = 0
player_2_score = 0
while true do
if player_1 > player_2
return player_1_score = player_1_score + 1
elsif player_2 > player_1
return player_2_score = player_2_score + 1
end
if player_1_score == 2
puts "Player 1 wins!"
break
elsif player_2_score == 2
puts "Player 2 wins!"
break
end
end
At the moment it is only producing the two random numbers, no error messages. I need to it to produce a number of rounds and either "Player 1 wins!" or "Player 2 wins!" when one of them has won two rounds.
Am I on the right track or missing something completely?
Can you have more than one if statement in a while loop?
Any help on understanding while loops would be really appreciated!
Thanks
As stated in comments, you need to throw dices inside the loop.
For example defining a method that returns an array with the two results.
def throw_dices
return rand(1..6), rand(1..6)
end
You can assign the result to the variables using array decomposition:
player_1, player_2 = throw_dices
Once you have the method you can use it inside the loop.
player_1_score = 0
player_2_score = 0
while true do
player_1, player_2 = throw_dices # here is the decomposition
puts "player_1: #{player_1} - player_2: #{player_2}" # print the throw result or whatever
player_1_score += 1 if player_1 > player_2
player_2_score += 1 if player_2 > player_1
break if player_1_score == 2 || player_2_score == 2
end
p player_1_score
p player_2_score
# do whatever you want with the final score.
Note also the use of abbreviated assignments: player_1_score += 1

Increment variables within a method?

I'm trying to build a rock, paper, scissors game. Could someone please tell me or point me in the right direction of where I'm going wrong with this code?
I want the players_score and cpu_score variables to increment by 1 each time either one wins a round.
choice = ["rock", "paper", "Scissors"]
players_score = 0
cpu_score = 0
def win_lose(a,b,c,d)
if a == "rock" && b == "scissors"
c+=1
puts "YOU WIN!!"
elsif a == "scissors" && b == "rock"
d+=1
puts "YOU LOSE!!"
elsif a =="paper" && b == "rock"
c+=1
puts "YOU WIN!!"
elsif a =="rock" && b == "paper"
d+=1
puts "YOU LOSE!!"
elsif a == "scissors" && b == "paper"
c+=1
puts "YOU WIN!!"
elsif a == "paper" && b == "scissors"
d+=1
puts "YOU LOSE!!"
else a == b
puts "Its a Draw this time!!"
end
end
while players_score < 2 && cpu_score < 2
print "Lets play. Plese choose rock, paper or scissors: "
players = gets.chomp.downcase
puts "You have #{players}"
cpu = choice.sample.downcase
puts "Computer has #{cpu}"
win_lose(players, cpu, players_score, cpu_score)
puts "scores are player #{players_score} , cpu #{cpu_score}"
end
Return a value from win_lose, and use that to determine increment the variable, e.g.:
def win_lose(a,b,c,d)
if a == "rock" && b == "scissors"
c+=1
winner = 'player'
elsif a == "scissors" && b == "rock"
d+=1
winner = 'computer'
# [.. etc ..]
else a == b
winner = 'draw'
end
return winner
end
player_score = 0
cpu_score = 0
choice = ["rock", "paper", "Scissors"]
while players_score < 2 && cpu_score < 2
print "Lets play. Plese choose rock, paper or scissors: "
players = gets.chomp.downcase
puts "You have #{players}"
cpu = choice.sample.downcase
puts "Computer has #{cpu}"
winner = win_lose(players, cpu, players_score, cpu_score)
if winner == 'player'
player_score += 1
puts "YOU WIN!!"
elsif winner == 'computer'
cpu_score += 1
puts "YOU LOSE!!"
else
puts "It's a draw"
end
puts "scores are player #{players_score} , cpu #{cpu_score}"
end
As you can see, this also allows you to not repeat the same "YOU WIN!!" sentence over and over again. I moved that outside the win_lose function so that this function does only one thing: determine the winner or loser, rather than two things: determine the winner or loser, and inform them of this.
This distinction is perhaps not important for such a small program, but as your programs grow it will be critical to keep your program understandable.

How do I update one element in an array and show new version of my array in the tictactoe grid?

This is a tictactoe game. When a player chooses a grid via a number, it is supposed to update the picked number with X or O, and then show the updated grid. Unfortunately, it continues to output the same default grid of numbers. I used map! but it does not work. If I switch #player1 : x to x: #player1, it changes the entire array to X or O.
Once I figure this out, the win method will be the next task to check. Will win work to determine the winning combinations?
#a = [1,2,3,4,5,6,7,8,9]
def game
#game_board = "#{#a[0]}|#{#a[1]}|#{#a[2]}\n" "------\n" "#{#a[3]}|#{#a[4]}|#{#a[5]}\n" "------\n" "#{#a[6]}|#{#a[7]}|#{#a[8]}\n"
#game_board
end
def secondchoice
if #player1 == "X"
#player2 = "O"
elsif #player1 == "O"
#player2 = "X"
else puts "please pick a valid number"
end
end
def start
puts " Player One, Pick Your Tic or Toe , X or O"
#player1 = gets.chomp.upcase
#player2 = secondchoice
puts "Player one is #{#player1}"
puts "Now Player Two is #{#player2}"
end
def player_turns
# player 1 gets then player 2 gets until game over == true
# nine total moves using a loop with a counter will work
moves = 1
while moves < 10
if moves.odd?
puts #game_board
puts "its player One's turn! place #{#player1} on the board by picking a number: "
cell = gets.chomp
#a.map! {|x|x == cell ? #player1 : x}
moves += 1
unless check_win == true
end
else
puts #game_board
puts "its player Two's turn! place #{#player2} on the board by picking a number: "
kell = gets.chomp
#a.map! {|x|x == kell ? #player2 : x}
moves += 1
unless check_win == true
end
end
end
end
def win
# 10 turns(count) with no combination is a draw
return [[#a[0]+ #a[1] + #a[2]],[#a[3] + #a[4] + #a[5]],[#a[6] + #a[7] + #a[8]],[#a[0]+ #a[3] + #a[6]],[#a[1] + #a[4] + #a[7]],[#a[2] + #a[5]+ #a[8]],[#a[0]+ #a[4] + #a[8]],[#a[2] + #a[4] + #a[6]]]
#game_over check if player has 3 in a row
end
# Create a loop that gives player turns. Player one then player two until a draw or three in row
def check_win
win.each do |arr|
str = arr.join
if str == "xxx"
puts "X Wins!"
return true
elsif str == "ooo"
puts "O Wins!"
return true
end
end
return false
end
while check_win != true
start
game
player_turns
end
It's not clear why map! would factor in here. A simple array manipulation is all that's required:
#a[cell.to_i - 1] = #player2
Specifying the cells as zero indexed would make this easier.
There's a lot of evidence of going against the grain here that's made for a ton more code than necessary. For example, variables like #player1 and #player2 are usually a sign of bad design. Why not #players = %w[ X O ]? That alone solves a lot of problems if you apply that array throughout your code instead of having per-player variables. Each turn: #players.unshift(#players.pop)

While loop continues despite conditions not being met?

When I run the game and type either "yes" or "no" at the end, it always reverts back to the start of the while loop at line 41, when the conditions for both that and the containing loop are not met.
replay = true
while replay
#Pre-var
print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
difficulty = gets.chomp.to_i
until difficulty == 1 || difficulty == 2 || difficulty == 3 do
print "Please type either 1, 2, or 3: "
difficulty = gets.chomp.to_i
end
#Variables
if difficulty == 1
r = Random.new
number = r.rand(100..1000)
puts "You have 15 guesses. "
print "Guess a number with three digits: "
within_num = 50
elsif difficulty == 2
r = Random.new
number = r.rand(1000..10000)
puts "You have 15 guesses. "
print "Guess a number with four digits: "
within_num = 500
elsif difficulty == 3
r = Random.new
number = r.rand(10000..100000)
puts "You have 15 guesses. "
print "Guess a number with five digits: "
within_num = 5000
end
guess = ""
num_guess = 0
guess_array = Array.new
array_location = 0
count_through = 0
array_print = count_through - 1
replay_inner = true
#Body
puts number
while num_guess <= 14 || replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
guess = gets.chomp.to_i
if guess > number * 2
print "That is more than double the number. Guess again: "
elsif guess < number / 2
print "That is less than half the number. Guess again: "
elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
print "You are close. That is too big. Guess again: "
elsif guess < number && guess > number - within_num
print "You are close. That is too small. Guess again: "
elsif guess < number
print "That is too small. Guess again: " #Hinting the user to how close they are.
elsif guess > number
print "That is too big. Guess again: "
elsif guess == number
puts "Congragulations! You win!"
print "Your "
print guess_array.length
print " incorrect guesses were: "
if num_guess == 0
sleep(0.5)
print "... Oh."
else
while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
print guess_array[count_through]
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
puts "Would you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
puts "no"
end
end
end
guess_array.push guess
num_guess += 1
#puts num_guess
#puts guess_array[array_location]
array_location += 1
if num_guess >= 15 && guess != number
puts "Sorry, you lost. "
print "Your "
print guess_array.size
print " guesses were: "
while count_through < num_guess
print guess_array[count_through] #Same as loop above; for when player fails to guess correctly
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
puts "Would you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
replay_inner = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
replay_inner = false
puts "no"
end
end
end
I think the condition in the while should be:
numberGuess<=14 && replay_inner == true
The way you and especially your fellow programmers will find this and future bugs is by properly commenting your code, e.g.:
# ask the user about an optional replay
replay_answer = gets.chomp
# only accept yes/y/no/n
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
## recoded this to a case, as I think it's much nicer :)
# determining whether to replay or to stop the loop
case replay_answer
when "yes", "y"
replay = true
puts "yes"
when "no", "n"
replay = false
puts "no"
replay_inner = false
end
I've modified a little your code, you should really use some decent editor with at least syntax errors highlighting. I've changed OR to AND condition in inner loop (comment in code) and added way of breaking from it when guessed number. I've also removed second occurence of code responsible of playing again, remember, DRY yourself.
replay = true
while replay
p replay
#Pre-var
print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
difficulty = gets.chomp.to_i
until difficulty == 1 || difficulty == 2 || difficulty == 3 do
print "Please type either 1, 2, or 3: "
difficulty = gets.chomp.to_i
end
#Variables
if difficulty == 1
r = Random.new
number = r.rand(100..1000)
puts "You have 15 guesses. "
print "Guess a number with three digits: "
within_num = 50
elsif difficulty == 2
r = Random.new
number = r.rand(1000..10000)
puts "You have 15 guesses. "
print "Guess a number with four digits: "
within_num = 500
elsif difficulty == 3
r = Random.new
number = r.rand(10000..100000)
puts "You have 15 guesses. "
print "Guess a number with five digits: "
within_num = 5000
end
guess = ""
num_guess = 0
guess_array = Array.new
array_location = 0
count_through = 0
array_print = count_through - 1
replay_inner = true
#Body
puts number
while num_guess <= 14 && replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
guess = gets.chomp.to_i
if guess > number * 2
print "That is more than double the number. Guess again: "
elsif guess < number / 2
print "That is less than half the number. Guess again: "
elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
print "You are close. That is too big. Guess again: "
elsif guess < number && guess > number - within_num
print "You are close. That is too small. Guess again: "
elsif guess < number
print "That is too small. Guess again: " #Hinting the user to how close they are.
elsif guess > number
print "That is too big. Guess again: "
elsif guess == number
puts "Congragulations! You win!"
print "Your "
print guess_array.length
print " incorrect guesses were: "
if num_guess == 0
sleep(0.5)
print "... Oh."
else
while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
print guess_array[count_through]
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
end
replay_inner = false # or just `break`, you have to break somehow from inner loop here
end
guess_array.push guess
num_guess += 1
#puts num_guess
#puts guess_array[array_location]
array_location += 1
if num_guess >= 15 && guess != number
puts "Sorry, you lost. "
print "Your "
print guess_array.size
print " guesses were: "
while count_through < num_guess
print guess_array[count_through] #Same as loop above; for when player fails to guess correctly
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
end
end
puts "\nWould you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
replay_inner = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
replay_inner = false
puts "no"
end
end

Resources