Kamis, 19 April 2012

game sederhana banget "tebak ingatan"






private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form17: TForm17;
  blank: TPoint;
  points: array [1..15] of TPoint;
  time: int64;
  done, go: boolean;
implementation

{$R *.dfm}
procedure RenderCell(id: integer);
begin
  TPanel(Form17.FindComponent('Panel' + inttostr(id))).Top := (61 * (points[id].y

- 1)) + 3;
  TPanel(Form17.FindComponent('Panel' + inttostr(id))).Left := (66 * (points[id].x

- 1)) + 4;
end;
function pe(p1, p2: TPoint): boolean;
begin
  if (p1.X = p2.x) and (p1.Y = p2.y) then begin
    Result := true;
  end else begin
    Result := false;
  end;
end;
procedure Check();
var
  timestr: string;
begin
 if (pe(points[1], Point(1 , 1))) and (pe(points[2], Point(2 , 1))) and

(pe(points[3], Point(3 , 1))) and (pe(points[4], Point(4 , 1)))
 and (pe(points[5], Point(1 , 2))) and (pe(points[6], Point(2 , 2))) and

(pe(points[7], Point(3 , 2))) and (pe(points[8], Point(4 , 2)))
 and (pe(points[9], Point(1 , 3))) and (pe(points[10], Point(2 , 3))) and

(pe(points[11], Point(3 , 3))) and (pe(points[12], Point(4 , 3)))
 and (pe(points[13], Point(1 , 4))) and (pe(points[14], Point(2 , 4))) and

(pe(points[15], Point(3 , 4))) then begin
   done := true;
   time := GetTickCount - time;
   go := false;
   if time div 1000 > 60 then begin
    timestr := IntToStr(time div 60000) + '.' + IntToStr((time mod 60000) div

6000) + ' Minutes';
   end else begin
    timestr := IntToStr(time div 1000) + '.' + IntToStr(time mod 1000) + '

Seconds';
   end;
   Showmessage('You Won!' + #13 + 'Time: ' + timestr);
   time := 0;
 end;
end;
procedure RandomizePuzzle();
var
  i, j, x: integer;
  buffer: array[1..15] of TPoint;
  bufpoint: TPoint;
label check;
begin
  for x := 0 to 35 do begin
  Randomize;
    for I := 1 to 15 do begin
      buffer[i] := points[i];
    end;
    j := randomrange(2, 16);
    for I := j downto 1 do begin
      points[i] := buffer[i + 1];
    end;
        points[j] := buffer[1];
    points[1] := buffer[2];
  end;
  bufpoint := points[14];
  points[14] := points[15];
  points[15] := bufpoint;
  for I := 1 to 15 do begin
      RenderCell(i);
  end;
  go := true;
end;
procedure TForm17.Button1Click(Sender: TObject);
begin
 RandomizePuzzle();
end;
procedure TForm17.Button3Click(Sender: TObject);
begin
  application.Terminate;
end;

procedure TForm17.FormCreate(Sender: TObject);
var
  i: integer;
begin
  time := 0;
  done := true;
  go := false;
  blank := Point(4, 4);
  for I := 1 to 15 do begin
    points[i] := Point((I mod 4) + 0, (I div 4) + 1);
    if points[i].x = 0 then begin
       points[i].x := 4;
    end;
    if i > 12 then begin
      points[i].Y :=  4;
    end;
    if i mod 4 = 0 then begin
      points[i].Y := points[i].Y - 1;
    end;
  end;
  points[1].X := 1;
  points[15].X := 3;
end;
procedure TForm17.Label1Click(Sender: TObject);
begin
  ShellExecute(handle, 'open', 'http://www.javajaf.blogspot.com', nil, nil,

SW_SHOWNORMAL);
end;

procedure TForm17.Panel15Click(Sender: TObject);
var
  id: integer;
  p, p2: TPoint;
begin
  if go then begin
  if done then begin
    time := GetTickCount();
  end;
  done := false;
  id := (Sender as TPanel).Tag;
  p := blank;
  p2 := point(0,0);
  if (p.x - 1 = points[id].x) and (p.y = points[id].y) then begin
    p2 := points[id];
    points[id] := p;
    p := p2;
  end else if (p.x + 1 = points[id].x)  and (p.y = points[id].y) then begin
    p2 := points[id];
    points[id] := p;
    p := p2;
  end else if (p.y - 1 = points[id].y) and (p.x = points[id].x) then begin
    p2 := points[id];
    points[id] := p;
    p := p2;
  end else if (p.y + 1 = points[id].y) and (p.x = points[id].x) then begin
    p2 := points[id];
    points[id] := p;
    p := p2;
  end;
  blank := p;
  RenderCell(id);
  Check();
  end;
end;
end.

Tidak ada komentar:

Posting Komentar