Creeaza.com - informatii profesionale despre


Evidentiem nevoile sociale din educatie - Referate profesionale unice
Acasa » scoala » informatica
MATEMATICA-INFORMATICA - CALUTUL INTELIGENT

MATEMATICA-INFORMATICA - CALUTUL INTELIGENT


 



Tema: "CALUTUL INTELIGENT"

Introducere in Turbo Pascal

Limbajul Pascal a fost elaborat in 1972 de catre Nicklaus Wirth de la Universitatea din Zurich in scopul unei mai bune invatari a programarii . Desi la inceput el a fost cunoscut doar de mediul universitar, apoi el a cunoscut un succes imens .

Structura programelor Pascal

Orice program incepe printr-un cuvint numit PROGRAM care este urmat de numele propriu-zis al programului, adica de un nume ales de utilizator si de semnul ; (punct si virgula)

Orice program contine cel putin odata cuvintele cu un inteles special BEGIN si END

Orice program se termina prin punct

Orice cuvint al programului poate fi scris cu litere mari sau mici, deoarece nu are importanta

. In versiunea Turbo, prima linie poate lipsi, desi nu se recomanda acest lucru, din ratiuni de ordine

Plasarea cuvintelor pe linie si numarul de spatii dintre ele sint la alegerea programatorului . Se poate scrie tot programul pe o singura linie, insa este bine ca programul sa fie scris in asa fel incit sa fie usor de inteles .

PREZENTARE APLICATIEI

Acest program realizat in limbajul de programare Turbo Pascal este un program in care un 'calut' asezat pe o tabla de dimensiuni introduse de la calculator de catre utilizator va pleca dintr-un punct specificat de utilizator si va parcurge fiecare casuta a tablei,nu neaparat intr-o anumita ordine,neparcurzand aceeasi casuta de 2 ori . Acest lucru este posibil datorita unui algoritm care 'gandeste' mutarile calutului .

Utilizatorul poate alege ca dupa fiecare mutare a calutului acesta sa se opreasca in casuta in care tocmai s-a mutat si continuandu-si mersul dupa ce este apasata orice tasta sau pauza dintre mutari sa fie una scurta mutarea facandu-se automat fara interventia utilizatorului .

Dimensiunea tablei poate avea valori cuprinse doar in intervalul inchis [5,12] .

In acest program s-au folosit urmatoarele variabile:

type imagine_ cal=array[1 . . 30,1 . . 30] of byte;

const cal:imagine_ cal= (

type matrice=array[1 . . 20,1 . . 20] of integer;

type coord=array[1 . . 8] of integer;

sir=array[1 . . 8] of integer;

const xc:coord= (-2,-1, 1, 2, 2, 1,-1,-2) ;

yc:coord= ( 1, 2, 2, 1,-1,-2,-2,-1) ;

var i,j:integer;

marime_ matrice:integer;

x_ pornire,y_ pornire:integer;

a:matrice

c:sir

x1,y1,x2,y2:integer;

oprire:boolean;

Functiile si procedurile care s-au folosit in realizarea acestui program sunt:

procedure initializare_ mod_ grafic;

var

grDriver: Integer;

grMode: Integer;

ErrCode: Integer;

begin

grDriver := Detect;

InitGraph (grDriver, grMode,'bgi') ;

if Graphresult<>0 then begin

writeln ('Modul grafic nu a putut fi initializat') ;

halt;

end;

end;

procedure cerere_ optiuni; c:char;

var eroare_ la_ introducerea_ datelor:boolean;

begin

window (1,1,80,25) ;

textbackground (black) ;

textcolor (white) ;

clrscr

textbackground (blue) ;

window (10,7,70,25) ;

write ('ÚAA AA AA AA Optiuni posibile pentru problema calului AA AA AA AA As') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('ÀAA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AÙ') ;

gotoxy (3,3) ;

write ('Marimea tablei de sah (maxim 14, minim 5) :') ;

gotoxy (3,5) ;

write ('Punctul din care se va porni (x) :') ;

gotoxy (3,6) ;

write ('Punctul din care se va porni (y) :') ;

gotoxy (4,8) ;

write ('Doriti ca la fiecare mutare programul sa se opreasca') ;

gotoxy (4,9) ;

write ('pana la apasarea unei taste sau sa astepte numai ') ;

gotoxy (4,10) ;

write ('cateva momente si sa continue [ prire/ ontinuare] !') ;

textcolor (red) ;

gotoxy (35,10) ;

write ('O') ;

gotoxy (42,10) ;

write ('C') ;

repeat

textcolor (white) ;

window (60,9,65,9) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (marime_ matrice) ;

textbackground (blue) ;

clrscr;

write (marime_ matrice) ;

if (marime_ matrice<15) and (marime_ matrice>4) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;


if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,11,60,11) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (x_ pornire) ;

textbackground (blue) ;

clrscr;

write (x_ pornire) ;

if (x_ pornire>0) and (x_ pornire<marime_ matrice+1) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,12,60,12) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (y_ pornire) ;

textbackground (blue) ;

clrscr;

write (y_ pornire) ;

if (y_ pornire>0) and (y_ pornire<marime_ matrice+1) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,17,60,17) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

c:=readkey;

if c in ['O','o','c','C'] then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

oprire:=c in ['o','O'];

until not eroare_ la_ introducerea_ datelor;

end;

Aceasta procedura se va opri in momentul in care de la tastura

se vor introduce date valide .

procedure desenare_ cal (x,y:integer;c1,c2:byte) ;

begin

for i:=1 to 30 do

for j:=1 to 30 do

if cal[j,i]=15 then putpixel (x+i,y+j,c1)

else putpixel (x+i,y+j,c2) ;

end;

procedure desenare_ patrat;

var i,x,y,j:integer;

s:string;

begin

x1:= (640- (marime_ matrice*31) ) div 2;

y1:= (480- (marime_ matrice*31) ) div 2;

x2:= (640+ (marime_ matrice*31) ) div 2;

y2:= (480+ (marime_ matrice*31) ) div 2;

line (x1,y1,x1,y2) ;

line (x1,y1,x2,y1) ;

line (x2,y2,x1,y2) ;

line (x2,y2,x2,y1) ;

i:=1;

x:=x1;

while i<=marime_ matrice do begin

line (x,y1,x,y2) ;

i:=i+1;

x:=x+31;

end;

i:=1;

y:=y1;

while i<=marime_ matrice do begin

line (x1,y,x2,y) ;

i:=i+1;

y:=y+31;

end;

end;

Prin desenarea patratului si liniilor verticale cat si orizontale

se va obtine tabla pe care se va deplasa calutul .

function numara_ posibilitatile_ de_ mutare (i,j:integer) :integer;

var w:integer;

nr:integer;

begin

nr:=0;

for w:=1 to 8 do begin

if (j+yc[w]<=marime_ matrice) and (j+yc[w]>0) then

if (i+xc[w]<=marime_ matrice) and (i+xc[w]>0) then

if a[i+xc[w],j+yc[w]]=0 then inc (nr) ;

end;

numara_ posibilitatile_ de_ mutare:=nr;

end;

procedure calculul_ efectiv;

var i,j:integer;

gata:boolean;

w,q:integer;

min,poz,singura:integer;

unde:integer;

s:string;

begin

gata:=false;

i:=x_ pornire;

j:=y_ pornire;

unde:=1;

a[i,j]:=unde;

outtextxy (x1+13+ (i-1) *31,y1+12+ (j-1) *31,'1') ;

while not gata do begin

for w:=1 to 8 do begin

c[w]:=0;

if (j+yc[w]<=marime_ matrice) and (j+yc[w]>0) and

(i+xc[w]<=marime_ matrice) and (i+xc[w]>0) then

if a[i+xc[w],j+yc[w]]=0 then begin

singura:=w;

c[w]:=numara_ posibilitatile_ de_ mutare (i+xc[w],j+yc[w]) ;

end;

end;

min:=8;

poz:=1;

gata:=true;

for w:=1 to 8 do

if (min>c[w]) and (c[w]>0) then begin

min:=c[w];

poz:=w;

gata:=false;

end;

if gata then poz:=singura;

i:=i+xc[poz];

j:=j+yc[poz];

unde:=unde+1;

a[i,j]:=unde;

