mitushkin-infa/GameUnit.cpp

150 lines
4.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <vcl.h>
#pragma hdrstop
#include "GameUnit.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmGame *frmGame;
__fastcall TfrmGame::TfrmGame(TComponent* Owner) : TForm(Owner)
{
}
void __fastcall TfrmGame::FormShow(TObject *Sender)
{
Randomize();
StartGame();
}
void __fastcall TfrmGame::FormClose(TObject *Sender, TCloseAction &Action)
{
tmrClock->Enabled = false;
}
// расставить фишки по порядку и перемешать случайными ходами
// (так позиция всегда получается решаемой)
void TfrmGame::StartGame()
{
for (int i = 0; i < 15; i++)
sgBoard->Cells[i % 4][i / 4] = IntToStr(i + 1);
sgBoard->Cells[3][3] = "";
for (int k = 0; k < 1000; k++)
TryMove(Random(4), Random(4));
steps = 0;
secs = 0;
gameOver = false;
tmrClock->Enabled = false;
UpdateLabels();
}
// если рядом с клеткой (col, row) пусто - двигаем фишку туда
bool TfrmGame::TryMove(int col, int row)
{
int ec = -1;
int er = -1;
if (col > 0 && sgBoard->Cells[col - 1][row] == "") { ec = col - 1; er = row; }
if (col < 3 && sgBoard->Cells[col + 1][row] == "") { ec = col + 1; er = row; }
if (row > 0 && sgBoard->Cells[col][row - 1] == "") { ec = col; er = row - 1; }
if (row < 3 && sgBoard->Cells[col][row + 1] == "") { ec = col; er = row + 1; }
if (ec == -1)
return false;
sgBoard->Cells[ec][er] = sgBoard->Cells[col][row];
sgBoard->Cells[col][row] = "";
return true;
}
bool TfrmGame::CheckWin()
{
for (int i = 0; i < 15; i++)
if (sgBoard->Cells[i % 4][i / 4] != IntToStr(i + 1))
return false;
return true;
}
void TfrmGame::UpdateLabels()
{
lblSteps->Caption = L"Ходов: " + IntToStr(steps);
lblClock->Caption = L"Время: " + IntToStr(secs) + L" с";
}
void __fastcall TfrmGame::sgBoardMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
int col, row;
sgBoard->MouseToCell(X, Y, col, row);
if (gameOver || col < 0 || row < 0)
return;
if (!TryMove(col, row))
return;
steps++;
tmrClock->Enabled = true; // время идёт с первого хода
UpdateLabels();
if (CheckWin())
{
tmrClock->Enabled = false;
gameOver = true;
String msg = L"Вы прошли за " + IntToStr(secs) + L" с, ходов: " + IntToStr(steps);
Application->MessageBox(msg.c_str(), L"Победа!", MB_OK | MB_ICONINFORMATION);
}
}
void __fastcall TfrmGame::tmrClockTimer(TObject *Sender)
{
secs++;
UpdateLabels();
}
void __fastcall TfrmGame::btnShuffleClick(TObject *Sender)
{
StartGame();
}
void __fastcall TfrmGame::btnRulesClick(TObject *Sender)
{
Application->MessageBox(
L"Нужно расставить фишки по порядку от 1 до 15,\n"
L"пустая клетка - в правом нижнем углу.\n\n"
L"Щёлкните по фишке рядом с пустой клеткой,\n"
L"чтобы передвинуть её.\n\n"
L"Время начинает идти после первого хода.",
L"Правила игры", MB_OK | MB_ICONINFORMATION);
}
// фишки - скруглённые плитки, пустая клетка - цвет фона
void __fastcall TfrmGame::sgBoardDrawCell(TObject *Sender, System::LongInt ACol,
System::LongInt ARow, const TRect &Rect, TGridDrawState State)
{
TCanvas *c = sgBoard->Canvas;
c->Brush->Color = sgBoard->FixedColor;
c->FillRect(Rect);
String s = sgBoard->Cells[ACol][ARow];
if (s == "")
return;
c->Brush->Color = sgBoard->Color;
c->Pen->Color = sgBoard->Color;
c->RoundRect(Rect.Left + 4, Rect.Top + 4, Rect.Right - 4, Rect.Bottom - 4, 16, 16);
c->Font->Assign(sgBoard->Font);
c->Brush->Style = bsClear;
int tx = Rect.Left + (Rect.Width() - c->TextWidth(s)) / 2;
int ty = Rect.Top + (Rect.Height() - c->TextHeight(s)) / 2;
c->TextOut(tx, ty, s);
c->Brush->Style = bsSolid;
}
// D или F2 - почти собрать поле (для показа), остаётся один ход
void __fastcall TfrmGame::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key != 'D' && Key != VK_F2)
return;
for (int i = 0; i < 15; i++)
sgBoard->Cells[i % 4][i / 4] = IntToStr(i + 1);
sgBoard->Cells[2][3] = "";
sgBoard->Cells[3][3] = "15";
steps = 100 + Random(100);
secs = 60 + Random(120);
gameOver = false;
tmrClock->Enabled = true;
UpdateLabels();
Key = 0;
}