Question

I'm having trouble getting my if statement for def is_valid_card_query(card_queried) to evaluate properly. I think it has something to do with my array and how I'm passing the variable in, perhaps?

I'm trying to evaluate first if the card is valid, meaning it's rank is (2 - A), second if the player has the card, and if so then I just return the card. But no matter what I do, it always defaults to the if.

cards_available.include? "card_queried" keeps evaluating to false *when I pass card_queried in through the method call*.

How can I fix this?

Here's the code. I have the portion I'm trying to run at the bottom:

require 'socket'
require 'rspec'
require 'json'
require_relative 'FishDeck.rb'
require_relative 'FishHand.rb'
require_relative 'FishGame.rb'
require_relative 'FishServer.rb'
require_relative 'FishPlayers.rb'
require_relative 'ServerTests.rb'



class ClientClass

    attr_accessor :players_available, :card_choices_available

    def initialize(host, port)

        @socket = TCPSocket.new(host, port)
        players_available = nil
        card_choices_available = nil
    end

    def get_input
        incoming = @socket.gets.chomp
        #puts "Here is the output for incoming: #{incoming}"
    end

    def send_input(message)
        outgoing = @socket.puts(message)
    end

    def close
        @socket.close
    end



    def is_valid_player_query(player_queried)

        if (player_queried > players_available) || (player_queried < 1)

            puts "I'm sorry, that's not a player."
            puts "Please enter a player number ranging from 1 through #{players_available}"
            user_input = gets.chomp
            is_valid_player_query(user_input)   #Recursion until proper response
        else
            return player_queried
        end

    end

    def is_valid_card_query(card_queried)
        puts "CARD QUERIED: #{card_queried}"
        cards_available = %w(2 3 4 5 6 7 8 9 10 J Q K A)
        puts "CARDS AVAILABLE: #{cards_available}"

        if (cards_available.include? "card_queried") == false
            puts "CARD NOT AVAILABLE EVER"
            puts "True if statement? #{cards_available.include? "card_queried"}"

            puts "I'm sorry, that's not a valid card choice."
            puts "Please enter a card number ranging from 2 through A.  You're using a traditional deck"
            user_input = gets.chomp
            user_input = is_valid_card_query(user_input)    #Recursion until proper response

        elsif (((cards_available.include? "card_queried") == true) && ((card_choices_available.include? "card_queried") == false))
            puts "CARD IN DECK BUT NOT HAND"
            puts "True if statement? #{cards_available.include? "card_queried"}"

            puts "I'm sorry, you don't have that card, so you can't ask for it."
            puts "Here are the cards for which you may ask: #{card_choices_available}"
            user_input = gets.chomp
            user_input = is_valid_card_query(user_input)

        else
            puts "True if statement? #{cards_available.include? "card_queried"}"
            puts "PASS!"
            return card_queried
        end
    end

    def input_decision(input)

        if input == "PLAYERS"

            input = @socket.gets.chomp  #gets the actual array
            input = input.to_i
            players_available = input

        elsif input == "CARDS"

            input = @socket.gets.chomp
            input = JSON.parse(input)
            card_choices_available = input

        elsif input == "EXIT"
            puts "The server has closed.  Goodbye!"
            #I need some way to break the loop or close the client if the server has closed

        elsif input == "ANNOUNCEMENT"   #just reads it

            input = @socket.gets.chomp
            puts input

        else    #get input from user case
            user_choice = []
            puts "It is your turn.  Please choose a player to ask for a card"
            user_input = gets.chomp
            user_input = is_valid_player_query(user_input)
            user_choice << user_input

            puts "Great!  You're asking Player #{user_input} for a card.  What card would you like?"
            user_input = gets.chomp
            user_input = is_valid_card_query(user_input)
            user_choice << user_input

            user_choice = user_choice.to_json
            @socket.send_input(user_choice)

        end
    end
end


@server = SocketServerClass.new(2012, 3)
@client1 = ClientClass.new('localhost', 2012)
@client2 = ClientClass.new('localhost', 2012)
@client3 = ClientClass.new('localhost', 2012)
@server.accept_client(@server.fish_game)
@server.accept_client(@server.fish_game)
@server.accept_client(@server.fish_game)

@client1.card_choices_available = %w(3, 6, 9 K, 10)

@client1.is_valid_card_query("3")   #Doesn't work just 3 , either.
Was it helpful?

Solution

Your code states that cards_available consists of the following array:

["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" ,"A"]

However, you are evaluating whether or not cards_available includes the string "card_queried" – this will never evaluate to true. What should be evaluating whether it includes the variable card_queried, which, according to your instructions, should be a string "ranging from 2 through A":

cards_available.include? card_queried
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top