[英]Basic C# Dice Game
我是c#和編碼的新手。 為了嘗試提高我的技能,我正在嘗試創建一個基本游戲,其中兩個玩家擲骰子並記錄他們的分數。 玩家通過達到20贏得勝利。每個玩家輪流擲骰子將他們的第一個擲骰子添加到他們的第二個等等,直到其中一個達到20.玩家如果擲出六個就可以再次擲骰子。
我目前的代碼是:
do
{
Console.Write("Enter the name of Player 1: ");
Player[0] = Console.ReadLine();
Console.Write("Enter the name of Player 2: ");
Player[1] = Console.ReadLine();
Random DiceRandom = new Random();
DiceThrow[0] = DiceRandom.Next(1, 7);
int i = 0;
while (i <= 1)
{
DiceThrow[0 + i] = DiceRandom.Next(1, 7);
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + DiceThrow[0 + i]);
if (DiceThrow[0 + i] != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();
}
while (PlayerTotal[0] == 20);
while (PlayerTotal[1] == 20);
我特別掙扎的是將球員第一次加注到第二次滾動。 如果一個玩家擲出6,那么它會將6加到他們在重新擲骰中得到的值。
任何幫助都將非常感謝。
您的代碼存在許多問題:
這只是一個簡短的概述..
這可以是一個解決方案:
Console.Write("Enter the name of Player 1: ");
Player[0] = Console.ReadLine();
Console.Write("Enter the name of Player 2: ");
Player[1] = Console.ReadLine();
Random DiceRandom = new Random();
do
{
int i = 0;
while (i <= 1)
{
int thisThrow = DiceRandom.Next(1, 6);
DiceThrow[0 + i] += thisThrow;
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + thisThrow);
if (thisThrow != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();
}
while (PlayerTotal[0] < 20 && PlayerTotal[1] < 20);
您的問題是您使用以下行重置以前的分數:
PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];
您應該將其更改為使用+=
如下所示:
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
這就像說: PlayerTotal[0] = PlayerTotal[0] + DiceThrow[0];
除此之外,您的代碼中還有一些問題。
例如,你有一個Do
的代碼,但2月初while
小號......你可能想創建一個While
使用AND語句。 此外, Do
語句需要在您獲得用戶名后...
例如://獲取用戶名
do
{
// All your Dice throwing logic
}
while (PlayerTotal[0] != 20 && PlayerTotal[1] != 20);
int i = 0;
while (i <= 1)
{
int thisThrow = DiceRandom.Next(1, 6);
DiceThrow[0 + i] += thisThrow;
Console.ReadLine();
Console.Write(Player[0 + i] + " rolled a " + thisThrow);
if (thisThrow != 6) i++;
}
Console.ReadLine();
PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];
Console.ReadLine();
Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
Console.ReadLine();
Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
Console.ReadLine();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.