无法获得if语句正确评估 - .include?总是评估为假

时间:2014-01-04 03:24:52

标签: ruby arrays if-statement methods boolean-logic

我无法让def is_valid_card_query(card_queried)的if语句正确评估。我认为它与我的数组有关,以及我如何传递变量,或许?

我正在尝试首先评估该卡是否有效,这意味着它的排名为(2 - A),如果玩家有卡,则为秒,如果是,那么我只是退回卡。但无论我做什么, 它总是默认为if

当我通过方法调用*传递card_queried时,

cards_available.include? "card_queried"继续评估为false *。

我该如何解决这个问题?

这是代码。我有我试图在底部运行的部分:

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.

1 个答案:

答案 0 :(得分:2)

您的代码指出cards_available包含以下数组:

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

但是,您正在评估cards_available是否包含字符串 "card_queried" - 这将永远不会评估为true应该评估它是否包含变量 card_queried,根据您的说明,它应该是一个“从2到A的字符串”:

cards_available.include? card_queried