str (unde,s) ;

for q:=1 to 3 do begin

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,white,black) ;

delay (200) ;

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,black,white) ;

delay (200) ;

end;

if oprire then readkey

else delay (600) ;

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,black,black) ;

if length (S) =1 then outtextxy (x1+13+ (i-1) *31,y1+12+ (j-1) *31,s)

else

if length (s) =2 then outtextxy (x1+9+ (i-1) *31,y1+12+ (j-1) *31,s)

else outtextxy (x1+4+ (i-1) *31,y1+12+ (j-1) *31,s) ;

end;

end;

Acum urmeaza corpul progamului in care se vor apela functiile

si procedurile create in uramtoarea ordine:

cerere_ optiuni;

initializare_ mod_ grafic;

desenare_ patrat;

calculul_ efectiv;

Codul sursa:

uses graph,crt;

type imagine_ cal=array[1 . . 30,1 . . 30] of byte;

const cal:imagine_ cal= (

type matrice=array[1 . . 20,1 . . 20] of integer;

type coord=array[1 . . 8] of integer;

sir=array[1 . . 8] of integer;

const xc:coord= (-2,-1, 1, 2, 2, 1,-1,-2) ;

yc:coord= ( 1, 2, 2, 1,-1,-2,-2,-1) ;

var i,j:integer;

marime_ matrice:integer;

x_ pornire,y_ pornire:integer;

a:matrice;

c:sir;

x1,y1,x2,y2:integer;

oprire:boolean;

procedure initializare_ mod_ grafic;

var

grDriver: Integer;

grMode: Integer;

ErrCode: Integer;

begin

grDriver := Detect;

InitGraph (grDriver, grMode,'bgi') ;

if Graphresult<>0 then begin

writeln ('Modul grafic nu a putut fi initializat') ;

halt;

end;

end;

procedure cerere_ optiuni;

var eroare_ la_ introducerea_ datelor:boolean;

c:char;

begin

window (1,1,80,25) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

textbackground (blue) ;

window (10,7,70,25) ;

write ('ÚAA AA AA AA Optiuni posibile pentru problema calului AA AA AA AA As') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('³ ³') ;

write ('ÀAA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AÙ') ;

gotoxy (3,3) ;

write ('Marimea tablei de sah (maxim 14, minim 5) :') ;

gotoxy (3,5) ;

write ('Punctul din care se va porni (x) :') ;

gotoxy (3,6) ;

write ('Punctul din care se va porni (y) :') ;

gotoxy (4,8) ;

write ('Doriti ca la fiecare mutare programul sa se opreasca') ;

gotoxy (4,9) ;

write ('pana la apasarea unei taste sau sa astepte numai ') ;

gotoxy (4,10) ;

write ('cateva momente si sa continue [ prire/ ontinuare] !') ;

textcolor (red) ;

gotoxy (35,10) ;

write ('O') ;

gotoxy (42,10) ;

write ('C') ;

repeat

textcolor (white) ;

window (60,9,65,9) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (marime_ matrice) ;

textbackground (blue) ;

clrscr;

write (marime_ matrice) ;

if (marime_ matrice<15) and (marime_ matrice>4) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,11,60,11) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (x_ pornire) ;

textbackground (blue) ;

clrscr;

write (x_ pornire) ;

if (x_ pornire>0) and (x_ pornire<marime_ matrice+1) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,12,60,12) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

readln (y_ pornire) ;

textbackground (blue) ;

clrscr;

write (y_ pornire) ;

if (y_ pornire>0) and (y_ pornire<marime_ matrice+1) then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

if IOresult<>0 then eroare_ la_ introducerea_ datelor:=true;

until not eroare_ la_ introducerea_ datelor;

repeat

window (50,17,60,17) ;

textbackground (black) ;

textcolor (white) ;

clrscr;

c:=readkey;

if c in ['O','o','c','C'] then

eroare_ la_ introducerea_ datelor:=false

else

eroare_ la_ introducerea_ datelor:=true;

oprire:=c in ['o','O'];

until not eroare_ la_ introducerea_ datelor;

end;

procedure desenare_ cal (x,y:integer;c1,c2:byte) ;

begin

for i:=1 to 30 do

