我目前正在编写一个tic tac toe游戏的代码。我正在寻求一些建议,以便在使用命令行播放游戏时创建网格的最佳方法是什么。我打算使用哈希作为场所,并使用多维数组来获得可能的获胜组合。
@places = {
"a1"=>" ","a2"=>" ","a3"=>" ",
"b1"=>" ","b2"=>" ","b3"=>" ",
"c1"=>" ","c2"=>" ","c3"=>" "
}
@columns = [
['a1','a2','a3'],
['b1','b2','b3'],
['c1','c2','c3'],
['a1','b1','c1'],
['a2','b2','c2'],
['a3','b3','c3'],
['a1','b2','c3'],
['c1','b2','a3']
]
def put_line
puts "-----------------------------------------------------------------------------"
end
def put_bar
puts "#############################################################################"
puts "#############################################################################"
end
我认为我最大的困惑在于如何使用board_spec.rb和board.rb来编写它。
如果有人能就此提供任何指导,我们将不胜感激
答案 0 :(得分:0)
当然 - 你打算让董事会上课吗?我认为测试看起来像:
require 'rspec'
require_relative 'tictactoe'
describe Board do
it 'has a 3 x 3 grid' do
board = Board.new
expect(board.board[0].size).to eq 3
expect(board.board.size).to eq 3
end
end
让你可以做的测试通过:
class Board
attr_reader :board
def initialize
@board = [
['-', '-', '-'],
['-', '-', '-'],
['-', '-', '-']
]
end
def printb
@board.each do |row|
print "|"
row.each { |cell| print "#{cell}|" }
puts "\n-------"
end
end
end
board = Board.new
board.printb