
Полная версия
Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 8: Программирование на Visual C# искусственного интеллекта. Издание 2. Продолжение 1
g.DrawImage(bankImage, 0, StayToolStripMenuItem.Height);
Utilities.BigText("Bank : " + pot.PotValue.ToString(),
10, 40, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Bet : " +
pot.BetValue.ToString(), 10, 80, Color.Black,
Color.Yellow, messageFont, g);
break;
case GameMode.PlayerActive:
dealerHand.DrawHand(g, 10, 30, 80, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
break;
case GameMode.PlayerWon:
case GameMode.PocketJack:
dealerHand.DrawHand(g, 10, 30, 20, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(dealerHand.BlackJackScoreHand().
ToString(), 140, 45, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Вы выиграли!",
20, 80, Color.Black, Color.Yellow, messageFont, g);
break;
case GameMode.PlayerBust:
dealerHand.DrawHand(g, 10, 30, 80, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Вы перебрали.",
20, 80, Color.Black, Color.Red, messageFont, g);
break;
case GameMode.DealerActive:
dealerHand.DrawHand(g, 10, 30, 20, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(dealerHand.BlackJackScoreHand().
ToString(), 140, 45, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
break;
case GameMode.DealerWon:
dealerHand.DrawHand(g, 10, 30, 20, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(dealerHand.BlackJackScoreHand().
ToString(), 140, 45, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Вы потеряли.",
20, 80, Color.Black, Color.Red, messageFont, g);
break;
case GameMode.DealerBust:
dealerHand.DrawHand(g, 10, 30, 20, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(dealerHand.BlackJackScoreHand().
ToString(), 140, 45, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Dealer Bust",
20, 80, Color.Black, Color.Yellow, messageFont, g);
break;
case GameMode.Push:
dealerHand.DrawHand(g, 10, 30, 20, 25);
playerHand.DrawHand(g, 10, 135, 20, 25);
Utilities.BigText(dealerHand.BlackJackScoreHand().
ToString(), 140, 45, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText(playerHand.BlackJackScoreHand().
ToString(), 140, 150, Color.Black,
Color.Yellow, messageFont, g);
Utilities.BigText("Вы выиграли.",
20, 80, Color.Black, Color.Yellow, messageFont, g);
break;
}
}
void playerHits()
{
if (playerHand.BlackJackScoreHand() < 21)
{
playerHand.Add(shoe.DealCard());
if (playerHand.BlackJackScoreHand() > 21)
{
//We write in the original:
pot.DoPlaceBet();
pot.HouseWins();
showPot();
mode = GameMode.PlayerBust;
}
this.Invalidate();
}
}
void playerStays()
{
dealerHoleCard.FaceUp = true;
mode = GameMode.DealerActive;
this.Refresh();
System.Threading.Thread.Sleep(750);
while (dealerHand.BlackJackScoreHand() < 17)
{
dealerHand.Add(shoe.DealCard());
this.Refresh();
System.Threading.Thread.Sleep(750);
}
if (dealerHand.BlackJackScoreHand() > 21)
{
mode = GameMode.DealerBust;
pot.PlayerWins();
showPot();
return;
}
if (playerHand.BlackJackScoreHand() >
dealerHand.BlackJackScoreHand())
{
mode = GameMode.PlayerWon;
pot.PlayerWins();
showPot();
return;
}
if (playerHand.BlackJackScoreHand() <
dealerHand.BlackJackScoreHand())
{
mode = GameMode.DealerWon;
//We write in the original:
pot.DoPlaceBet();
pot.HouseWins();
showPot();
return;
}
if (playerHand.BlackJackScoreHand() ==
dealerHand.BlackJackScoreHand())
{
mode = GameMode.Push;
pot.DoPushBet();
showPot();
return;
}
}
void doLeftMenuKey()
{
switch (mode)
{
case GameMode.LoadingDisplay:
break;
case GameMode.PlacingBets:
startPlay();
break;
case GameMode.PlayerActive:
playerStays();
break;
case GameMode.PocketJack:
case GameMode.PlayerWon:
case GameMode.PlayerBust:
case GameMode.DealerActive:
case GameMode.DealerWon:
case GameMode.DealerBust:
case GameMode.Push:
startHand();
break;
}
}
void doEnter()
{
switch (mode)
{
case GameMode.LoadingDisplay:
break;
case GameMode.PlacingBets:
startPlay();
break;
case GameMode.PlayerActive:
playerHits();
break;
case GameMode.PocketJack:
case GameMode.PlayerWon:
case GameMode.PlayerBust:
case GameMode.DealerActive:
case GameMode.DealerWon:
case GameMode.DealerBust:
case GameMode.Push:
startHand();
break;
}
}
void doUp()
{
switch (mode)
{
case GameMode.PlacingBets:
pot.DoIncreaseBet();
showPot();
this.Invalidate();
break;
}
}
void doDown()
{
switch (mode)
{
case GameMode.PlacingBets:
pot.DoDecreaseBet();
showPot();
this.Invalidate();
break;
}
}
void showHelp()
{
helpForm.ShowDialog();
}
В панели Properties (для Form1) на вкладке Events дважды щёлкаем по имени события Load. Появившийся шаблон метода Form1_Load после записи нашего кода принимает следующий вид.
Листинг 1.2. Метод для загрузки файлов объектов.
private void Form1_Load(object sender, EventArgs e)
{
//We load the game objects:
init();
startGame();
}
В панели Properties (для Form1) на вкладке Events дважды щёлкаем по имени события Paint. Появившийся шаблон метода Form1_Paint после записи нашего кода принимает следующий вид. Напомним, что другие варианты вывода изображения, например, на элемент управления PictureBox и после щелчка по какому-либо элементу управления уже приводились ранее.
Листинг 1.3. Метод для рисования изображений.
private void Form1_Paint(object sender, PaintEventArgs e)
{
paintForm(e.Graphics);
}
Для задания режимов и управления игрой воспользуемся каким-либо элементом управления или компонентом. Как и выше, с панели инструментов Toolbox переносим на форму компонент MenuStrip и щёлкаем по нему (ниже формы в режиме проектирования). На форме Form1 появляются окна с надписью Type Here, в которые записываем команды (по второму варианту, можно записывать в панели Properties в свойстве Text): Stay, Menu, Exit, HitMe, BetPlus, BetMinus, Sound, Help, рис. 1.30. Для команды, слева от которой поставлен флажок, в панели Properties для этой команды значение Checked следует задать как True.

Рис. 1.30. Команды элемента управления MenuStrip.
В режиме редактирования дважды щёлкаем по левой команде Stay (для приостановки игры). Появившийся шаблон метода после записи строки принимает следующий вид.
Листинг 1.4. Метод для приостановки игры.
private void StayToolStripMenuItem_Click(object sender, EventArgs e)
{
doLeftMenuKey();
}
Аналогично дважды щёлкаем по команде Exit. Появившийся шаблон метода после записи нашего кода принимает следующий вид.
Листинг 1.5. Метод для окончания игры.
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
Аналогично дважды щёлкаем по команде “Hit Me”. Появившийся шаблон метода после записи нашего кода принимает следующий вид.
Листинг 1.6. Метод для выдачи карты игроку.
private void HitMeToolStripMenuItem_Click(object sender, EventArgs e)
{
playerHits();
}
Аналогично дважды щёлкаем по команде “BetPlus”. Появившийся шаблон метода после записи нашего кода принимает следующий вид.
Листинг 1.7. Метод для увеличения Ставки.
private void BetPlusToolStripMenuItem_Click(object sender, EventArgs e)
{
doUp();
}
Аналогично дважды щёлкаем по команде “Ставка -” (Bet -). Появившийся шаблон метода после записи нашего кода принимает следующий вид.
Листинг 1.8. Метод для уменьшения Ставки.
private void BetMinusToolStripMenuItem1_Click(object sender, EventArgs e)
{
doDown();
}
Методика приостановки и возобновления звука при помощи установки и удаления флажка в команде Звук (Sound) была описана выше. В данной игре, звук включён постоянно.
Аналогично дважды щёлкаем по команде Help. Появившийся шаблон метода после записи нашего кода принимает следующий вид.
Листинг 1.9. Метод для вывода справочной формы.
private void HelpToolStripMenuItem_Click(object sender, EventArgs e)
{
showHelp();
}
Теперь программа должна управлять карточной игрой, используя любые клавиши, например, верхнюю (Up) и нижнюю (Down), левую (Left) и правую (Right) клавиши со стрелками, а также клавишу Enter (для начальной сдачи карт игроку и Банкомёту и последующей сдачи по одной карте игроку). В панели Properties (для формы Form1) на вкладке Events дважды щёлкаем по имени события KeyDown. Появившийся шаблон обработки нажатий всех клавиш после записи нашего кода для нажатий только трех клавиш (относящимся к тематике данной главы) принимает следующий вид.
Листинг 1.10. Метод для обработки нажатий клавиш.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
doUp();
e.Handled = true;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
doDown();
e.Handled = true;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
//Набираем себе карты:
doEnter();
}
}
Мы закончили написание программы в главный класс Form1 (для формы Form1 с пользовательским интерфейсом игры). В этом проекте движок игры (Engine Game) находится не в файле Form1.cs (как обычно бывает), а в следующем файле CardEngine.cs.
Теперь в наш проект добавляем новые файлы (для программирования соответствующих игровых действий) по следующей схеме.
В панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item. В панели Add New Item выделяем шаблон Code File, в окне Name записываем имя нового файла с расширением *.cs и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем следующий код.
Листинг 1.11. Новый файл CardEngine.cs.
using System;
using System.Collections;
using System.Drawing;
namespace PocketJack
{
///
/// Provides the behaviours required to manage and draw cards
///
public class Card
{
///
/// The number of the card, in the range 1 to 52
///
public byte CardNo;
///
/// Indicates if the card is to be drawn face up.
/// True by default.
///
public bool FaceUp = true;
///
/// The images of the cards. Stored for all the cards.
/// The image with number 0 is the
/// back pattern of the card
///
static private Image[] cardImages = new Bitmap[53];
///
/// The attribute to be used when drawing the card
/// to implement transpancy
///
static public System.Drawing.Imaging.ImageAttributes
cardAttributes;
///
/// Used when loading card images prior to drawing
///
static private System.Reflection.Assembly execAssem;
///
/// Sets up the color and attribute values.
///
static Card()
{
cardAttributes =
new System.Drawing.Imaging.ImageAttributes();
cardAttributes.SetColorKey(Color.Green, Color.Green);
execAssem =
System.Reflection.Assembly.GetExecutingAssembly();
}
///
/// Scores for each of the cards in a suit
///
static private byte[] scores =
new byte[] { 11, //ace
2,3,4,5,6,7,8,9,10, //spot cards
10,10,10}; //jack, queen, king
///
/// Picture information for each card in a suit
///
static private bool[] isPicture =
new bool[] { false, //ace
false,false,false,false,false,false,
false,false,false, //spot cards
true,true,true}; //jack, queen, king
///
/// Names of the suits, in the order that of the suits
/// in the number sequence
///
static private string[] suitNames =
new string[] { "club", "diamond", "heart", "spade" };
///
/// Names of individual cards, in the order of the cards
/// in a suit
///
static private string[] valueNames =
new string[] {"Ace", "Deuce","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten", "Jack","Queen","King" };
///
/// Returns the value in points of a given card,
/// according to BlackJack rules
///
public int BlackJackScore
{
get
{
return scores[(CardNo – 1) % 13];
}
}
///
/// Returns true if the card is a picture
/// (i.e. jack, queen or king)
///
public bool IsPicture
{
get
{
return isPicture[(CardNo – 1) % 13];
}
}
///
/// Returns text of the suit of this card
///
public string Suit
{
get
{
return suitNames[(CardNo – 1) / 13];
}
}
///
/// Returns the text of the value of this card
///
public string ValueName
{
get
{
return valueNames[(CardNo – 1) % 13];
}
}
///
/// Returns true if this is a red card
///
public bool Red
{
get
{
int suit = (CardNo – 1) / 13;
return ((suit == 1) || (suit == 2));
}
}
///
/// Returns true if this is a black card
///
public bool Black
{
get
{
return !Red;
}
}
///
/// Returns an image which can be used to draw this card
///
public Image CardImage
{
get
{
int dispNo = CardNo;
if (!FaceUp)
{
dispNo = 0;
}
if (cardImages[dispNo] == null)
{
cardImages[dispNo] = new Bitmap(
execAssem.GetManifestResourceStream(
@"PocketJack.images." + dispNo + @".gif"));
}
return cardImages[dispNo];
}
}
///
/// Constructs a card with a partiuclar number
///
/// number of the card
/// in the range 1 to 52
/// true if the card
/// is to be drawn face up
public Card(byte cardNo, bool faceUp)
{
CardNo = cardNo;
FaceUp = faceUp;
}
///
/// Constructs a face up card with that number
///
///
public Card(byte cardNo)
: this(cardNo, true)
{
}
///
/// String description of the card
///
///
public override string ToString()
{
return ValueName + " of " + Suit;
}
}
///
/// Provides a container for a number of cards.
/// May be used to draw the cards and compute their score.
///
public class CardHand : ArrayList
{
///
/// Used as a destination of teh draw action
///
private static Rectangle drawRect;
///
/// Draws the hand on the graphics.
///
/// graphics to draw with
/// left edge of first card
/// top of first card
/// x gap between each card
/// y gap between each card
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;
}
}
///
/// Computes the score of the hand
///
///
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;
}
}
///
/// Contains a number of card decks
/// which can be dealt one at a time.
///
public class CardShoe
{
private int noOfDecks = 1;
private byte[] decks;
private int nextCard;
private bool testShoe = false;
///
/// True if the deck is "stacked",
/// i.e. was created from a byte array
///
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;
}
///
/// Gets the next card number from the deck
///
///
public byte NextCardNo()
{
if (nextCard == decks.Length)
{
shuffleShoe();
}
return decks[nextCard++];
}
///
/// Gets the next card from the deck.
///
///
public Card DealCard()
{
return new Card(NextCardNo());
}
///
/// Constructs a shoe containing a number of decks
///
///
public CardShoe(int noOfDecks)
{
this.noOfDecks = noOfDecks;
makeShoe();
shuffleShoe();
testShoe = false;
}
///
/// Constructs a shoe containing a single deck
///
public CardShoe()
: this(1)
{
}
///
/// Creates a stacked deck for test purposes.
///
/// array of bytes
public CardShoe(byte[] stackedDeck)
{
decks = stackedDeck;
testShoe = true;
}
}
}
В панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item. В панели Add New Item выделяем шаблон Code File, в окне Name записываем имя нового файла с расширением *.cs и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем следующий код.
Листинг 1.12. Новый файл Pot.cs .
using System;
namespace PocketJack
{
///
/// Summary description for Betting.
///
public class Pot
{
private int betValueChangeValue;
private int betValue;
private int potValue;
private const int INITIAL_POT_VALUE = 500;
private const int INITIAL_BET_CHANGE_VALUE = 5;
public int BetValue
{
get
{
return betValue;
}
}
public int PotValue
{
get
{
return potValue;
}
}
public void ResetPot()
{
betValueChangeValue = INITIAL_BET_CHANGE_VALUE;
betValue = INITIAL_BET_CHANGE_VALUE;
potValue = INITIAL_POT_VALUE;
}
public void CheckPot()
{
if (betValue > potValue)
{
if (System.Windows.Forms.MessageBox.Show(
"Insufficient funds for the bet." +
"Do you want to reload the pot?",
"Bank",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
System.Windows.Forms.
MessageBoxDefaultButton.Button1) ==
System.Windows.Forms.DialogResult.Yes)
{
ResetPot();
}
else
{
betValue = potValue;
}
}
}
public void DoIncreaseBet()
{
betValue = betValue + betValueChangeValue;
CheckPot();
}
public void DoDecreaseBet()
{
if (betValue >= betValueChangeValue)
{
betValue = betValue – betValueChangeValue;
}
}
public void PlayerWins()
{
// win back 2 * our stake
potValue = potValue + betValue;
//potValue = potValue + betValue; //We commented out.
}
public void HouseWins()
{
CheckPot();
}
public void DoPushBet()
{
// put the betValue back in the potValue
potValue = potValue + betValue;
}
public void DoPlaceBet()
{
potValue = potValue – betValue;
}
public Pot()
{
ResetPot();
}
}
}
После этого добавления в панели Solution Explorer должны быть файлы, показанные на рисунке выше. Дважды щёлкая по имени файла, любой файл можно открыть, изучить и редактировать.
В этих файлах использованы XML-комментарии (XML comment), где XML – Extensible Markup Language – расширяемый язык разметки, типа:
///
/// Description of a variable:
///
который состоит из начального тэга (start tag):
///
и конечного тэга (end tag):
///
между которыми записывается сам комментарий:
/// Описание переменной:
/// Description of a variable:
В отличие от обычных комментариев после двойного слеша // или между двух символов /*…*/, в любом месте программы при наведении указателя мыши на переменную с XML-комментарием появляется подсказка, в которой имеется не только тип и класс переменной (как для переменной с обычным комментарием или вообще без комментария), но также имеется и её описание на любом языке, в том числе на русском языке, которое мы записали между начальным и конечным тэгами. XML-комментарии имеют и другие преимущества, описанные в специальной литературе.