2017-04-07 274 views
0

我已經設置了一個賦值,這意味着我需要創建一個'3個或更多的骰子游戲'。我堅持的是這款遊戲需要的評分系統,它如下: 「 玩家反過來擲出所有五個骰子,並得分爲三類或更好。如果玩家只有如果沒有匹配的數字滾動,玩家得分爲0.如何計算一個數組中值的出現次數c#

玩家可以相應地得到下面的點數:

3-的一類:3分 4-的一類:6分 5-的一類:12分

輪的一組數被播放(比如說50)和在比賽結束時總得分最高的玩家是勝利者。 「 我需要找出如何添加了隨機骰子的數字,看看哪個號碼匹配。

  namespace DiceGame 
      { 
       class Program 
       { 
        static void Main(string[] args) 
        { 

         Console.WriteLine("Hello, I am your computer and I 
    am speaking to you, welcome to the dice game, here are the rules:"); 
         Console.WriteLine("3-of-a-kind: 3 points "); 
         Console.WriteLine("4-of-a-kind: 6 points "); 
         Console.WriteLine("5-of-a-kind: 12 points"); 
         Console.WriteLine("First player to reach 50 wins"); 

         //integer array to store 5 values for the dice 
         int[] Rolls = new int[5]; 


         //Calls from class Dice (calls integer value) 
         for (int numdice = 0; numdice < 5; numdice++) 
         { 
          Rolls[numdice] = Dice.Roll(); 
          //For each loop, get position of array and place 
number we want in that position 
          Console.WriteLine(Rolls[numdice]); 
         } 
         //Makes a new game object which is used to call 
functions 
         Game game = new Game(); 
         game.startGame(); 
         Console.ReadLine(); 
        } 

        //produces one random number 
        public class Dice 
        { 
         static Random rng = new Random(); 
         public static int Roll() 
         { 
          //create a random number generator 
          return rng.Next(1, 7); 
         } 
        } 

        public class Game 
        { 
         public void startGame() 
         { 
          //prompts user to input value and then stores it 
          int playerNo = numberofPlayers(); 
          while (playerNo < 2) 
          { 
           Console.WriteLine("Please enter a number 
between 2-4"); 
           playerNo = numberofPlayers(); 
          } 
          //Now have number of players, need to loop 
through now 

          //creates the number of players in array 
          player[] listofPlayers = new player[playerNo]; 
          //this looks at the current player that the code 
is looking at 
          for (int currentPlayer = 0; currentPlayer < 
playerNo; currentPlayer++) 
          { 
           listofPlayers[currentPlayer] = new player(); 
           Console.WriteLine("It is player {0}'s turn", 
currentPlayer + 1); 


listofPlayers[currentPlayer].rollplayerDice(); 

Console.WriteLine(listofPlayers[currentPlayer].score); 

           listofPlayers[currentPlayer].playersScore(); 

          } 
         } 

         void inputPlayers() 
         { 
          //create number of players code 
          //create a way to input name of players 
          //depending on the number of players, repeat the 
code below that many times 
          string player1 = Console.ReadLine(); 
         } 

         public int numberofPlayers() 
         { 
          int playerNum = 0; 
          try 
          { 
           Console.WriteLine("Please Enter the number 
of players you would like to play with 2-4"); 

           playerNum = int.Parse(Console.ReadLine()); 
          } 
          catch (Exception e) 
          { 
           Console.WriteLine(e.Message); 
          } 
          return playerNum; 

          //if playerNo is equal to 2 = 2 players 
          //if playerNO is equal to 3 = 3 players 
          //if playerNO is equal to 4 = 4 players 
          //if playerNo is less than 2 and more than 4 
then loop back through the if statement and ask to input and number "between 
2-4" 
         } 

         public class player 
         { 
          public int score = 0; 
          int[] playerRoll = new int[5]; 

          public void rollplayerDice() 
          { 

           for (int currentDice = 0; currentDice < 
playerRoll.Length; currentDice++) 
           { 
            playerRoll[currentDice] = Dice.Roll(); 
            Console.WriteLine("Dice {0} rolled a 
{1}", currentDice, playerRoll[currentDice]); 
           } 
          } 

          public int playersScore() 
          { 
           int[] diceFaces = new int[6]; 

           /*for (int diceFace = 0; diceFace < 
playerRoll.Length; diceFace++) 
           { 
             int oneCounter = 0; 


             //number of 1's = 
             //number of 2's = 
             //number of 3's = 
             //number of 4's = 
             //number of 5's = 
             //number of 6's = 
             //create a system to work out the 
score 





             //create a switch to see what the 
player score is equal to (switch 3, 4, 5 and add up the points that 
correlate) 
            } 
            */ 

           foreach (int d in playerRoll) 
           { 
            diceFaces[d]++; 
           } 




           int caseSwitch = 0; 
           switch (caseSwitch) 

           { 
            case 3: 
             //add on the points to a players 
score 
             score += 3; 
             break; 
            case 4: 
             //add on the points to a player 
score 
             break; 

            case 5: 
             //add on the points of a players 
score 
             break; 
           } 
           return 0; 
          } 

         } 

        } 
       } 
      } 

^以上是我的全部代碼和下面是我在工作的代碼,現在在嘗試的。得分

public int playersScore() 
          { 
           int[] diceFaces = new int[6]; 

           /*for (int diceFace = 0; diceFace < 
playerRoll.Length; diceFace++) 
           { 
             int oneCounter = 0; 


             //number of 1's = 
             //number of 2's = 
             //number of 3's = 
             //number of 4's = 
             //number of 5's = 
             //number of 6's = 
             //create a system to work out the 
score 





             //create a switch to see what the 
player score is equal to (switch 3, 4, 5 and add up the points that 
correlate) 
            } 
            */ 

           foreach (int d in playerRoll) 
           { 
            diceFaces[d]++; 
           } 




           int caseSwitch = 0; 
           switch (caseSwitch) 

           { 
            case 3: 
             //add on the points to a players 
score 
             score += 3; 
             break; 
            case 4: 
             //add on the points to a player 
score 
             break; 

            case 5: 
             //add on the points of a players 
score 
             break; 
           } 
           return 0; 
          } 

         } 
+0

你是否熟悉Lambda的..?你可以很容易地做到這一點寫一個簡單的lambda表達式..做一個谷歌搜索大量的工作示例 – MethodMan

+0

'var numOnes = dice.Where(die => die == 1).Count();' –

+2

您應該提供一個簡單的例子,格式化您的代碼並刪除不需要的評論。 –

回答

0

您可以使用GroupBy(),然後Count()

int score = 0; 
var grouped = dice.GroupBy(x => x); 
//Score 3 of a kinds 
score += grouped.Count(x => x.Count() == 3) * 3; 
//Score 4 of a kinds 
score += grouped.Count(x => x.Count() == 4) * 6; 
//Score 5 of a kinds 
score += grouped.Count(x => x.Count() == 5) * 12; 

工作fiddle

相關問題