首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在游戏中设置两个玩家

在游戏中设置两个玩家
EN

Stack Overflow用户
提问于 2011-09-07 13:08:48
回答 1查看 839关注 0票数 0

现在,我正在尝试创建一个基本的井字游戏。在我开始编写AI代码之前,我想用两个人类玩家来设置游戏,然后再添加到计算机中。不过,我不太确定设置多个玩家的最佳方式。(我的代码是Ruby)

代码语言:javascript
复制
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对抗的情况。有人能帮我找到解决这个问题的最好方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-07 13:30:14

在变量名中使用数字作为后缀通常是您需要一个数组的标志。

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7329097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档