tema: Karte u c#
Prikaz jedne poruke
Stara 28.5.2010, 20:59   #3
Ivan-94
Veteran
 
Član od: 15.3.2009.
Lokacija: Beograd
Poruke: 654
Zahvalnice: 240
Zahvaljeno 63 puta na 43 poruka
Slanje poruke preko MSN-a korisniku Ivan-94 Slanje poruke preko Skypea korisniku Ivan-94
Određen forumom Re: Karte u c#

Posto citam knjigu Beginning Microsoft Visual C# 2008 skoro sam dosao do dela OOP.Tu imas sjajan primer bas za Card Library.

Za karte(pik,tref...):
Kod:
using System;
using System.Collections.Generic;
using System.Text;
namespace CardLibrary
{
public enum Suit
{
Club,
Diamond,
Heart,
Spade,
}
}
Za njihove vrednosti:
Kod:
using System;
using System.Collections.Generic;
using System.Text;
namespace CardLibrary
{
public enum Rank
{
Ace = 1,
Deuce,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
}
}
Svaki od njih u posebnoj klasi,znaci Suit.cs i Rank.cs.

Klasa za karte:
Kod:
public class Card
{
public readonly Suit suit;
public readonly Rank rank;
public Card(Suit newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
}
private Card()
{
}
public override string ToString()
{
return "The " + rank + " of " + suit + "s";//Kad pozivas .ToString() ovo se prikazuje a ne nesto sto ne razumes.
}
}
Za spil karata:

Kod:
using System;
using System.Collections.Generic;
using System.Text;
namespace CardLibrary
{
public class Deck
{
private Card[] cards;
public Deck()
{
cards = new Card[52];
for (int suitVal = 0; suitVal < 4; suitVal++)
{
for (int rankVal = 1; rankVal < 14; rankVal++)
{
cards[suitVal * 13 + rankVal -1] = new Card((Suit)suitVal,
(Rank)rankVal);
}
}
}
public Card GetCard(int cardNum)
{
if (cardNum > = 0 & & cardNum < = 51)
return cards[cardNum];
else
throw (new System.ArgumentOutOfRangeException("cardNum", cardNum,
"Value must be between 0 and 51."));
}
public void Shuffle()//Mesa karte
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random sourceGen = new Random();
for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;
while (foundCard == false)
{
destCard = sourceGen.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);//Kad bi koristio newDeck=cards doslo bi do greske
}
}
}
E sad ti to prilagodi tebi ovo je bukvalno copy/paste iz knjige sa par mojih komentara.Ako ti nesto nije jasno pitaj.
Ivan-94 je offline   Odgovor sa citatom ove poruke