Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with entrees_sorties; use entrees_sorties;
- procedure morpion is
- function getColByNum(num : Integer) return Integer is
- begin
- return ((num-1) mod 3) + 1;
- end getColByNum;
- function getLineByNum(num : Integer) return Integer is
- begin
- return ((num-1) / 3) + 1;
- end getLineByNum;
- function getOccupant(grille, lig, col : Integer) return Integer is
- begin
- return ((grille/(1000**(lig-1)))/(10**(col-1)) mod 10);
- end getOccupant;
- procedure ajouterMarque(grille : in out Integer; marque, lig, col : Integer) is
- begin
- grille := grille + (marque*((1000**(lig-1))*((10**(col-1)))));
- end ajouterMarque;
- function caseDejaOccupee(grille, lig, col : Integer) return Boolean is
- begin
- return (getOccupant(grille, lig, col) /= 0);
- end caseDejaOccupee;
- function estPleine(grille : Integer) return Boolean is
- pleine : Boolean;
- begin
- pleine := TRUE;
- for a in 1..9 loop
- if(((grille / (10**(a-1))) mod 10) = 0) then
- pleine := FALSE;
- end if;
- end loop;
- return pleine;
- end estPleine;
- procedure afficherBordSuperieur is
- begin
- put("^^^^^");
- new_line;
- end afficherBordSuperieur;
- procedure afficherBordInferieur is
- begin
- put("vvvvv");
- new_line;
- end afficherBordInferieur;
- function getCharFromInteger(int1 : Integer) return Character is
- begin
- if(int1 = 1) then
- return 'X';
- elsif(int1 = 2) then
- return 'O';
- else
- return ' ';
- end if;
- end getCharFromInteger;
- procedure extraireLigne(grille, numLigne : in Integer; case1, case2, case3 : out Integer) is
- begin
- case1 := ((grille/(1000**(numLigne-1))) mod 10);
- case2 := ((grille/(1000**(numLigne-1)))/10 mod 10);
- case3 := ((grille/(1000**(numLigne-1)))/100 mod 10);
- end extraireLigne;
- procedure extraireColonne(grille, numColonne : in Integer; case1, case2, case3 : out Integer) is
- begin
- case1 := ((grille/(1000**(0)))/(10**(numColonne-1)) mod 10);
- case2 := ((grille/(1000**(1)))/(10**(numColonne-1)) mod 10);
- case3 := ((grille/(1000**(2)))/(10**(numColonne-1)) mod 10);
- end extraireColonne;
- procedure extraireDiagonale(grille, numDiagonale : in Integer; case1, case2, case3 : out Integer) is
- begin
- case1 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*0)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*0))-1)) mod 10);
- case2 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*1)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*1))-1)) mod 10);
- case3 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*2)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*2))-1)) mod 10);
- end extraireDiagonale;
- function ilYAUnGagnant(grille : Integer) return Integer is
- case1, case2, case3 : Integer;
- gagnant : Integer;
- begin
- gagnant := 0;
- for lig in 1..3 loop
- extraireLigne(grille, lig, case1, case2, case3);
- if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
- gagnant := case1;
- end if;
- end loop;
- for col in 1..3 loop
- extraireColonne(grille, col, case1, case2, case3);
- if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
- gagnant := case1;
- end if;
- end loop;
- for num in 1..2 loop
- extraireDiagonale(grille, num, case1, case2, case3);
- if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
- gagnant := case1;
- end if;
- end loop;
- return gagnant;
- end ilYAUnGagnant;
- procedure afficherLigne(case1, case2, case3 : Integer) is
- begin
- put('<'&getCharFromInteger(case1)&'|'
- &getCharFromInteger(case2)&'|'
- &getCharFromInteger(case3)&'>');
- new_line;
- end afficherLigne;
- procedure afficherSeparationLigne is
- begin
- put("<-+-+->");
- new_line;
- end afficherSeparationLigne;
- procedure afficherGrille(grille : Integer) is
- case1, case2, case3 : Integer;
- numLigne : Integer;
- begin
- afficherBordSuperieur;
- extraireLigne(grille, 3, case1, case2, case3);
- afficherLigne(case1, case2, case3);
- afficherSeparationLigne;
- extraireLigne(grille, 2, case1, case2, case3);
- afficherLigne(case1, case2, case3);
- afficherSeparationLigne;
- extraireLigne(grille, 1, case1, case2, case3);
- afficherLigne(case1, case2, case3);
- afficherBordInferieur;
- end afficherGrille;
- lig, col : Integer;
- ch : Integer;
- grille : Integer;
- joueur, gagnant : Integer;
- begin
- grille := 0;
- afficherGrille(grille);
- joueur := 1;
- while(ilYAUnGagnant(grille) = 0 AND not(estPleine(grille))) loop
- get(ch);
- lig := getLineByNum(ch);
- col := getColByNum(ch);
- put("Ligne ="&Integer'Image(lig));
- new_line;
- put("Col ="&Integer'Image(lig));
- new_line;
- put("occupant ="&Integer'Image(getOccupant(grille, lig, col)));
- new_line;
- if(caseDejaOccupee(grille, lig, col)) then
- put("La case est deja occupee !");
- new_line;
- else
- ajouterMarque(grille, joueur, lig, col);
- joueur := ((joueur) mod 2) + 1;
- end if;
- new_line;
- afficherGrille(grille);
- new_line;
- new_line;
- end loop;
- gagnant := ilYAUnGagnant(grille);
- if(gagnant /= 0) then
- put("Le gagnant est"&Integer'Image(gagnant));
- else
- put("Il n'y a pas de gagnant...");
- end if;
- new_line;
- end morpion;
Advertisement
Add Comment
Please, Sign In to add comment