Click here to Skip to main content
15,745,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following is the code for a minesweeper project. In the ApplyBlocks procedure, I want to check all adjacent blocks (blocks are stored in a two-dim array as coordinates), and if any are bombs (or = 9), then increase there value by 1. Unfortunately I can not get this to cork and I do not understand why. Thanks in advance for any help offered.

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ExtCtrls, StrUtils, StdCtrls;

type
  TMainWin = class(TForm)
    MainMenu1: TMainMenu;
    Game1: TMenuItem;
    Help1: TMenuItem;
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure BlockMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CreateBlocks(Sender: TObject);
    procedure CreateBombs(Sender: TObject);
    procedure ApplyBombs(Sender: TObject);
    procedure ApplyBlocks(Sender: TObject);
    procedure ShowBombs(Sender: TObject);
    procedure ShowNumbers(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TestBombs;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  BOMBCOUNT = 10;
  WIDTHQ = 9;
  HEIGHTQ = 9;
  PREFIX = 'block';

var
  MainWin: TMainWin;
  aBlocks : array[1..WIDTHQ] of array[1..HEIGHTQ] of integer;
  aBombArray : Array[1..BOMBCOUNT] of String;
implementation

{$R *.dfm}

procedure TMainWin.FormCreate(Sender: TObject);
begin
  Randomize;
  CreateBlocks(Sender);
  CreateBombs(Sender);
  //TestBombs;
  ApplyBombs(Sender);
  ApplyBlocks(Sender);
  ShowBombs(Sender);
  ShowNumbers(Sender);

end;

procedure TMainWin.CreateBlocks(Sender: TObject);
  var BlockImage : TImage;
      ix, iy : integer;
begin

  for ix := 1 to WIDTHQ do begin
    for iy := 1 to HEIGHTQ do begin
      BlockImage := TImage.Create(self);
      BlockImage.Picture.LoadFromFile('Winmine__XP_410.bmp');
      BlockImage.Name := (PREFIX + inttostr(ix) + 'Q' + inttostr(iy));
      BlockImage.Width := 16;
      BlockImage.Height := 16;
      BlockImage.Top := iy * 16;
      BlockImage.Left := ix * 16;
      BlockImage.Visible := true;
      BlockImage.Parent := self;

      BlockImage.OnMouseDown := BlockMouseDown;
      //BlockImage.OnMouseUp := BlockMouseUp(Sender);
    end;
  end;
end;

procedure TMainWin.BlockMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var BlockImage : TImage;
begin
  BlockImage := Sender as TImage;
  BlockImage.Picture.LoadFromFile('res/BlankBlock.bmp');
end;

procedure TMainWin.CreateBombs(Sender: TObject);
  var
        i  : integer;
        AllBombs  : AnsiString;
        temp : string;
begin

  AllBombs := '';

  for i := 1 to BOMBCOUNT do begin
    temp := (inttostr(Random(8) + 1) + 'Q' + inttostr(Random(8) + 1));


    while ansiContainsStr(AllBombs, temp) do begin
      temp := (inttostr(Random(8) + 1) + 'Q' + inttostr(Random(8) + 1));
    end;

    aBombArray[i] := temp;
    AllBombs := AllBombs + aBombArray[i] + ',';

  end;
end;

procedure TMainWin.ApplyBombs(Sender: TObject);
  var i : integer;
      QPos, NameLength : integer;
      xPos, yPos : String;
begin

  for i := 1 to BOMBCOUNT do begin
    NameLength := Length(aBombArray[i]);
    QPos := AnsiPos('Q', aBombArray[i]);
    xPos := Copy(aBombArray[i], 1, QPos - 1);
    yPos := Copy(aBombArray[i], QPos+1, NameLength - QPos);

    aBlocks[strtoint(xPos), strtoint(yPos)] := 9;
  end;
end;

procedure TMainWin.ApplyBlocks(Sender: TObject);
  var ix, iy, BlockNumber : integer;
begin

  BlockNumber := 0;

  for ix := 1 to WIDTHQ do begin
    for iy := 1 to HEIGHTQ do begin
      if not (aBlocks[ix,iy] = 9) then begin

        aBlocks[ix,iy] := 0;

        if aBlocks[ix-1,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
        if aBlocks[ix,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
        if aBlocks[ix+1,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;

        if aBlocks[ix-1,iy] = 9 then BlockNumber := BlockNumber+1;
        if aBlocks[ix+1,iy] = 9 then BlockNumber := BlockNumber+1;

        if aBlocks[ix-1,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
        if aBlocks[ix,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
        if aBlocks[ix+1,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;

        aBlocks[ix,iy] := BlockNumber;
        BlockNumber := 0;

      end;
    end;
  end;
end;

procedure TMainWin.ShowBombs(Sender: TObject);
  var ix, iy : integer;
begin
  for ix := 1 to WIDTHQ do begin
    for iy := 1 to HEIGHTQ do begin
      if aBlocks[ix, iy] = 9 then begin
        with TImage(FindComponent(PREFIX + inttostr(ix) + 'Q' + inttostr(iy))) do begin
          Picture.LoadFromFile('BombBlock.bmp');
        end;
      end;
    end;
  end;
end;
Posted
Comments
Sergey Alexandrovich Kryukov 8-Nov-14 1:22am    
I don't want to dig into it, but will tell you one idea: storing coordinates in an array element is redundant. Coordinates are the array indices. It's enough to store 1 bit per array element (or, say 2-3 bits).
—SA

1 solution

These lines:

Delphi
if aBlocks[ix-1,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy-WIDTHQ] = 9 then BlockNumber := BlockNumber+1;

if aBlocks[ix-1,iy] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy] = 9 then BlockNumber := BlockNumber+1;

if aBlocks[ix-1,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy+WIDTHQ] = 9 then BlockNumber := BlockNumber+1;


should be
Delphi
if aBlocks[ix-1,iy-1] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix,iy-1] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy-1] = 9 then BlockNumber := BlockNumber+1;

if aBlocks[ix-1,iy] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy] = 9 then BlockNumber := BlockNumber+1;

if aBlocks[ix-1,iy+1] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix,iy+1] = 9 then BlockNumber := BlockNumber+1;
if aBlocks[ix+1,iy+1] = 9 then BlockNumber := BlockNumber+1;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900