А - Разработка программы на языке C++, реализующей игру "Морской бой"

ТЕКСТ ПРОГРАММЫ

Текст программы состоит из следующих модулей: UShipBattle. h, UShipBattle. cpp, ShipBattle. cpp

ShipBattle. cpp

// -

#include <vcl. h>

#pragma hdrstop

// -

USEFORM ("UShipBattle. cpp", Form1);

// -

WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)

{

Try

{

Application->Initialize();

Application->CreateForm (__classid(TForm1), &;Form1);

Application->Run();

}

Catch (Exception &;exception)

{

Application->ShowException(&;exception);

}

Catch (...)

{

Try

{

Throw Exception("");

}

Catch (Exception &;exception)

{

Application->ShowException(&;exception);

}

}

Return 0;

}

// -

UShipBattle. cpp

// -

#include <vcl. h>

#pragma hdrstop

#include "UShipBattle. h"

// -

#pragma package (smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

// -

__fastcall TForm1:TForm1 (TComponent* Owner)

: TForm(Owner)

{

HE = new HandlersOfEvents (Fild1, Fild2);

}

// -

IntFlot: GetStatus()

{

Destroy = true;

For (j = 0; j < 10; j++)

For (i = 0; i< ship[j].col_deck; i++)

If (ship[j].decks[i])

{

Destroy = false;

Return 1;

}

Return 0;

}

// -

Int AI: Generation (Flot *CPU, intcol_ship, intcol_deck)

{

Int x1, y1, k, i, j;

Bool vertical = false, regen;

Do {

Randomize();

X1 = rand()% 10;

Y1 = rand()% 10;

Regen = false;

For (k = 0; k <col_deck; k++)

For (i = 0; i<col_ship; i++)

For (j = 0; j < CPU -> ship[i].col_deck; j++)

{

If(regen) break;

If(! vertical)

{

If((CPU -> ship[i].desp_of_part[j].x == x1 + k &;&;

CPU -> ship[i].desp_of_part[j].y == y1) || (x1 + k >= 10))

{

Vertical = true;

Regen = true;

Continue;

}

If((CPU -> ship[i].desp_of_part[j].x - 1 == x1 + k ||

CPU -> ship[i].desp_of_part[j].x + 1 == x1 + k) &;&;

(CPU -> ship[i].desp_of_part[j].y - 1 == y1 ||

CPU -> ship[i].desp_of_part[j].y + 1 == y1))

{

Vertical = true;

Regen = true;

Continue;

}

}

If(vertical)

{

If((CPU -> ship[i].desp_of_part[j].x == x1 &;&;

CPU -> ship[i].desp_of_part[j].y == y1 + k) ||

(y1 + k >= 10))

{

Vertical = false;

Regen = true;

Continue;

}

If((CPU -> ship[i].desp_of_part[j].x - 1 == x1 ||

CPU -> ship[i].desp_of_part[j].x + 1 == x1) &;&;

(CPU -> ship[i].desp_of_part[j].y - 1 == y1 + k ||

CPU -> ship[i].desp_of_part[j].y + 1 == y1 + k))

{

Vertical = true;

Regen = true;

Continue;

}

}

}

} while(regen);

If(! vertical)

For (i = 0; i<col_deck; i++)

{

CPU -> ship [col_ship - 1].desp_of_part[i].x = x1 + i;

CPU -> ship [col_ship - 1].desp_of_part[i].y = y1;

}

Else

For (i = 0; i<col_deck; i++)

{

CPU -> ship [col_ship - 1].desp_of_part[i].x = x1;

CPU -> ship [col_ship - 1].desp_of_part[i].y = y1 + i;

}

Return 0;

}

// -

Int Referee: GoChecking (int x, int y, int n)

{

If (n == 1)

If (! Check(net1, x, y))

Return 0;

If (n == 2)

If (! Check(net2, x, y))

Return 0;

Return 1;

}

// -

Int Referee: Check (bool net[10] [10], int x, int y)

{

X /= 20; y /= 20;

If (! net[x] [y]) return 0;

Return 1;

}

// -

Int Referee: GoScaning (int x, int y, int n)

{

If (n == 1)

If (! Scan(player, x, y))

Return 0;

If (n == 2)

If (! Scan(CPU, x, y))

Return 0;

Return 1;

}

// -

Int Referee: Scan (Flot *fl, int x, int y)

{

X /= 20; y /= 20;

For (i = 0; i< 10; i++)

For (j = 0; j <fl -> ship[i].col_deck; j++)

If (fl -> ship[i].desp_of_part[j].x == x &;&;

Fl -> ship[i].desp_of_part[j].y == y)

{

Fl -> ship[i].decks[j] = false;

Return 0;

}

Return 1;

}

// -

Int Referee: Miss (int x, int y, int n)

{

X /= 20; y /= 20;

If(n == 1) net1 [x] [y] = false;

If(n == 2) net2 [x] [y] = false;

Return1;

}

// -

Int Referee: EndRaund (int n)

{

If (n == 2)

Form1 -> Panel2 -> Caption = "Выпобедили!";

If (n == 1)

Form1 -> Panel2 -> Caption = "Выпроиграли!";

Gameover = true;

Return 0;

}

// -

IntHandlersOfEvents: Play (TImage *Im1, TImage *Im2)

{

Im1 -> Enabled = true;

Im2 -> Enabled = true;

Return 0;

}

// -

IntHandlersOfEvents: Desposition (int x, int y, TMouseButton Button)

{

If(play) return 0;

X /= 20; y /= 20;

For (k = 0; k <col_deck; k++)

For (i = 0; i<col_ship; i++)

For (j = 0; j < player -> ship[i].col_deck; j++)

{

If (Button == mbLeft)

{

If (player -> ship[i].desp_of_part[j].x == x + k &;&;

Player -> ship[i].desp_of_part[j].y == y)

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

If (x + k >= 10)

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

If((player -> ship[i].desp_of_part[j].x - 1 == x + k ||

Player -> ship[i].desp_of_part[j].x + 1 == x + k) &;&;

Player -> ship[i].desp_of_part[j].y - 1 == y ||

Player -> ship[i].desp_of_part[j].y + 1 == y))

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

}

If (Button == mbRight)

{

If (player -> ship[i].desp_of_part[j].x == x &;&;

Player -> ship[i].desp_of_part[j].y == y + k)

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

If (y + k >= 10)

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

If((player -> ship[i].desp_of_part[j].x - 1 == x ||

Player-> ship[i].desp_of_part[j].x + 1 == x) &;&;

(player-> ship[i].desp_of_part[j].y - 1 == y + k ||

Player-> ship[i].desp_of_part[j].y + 1 == y + k))

{

ShowMessage ("Невозможно выполнить действие!");

Return 0;

}

}

}

If (Button == mbLeft)

Draw ("position_h", x*20, y*20, 1);

Else

Draw ("position_v", x*20, y*20, 1);

Generation (CPU, col_ship, col_deck);

If (col_ship == 1) col_deck -;

If (col_ship == 3) col_deck -;

If (col_ship == 6) col_deck -;

If (col_ship == 10) play = true;

If(play) Form1 -> Panel2 -> Caption = "Поехали";

Col_ship++;

Return 0;

};

// -

IntHandlersOfEvents: Shoot (int x, int y)

{

If (game_over) return 0;

Intshoot_player = ShootPlayer (x, y, 2);

If (shoot_player> 0)

Do {

X = rand()% 200;

Y = rand()% 200;

Intshoot_cpu = ShootPlayer (x, y, 1);

If (shoot_cpu> 0)

Break;

If (shoot_cpu< 0)

Player ->GetStatus();

If (player -> destroy || target_CPU> 19)

{

EndRaund(1);

Return 0;

}

} while(true);

Else basekorabl kletka korpotomok алгоритм

{

If (shoot_player< 0)

CPU ->GetStatus();

If (CPU -> destroy || target_player> 19)

{

EndRaund(2);

Return 0;

}

}

Return 0;

}

// -

IntHandlersOfEvents: ShootPlayer (int x, int y, int n)

{

If(! play) return 0;

If (! GoChecking(x, y, n))

{

If (n == 2) Form1 -> Panel2 -> Caption = "Ещераз! Тудаужестреляли!";

Return 0;

}

Else if (! GoScaning(x, y, n))

{

Draw ("target", x, y, n);

Miss (x, y, n);

If (n == 2)

{

Form1 -> Panel2 -> Caption = "Попал! Ещераз!";

Target_player++;

}

Elsetarget_CPU++;

Return -1;

}

Miss (x, y, n);

If (n == 2) Form1 -> Panel2 -> Caption = "Мимо! Ходопонента";

Draw ("miss", x, y, n);

Return 1;

}

// -

IntHandlersOfEvents: Draw (String key, int x, int y, int n)

{

TImage *Im;

X /= 20; y /= 20;

If (n == 1) Im = Form1 -> Fild1;

If (n == 2) Im = Form1 -> Fild2;

If (key == "target")

{

Im -> Canvas -> Rectangle (x*20, y*20, x*20 + 20, y*20 + 20);

Im -> Canvas -> Brush -> Color = clYellow;

Im -> Canvas -> Rectangle (x*20, y*20 + 20, x*20 + 20, y*20);

Im -> Canvas -> Brush -> Color = clWhite;

}

If (key == "miss")

{

Im -> Canvas -> Ellipse (x*20, y*20, x*20 + 20, y*20 + 20);

Im -> Canvas -> Ellipse (x*20 + 5, y*20 + 5, x*20 + 15, y*20 + 15);

}

If (key == "position_h")

{

For (i = 0; i<col_deck; i++)

{

Im -> Canvas -> Brush -> Color = clBlue;

Im -> Canvas -> Rectangle (x*20 + i*20, y*20, x*20 + 20 + i*20, y*20 + 20);

Player -> ship [col_ship - 1].desp_of_part[i].x = x + i;

Player -> ship [col_ship - 1].desp_of_part[i].y = y;

Im -> Canvas -> Brush -> Color = clWhite;

}

}

If (key == "position_v")

{

For (i = 0; i<col_deck; i++)

{

Im -> Canvas -> Brush -> Color = clBlue;

Im -> Canvas -> Rectangle (x*20, y*20 + i*20, x*20 + 20, y*20 + 20 + i*20);

Player -> ship [col_ship - 1].desp_of_part[i].x = x;

Player -> ship [col_ship - 1].desp_of_part[i].y = y + i;

Im -> Canvas -> Brush -> Color = clWhite;

}

}

Return 0;

}

