纸牌游戏中的玩家顺序
我对玩家的顺序有疑问。我需要第一个玩家成为该轮的获胜者。所以我有四个按钮,有可能阻止错误玩家的移动,比如“不是你的回合,玩家二是第一个,因为是上一轮的获胜者”
我如何控制?最终一种方法来验证
我有四个卡片标签,
每个按钮的每个玩家手上有五张卡片,设置我做的对应卡片
//PLAYER1
//card1
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gr.getCounter1() < 5) {
gr.setCounter1(gr.getCounter1() + 1);
test1.setIcon(play1a);
pn1.setText(Integer.toString(play2a));
pn5.setText(play3a);
pn50.setText(play4a);
} else {
pn5.setText("No more cards");
}
}
};
arraybtn[1].addActionListener(one);
arraybtn[1].setPreferredSize(new Dimension(120, 20));
play1a = gr.GameRules1().getImage(); //image of card
play2a = gr.GameRules2(); // value
play3a = gr.GameRules3(); // king of hearts for exemple
play4a = gr.GameRules4(); // hearts
arraybtn[1].setText(gr.GameRules3()); // button name
i have a problem with the order of players. I need that the first player be the winer of round. So i have, four buttons, there is any possibility to block the move of incorrect player, something like "is not your turn, player two is the first because is the winer of previous round"
how i can control that? eventually a method to verify
i have four labels for cards, and five cards in hand for each player
for each button, set the correspondent card i do
//PLAYER1
//card1
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gr.getCounter1() < 5) {
gr.setCounter1(gr.getCounter1() + 1);
test1.setIcon(play1a);
pn1.setText(Integer.toString(play2a));
pn5.setText(play3a);
pn50.setText(play4a);
} else {
pn5.setText("No more cards");
}
}
};
arraybtn[1].addActionListener(one);
arraybtn[1].setPreferredSize(new Dimension(120, 20));
play1a = gr.GameRules1().getImage(); //image of card
play2a = gr.GameRules2(); // value
play3a = gr.GameRules3(); // king of hearts for exemple
play4a = gr.GameRules4(); // hearts
arraybtn[1].setText(gr.GameRules3()); // button name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你们的课程有多少个课程?希望它有一个驱动 GUI 的非 GUI 模型,并且应该是这个完全相同的模型来决定游戏的控制。我不会像本线程中的一位海报所建议的那样抛出错误,而是使用游戏控制器模型只允许正确的玩家玩。 GUI 将通过禁用所有不应按下的按钮并仅启用应按下的按钮来反映这一点。如何在代码中实现这一点取决于您当前程序的代码和整体结构。
编辑:答案的补充
不确定你是否还在,但这里有一个快速拼凑起来的例子来说明我的意思。 GamePlayer 类(显然)代表游戏的玩家,并且有一个字段 myTurn,仅当轮到该玩家时才为 true,并且该字段具有关联的 void setMyTurn(boolean) 方法和 boolean isMyTurn() 方法。
GameModel 类有一个名为players 的 GamePlayer 数组、一个允许其他类获取数组中每个 Player 的 getPlayer(int) 方法、一个表示当前轮到的玩家数组中索引的playerTurnIndex 以及一个 advancePlay( ) 方法,该方法增加此索引(按玩家数组的长度进行修改,以便它环绕),然后循环遍历玩家数组,根据playerTurnIndex 值为每个玩家设置 myTurn 字段。
GameGui 类有一个名为 gameModel 的 GameModel 字段,并保存 JButton 的集合(实际上是一个 HashMap),在每个按钮的 ActionPerformed 方法中,调用模型的 advancePlay() 方法,并调用一个名为 enablePlayerTurns() 的私有方法来启用或禁用玩家 JButtons 取决于模型中相应玩家的值:
How many classes does your program have? Hopefully it has a non-GUI model that drives the GUI, and it should be this very same model that dictates the control of game play. Rather than throw an error as one poster suggested in this thread, I would use the game controller model only allow the correct player to play. The GUI would reflect this by disabling all buttons that shouldn't be pushed and only enabling the ones that should be pushed. How this is implemented in code depends on your current program's code and overall structure.
edit: addition to answer
Not sure if you're still around, but here's a quickly slapped-together example of what I meant. The GamePlayer class represents (obviously) a player of the game and has a field, myTurn that is true only if it is that player's turn, and this field has an associated void setMyTurn(boolean) method and a boolean isMyTurn() method.
The GameModel class has an array of GamePlayer called players, a getPlayer(int) method that allows other classes to get each Player in the array, a playerTurnIndex that represents the index in the array of the player whose turn it currently is and a advancePlay() method that increments this index (mod'd by the length of the players array so it wraps around) and then loops through the players array setting the myTurn fields for each player depending on the playerTurnIndex value.
The GameGui class had a GameModel field called gameModel and holds a collection of JButtons (actually a HashMap), and in each button's ActionPerformed method, the model's advancePlay() method is called and a private method called enablePlayerTurns() is called that enables or disables the player JButtons depending on the value of the corresponding player in the model:
执行操作时:
非常简单。
When the action is performed:
Pretty straightforward.