Чтение онлайн

ЖАНРЫ

Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 8: Программирование на Visual C# искусственного интеллекта. Издание 2. Продолжение 1
Шрифт:

/// Constructs a face up card with that number

/// </summary>

/// <param name="cardNo"></param>

public Card(byte cardNo)

: this(cardNo, true)

{

}

/// <summary>

/// String description of the card

/// </summary>

/// <returns>the name and suit of the card</returns>

public override string ToString

{

return ValueName + " of " + Suit;

}

}

/// <summary>

/// Provides a container for a number of cards.

/// May be used to draw the cards and compute their score.

/// </summary>

public class CardHand : ArrayList

{

/// <summary>

/// Used as a destination of teh draw action

/// </summary>

private static Rectangle drawRect;

/// <summary>

/// Draws the hand on the graphics.

/// </summary>

/// <param name="g">graphics to draw with</param>

/// <param name="startx">left edge of first card</param>

/// <param name="starty">top of first card</param>

/// <param name="gapx">x gap between each card</param>

/// <param name="gapy">y gap between each card</param>

public void DrawHand(Graphics g, int startx, int starty,

int gapx, int gapy)

{

drawRect.X = startx;

drawRect.Y = starty;

foreach (Card card in this)

{

drawRect.Width = card.CardImage.Width;

drawRect.Height = card.CardImage.Height;

g.DrawImage(

card.CardImage, // Image

drawRect, // destination rectange

0, // srcX

0, // srcY

card.CardImage.Width, // srcWidth

card.CardImage.Height, // srcHeight

GraphicsUnit.Pixel, // srcUnit

Card.cardAttributes); // ImageAttributes

drawRect.X += gapx;

drawRect.Y += gapy;

}

}

/// <summary>

/// Computes the score of the hand

/// </summary>

/// <returns>the value of the score</returns>

public int BlackJackScoreHand

{

int score = 0;

int aces = 0;

foreach (Card card in this)

{

score += card.BlackJackScore;

if (card.BlackJackScore == 11)

{

aces++;

}

}

while ((score > 21) && (aces > 0))

{

score -= 10;

aces–;

}

return score;

}

}

/// <summary>

/// Contains a number of card decks

/// which can be dealt one at a time.

/// </summary>

public class CardShoe

{

private int noOfDecks = 1;

private byte[] decks;

private int nextCard;

private bool testShoe = false;

/// <summary>

/// True if the deck is "stacked",

/// i.e. was created from a byte array

/// </summary>

public bool TestShoe

{

get

{

return testShoe;

}

}

private void makeShoe

{

decks = new byte[noOfDecks * 52];

int cardPos = 0;

for (int i = 0; i < noOfDecks; i++)

{

for (byte j = 1; j < 53; j++)

{

decks[cardPos] = j;

cardPos++;

}

}

nextCard = 0;

}

private void shuffleShoe

{

if (!testShoe)

{

System.Random rand = new Random;

byte swap;

int p1, p2;

for (int i = 0; i < decks.Length; i++)

{

p1 = rand.Next(decks.Length);

p2 = rand.Next(decks.Length);

swap = decks[p1];

decks[p1] = decks[p2];

decks[p2] = swap;

}

}

nextCard = 0;

}

/// <summary>

/// Gets the next card number from the deck

/// </summary>

/// <returns>The number of the next card</returns>

public byte NextCardNo

{

if (nextCard == decks.Length)

{

shuffleShoe;

}

return decks[nextCard++];

}

/// <summary>

/// Gets the next card from the deck.

/// </summary>

/// <returns>A new instance of the card</returns>

public Card DealCard

{

return new Card(NextCardNo);

}

/// <summary>

/// Constructs a shoe containing a number of decks

/// </summary>

/// <param name="noOfDecks"></param>

public CardShoe(int noOfDecks)

{

this.noOfDecks = noOfDecks;

makeShoe;

shuffleShoe;

testShoe = false;

}

/// <summary>

/// Constructs a shoe containing a single deck

/// </summary>

public CardShoe

: this(1)

{

}

/// <summary>

/// Creates a stacked deck for test purposes.

/// </summary>

/// <param name="stackedDeck">array of bytes</param>

public CardShoe(byte[] stackedDeck)

{

decks = stackedDeck;

testShoe = true;

}

}

}

В

панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item. В панели Add New Item выделяем шаблон Code File, в окне Name записываем имя нового файла с расширением *.cs и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем следующий код.

Поделиться с друзьями: