如何在Console.WriteLine()语句中将数字转换为字母

 小男生2502863203 发布于 2022-12-04 13:59

下面是我一直在努力的一场垃圾游戏.我已经进入了一个我无法通过的基本封锁.

在这部分代码中,我无法找到一种方法将来自我的数组的数字转换为它们对应的面和套装.

Console.WriteLine("A " + deck[Game.DrawCard(deck), 0] + " of " + deck[Game.DrawCard(deck), 1] + " was drawn from the deck and then placed on top");

我可以模糊地想象一个使用一系列变量和if语句的路线,但我真的怀疑没有一个明显的,更简单的方法.

其余的代码看起来像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CribbageInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables//
            int currentPlayer;
            short score1 = 0;
            short score2 = 0;
            short round = 1;
            int[,] deck = new int[52, 2];

            Console.WriteLine("Welcome" + "\r" + "\r" + "Enter Number of Players (Only 2 player mode works right now)");
            while ((Console.ReadLine() != "2"))
            {
                Console.WriteLine("Only two player is working right now \n Make sure you are entering a number between 1-3 \n");
            }
            //A flip is made to determine who goes first//
            currentPlayer = Game.CoinFlip();
            Console.WriteLine("A coin was flipped and it was determined that player " + currentPlayer + " will go first \n");
            //Game.PrintDeck(deck);
            //Now the game is started//
            do
            {
                Console.WriteLine("Shuffling and dealing cards...");
                System.Threading.Thread.Sleep(5000);
                Game.InitDeck(deck);
                Console.WriteLine("Round " + round + "\n");
                //Cutting the Deck now that discarding has been done//
                Console.WriteLine("Cutting the deck... \n");
                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("A " + deck[Game.DrawCard(deck), 0] + " of " + deck[Game.DrawCard(deck), 1] + " was drawn from the deck and then placed on top");    //Insert a swtich statement to convert pairs of numbers into actual cards
                //Player 2's turn is started now that the cut is over//
                Console.WriteLine("");

                //pick up here

                Console.WriteLine("Player " + currentPlayer + " ");


                round++;
            } while ((score1 < 121) && (score2 < 121));  //Loops until either score exceeds 121 points//
        }
    }

    class Game  //Used for functions neccesary to the function of the game that are not neccesarily dependant on turn order and do not create win conditions.
    {
        public static int CoinFlip()    //Flips to see who goes first, results are between 1-2
        {
            var rnd = new Random(DateTime.Now.Millisecond);
            int firstPlayer = rnd.Next(1, 3);
            return (firstPlayer);
        }
        public static void InitDeck(int[,] deck) // First column 11==Jack, 12==queen, 13==king && Second Column 0==diamonds, 1==clubs, 2==spades, 3==hearts
        {
            //Initiallizing the first column==Faces
            for (int i = 0; i < 4; i++)
            {
                deck[i, 0] = 13;
                deck[(i + 4), 0] = 12;
                deck[(i + 8), 0] = 11;
                deck[(i + 12), 0] = 10;
                deck[(i + 16), 0] = 9;
                deck[(i + 20), 0] = 8;
                deck[(i + 24), 0] = 7;
                deck[(i + 28), 0] = 6;
                deck[(i + 32), 0] = 5;
                deck[(i + 36), 0] = 4;
                deck[(i + 40), 0] = 3;
                deck[(i + 44), 0] = 2;
                deck[(i + 48), 0] = 1;
            }
            //Initiallizing second column==Suit
            for (int i = 0; i < 4; i++)
            {
                deck[i, 1] = i;
                deck[(i + 4), 1] = i;
                deck[(i + 8), 1] = i;
                deck[(i + 12), 1] = i;
                deck[(i + 16), 1] = i;
                deck[(i + 20), 1] = i;
                deck[(i + 24), 1] = i;
                deck[(i + 28), 1] = i;
                deck[(i + 32), 1] = i;
                deck[(i + 36), 1] = i;
                deck[(i + 40), 1] = i;
                deck[(i + 44), 1] = i;
                deck[(i + 48), 1] = i;
            }
        }
        public static void PrintDeck(int[,] deck)
        {
            for (int i = 0; i < 52; i++)
            {
                Console.WriteLine(deck[i, 0] + "F");
                Console.WriteLine(deck[i, 1] + "S");
            }
            Console.ReadLine();
        }
        public static int DrawCard(int[,] deck) //Draws a card from the deck, ignoring Cards already drawn this turn. IF THERE ARE ANY ERRORS COME HERE AND CHECK THIS FIRST
        {
            var rnd = new Random(DateTime.Now.Millisecond);
            int o = rnd.Next(0, 51);
            while (deck[o,0]==0)
            {
                System.Threading.Thread.Sleep(1000);
                rnd = new Random(DateTime.Now.Millisecond);
                o = rnd.Next(0, 51);
            }
            int drawnCard = o;
            deck[o, 0] = 0;

            return (drawnCard);
        }

    }
}

我也有一种感觉,卡片的阵列可能不是最明智的决定而不是列表,但我并不积极.

我真的道歉了.虽然其他人可能已经明白我的主题已经被覆盖,但我对C#的有限理解不允许我解释我在过去一小时左右发现的任何帖子的任何答案.我试图通过控制台窗口编写一个简单的游戏来学习C#,因为这就是我学习C++的方法.

提前感谢任何人的帮助.我真的很感激!

1 个回答
  • 首先,我会将洗牌后的卡片存放在一个List,甚至更好的一个Queue.这允许您从牌组中"挑选"一张牌.

    要获得卡片和套件,我会创建两个枚举:

    public enum Rank
    {
       Ace,
       Two,
       Three,
       ...
    }
    
    public enum Suits
    {
       Hearts,
       Spades,
       ...
    }
    

    现在您定义卡片,使得0 = aH,1 = 2H,13 = aS,等等.

    现在你可以这样做:

    public Rank GetRankForCard(int card)
    {
         return (Rank)(card % 13);
    }
    
    public Suit GetSuitForCard(int card)
    {
        return (Suit)(card / 13);
    }
    

    %(模数)运算符在这里非常有用.作为最后一点,cribbage 并不是一个琐碎的游戏(在WPF中自己完成),祝你好运!

    2022-12-11 02:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有