{ Questo file contiene un sorgente pascal per giocare a Master Mind. Versione preliminare ad opera di Rizzi Romeo. Progetto da portare avanti da parte della IIi. e-mail: rrizzi@science.unitn.it home-page: http:\\www-math.science.unitn.it\~rrizzi } program MasterMind; uses crt; const NUM_COLORS = 5; NUM_PIOLI = 4; NUM_TENTATIVI = 11; ENTER = 13; UP = 72; DOWN = 80; colore : array [1..NUM_COLORS] of integer = (Red, Yellow, Green, Magenta, Cyan); var chiave, prova : array [1..NUM_PIOLI] of integer; i : integer; procedure Incrementa(var n: integer); begin n := n +1; end; procedure Decrementa(var n: integer); begin n := n -1; end; function Min(a,b : integer): integer; begin if a < b then Min := a else Min := b; end; procedure WriteXYColor(x,y,color: integer; text:string); begin TextColor(color); GotoXY(x,y); Write(text); TextColor(White); end; function RandColor: integer; begin RandColor := random(NUM_COLORS)+1; end; procedure SiglaApertura; begin end; procedure SiglaChiusura; begin end; procedure SchermataIniziale; begin TextBackground(Blue); ClrScr; end; procedure Partita; var mossa, neri, bianchi : integer; procedure InputRiga(riga :integer); var c : char; j : integer; procedure InputPiolo(riga, col :integer; var inputColor : integer); begin InputColor := 1; repeat WriteXYColor(2*col,2*riga, colore[InputColor], ' Û'); GotoXY(2*col+1,2*riga); c := ReadKey; if Ord(c) = UP then Incrementa(InputColor); if Ord(c) = DOWN then Decrementa(InputColor); if InputColor <= 0 then InputColor := NUM_COLORS; if InputColor > NUM_COLORS then InputColor := 1; until Ord(c) = ENTER; end; begin for j := 1 to NUM_PIOLI do InputPiolo(riga,j,prova[j]); end; procedure ValutaRiga(riga :integer); var j,k : integer; massaPColore, massaCColore : integer; begin neri := 0; bianchi := 0; for j := 1 to NUM_PIOLI do if prova[j] = chiave[j] then Incrementa(neri); for j := 1 to NUM_COLORS do begin massaPColore := 0; massaCColore := 0; for k := 1 to NUM_PIOLI do begin if prova[k] = j then Incrementa(massaPColore); if chiave[k] = j then Incrementa(massaCColore); end; bianchi := bianchi + Min(massaPColore, massaCColore); end; bianchi := bianchi - neri; for j := 1 to neri do WriteXYColor(10+2*j, 2*riga,Black,' Û'); for j := 1 to bianchi do WriteXYColor(10+2*(j+neri), 2*riga,White,' Ü'); end; begin mossa := 0; repeat Incrementa(mossa); InputRiga(mossa); ValutaRiga(mossa); until (neri = 4) or (mossa >= NUM_TENTATIVI); end; begin Randomize; SiglaApertura; SchermataIniziale; for i := 1 to NUM_PIOLI do chiave[i] := RandColor; Partita; SiglaChiusura; end.