0% found this document useful (0 votes)
157 views

Playing Cards C#

The document defines classes and methods for creating and managing a deck of cards and playing a card game. It includes classes for cards, figures, suits, and games. Methods are defined for creating a deck, shuffling cards, drawing cards, and printing card information. The Game class initializes a new game by creating a deck, shuffling it, dealing starting hands to players, and determining the first player.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Playing Cards C#

The document defines classes and methods for creating and managing a deck of cards and playing a card game. It includes classes for cards, figures, suits, and games. Methods are defined for creating a deck, shuffling cards, drawing cards, and printing card information. The Game class initializes a new game by creating a deck, shuffling it, dealing starting hands to players, and determining the first player.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

namespace Cards
{
class Program
{
static void Main()
{
Game NewGame = new Game();

System.Threading.Thread.Sleep(20000);
}
}
public enum Seme { Cuori = 0, Denari = 1, Picche = 2, Fiori = 4 }
public enum Figura { Default = 0, Asso = 1, Re = 10, Donna = 9, Fante = 8 }
public class Card
{
public Card() { ItsValue = 0; }
public Card(int itsValue, Seme itsKind)
{
ItsKind = itsKind; ItsValue = itsValue; Dressed = Figura.Default;
switch (ItsValue)
{
case 1:
Dressed = Figura.Asso; break;
case 8:
Dressed = Figura.Fante; break;
case 9:
Dressed = Figura.Donna; break;
case 10:
Dressed = Figura.Re; break;
default:
break;
}
}
public Seme ItsKind;
public int ItsValue;
public Figura Dressed;
}
public static class Action
{
public static Card[] CreatePack()
{
Card[] pack = new Card[40];
int j;
for (int i = 0; i < 10; i++)
{
j = i; j++;
pack[i] = new Card(j, Seme.Cuori);
pack[i + 10] = new Card(j, Seme.Denari);
pack[i + 20] = new Card(j, Seme.Fiori);
pack[i + 30] = new Card(j, Seme.Picche);
}
return pack;
}
public static void PickCard( ref Card[] pack, Card card)
{
Card[] temp = new Card[pack.Length - 1];
int j = 0;
for (int i = 0; i < pack.Length; i++)
if (pack[i] != card)
{
temp[j] = pack[i];
j++;
}
pack = temp;
}
public static Card PickCard( ref Card[] pack, int pos)
{
Card[] temp = new Card[pack.Length - 1];
int j = 0;
Card card = pack[pos];
for (int i = 0; i < pack.Length; i++)
if (pack[i] != card)
{
temp[j] = pack[i];
j++;
}
pack = temp;
return card;
}
public static Card DrawCard(ref Card[] pack)
{
Card[] temp = new Card[pack.Length - 1];
Card card = pack[0];
for (int i = 1; i < pack.Length; i++)
temp[i - 1] = pack[i];
pack = temp;
return card;
}
public static void PrintCard(Card card)
{
if (card.Dressed == Figura.Default)
{
Console.Write(card.ItsValue + " di " + card.ItsKind);
Console.WriteLine();
}
else if (card.Dressed != Figura.Default)
{
Console.Write(card.Dressed + " di " + card.ItsKind);
Console.WriteLine();
}
}
public static void PrintPack(Card[] pack)
{
Console.WriteLine("*** n " + pack.Length + " carte ***");
for (int i = 0; i < pack.Length; i++)
{
if(pack[i].Dressed==Figura.Default)
{
Console.Write(pack[i].ItsValue + " di " + pack[i].ItsKind);
Console.WriteLine();
}
else if (pack[i].Dressed != Figura.Default)
{
Console.Write(pack[i].Dressed + " di " + pack[i].ItsKind);
Console.WriteLine();
}
}
}
public static void ShuffleCards(ref Card[] pack)
{
Card[] temp = new Card[pack.Length];
for (int i = 0; i < temp.Length; i++)
temp[i] = new Card();
Random rnd = new Random();
for (int i = 0; i < temp.Length; )
{
int randomNumber = rnd.Next(0, pack.Length);
if (temp[randomNumber].ItsValue == 0)
{
temp[randomNumber] = pack[i];
i++;
}
}
pack = temp;
}
public static bool TossCoin()
{
Random rnd = new Random();
int test = rnd.Next(0, 1);
bool result = false;
switch (test)
{
case 0:
result = true; break;
case 1:
break;
}
return result;
}
}
public class Game
{
public Game()
{
Console.WriteLine("Giocatore 1: inserisci il tuo nome.");
Player1 = Console.ReadLine();
Console.WriteLine("\nGiocatore 2: inserisci il tuo nome");
Player2 = Console.ReadLine();
Console.WriteLine("\nBenvenuti " + Player1 + " e " + Player2 + "!\n\
nInizializzo il gioco...");
Console.WriteLine("\nCreo il mazzo...");
Pack = Action.CreatePack();
Console.WriteLine("\nMischio il mazzo...");
Action.ShuffleCards(ref Pack);
Console.WriteLine("\nCarte in tavola:");
Stack = new Card[4];
for (int i = 0; i < 4; i++)
Stack[i] = Action.DrawCard(ref Pack);
Action.PrintPack(Stack);
Console.WriteLine("\nMano giocatore 1:");
PlayerHand1 = new Card[3];
for (int i = 0; i < 3; i++)
PlayerHand1[i] = Action.DrawCard(ref Pack);
Action.PrintPack(PlayerHand1);
Console.WriteLine("\nCarte giocatore 2:");
PlayerHand2 = new Card[3];
for (int i = 0; i < 3; i++)
PlayerHand2[i] = Action.DrawCard(ref Pack);
Action.PrintPack(PlayerHand2);
}
public bool Play()
{
int test;
string str;
Console.WriteLine("Chi inzia per primo?\n<1 - Decidete voi> <2 - Dec
ide il software>");
test = (int)Console.ReadLine();
}
public string Player1;
public string Player2;
public Card[] Pack;
public Card[] PlayerHand1;
public Card[] PlayerHand2;
public Card[] Stack;
public Card[] StackPlayer1;
public Card[] StackPlayer2;
}
}

You might also like