Программирование

Автор: Пользователь скрыл имя, 08 Апреля 2012 в 11:29, курсовая работа

Описание работы

Данная программа предназначена для шифрования файлов наложением кода ключа по методу XOR(исключающего или).

Содержание

1.Программа шифрования текста методом наложения кода ключа…..стр.3

2.Графическое приложение: игра PacMan……………………………..стр.13

3.Клиент базы данных Microsoft Access……………………………….стр.26

Работа содержит 1 файл

Курсовой.doc

— 566.50 Кб (Скачать)

                                             Active = true;

                                             ActiveTime = 10;

                                             Maze[X/CellSize][Y/CellSize].map = mpNone;

                                             break;

                        case mpPacmanStart : if (Bonus.Life != 0)

                                             {

                                                                                                                                                                        MediaPlayer[sEatBonus]->Play();

                                                Active = true;

                                                ActiveTime = 10;

                                                Bonus.Kind = 0;

                                                                                                                                                                        Bonus.Life = 0;

                                                                                                                                                                        p_Life++;

                                             }

                                             break;

                    }

                    LevelRenewSeed();

                    VerifyDir();

                }

 

                if (DirFlag[Dir] == true)

                    switch (Dir) {

                        case 0: X--; break;

                        case 1: Y--; break;

                        case 2: X++; break;

                        case 3: Y++; break;

                    }

            }

    } Pacman;

 

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

 

    class TGhost

    {

        public:

            int X;

            int Y;

            int StartX;

            int StartY;

            int Dir;

            int SpriteNum;

            bool Shy;

            int ShyTime;

            int CurrN;

            bool DirFlag[3];

 

            void NextStep()

            {

                if ((Maze[X/CellSize - 1][Y/CellSize].mark == CurrN - 1) && (Maze[X/CellSize][Y/CellSize].left_wall == '0'))   Dir = 0;

                if ((Maze[X/CellSize][Y/CellSize-1].mark == CurrN - 1)   && (Maze[X/CellSize][Y/CellSize].up_wall == '0'))     Dir = 1;

                if ((Maze[X/CellSize+1][Y/CellSize].mark == CurrN - 1)   && (Maze[X/CellSize+1][Y/CellSize].left_wall == '0')) Dir = 2;

                if ((Maze[X/CellSize][Y/CellSize+1].mark == CurrN - 1)   && (Maze[X/CellSize][Y/CellSize+1].up_wall == '0'))   Dir = 3;

            }

 

            void VerifyDir()

            {

                if (Maze[X/CellSize][Y/CellSize].left_wall == '1')     DirFlag[0] = false;

                    else DirFlag[0] = true;

                if (Maze[X/CellSize][Y/CellSize].up_wall == '1')       DirFlag[1] = false;

                    else DirFlag[1] = true;

                if (Maze[X/CellSize + 1][Y/CellSize].left_wall == '1') DirFlag[2] = false;

                    else DirFlag[2] = true;

                if (Maze[X/CellSize][Y/CellSize + 1].up_wall == '1')   DirFlag[3] = false;

                    else DirFlag[3] = true;

            }

 

            void Go(int a)

            {

                if ((X % CellSize == 0) && (Y % CellSize == 0))

                {

                    VerifyDir();

                    if (random(a*2) == 0) {CurrN = Maze[X/CellSize][Y/CellSize].mark; NextStep();}

                    else CurrN++;

                }

                if ((Shy == false) && (DirFlag[Dir] == true))

                    switch (Dir) {

                        case 0: X--; break;

                        case 1: Y--; break;

                        case 2: X++; break;

                        case 3: Y++; break;

                    }

            }

    } Ghost[3];

 

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

 

__fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

}

 

//---------------------ФУНКЦИИ-ФОРМЫ-----------------------------------------

 

void __fastcall TForm1::FormCreate(TObject *Sender)

