Undefined Method


#1

I’m going crazy trying to determine the cause of this “undefined method”
error. Any advice would be amazing


C:/Users/Glenn/workspace/FirstProject/Blackjack.rb:51:in play_game': undefined methoddetermine_winner’ for #Game:0x00000002b941a8
(NoMethodError)
from C:/Users/Glenn/workspace/FirstProject/Blackjack.rb:195:in block in <main>' from C:/Users/Glenn/workspace/FirstProject/Blackjack.rb:193:inloop’
from C:/Users/Glenn/workspace/FirstProject/Blackjack.rb:193:in
`’

class Screen
def cls
puts ("\n" * 25)
puts “\a”
end
def pause
STDIN.gets

end
end
class Game
def display_greeting
Console_Screen.cls
print “\t\t\tWelcome to the Ruby Blackjack Game!” +
“\n\n\n\n\n\n\n\nPress Enter to” +
" continue."
end
def display_instructions
puts “these are the instructions”
Console_Screen.pause

end

def play_game

Console_Screen.cls

#gives player initial hand

playerHand = get_new_card
dealerHand = get_new_card

#calls method that deals new card

playerHand = complete_player_hand(playerHand, dealerHand)

#if the player hasn’t buster call the dealer’s hand

if playerHand <= 21 then
dealerHand = play_dealer_hand(dealerHand)
end
#call method to determin who won

determine_winner(playerHand, dealerHand)

end

end

def get_new_card
#assign random number from 1-13
card = 1 + rand(13)

return 11 if card == 1 #if its an Ace
return 10 if card >= 10

return card

end
#---------------------Says the problem is here-------------
def complete_player_hand(playerHand, dealerHand)
#-----------------------------------------------------
loop do

  Console_Screen.cls

  #shows the current states of the hands
  puts "Player hand: " + playerHand.to_s + "\n\n"
    puts "Dealer's hand: " + dealerHand.to_s +
    "\n\n\n\n\n"
    print "Would you like another card? (Y/N) "

    reply = STDIN.gets
    reply.chop!

    if reply =~ /y/i then
      #call new hand method
      playerHand = playerHand + get_new_card
    end

    if reply =~ /n/i then
      break
    end

    if playerHand > 21 then
      break
    end
end
     return playerHand

    end

    def play_dealer_hand(dealerHand)

      loop do
        #if dealers hand is less than 17 give another card

        if dealerHand < 17 then
          dealerHand = dealerHand + get_new_card
        else
          break
        end
        return dealerHand

     end

     def determine_winner(playerHand, dealerHand)

       Console_Screen.cls

       #shows the hands
       puts "Player's hand: " + playerHand.to_s + "\n\n"
       puts "Dealer's hand: " + dealerHand.to_s +
         "\n\n\n\n\n\n"

         if playerHand > 21 then
           puts "You have gone bust!\n\n"
           print "Press Enter to continue"
         else
           if playerHand == dealerHand then
             puts "Tie!\n\n"
           end
           if dealerHand > 21 then
             puts "The dealer has gone bust!\n\n"
             print "Press Enter to continue"
           else
             if playerHand > dealerHand then
               puts "You have won!\n\n"
               print "Press Enter to Continue"
             end
             if playerHand < dealerHand then
               puts "The Dealer has won!\n\n\""
               print "Press Enter to continue."

             end
          end
        end

         Console_Screen.pause

     end

    end
     def display_credits

       Console_Screen.cls

       puts "\t\t\   Thank you for playing"
       puts "\n\t\t\t Developed by Glenn B. Chamberlain"
       puts "\t\t\t\t Copyright 2017\n\n"
       puts "\t\t\t\tURL: http://www.glennisthebest.com" +
         "\n\n\n\n\n\n"
     end

     #Main

logic------------------------------------------------------------------------------------------
Console_Screen = Screen.new

     BJ = Game.new
     BJ.display_greeting

     answer = ""

     #step 14
     loop do
       Console_Screen.cls

       print "Are you ready to play Ruby Blackjack? (y/n) : "
       answer = STDIN.gets
       answer.chop!

       break if answer =~ /y|n/i

     end

     if answer =~ /n/i

       Console_Screen.cls

       puts "Okay, perhaps another time.\n\n"
       Console_Screen.pause

     else

       BJ.display_instructions

       playAgain = ""

       loop do

         BJ.play_game
         loop do
           Console_Screen.cls
           print "Would you like to play another round? (y/n)"

           playAgain = STDIN.gets
           playAgain.chop!

           break if playAgain +~ /n|y/i

         end

         break if playAgain =~ /n/i

       end

       BJ.display_credits
     end

#2

Looks like you are defining the determine_winner function INSIDE the
play_dealer_hand function.

Should probably be:

    def play_dealer_hand(dealerHand)

      loop do
        #if dealers hand is less than 17 give another card

        if dealerHand < 17 then
          dealerHand = dealerHand + get_new_card
        else
          break
        end
        return dealerHand

     end
     end
     def determine_winner(playerHand, dealerHand)