【问题标题】:I want to make a simple card game but what's wrong with it?我想做一个简单的纸牌游戏,但有什么问题?
【发布时间】:2021-07-03 13:35:09
【问题描述】:

该游戏使用 40 张牌,20 张红和 20 张绿。

卡片:1, 2, 3, 4, 5, ...., 20.

颜色:红色、绿色

它由两个玩家一起玩。球员的名字被取走。

选择要玩的牌张数。 (N

给玩家随机的 N'er 牌。

依次比较玩家 1 和玩家 2 的 N'er 牌。

如果比较的一张牌是绿色的,一张是红色的,那张牌是绿色的玩家的分数就会增加。

如果比较的两张卡片颜色相同,则卡片分数较高的玩家的分数增加。

玩家 1 和玩家 2 的牌和颜色都写在屏幕上。如Y1、K1、Y3

打印球员的分数。

得分较高的玩家被宣布为获胜者。

using System;

namespace Card_Game
{
    class Program
    {
        static void Main(string[] args)
        {
            int player1score=0, player2score=0;
            string player1, player2;
            Console.WriteLine("enter player name 1");
            player1 = Console.ReadLine();
            Console.WriteLine("enter player name 2");
            player2 = Console.ReadLine();
           
            int number;
            Console.WriteLine("Enter how many cards you want to be given, " +
                "you can choose a maximum of 20, there are 40 cards in total in the game.");
            number = Convert.ToInt32(Console.ReadLine());
           
                Random random = new Random();
            int s1=0, s2=0;
            for (int i = 0; i < number; i++)
                {
               
                int player1card1 = random.Next(1, 3);
                /*if the number is 1 ,it means our card colour is green or number is 2,
                 * it means our card colour is red*/
                int player1card2 = random.Next(1, 21);
                int player2card1 = random.Next(1, 3);
                int player2card2 = random.Next(1, 21);
                Console.WriteLine("{0} player's card is given", player1);
                
                Console.WriteLine( "colour :"+ player1card1);

                Console.WriteLine("number:" + player1card2);
                Console.WriteLine("{0} player's card is given", player2);
                
                Console.WriteLine("colour :" + player2card1);
                
                Console.WriteLine("number :" + player2card2);
               
                if (player1card1 == 1 && player2card1 != 1)
                {
                    player1score  ++;
                }
                else if(player1card1 != 1 && player2card1 == 1)
                {
                    player2score  ++;
                }
                else
                {
                    if (player1card2 > player2card2)
                        player1score ++;
                    else
                        player2score ++;
                }

            }
            if (player1score > player2score)
                Console.WriteLine("Player {0} wins with {1} points Score of player {2} " +
                     "{3}", player1, player1score, player2, player2score);
            else if (player1score < player2score)
                Console.WriteLine("Player {0} wins with {1} points Score of player {2} " +
                     "{3}", player2, player2score,player1, player1score);
            else
                Console.WriteLine("the game is drawn with {0} points ", player1score);

        }
    }
}

我想做的是通过生成2个随机数而不是定义单独的卡片来实现这个功能。

如果第一个随机数为 1,则卡片为绿色,另一张卡片为红色。但是这里有两个问题,卡片的数量是 20,当我定义随机数时,会出现相同的数字。怎么设置卡数相等?

【问题讨论】:

    标签: c#


    【解决方案1】:

    想想纸牌游戏通常是如何玩的:

    1. 首先,创建卡片组(打印时!)
    2. 然后洗牌(但还没有人有牌!)
    3. 卡片从洗好的牌堆中按顺序分发

    因此,发牌时并没有确定牌 - 它们的随机性在洗牌时已经分配。当它们被洗牌时,它们已经存在了! 因此,为了使您的方法与此类似,您应该生成所有卡片,然后随机排序:

    List<int> allCards = new List<int>();
    
    // create the deck
    for (int i = 0; i < 20; ++i) {
       allCards.Add(0); // one of each card
       allCards.Add(1);
    }
    var shuffledDeck = allCards.OrderBy(c => random.Next()).ToList(); // shuffle the deck
    
    // get the cards for the persons hand
    

    【讨论】:

    • 感谢您的建议@Rob G
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2011-08-02
    • 1970-01-01
    • 2013-11-02
    • 2010-10-12
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多