2016-04-25 62 views
0

我有一个扑克游戏,其中2到5个玩家在按下开始按钮之前加入游戏。他们作为包含用户名和用户名的对象添加到玩家阵列数组中。需要通过一个动态大小的数组循环php

我需要填充我的播放器对象currentUser然后点击一个按钮(呼叫/升/倍)移动到阵列中的下给游戏活动座椅的错觉座位

当启动按钮被点击我将当前用户设置为数组中的第一个元素。

$thisPokergame->State['CurrentUser'] = RESET($thisPokergame->State['Players']); 

然后即时如果尝试这种

if($thisPokergame->State['CurrentUser'] == end($thisPokergame->State['Players'])) 
    { 
     $thisPokergame->State['CurrentUser'] = reset($thisPokergame->State['Players']); 
    } 
    else{ 
     $thisPokergame->State['CurrentUser'] = next($thisPokergame->State['Players']);; 
    } 

作品本身e.g如果阵列中的最后一名选手将它设置当前用户为第一,所以做了其他内声明。

当我把它们放在一起我得到currentuser为false,我不能看到原因。任何人有解决方案或更好的方法来做到这一点?

+0

为什么你不使用变量来跟踪当前玩家的位置。你从'$ activePosition = 0'开始为player1,然后使用'$ currentUser = $ thisPokergame-> State ['Players] [$ activePosition];'然后当玩家改变你增加计数器'$ counter = ++ $ counter/5;'如果你有5个玩家。或者,你的“国家”中的位置很重要? – olibiaz

回答

1

你有这个问题,因为你正在使用的功能移动内部数组指针。

你总是会得到false在有条件的else部分,因为当你在if条件下使用end($thisPokergame->State['Players']),您已经移动内部数组指针的最后一个元素。然后在else当你做next($thisPokergame->State['Players']),你得到false,因为没有更多的元素。

不知道肯定什么是CurrentUserPlayers我不能确定这到底是怎么来代替处理,但我认为这可能工作:

$player = array_shift($thisPokergame->State['Players']); 
$thisPokergame->State['Players'][] = $player; 
$thisPokergame->State['CurrentUser'] = reset($thisPokergame->State['Players']); 

基本上只是旋转列表中的第一个球员玩家到列表末尾,然后将当前用户设置为列表中新的第一个玩家。

+0

是啊,这使得完美sense.is有任何替代方法来做到这一点? –

+0

@stephenreilly我刚刚更新了我认为可能有效的答案。尽管如此,它还没有经过测试。只是我的理论。 –

+0

好的,谢谢你在哪里? –