{

    Form1->ClientWidth = ::Width * CellSize + 1;

    Form1->ClientHeight = ::Height * CellSize + Form1->Label1->Height + 1;

 

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

    {

        MediaPlayer[i] = new TMediaPlayer(this);

        MediaPlayer[i]->Parent = this;

        MediaPlayer[i]->Visible = false;

        MediaPlayer[i]->FileName ="sound" + IntToStr(i) + ".wav";

        MediaPlayer[i]->Open();

    }

 

    ImageBack   = new Graphics::TBitmap();

    ImageBack->Width  = ::Width * CellSize + 1;

    ImageBack->Height = ::Height * CellSize + 1;

 

    ImageBuffer = new Graphics::TBitmap();

    ImageBuffer->Width  = ImageBack->Width;

    ImageBuffer->Height = ImageBack->Height;

 

    Randomize;

    Form1->DoubleBuffered = true;

    TimersRun(false);

}

 

//---------------------МЕНЮ-ФОРМЫ--------------------------------------------

 

void __fastcall TForm1::MenuNewClick(TObject *Sender)

{

    LevelCreate();

}

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

 

void __fastcall TForm1::MenuExitClick(TObject *Sender)

{

    Form1->Close();

}

//---------------------ОБРАБОТКА-КЛАВИШ----------------------------------

 

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,

      TShiftState Shift)

{

    if ((Key >= 37) && (Key <= 40)) Pacman.NewDir = Key - 37;

}

 

//---------------------ТАЙМЕРЫ-----------------------------------------------

 

void __fastcall TForm1::TimerMoveTimer(TObject *Sender)

{

    ImageBuffer->Canvas->Draw(0,0,ImageBack); // выводим все  в буфер

 

    Bonus.Show();

    Pacman.Go();

 

    for (int a = 0; a < 4; a++)

    {

        Ghost[a].Go(a);

        if (Ghost[a].Shy == false)

            Form1->Sprites->Draw(ImageBuffer->Canvas, Ghost[a].X, Ghost[a].Y, 12 * (a + 1) + Ghost[a].Dir * 3 + Ghost[a].SpriteNum);

                            else

            Form1->Sprites->Draw(ImageBuffer->Canvas, Ghost[a].X, Ghost[a].Y, 12 * (random(3) + 1) + Ghost[a].Dir * 3 + Ghost[a].SpriteNum);

 

        if (((abs(Pacman.Y - Ghost[a].Y) < 10) && (abs(Pacman.X - Ghost[a].X) < 10))

            && (((Pacman.X % CellSize == 0) && (Ghost[a].X % CellSize == 0))

                || ((Pacman.Y % CellSize == 0) && (Ghost[a].Y % CellSize == 0))))

        {

            if (Pacman.Active == true)

            {

                MediaPlayer[sEatGhost]->Play();

                Ghost[a].Shy = true;

                Ghost[a].ShyTime = 15;

            }

            else

            {

                MediaPlayer[sDie]->Play();

                                                        Form1->TimersRun(false);

                                                        p_Life--;

                MessageDlg("  ТЕБЯ СЪЕЛИ !!!  ", mtInformation, TMsgDlgButtons() << mbOK, 0);

                CheckStartPlace();

                Form1->TimersRun(true);

            }

        }

    }

 

              Label1->Caption = "Всего очков = " + IntToStr(LevelSeed + LevelEnergizer * 5) + ", Очков = " + IntToStr(Score) + ", Бонус = " + IntToStr(Pacman.ActiveTime)+ ", Жизней = " + IntToStr(p_Life);

 

    // если съедены все зерна в уровне, то он пройден - грузим следующий

    if (Score == LevelSeed + LevelEnergizer * 5)

    {

        MediaPlayer[sWin]->Play();

                            TimersRun(false);

        MessageDlg("  Вы победили !!!  ", mtInformation, TMsgDlgButtons() << mbOK, 0);

                            LevelCreate();

    }

 

              // если кончились жизни

              if (p_Life==0)

              {

                            MediaPlayer[sWin]->Play();

                            TimersRun(false);

                            MessageDlg("  Вы проиграли !!!  ", mtInformation, TMsgDlgButtons() << mbOK, 0);

                            LevelCreate();

              }

Form1->Canvas->Draw(0,0,ImageBuffer);     // выводим все на экран

}

 

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

 

