现在,我正在尝试创建一个基本的井字游戏。在我开始编写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对抗的情况。有人能帮我找到解决这个问题的最好方法吗?
发布于 2011-09-07 13:30:14
在变量名中使用数字作为后缀通常是您需要一个数组的标志。
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
endhttps://stackoverflow.com/questions/7329097
复制相似问题