我正在为我的学校开发一个简单的C#二十一点控制台应用程序游戏,我给了一个例子来看看。这个例子以某种方式在控制台窗口中绘制了一张卡的图片,我无法弄清楚如何复制这个,而没有为52个唯一卡中的每一张指定数百个Console.Write。
in game scene这就是你实际玩游戏时的样子。相当不错。
shuffle and show deck主菜单中还有一个选项可以随机播放和显示所有52张卡片。
那个巫术是什么?他们真的花了很多时间硬编码每张独特的卡片是如何打印出来的吗?我当然希望不是。这就是我想要复制的内容,除了硬编码之外,我对这些想法感到茫然。谢谢你的帮助。
感谢Damien_The_Unbeliever的评论,我能够在我的卡片类中提出这两种方法。还要感谢vik_78的评论让我知道我需要UTF8编码才能看到卡符号。
public void PrintCard()
{
if (_value == 1)
{
_printString =
" V " +
" " +
" " +
" S " +
" " +
" " +
" V " ;
PrintMethod();
}
if (_value == 2)
{
_printString =
" V " +
" S " +
" " +
" " +
" " +
" S " +
" V ";
PrintMethod();
}
if (_value == 3)
{
_printString =
" V " +
" S " +
" " +
" S " +
" " +
" S " +
" V ";
PrintMethod();
}
if (_value == 4)
{
_printString =
" V " +
" S S " +
" " +
" " +
" " +
" S S " +
" V ";
PrintMethod();
}
if (_value == 5)
{
_printString =
" V " +
" S S " +
" " +
" S " +
" " +
" S S " +
" V ";
PrintMethod();
}
if (_value == 6)
{
_printString =
" V " +
" S S " +
" " +
" S S " +
" " +
" S S " +
" V ";
PrintMethod();
}
if (_value == 7)
{
_printString =
" V " +
" S S " +
" S " +
" S S " +
" " +
" S S " +
" V ";
PrintMethod();
}
if (_value == 8)
{
_printString =
" V " +
" S S " +
" S " +
" S S " +
" S " +
" S S " +
" V ";
PrintMethod();
}
if (_value == 9)
{
_printString =
" V " +
" S S S " +
" " +
" S S S " +
" " +
" S S S " +
" V ";
PrintMethod();
}
if (_value == 10 || _value == 11 || _value == 12 || _value == 13)
{
_printString =
" V " +
" S S " +
" S " +
" S S S S " +
" S " +
" S S " +
" V ";
PrintMethod();
}
}
private void PrintMethod()
{
bool hasWrittenFirstNumber = false;
switch (_suit)
{
case "Hearts":
case "Diamonds":
Console.ForegroundColor = ConsoleColor.Red;
break;
case "Clubs":
case "Spades":
Console.ForegroundColor = ConsoleColor.Black;
break;
}
for (int i = 0; i < _printString.Length; i++)
{
Console.BackgroundColor = ConsoleColor.White;
if (i % 11 == 0 && i != 0)
{
Console.CursorLeft -= 11;
Console.CursorTop += 1;
}
if (_printString[i] == 'S')
{
switch (_suit)
{
case "Hearts":
Console.Write('♥');
break;
case "Clubs":
Console.Write("♣");
break;
case "Diamonds":
Console.Write("♦");
break;
case "Spades":
Console.Write("♠");
break;
}
continue;
}
else if (_printString[i] == 'V')
{
if (_value == 10)
{
if (hasWrittenFirstNumber == false)
{
Console.Write(10);
hasWrittenFirstNumber = true;
i++;
}
else
{
Console.CursorLeft--;
Console.Write(10);
}
continue;
}
else if (_value == 11)
{
Console.Write("J");
}
else if (_value == 12)
{
Console.Write("Q");
}
else if (_value == 13)
{
Console.Write("K");
}
else if (_value == 1)
{
Console.Write("A");
}
else
{
Console.Write(_value);
}
}
else
{
Console.Write(_printString[i]);
}
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
您不需要卡片图像。你已经有了它们。从alt + 3
到alt + 6
(在数字键盘上)
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♥ ♦ ♣ ♠");