I am building a Tic Tac Toe game in which the user can play a computer, or computers can play each other. While building the AI I am running into the below error. How can I debug this? I understand it is to do with a loop somewhere but I can't find it.
ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `each': stack level too deep (SystemStackError)
from ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `detect'
from ttt-with-ai-project-cb-000/lib/players/computer.rb:13:in `check_move'
from ttt-with-ai-project-cb-000/lib/players/computer.rb:8:in `move'
from ttt-with-ai-project-cb-000/lib/game.rb:61:in `wargame_turn'
from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
from ttt-with-ai-project-cb-000/lib/game.rb:64:in `wargame_turn'
... 11900 levels...
from bin/tictactoe:37:in `block in run_game'
from bin/tictactoe:35:in `times'
from bin/tictactoe:35:in `run_game'
from bin/tictactoe:79:in `<main>'
The Offending Methods
lib/players/computer.rb
def move(board)
check_move(board)
end
def check_move(board)
win_combo = Game::WIN_COMBINATIONS.detect do |indices|
board.cells[indices[0]] == token && board.cells[indices[1]] == token || board.cells[indices[1]] == token && board.cells[indices[2]] == token || board.cells[indices[0]] == token && board.cells[indices[2]] == token
end
win_combo.detect {|index| board.cells[index] == " "}.join if win_combo
end
lib/game.rb
def wargame_turn
input = current_player.move(board)
if !board.valid_move?(input)
wargame_turn
else
board.update(input, current_player)
end
end
bin/tictactoe calls the method in the following block within #run_game:
100.times do
game = Game.new(Players::Computer.new("X"), player_2 = Players::Computer.new("O"))
game.wargames
if game.winner == "X"
x_wins += 1
elsif game.winner == "O"
x_wins += 1
elsif game.draw?
draws += 1
end
end
You can use the ruby Tracer class:
ruby -r tracer your_main_script.rb
Also, you can take a look at your code and see where the loop may happen:
def wargame_turn
input = current_player.move(board)
if !board.valid_move?(input)
wargame_turn #### HERE'S A POTENTIAL INFINITE LOOP
So the heart of the problem seems at:
if !board.valid_move?(input)
It could be a good start.
Related
I am trying to write a simple custom function for fibonacci, But i am getting a error:
My code :
class Fibonacci
def fib(num)
#num = num.to_i
series = Array.new
series[0] = 0
series[1] = 1
for i in 0..series[#num]
series[#num+2] = series[#num] + series[#num+1]
end
return series
end
end
obj = Fibonacci.new
obj.fib(8)
Error :
ruby fibonacci.rb
fibonacci.rb:9:in `fib': bad value for range (ArgumentError)
from fibonacci.rb:19:in `<main>'
You're getting ArgumentError from 0..series[#num], where series[#num] will be nil at that point.
I think you meant to have:
for i in 0..#num
series[i+2] = series[i] + series[i+1]
end
I am trying to create the game Mastermind using test-driven development. I am having issues figuring out how to pass this specific test:
it "should valiate secret contains four accepted uppercase characters" do
#game.secret = "XXXX"
secret = "RRRR"
valid1 = #game.checksecret(secret)
valid2 = check_valid(secret)
valid1.should == valid2 && #game.secret.should == secret && valid1.should == 0
end
This is the code I have at the moment:
def checksecret(secret)
secret = "RRRR"
valid1 = secret
valid2 = check_valid(secret)
valid1.should == valid2 && secret.should == secret && valid1.should == 0
end
This is the error message I get.
1) OXs_Game::Game#Mastermind game should valiate secret contains four accepted uppercase characters
Failure/Error: #game.secret = secret
NoMethodError:
undefined method `secret=' for nil:NilClass
# ./wad_mastermind_spec_01.rb:13:in `check_valid'
# ./wad_mastermind_gen_01.rb:61:in `checksecret'
# ./wad_mastermind_spec_01.rb:92:in `block (3 levels) in <module:OXs_Game>'
I feel like I am trying to solve it the wrong way but can't figure out how I should be solving the problem.
I'm new to Ruby and in various open source software I've noticed a number of "statements" in some RSpec descriptions that appear not to accomplish what they intended, like they wanted to make an assertion, but didn't. Are these coding errors or is there some RSpec or Ruby magic I'm missing? (Likelihood of weirdly overloaded operators?)
The examples, with #??? added to the suspect lines:
(rubinius/spec/ruby/core/array/permutation_spec.rb)
it "returns no permutations when the given length has no permutations" do
#numbers.permutation(9).entries.size == 0 #???
#numbers.permutation(9) { |n| #yielded << n }
#yielded.should == []
end
(discourse/spec/models/topic_link_spec.rb)
it 'works' do
# ensure other_topic has a post
post
url = "http://#{test_uri.host}/t/#{other_topic.slug}/#{other_topic.id}"
topic.posts.create(user: user, raw: 'initial post')
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
TopicLink.extract_from(linked_post)
link = topic.topic_links.first
expect(link).to be_present
expect(link).to be_internal
expect(link.url).to eq(url)
expect(link.domain).to eq(test_uri.host)
link.link_topic_id == other_topic.id #???
expect(link).not_to be_reflection
...
(chef/spec/unit/chef_fs/parallelizer.rb)
context "With :ordered => false (unordered output)" do
it "An empty input produces an empty output" do
parallelize([], :ordered => false) do
sleep 10
end.to_a == [] #???
expect(elapsed_time).to be < 0.1
end
(bosh/spec/external/aws_bootstrap_spec.rb)
it "configures ELBs" do
load_balancer = elb.load_balancers.detect { |lb| lb.name == "cfrouter" }
expect(load_balancer).not_to be_nil
expect(load_balancer.subnets.sort {|s1, s2| s1.id <=> s2.id }).to eq([cf_elb1_subnet, cf_elb2_subnet].sort {|s1, s2| s1.id <=> s2.id })
expect(load_balancer.security_groups.map(&:name)).to eq(["web"])
config = Bosh::AwsCliPlugin::AwsConfig.new(aws_configuration_template)
hosted_zone = route53.hosted_zones.detect { |zone| zone.name == "#{config.vpc_generated_domain}." }
record_set = hosted_zone.resource_record_sets["\\052.#{config.vpc_generated_domain}.", 'CNAME'] # E.g. "*.midway.cf-app.com."
expect(record_set).not_to be_nil
record_set.resource_records.first[:value] == load_balancer.dns_name #???
expect(record_set.ttl).to eq(60)
end
I don't think there is any special behavior. I think you've found errors in the test code.
This doesn't work because there's no assertion, only a comparison:
#numbers.permutation(9).entries.size == 0
It would need to be written as:
#numbers.permutation(9).entries.size.should == 0
Or using the newer RSpec syntax:
expect(#numbers.permutation(9).entries.size).to eq(0)
I am using the Mongo Ruby driver and have this block of Ruby code before and after line 171 in my code, which is apparently the source of the error below it (the query.each line is line 171):
query = get_callback.call( "databases", id )
if query.count > 0
puts query.count.inspect + " results: " + query.inspect
res = {}
query.each do |result|
puts result.inspect
end
else
puts "No results" + res.inspect
res = {}
end
The error:
1 results: <Mongo::Cursor:0x3fc15642c154 namespace='myproj.databases' #selector={"_id"=>BSON::ObjectId('4fe120e4a2f9a386ed000001')} #cursor_id=>
TypeError - can't convert Mongo::Cursor into Integer:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.6.4/lib/bson/byte_buffer.rb:156:in `pack'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.6.4/lib/bson/byte_buffer.rb:156:in `put_int'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:603:in `construct_query_message'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:466:in `send_initial_query'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:459:in `refresh'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:128:in `next'
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:291:in `each'
/Users/myuser/Code/myproj/my_file.rb:171:in `block in initialize'
My query object: {"_id"=>BSON::ObjectId('4fe120e4a2f9a386ed000001')}
I have not the faintest idea what's causing this. I've verified the object I'm finding exists and the query.count shows that there's a result in my Mongo::Cursor.
I've not found any examples of the issue on Google and every Mongo/Ruby on the web I've found uses an each iterator just like I do. Anyone know what's the cause of this error? I notice I also get it when trying to use to_a to cast the collection to a JSON-usable object.
For what it's worth, here's the relevant part of byte_buffer.rb is below. The line with << is line 156.
def put_int(i, offset=nil)
#cursor = offset if offset
if more?
#str[#cursor, 4] = [i].pack(#int_pack_order)
else
ensure_length(#cursor)
#str << [i].pack(#int_pack_order)
end
#cursor += 4
end
This happens when you pass nil to a Ruby Mongo driver limit() method.
I'm getting some weirdness with QtRuby when using a TableWidget. The table widget loads, but when you click on the elements in the row, the app segfaults and crashes.
require 'Qt4'
class SimpleModel < Qt::AbstractTableModel
def rowCount(parent)
return 1
end
def columnCount(parent)
return 1
end
def data(index, role=Qt::DisplayRole)
return Qt::Variant.new("Really Long String") if index.row == 0 and index.column == 0 and role == Qt::DisplayRole
return Qt::Variant.new
end
end
Qt::Application.new(ARGV) do
Qt::TableWidget.new(1, 1) do
set_model SimpleModel.new
show
end
exec
end
The backtrace seems to imply that it is bombing in mousePressEvent
#6 0x01624643 in QAbstractItemView::pressed(QModelIndex const&) () from /usr/lib/libQtGui.so.4
#7 0x016306f5 in QAbstractItemView::mousePressEvent(QMouseEvent*) () from /usr/lib/libQtGui.so.4
If I override mousePressEvent and mouseMoveEvent, these kinds of crashes no longer happen. Am I doing something wrong over here, or can I chalk this up as a bug in QtRuby?
I'm on fedora11, with the following packages installed:
QtRuby-4.4.0-1.fc11.i586
ruby-1.8.6.369-1.fc11.i586
These crashes also happen when running the script on Windows.
You're using a Qt::TableWidget when you should be using a Qt::TableView. The following code fixed the crash for me. In addition to a switch from Qt::TableWidget to Qt::TableView, I also reimplemented the index method, just in case. :)
require 'Qt4'
class SimpleModel < Qt::AbstractTableModel
def rowCount(parent)
return 1
end
def columnCount(parent)
return 1
end
def data(index, role=Qt::DisplayRole)
return Qt::Variant.new("Really Long String") if index.row == 0 and index.column == 0 and role == Qt::DisplayRole
return Qt::Variant.new
end
def index(row, column, parent)
if (row > 0 || column > 0)
return Qt::ModelIndex.new
else
return createIndex(row, column, 128*row*column)
end
end
end
Qt::Application.new(ARGV) do
Qt::TableView.new do
set_model SimpleModel.new
show
end
exec
end