void __fastcall TForm1::TimerActiveTimer(TObject *Sender)

{

    if (Pacman.ActiveTime > 0)

        Pacman.ActiveTime--;

    if (Pacman.ActiveTime == 0)

        Pacman.Active = false;

 

    for (int a = 0; a < 4; a++)

    {

        if (Ghost[a].ShyTime > 0)

            Ghost[a].ShyTime--;

        if (Ghost[a].ShyTime == 0)

            Ghost[a].Shy = false;

    }

}

 

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

 

void __fastcall TForm1::TimerSpriteTimer(TObject *Sender)

{

    if (Pacman.SpriteNum < 2) Pacman.SpriteNum++;

    else

    {

        Pacman.SpriteNum = 0;

        MediaPlayer[sMove]->Play();

    }

 

    for (int a = 0; a < 4; a++)

        if (Ghost[a].SpriteNum < 2) Ghost[a].SpriteNum++;

        else Ghost[a].SpriteNum = 0;

}

 

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

 

void TForm1::TimersRun(bool active)

{

    TimerMove->Enabled   = active;

    TimerSprite->Enabled = active;

    TimerActive->Enabled = active;

}

 

//---------------------ОБЩИЕ-ФУНКЦИИ-----------------------------------------

//-------------генерация лабиринта по алгоритму Прима------------------------

 

void BreakWall(int x, int y, int dx, int dy)

//вспомогательная функция. разбивает стену

{

    if (dx == -1) Maze[x][y].left_wall     = '0';

    if (dx ==  1) Maze[x + 1][y].left_wall = '0';

    if (dy == -1) Maze[x][y].up_wall       = '0';

    if (dy ==  1) Maze[x][y + 1].up_wall   = '0';

}

 

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

 

void PrimGenerateMaze()

{

    Form1->Label1->Caption="Идет генерация лабиринта по алгоритму Прима";

 

    const int Inside  = 1;

              const int Border  = 2;

    const int Outside = 0;

 

    int x, y;

    int xc, yc;

    int xloc, yloc;

    bool IsEnd;

    int counter;

 

    //вначале есть все стены и все локации внешние

    for (x = 0; x < Width; x++)

        for (y = 0; y < Height; y++)

        {

            Maze[x][y].attr = Outside;

            Maze[x][y].left_wall = '1';

            Maze[x][y].up_wall = '1';

        }

 

    //нижняя и правая границы

    for (byte i = 0; i < Height; i++)

    {

        Maze[i][Height].up_wall = '1';

        Maze[Height][i].left_wall = '1';

    }

 

    //выбираем локацию, с которой начнется генерация

    x = random(Width);

    y = random(Height);

    Maze[x][y].attr = Inside;

 

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

    {

        xc = x + dx[i];

        yc = y + dy[i];

        if ((xc >= 0) && (yc >= 0) && (xc < Width) && (yc < Height))

            Maze[xc][yc].attr = Border;

    }

 

    do

    {

        IsEnd = true;

        counter = 0;

 

        //выбираем случайную локацию с атрибутом Border

        for (x = 0; x < Width; x++)

            for (y = 0; y < Height; y++)

                if (Maze[x][y].attr == Border)

                    counter++;

        counter = random(counter) + 1;

        for (x = 0; x < Width; x++)

            for (y = 0; y < Height; y++)

                if (Maze[x][y].attr == Border)

                {

                    counter--;

                    if (counter == 0)

                    {

                        xloc = x;

                        yloc = y;

                        goto ExitFor1;

                    }

        }

 

        ExitFor1:

            //присваиваем ей атрибут Inside

            Maze[xloc][yloc].attr = Inside;

            counter = 0;

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

            {

                xc = xloc + dx[i];

                yc = yloc + dy[i];

                if ((xc >= 0) && (yc >= 0) && (xc < Width) && (yc < Height))

                {

                    if (Maze[xc][yc].attr == Inside) counter++;

                    if (Maze[xc][yc].attr == Outside) Maze[xc][yc].attr = Border;

                }

            }

 

            counter = random(counter) + 1;

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

            {

                xc = xloc + dx[i];

                yc = yloc + dy[i];

                if ((xc >= 0) && (yc >= 0) && (xc <= Width) && (yc <= Height) && (Maze[xc][yc].attr == Inside))

                {

                    counter--;

                    if (counter == 0)

                    BreakWall(xloc, yloc, dx[i], dy[i]);

                }

            }

 

            for (x = 0; x < Width; x++)

                for (y = 0; y < Height; y++)

                    if (Maze[x][y].attr == Border)

                    {

                        IsEnd = false;

                        goto ExitFor2;

                    }

 

            ExitFor2:

                ShowMaze();

                Sleep(50);

                Application->ProcessMessages();

    }

    while (!IsEnd);

}

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

 

