骰子游戏:如何允许多个玩家 [英] Dice Game: How to allow for multiple players
问题描述
我最近要求对此问题提供一些帮助: 骰子游戏:获得分数总和 [ ^ ]
我现在正在尝试允许多个玩家(本地)玩游戏。每个玩家在前一个玩家完成后轮到他们玩。
我已经意识到我需要创建玩家
我可以在其中创建多个玩家实例来玩游戏。
我的问题是,玩家
类看起来像?正如我在上面提到的问题中所提到的,我只是学习编程所以我的理解非常基础。
我目前的游戏规则是:
- 玩家掷5个骰子并将所有面值的总和加到它们的总分数中
- 滚动1或4会导致无效滚动。没有得分并且这些骰子被从比赛中移除。
- 玩家继续掷骰子直到他们没有留下骰子,导致下一个玩家开始转弯。
到目前为止,这是我的代码:
public < span class =code-keyword> partial class Form1:Form
{
Image [] diceImages;
Label []标签;
int [] dice;
int diceTotal;
bool rolledOneOrFour;
随机r;
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
diceImages = new 图片[]
{
Properties.Resources.blank,
Properties.Resources.one,
Properties.Resources.two,
Properties.Resources.three,
Properties.Resources.four,
Properties.Resources.five,
Properties.Resources.six
};
dice = new int [ 5 跨度>];
r = new Random();
diceTotal = 0 ;
labels = new 标签[]
{
lbl_dice1,
lbl_dice2,
lbl_dice3 ,
lbl_dice4,
lbl_dice5
};
}
public void btn_roll_Click( object sender,EventArgs e)
{
rollDice();
}
private void rollDice()
{
bool OneOrFourRolled = false ;
int rollTotal = 0 ;
int sum = 0 ;
int playerScore = 0 ;
for ( int i = 0 ; i < dice.Length; i ++)
{
if (rolledOneOrFour)
{
if (dice [i] == 1 || dice [i] == 4 )
{
dice [i] = 0 跨度>;
}
if (dice [i] == 0 )
{
继续;
}
}
int rollDice = r.Next( 1 , 7 );
骰子[i] = rollDice;
sum + = rollDice;
如果(骰子[roll] == 1 ||骰子[roll] ] == 4 )
{
TwoOrFiveRolled = true ;
}
rollTotal + =骰子[roll];
}
if (!TwoOrFiveRolled)
{
diceTotal + = rollTotal;
lb_liveResults.Items.Add( 你得分 + sum + < span class =code-string> 此滚动的点数和 + diceTotal + 总计);
}
penalty = true ;
for ( int i = 0 ; i < labels.Length; i ++)
{
labels [i] .Image = diceImages [dice [一世]];
}
}
当前播放器类为空,因为我不确定从哪里开始:
public class 玩家
{
}
这取决于Player类的对象需要做什么(它的方法)以及它需要保留的东西(它的属性)。
- 每个玩家是否依次掷骰子并保持累积分数?
- 玩家在双倍后获得多次投掷吗?
- 玩家是否因任何原因错过投掷?
...
超过玩家
类,我认为你需要一个Player
类(注意单数形式。
因为您可以在列表< Player>
中管理一系列Player对象。
类似于:
列表<播放> players = new List< Player>();
players.Add( new 播放器( John);
players.Add( new Player( Jane);
然后,您可以遍历集合并使用播放器执行任何操作获得的对象:
foreach (播放器播放器 players){
// ...
}
希望这会有所帮助。如果我误解了你的问题,或者你有什么不清楚的话,请告诉我。
I recently asked for some assistance for this question: Dice Game: Obtaining the sum of scores[^]
I am now trying to allow for multiple players to play (locally) the game. Each player getting their turn to play after the previous player has finished.
I have realized that I will need to create a Players
class in which I can create multiple instances of players to play the game.
My question is, what would the Players
class look like? As I mentioned in the question linked above, I am just learning to program so my understanding is very basic.
My current game rules are:
- Players roll 5 dice and add up the sum of all the face values to their totalScore
- Rolling a 1 or 4 results in an invalid roll. No score is given and those dice are removed from play.
- Players continue to roll their remaining dice until they are left with none, resulting in the start of the next players turn.
Here is my code so far:
public partial class Form1 : Form
{
Image[] diceImages;
Label[] labels;
int[] dice;
int diceTotal;
bool rolledOneOrFour;
Random r;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
diceImages = new Image[]
{
Properties.Resources.blank,
Properties.Resources.one,
Properties.Resources.two,
Properties.Resources.three,
Properties.Resources.four,
Properties.Resources.five,
Properties.Resources.six
};
dice = new int[5];
r = new Random();
diceTotal = 0;
labels = new Label[]
{
lbl_dice1,
lbl_dice2,
lbl_dice3,
lbl_dice4,
lbl_dice5
};
}
public void btn_roll_Click(object sender, EventArgs e)
{
rollDice();
}
private void rollDice()
{
bool OneOrFourRolled = false;
int rollTotal = 0;
int sum = 0;
int playerScore = 0;
for (int i = 0; i < dice.Length; i++)
{
if (rolledOneOrFour)
{
if (dice[i] == 1 || dice[i] == 4)
{
dice[i] = 0;
}
if (dice[i] == 0)
{
continue;
}
}
int rollDice = r.Next(1, 7);
dice[i] = rollDice;
sum += rollDice;
if (dice[roll] == 1 || dice[roll] == 4)
{
TwoOrFiveRolled = true;
}
rollTotal += dice[roll];
}
if (!TwoOrFiveRolled)
{
diceTotal += rollTotal;
lb_liveResults.Items.Add("You scored " + sum + " points for this roll and " + diceTotal + " in total");
}
penalty = true;
for (int i = 0; i < labels.Length; i++)
{
labels[i].Image = diceImages[dice[i]];
}
}
Current Player class empty, as I am unsure on where to start:
public class Player
{
}
It depends on what an object of the Player class needs to do (its methods) and what things it needs to keep (its properties).
- Does each player throw the dice in turn and keep an accumulated score?
- Does a player get more than one throw after a double?
- Does a player miss a throw for any reason?
...
More than aPlayers
class, I think you need aPlayer
class (note the singular form).
Because you can manage a collection of Player objects in aList<Player>
.
Something like:
List<Player> players = new List<Player>(); players.Add(new Player("John"); players.Add(new Player("Jane");
You can then iterate over the collection and do whatever you need with the Player object obtained:
foreach (Player player in players) { // ... }
Hope this helps. Let me know if I misunderstood your question, or if something is still unclear to you.
这篇关于骰子游戏:如何允许多个玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!