在 C# 控制台应用程序中,如何为二十一点游戏中的纸牌创建 'images'?
In a C# console app, how would I create 'images' for playing cards in a blackjack game?
我正在为我的学校开发一个简单的 C# 二十一点控制台应用程序游戏,我得到了一个示例来查看。这个例子以某种方式在控制台 window 中绘制了一张卡片的图片,如果不为 52 张独特的卡片中的每一张指定数百张 Console.Write,我无法弄清楚如何复制它。
in game scene
这就是您实际玩游戏时的样子。挺好的
shuffle and show deck
主菜单中还有一个选项可以洗牌并显示所有 52 张牌。
那么这个魔法是什么?他们真的花了很多时间硬编码如何打印出每张独特的卡片吗?我当然希望不会。这就是我要复制的东西,除了硬编码之外,我对其他想法一无所知。感谢您的帮助。
卡片不需要图片。你已经有了它们。按从 alt + 3
到 alt + 6
(在数字键盘上)
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♥ ♦ ♣ ♠");
多亏了 Damien_The_Unbeliever 的评论,我才能够在我的卡片中想出这两种方法 class。还要感谢 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;
}
vik_78's answer
Damien_The_Unbeliever's comment
我正在为我的学校开发一个简单的 C# 二十一点控制台应用程序游戏,我得到了一个示例来查看。这个例子以某种方式在控制台 window 中绘制了一张卡片的图片,如果不为 52 张独特的卡片中的每一张指定数百张 Console.Write,我无法弄清楚如何复制它。
in game scene 这就是您实际玩游戏时的样子。挺好的
shuffle and show deck 主菜单中还有一个选项可以洗牌并显示所有 52 张牌。
那么这个魔法是什么?他们真的花了很多时间硬编码如何打印出每张独特的卡片吗?我当然希望不会。这就是我要复制的东西,除了硬编码之外,我对其他想法一无所知。感谢您的帮助。
卡片不需要图片。你已经有了它们。按从 alt + 3
到 alt + 6
(在数字键盘上)
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♥ ♦ ♣ ♠");
多亏了 Damien_The_Unbeliever 的评论,我才能够在我的卡片中想出这两种方法 class。还要感谢 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;
}
vik_78's answer
Damien_The_Unbeliever's comment