for j:=1 to 30 do

if cal[j,i]=15 then putpixel (x+i,y+j,c1)

else putpixel (x+i,y+j,c2) ;

end;

procedure desenare_ patrat;

var i,x,y,j:integer;

s:string;

begin

x1:= (640- (marime_ matrice*31) ) div 2;

y1:= (480- (marime_ matrice*31) ) div 2;

x2:= (640+ (marime_ matrice*31) ) div 2;

y2:= (480+ (marime_ matrice*31) ) div 2;

line (x1,y1,x1,y2) ;

line (x1,y1,x2,y1) ;

line (x2,y2,x1,y2) ;

line (x2,y2,x2,y1) ;

i:=1;

x:=x1;

while i<=marime_ matrice do begin

line (x,y1,x,y2) ;

i:=i+1;

x:=x+31;

end;

i:=1;

y:=y1;

while i<=marime_ matrice do begin

line (x1,y,x2,y) ;

i:=i+1;

y:=y+31;

end;

end;

function numara_ posibilitatile_ de_ mutare (i,j:integer) :integer;

var w:integer;

nr:integer;

begin

nr:=0;

for w:=1 to 8 do begin

if (j+yc[w]<=marime_ matrice) and (j+yc[w]>0) then

if (i+xc[w]<=marime_ matrice) and (i+xc[w]>0) then

if a[i+xc[w],j+yc[w]]=0 then inc (nr) ;

end;

numara_ posibilitatile_ de_ mutare:=nr;

end;

procedure calculul_ efectiv;

var i,j:integer;

gata:boolean;

w,q:integer;

min,poz,singura:integer;

unde:integer;

s:string;

begin

gata:=false;

i:=x_ pornire;

j:=y_ pornire;

unde:=1;

a[i,j]:=unde;

outtextxy (x1+13+ (i-1) *31,y1+12+ (j-1) *31,'1') ;

while not gata do begin

for w:=1 to 8 do begin

c[w]:=0;

if (j+yc[w]<=marime_ matrice) and (j+yc[w]>0) and

(i+xc[w]<=marime_ matrice) and (i+xc[w]>0) then

if a[i+xc[w],j+yc[w]]=0 then begin

singura:=w;

c[w]:=numara_ posibilitatile_ de_ mutare (i+xc[w],j+yc[w]) ;

end;

end;

min:=8;

poz:=1;

gata:=true;

for w:=1 to 8 do

if (min>c[w]) and (c[w]>0) then begin

min:=c[w];

poz:=w;

gata:=false;

end;

if gata then poz:=singura;

i:=i+xc[poz];

j:=j+yc[poz];

unde:=unde+1;

a[i,j]:=unde;

str (unde,s) ;

for q:=1 to 3 do begin

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,white,black) ;

delay (200) ;

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,black,white) ;

delay (200) ;

end;

if oprire then readkey

else delay (600) ;

desenare_ cal (x1+ (i-1) *31,y1+ (j-1) *31,black,black) ;

if length (S) =1 then outtextxy (x1+13+ (i-1) *31,y1+12+ (j-1) *31,s)

else

if length (s) =2 then outtextxy (x1+9+ (i-1) *31,y1+12+ (j-1) *31,s)

else outtextxy (x1+4+ (i-1) *31,y1+12+ (j-1) *31,s) ;

end;

end;

begin

cerere_ optiuni;

initializare_ mod_ grafic;

desenare_ patrat;

calculul_ efectiv;

if not oprire then readkey;

end .

Bibliografie


L . Toca, A . R . Demco, C . Opincaru, A . Sindile

Informatica - Manual pentru clasa a X-a

Editura Niculescu - Bucuresti, 2000

Fl . Munteanu, T . Ionescu, Gh . Muscǎ, D . Saru, S . M . Dascǎlu

Programarea calculatoarelor - Manual pentru liceele de informatica,

clasele X-XII

Editura Didactica si Pedagogica - Bucuresti, 1995

S . Niculescu si colaboratori

Bacalaureat si atestat

Editura L&S, 1998





Politica de confidentialitate


creeaza logo.com Copyright © 2024 - Toate drepturile rezervate.
Toate documentele au caracter informativ cu scop educational.