简体   繁体   English

Ruby程序返回错误“将字符串隐式转换为Integer(TypeError)”

[英]Ruby program returns error “implicit conversion of String into Integer (TypeError)”

I'm trying to create a replica of Go Fish to help me learn more about arrays and hashes, and just how to go about structuring data. 我正在尝试创建Go Fish的副本,以帮助我了解有关数组和哈希以及如何构造数据的更多信息。 I'm on day two and have what looks to be much closer to the end goal. 我在第二天,看起来与最终目标更加接近。 Keep in mind, I'm new to this. 请记住,我是新手。 Anyway, here's the problem I'm running into: 无论如何,这是我遇到的问题:

=> gofish.rb:21:in `player_turn': no implicit conversion of String into Integer (TypeError) => gofish.rb:21:在`player_turn'中:没有将String隐式转换为Integer(TypeError)

I understand why I'm getting the error, but I can't figure out how to use the .shift method without giving an index number. 我知道为什么会收到错误,但是我不知道如何在不提供索引号的情况下使用.shift方法。 I would like to select which object to shift based on the value instead. 我想根据值选择要移动的对象。 So, if I correctly guess do you have an 'ace of spades', the card is removed from the cpu_hand array and is added to the my_hand array. 因此,如果我正确地猜到您是否拥有“黑桃王牌”,则将卡从cpu_hand数组中删除,然后添加到my_hand数组中。 With that said, I would just like to know the best way to go about this. 话虽如此,我只想知道最好的解决方法。

Here's my script: 这是我的脚本:

card_values = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'diamonds', 'hearts', 'clubs']

# creates array objects with every value and suit possible (full deck)
card_deck = card_values.product(suits).collect{|card, suit| "#{card} of #{suit}"}

def print_hand
  puts "Your hand: #{@my_hand.join(', ')}."
end

def player_turn
  puts "You go first!"
  puts "Do you have a..."
  puts @cpu_hand.join(', ')
  print "> "
  @card = $stdin.gets.chomp.downcase
  # if cpu has the card requested give it to the player and add to their array
    if @cpu_hand.include?(@card)
      puts "Ahhh...you got me. Here you go!"
      @my_hand.shift(@cpu_hand[@card]) # ****Here's the error(line:21) 
      print_hand
    else
      puts "Go fish!"
      @my_hand.shift(@card_deck[1])
      print_hand
    end
end


puts "There are #{card_deck.length} cards in this deck."
puts "Welcome to Go-Fish."
print "Your name please: "
player_name = $stdin.gets.chomp.capitalize

puts "Ok #{player_name}, lets get this deck shuffled..."
#sleep(1)
# shuffles card_deck using .shuffle method
card_deck = card_deck.shuffle
puts "Cards are perfectly shuffled!"
#sleep(1)
puts "Dealing cards..."
#sleep(1)
# assigns first 7 cards to user, removes from card_deck
@my_hand = Array.new
@my_hand = card_deck.shift(7)
# assigns next 7 cards to CPU, removes from card_deck
@cpu_hand = Array.new
@cpu_hand = card_deck.shift(7)

print_hand

until card_deck.length < 1 || @cpu_hand.length < 1 || @my_hand.length < 1
  player_turn
end

puts "GAME OVER!"

In your error line, 在您的错误行中

@my_hand.shift(@cpu_hand[@card])

your crash is caused by attempting to index into the @cpu_hand array with a string ( @card ). 您的崩溃是由于尝试使用字符串( @card )索引到@cpu_hand数组而引起的。 The second issue is trying to call shift using a string as a parameter. 第二个问题是尝试使用字符串作为参数调用shift Both of these need to be integers. 这两个都必须是整数。 shift removes the first element (or first n elements if an integer parameter is specified); shift删除第一个元素(如果指定了整数参数,则删除前n元素); this seems appropriate for taking an item from card_deck , but you'd want push , unshift , or << to add an element to @my_hand . 这似乎很适合从card_deck提取项目,但是您希望pushunshift<<@my_hand添加元素。 You'll also need a method to remove the specified card from @cpu_hand , delete : 您还需要一种从@cpu_hand delete指定卡的方法, delete

if @cpu_hand.include?(@card)
  puts "Ahhh...you got me. Here you go!"
  @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
  print_hand
else
  puts "Go fish!"
  @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
  print_hand
end

However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck , or make it global. 但是, card_deck与其他变量不同,它不是全局变量,因此您需要将其传递给函数并将函数头更改为def player_turn card_deck ,或使其def player_turn card_deck But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. 但是将所有内容全局化通常被认为是糟糕的设计,因为数据可以从任何地方进行突变,从而导致难以发现的错误。 Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game. 考虑编写诸如HandGoFish类的类,例如,它们封装了表示游戏可重用部分所需的所有逻辑。

I recommend taking a read through the array docs . 我建议您仔细阅读数组docs You'll surely find some cool methods and learn a few tidbits. 您一定会找到一些很酷的方法,并学到一些花絮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby:循环并调用关联数组(TypeError不会将String隐式转换为Integer) - Ruby: Looping and calling associative arrays (TypeError no implicit conversion of String into Integer) TypeError-在Ruby on Rails数组中没有将String隐式转换为Integer - TypeError - no implicit conversion of String into Integer in array Ruby on Rails Ruby Array:TypeError(没有将Symbol隐式转换为Integer) - Ruby Array: TypeError (no implicit conversion of Symbol into Integer) Ruby类型错误:没有将符号隐式转换为整数 - Ruby Type Error: no implicit conversion of Symbol into Integer 嵌套数组 ruby​​ TypeError:没有将哈希隐式转换为整数 - nested array ruby TypeError: no implicit conversion of Hash into Integer Rails-升级到ruby 2.2.2-没有将数组隐式转换为String(TypeError) - Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError) 错误:“没有将符号隐式转换为整数” - Error: “No implicit conversion of Symbol into Integer” 没有将nil隐式转换为String-ruby - No implicit conversion of nil into String - ruby Ruby on Rails数组-不将符号隐式转换为整数 - Ruby on rails array - no implicit conversion of symbol into integer 将数组添加到非Rails模型会产生TypeError-不会将String隐式转换为Integer - Adding array to non-rails model gives TypeError - no implicit conversion of String into Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM