Опубликован: 17.08.2010 | Доступ: свободный | Студентов: 999 / 59 | Оценка: 4.11 / 3.89 | Длительность: 29:38:00
Самостоятельная работа 11:

Игра в кубик

Файл объявления классов участников игры Game.h

Файл объявления классов участников игры Game.h
//  Файл Game.h
//---------------------------------------------------------------------------

#ifndef GameH
#define GameH
#include <stdlib.h>
//---------------------------------------------------------------------------

#define MY_SHOW WM_USER //Макроопределение сообщения
const int GAME_FINISH_SCORES = 120;// Глобальная константа
enum TActiveMember{PLAYER, PARTNER};
//---------------------------------------------------------------------------
class TDice // класс Кубик
{
   int Value;
 public:
   TDice(); // инициализировать при создании
   int GetDiceValue(){return Value;}// дать значение
   void Action(); // бросить
};
//---------------------------------------------------------------------------

class TPlayer // класс Игрок
{
 private:
   int CurrentScores;   // текущие очки
   int SumScores;       // суммарные очки
   int VictoryCount;    // число выигранных партий
 public:
   TPlayer();
   void Init(){CurrentScores = SumScores = 0;}
   int GetCurrentScores(){return CurrentScores;}
   int GetSumScores(){return SumScores;}
   void AddVictoryCount(){VictoryCount++;}
   int GetVictoryCount(){return VictoryCount;}
   void AddCurrentScores(int Scores)
                   {CurrentScores += Scores;}
   void AddSumScores()
                   {SumScores += CurrentScores;
                    CurrentScores = 0;}
};
//---------------------------------------------------------------------------


class TReferee // класс Судья
{
 private:
   TActiveMember activeMember;
   int PlayCount;
 public:
   TReferee(){PlayCount = 0;}
   TPlayer Player, Partner; //создание игроков
   TDice Dice;              //кубик
   void NewGame();          //новая игра
   void Move();             //передача хода
   bool GameFinish();       //проверка на конец партии
   int GetActiveMember(){return activeMember;}
   int GetPlayCount(){return PlayCount;}
};
//---------------------------------------------------------------------------
#endif

Файл реализации методов Game.cpp

Файл реализации методов Game.cpp
//  Файл Game.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Game.h" // Подключение объявлений

//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------------------------------------

TDice::TDice() // конструктор по умолчанию
{
 randomize();  // инициализация random()
 Value = 0;
}
//---------------------------------------------------------------------------
void TDice::Action()
{
 Value = random(6) + 1;
}
//---------------------------------------------------------------------------

TPlayer::TPlayer()
{
 VictoryCount = 0;
 Init();
}

//---------------------------------------------------------------------------

//новая игра
void TReferee::NewGame()
{
 PlayCount++; 
 Player.Init();
 Partner.Init();
 Dice.Action(); // Бросить кубик
 if(Dice.GetDiceValue() <= 3) // первым ходит игрок
   activeMember = PLAYER;
 else
   activeMember = PARTNER;
 SendMessage(FindWindow("TMainForm","Игра в кубик"),
             MY_SHOW, 0, 0);
}
//---------------------------------------------------------------------------

// Передача хода
void TReferee::Move()
{
 if(GetActiveMember() == PLAYER){
   Player.AddSumScores();
   activeMember = PARTNER;
 }
 else{
   Partner.AddSumScores();
   activeMember = PLAYER;
 }
 SendMessage(FindWindow("TMainForm","Игра в кубик"),
             MY_SHOW, 0, 0); 
}
//---------------------------------------------------------------------------


bool TReferee::GameFinish()  //проверка на конец партии
{
 int PlayerBalance = Player.GetSumScores();
 PlayerBalance += Player.GetCurrentScores();
 int PartnerBalance = Partner.GetSumScores();
 PartnerBalance += Partner.GetCurrentScores();
 return PlayerBalance >= GAME_FINISH_SCORES
     || PartnerBalance >= GAME_FINISH_SCORES;
}
//---------------------------------------------------------------------------

Заголовочный файл UAbout.h

Заголовочный файл UAbout.h
//---------------------------------------------------------------------------

#ifndef UAboutH
#define UAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TAboutBox : public TForm
{
__published:  // IDE-managed Components
        TImage *Image;
        TLabel *Label1;
        TLabel *Label2;
        TLabel *Label3;
        TLabel *Label4;
        TLabel *Label5;
private:  // User declarations
public:    // User declarations
        __fastcall TAboutBox(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TAboutBox *AboutBox;
//---------------------------------------------------------------------------
#endif

Файл реализации UAbout.cpp

Файл реализации UAbout.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TAboutBox *AboutBox;
//---------------------------------------------------------------------------
__fastcall TAboutBox::TAboutBox(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

Главное окно


Окно About


Александр Даниленко
Александр Даниленко
Стоит Windows 8 Pro, Visual Studio 2010 Express Edition .