// -

Void __fastcall TForm1: Fild2MouseDown (TObject *Sender,

TMouseButton Button, TShiftState Shift, int X, int Y)

{

HE -> Shoot (X, Y);

}

// -

Void __fastcall TForm1: BitBtn2Click (TObject *Sender)

{

HE -> Play (Fild1, Fild2);

BitBtn2 -> Visible = false;

BitBtn3 -> Visible = true;

Panel2 -> Caption = "Расставьтекорабли";

}

// -

Void __fastcall TForm1: Fild1MouseDown (TObject *Sender,

TMouseButton Button, TShiftState Shift, int X, int Y)

{

HE ->Desposition (X, Y, Button);

}

// -

Void __fastcall TForm1: BitBtn3Click (TObject *Sender)

{

Panel2 -> Caption = "Расставьтекорабли";

Fild1 -> Picture ->LoadFromFile ("net. bmp");

Fild2 -> Picture ->LoadFromFile ("net. bmp");

HE = new HandlersOfEvents (Fild1, Fild2);

}

// -

UShipBattle. h

// -

#ifndefUShipBattleH

#define UShipBattleH

// -

#include <Classes. hpp>

#include <Controls. hpp>

#include <StdCtrls. hpp>

#include <Forms. hpp>

#include <Buttons. hpp>

#include <ExtCtrls. hpp>

#include <Graphics. hpp>

#include <ComCtrls. hpp>

// -

Struct Ship

{

Bool destroy;

Intcol_deck;

Bool *decks;

TPoint *desp_of_part;

};

ClassFlot

{

Public:

Inti, j, n;

Bool destroy;

TImage *Im;

Ship ship[10];

Flot (TImage *I)

{

Destroy = false;

Im = I;

N = 1;

For (i = 0; i< 10; i++)

{

If (i< 6) n = 2;

If (i< 3) n = 3;

If (i == 0) n = 4;

Ship[i].destroy = false;

Ship[i].col_deck = n;

Ship[i].decks = new bool[n];

Ship[i].desp_of_part = new TPoint[n];

}

}

IntGetStatus();

};

Class AI

{

Public:

TPoint decks_[20];

Intcol_decks_;

Int Generation (Flot *CPU, intcol_ship, intcol_deck);

};

Class Referee: public AI

{

Public:

Inti, j, target_player, target_CPU;

Bool net1 [10] [10], net2 [10] [10], game_over;

Flot *player, *CPU;

Referee()

{

For (i = 0; i< 10; i++)

For (j = 0; j < 10; j++)

{

Net1 [i] [j] = true;

Net2 [i] [j] = true;

}

Game_over = false;

Target_player = 0;

Target_CPU = 0;

Col_decks_ = 0;

}

IntGoChecking (int x, int y, int n);

Int Check (bool net[10] [10], int x, int y);

IntGoScaning (int x, int y, int n);

Int Scan (Flot *fl, int x, int y);

Int Miss (int x, int y, int n);

IntEndRaund (int n);

};

ClassHandlersOfEvents: public Referee

{

Public:

Bool play;

Intcol_deck, col_ship, i, j, k;

HandlersOfEvents (TImage *Im1, TImage *Im2)

{

Play = false;

Col_deck = 4; col_ship = 1;

Player = new Flot(Im1);

CPU = new Flot(Im2);

}

Int Play (TImage *Im1, TImage *Im2);

Int Move();

IntDesposition (int x, int y, TMouseButton Button);

Int Draw (String key, int x, int y, int n);

IntShootPlayer (int x, int y, int n);

Int Shoot (int x, int y);

};

Class TForm1: public TForm

{

__published: // IDE-managed Components

TImage *Fild1;

TImage *Fild2;

TPanel *Panel1;

TBitBtn *BitBtn1;

TBitBtn *BitBtn2;

TBitBtn *BitBtn3;

TPanel *Panel2;

Void __fastcall Fild2MouseDown (TObject *Sender,

TMouseButton Button, TShiftState Shift, int X, int Y);

Void __fastcall BitBtn2Click (TObject *Sender);

Void __fastcall Fild1MouseDown (TObject *Sender,

TMouseButton Button, TShiftState Shift, int X, int Y);

Void __fastcall BitBtn3Click (TObject *Sender);

Private: // User declarations

Public: // User declarations

__fastcall TForm1 (TComponent* Owner);

HandlersOfEvents *HE;

};

// -

ExternPACKAGETForm1 *Form1;

// -

#endif

Похожие статьи




А - Разработка программы на языке C++, реализующей игру "Морской бой"

Предыдущая | Следующая