void ShowMaze()

//рисуем в ImageBack лабиринт и отображаем на канвасе формы

{

    ImageBack->Canvas->Brush->Color = clBlack;

    ImageBack->Canvas->Pen->Color = clYellow;

    ImageBack->Canvas->FillRect(Rect(0,0,Width * CellSize + 1, Height * CellSize + 1));

 

    for (byte i = 0; i <= Height; i++)

        for (byte j = 0; j <= Width; j++)

        {

            if (Maze[i][j].left_wall == '1')

            //если у данной локации есть левая стена, то рисуем ее

            {

                ImageBack->Canvas->MoveTo(i * CellSize, j * CellSize);

                ImageBack->Canvas->LineTo(i * CellSize, (j + 1) * CellSize);

            }

 

            if (Maze[i][j].up_wall == '1')

            //соответственно если есть верхняя стена, то и ее рисуем

            {

                ImageBack->Canvas->MoveTo(i * CellSize, j * CellSize);

                ImageBack->Canvas->LineTo((i+1) * CellSize, j * CellSize);

            }

 

            if (Maze[i][j].attr == 2)

            //рисуем кружки в Border-локациях при генерации лабирнта

            {

                ImageBack->Canvas->Brush->Color = clMaroon;

                ImageBack->Canvas->Ellipse((i + 0.5) * CellSize - 5, (j + 0.5) * CellSize - 5,(i + 0.5) * CellSize + 5, (j + 0.5) * CellSize + 5);

            }

        }

    Form1->Canvas->Draw(0,0,ImageBack);

}

 

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

 

void CreateMap()

// генерируем карту: заполняем все пространство Seed

// в углах ставим бонусные Energizer

// в центре mpPacmanStart

// + считаем количество Seed и Energizer

{

    LevelSeed    = 0;

    LevelEnergizer = 0;

    Score        = 0;

 

    Maze[0][0].map              = mpEnergizer;

    Maze[0][Width-1].map        = mpEnergizer;

    Maze[Height-1][0].map       = mpEnergizer;

    Maze[Height-1][Width-1].map = mpEnergizer;

    LevelEnergizer = 4;

 

    Maze[Height/2][Width/2].map = mpPacmanStart;

 

    for (byte i = 0; i < Height; i++)

        for (byte j = 0; j < Width; j++)

            if (Maze[i][j].map == mpNone)

            {

                Maze[i][j].map = mpSeed;

                LevelSeed++;

            }

}

 

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

 

void ShowMap()

// рисуем в ImageBack(там уже сгенерирован лабиринт) карту

// инициализируем Pacman.StartX и StartY

// вызываем CheckStartPlace()

{

    for (byte i = 0; i < Height; i++)

        for (byte j = 0; j < Width; j++)

            switch (Maze[i][j].map) {

                case mpSeed        : Form1->Sprites->Draw(ImageBack->Canvas, i * CellSize, j * CellSize, 60);

                                     break;

                case mpEnergizer     : Form1->Sprites->Draw(ImageBack->Canvas, i * CellSize, j * CellSize, 61);

                                     break;

                case mpPacmanStart : Pacman.StartX = i * CellSize;

Информация о работе Программирование