现在我正在尝试创建一个基本的tic tac toe游戏。在我开始编写AI之前,我想用两个人类玩家设置游戏,然后再添加到计算机中。我不确定最好的方式来设置多个玩家。 (我的代码在Ruby中)
num_of_users = 2
player1 = User.new
player2 = User.new
cpu = AI.new
if turn
# player1 stuff
turn = !turn
else
# player2 stuff
turn = !turn
end
这适用于两个玩家,但我不知道如何调整它,以便我能够对抗AI。有人可以帮我解决这个问题的最佳方法吗?
答案 0 :(得分:3)
在变量名中使用数字作为后缀通常表示您想要一个数组。
players = []
players[0] = User.new
players[1] = User.new # or AI.new
current_player = 0
game_over = false
while !game_over do
# the User#make_move and AI#make_move method are where you
# differentiate between the two - checking for game rules
# etc. should be the same for either.
players[current_player].make_move
if check_for_game_over
game_over = true
else
# general method to cycle the current turn among
# n players, and wrap around to 0 when the round
# ends (here n=2, of course)
current_player = (current_player + 1) % 2
end
end