HP Prime for All
English
Русский
Name | Mastermind |
Description | Well-done implementation of the famous code-breaking game. Each code peg can be one of 6 colors or vacant if you choose that option. When you submit your guess, black and white key pegs will provide feedback. |
Author | Tony Gallo |
Source code formatted by website engine
ICON resource lines were stripped.
BEGIN
local touch;
LOCAL X, Y;
local ColorX;
local Solution, Guess, SolFlag;
local SolColor, PegColor;
local G;
local B, W;
local I, J;
local choice;
local startcol;
local key;
local touch;
local message;
local done;
//title screen
RECT_P(0, 0, 319, 239, 0, RGB(0, 0, 0));
TEXTOUT_P("MASTERMIND", 120, 90, 2, RGB(255, 0, 0));
TEXTOUT_P("v1.2.3 2014", 126, 110, 1, RGB(255, 0, 0));
TEXTOUT_P("by Tony Gallo", 125, 130, 1, RGB(255, 0, 0));
wait(1.5);
//unselected(brown), red, green, blue, yellow, black, white
colorList := {RGB(185, 135, 105), RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255), RGB(255, 244, 43), RGB(0, 0, 0), RGB(2…
done := 0;
WHILE (done == 0) DO
CHOOSE(choice, "Allow Vacant Pegs?", "Yes", "No");
if choice == 0 then
//clicked outside choose box - default to Yes
choice := 1;
end;
if choice == 1 then
startcol := 0;
else
startcol := 1;
end;
//generate solution
Solution := {0, 0, 0, 0};
FOR I FROM 1 to 4 DO
Solution(I) := RANDINT(startcol, 6);
END;
//clear screen
RECT_P(0, 0, 319, 239, 0, RGB(142, 142, 142));
//credits
TEXTOUT_P("MASTERMIND", 10, 5, 2, RGB(0, 0, 0));
TEXTOUT_P("v1.2.3 2014", 10, 16, 1, RGB(0, 0, 0));
TEXTOUT_P("by Tony Gallo", 10, 26, 1, RGB(0, 0, 0));
//top rectangle
RECT_P(95, 5, 220, 35, 0, RGB(185, 135, 105));
//game board
RECT_P(10, 70, 310, 225, 0, RGB(185, 135, 105));
LINE_P(10, 195, 310, 195, RGB(0, 0, 0));
//lines and circles
FOR X := 1 to 10 DO
LINE_P(30*X+10, 70, 30*X+10, 225, RGB(0, 0, 0));
FOR Y := 1 to 4 DO
ARC_P(30*X-5, 30*Y+60, 10, RGB(0, 0, 0));
END;
ARC_P(30*X-10, 205, 3, RGB(0, 0, 0));
ARC_P(30*X, 205, 3, RGB(0, 0, 0));
ARC_P(30*X-10, 215, 3, RGB(0, 0, 0));
ARC_P(30*X, 215, 3, RGB(0, 0, 0));
END;
//position pointer
TEXTOUT_P("▲", 20, 222, 5, RGB(207, 3, 124));
//selected colour
TEXTOUT_P("Selected Color:", 12, 50, 2, RGB(0, 0, 0));
if choice == 1 then
//unselected - brown - 0
FilledCircle(120, 53, 10, RGB(185, 135, 105));
ARC_P(120, 53, 10, RGB(0, 0, 0));
TEXTOUT_P("CLR", 112, 49, 1, RGB(75, 0, 130));
end;
//red - 1
FilledCircle(150, 53, 10, RGB(255, 0, 0));
//green - 2
FilledCircle(180, 53, 10, RGB(0, 255, 0));
//blue - 3
FilledCircle(210, 53, 10, RGB(0, 0, 255));
//yellow - 4
FilledCircle(240, 53, 10, RGB(255, 244, 43));
//black - 5
FilledCircle(270, 53, 10, RGB(0, 0, 0));
//white - 6
FilledCircle(300, 53, 10, RGB(255, 255, 255));
//set default color
ColorX := 1;
//draw box
LINE_P(105 + (ColorX*30), 40, 135 + (ColorX*30), 40, RGB(0, 0, 0));
LINE_P(105 + (ColorX*30), 66, 135 + (ColorX*30), 66, RGB(0, 0, 0));
LINE_P(105 + (ColorX*30), 40, 105 + (ColorX*30), 66, RGB(0, 0, 0));
LINE_P(135 + (ColorX*30), 40, 135 + (ColorX*30), 66, RGB(0, 0, 0));
G := 1;
Guess := {0, 0, 0, 0};
message := 0;
WHILE ISKEYDOWN(4) <> 1 do
wait(.1);
touch := mouse();
key := -1;
key := getkey;
if (key > = 0 or size(touch(1))) and message == 1 then
RECT_P(225, 5, 300, 35, RGB(142, 142, 142), RGB(142, 142, 142));
message := 0;
end;
//color selecting using left and right
if key == 7 and ((choice == 1 and ColorX >= 1) or (choice == 2 and ColorX >= 2)) then
ColorX := ColorX-1;
BoxSelectedColor(ColorX);
continue;
end;
if key == 8 and ColorX <= 5 then
ColorX := ColorX+1;
BoxSelectedColor(ColorX);
continue;
end;
//Guessing
if key == 30 then
//if no vacant pegs alowed make sure none are vacant
if choice == 2 and (Guess(1) == 0 or Guess(2) == 0 or Guess(3) == 0 or Guess(4) == 0) then
TEXTOUT_P("No vacant pegs", 230, 10, 1, RGB(0, 0, 0));
TEXTOUT_P("allowed.", 242, 22, 1, RGB(0, 0, 0));
message := 1;
continue;
end;
//analyse
B := 0;
W := 0;
SolFlag := {0, 0, 0, 0};
//look for exact matches
FOR I FROM 1 TO 4 DO
if Guess(I) == Solution(I) then
B := B+1;
Guess(I) := 7; //flagged
SolFlag(I) := 1; //flagged
end;
END;
//look for right colour wrong spot
FOR I FROM 1 TO 4 DO
if Guess(I) <> 7 then
FOR J FROM 1 TO 4 DO
if SolFlag(J) == 0 AND Guess(I) == Solution(J) then
W := W+1;
Guess(I) := 7;
SolFlag(J) := 1;
end;
END;
end;
END;
Guess := {0, 0, 0, 0};
//place black and white pegs
FOR I FROM 1 to B DO
ShowResult(I, RGB(0, 0, 0), G);
END;
FOR I FROM B+1 to B+W DO
ShowResult(I, RGB(255, 255, 255), G);
END;
if B == 4 or G == 10 then
//game over
if B == 4 then
TEXTOUT_P("You win!", 250, 10, 2, RGB(0, 0, 0));
else //G == 10
TEXTOUT_P("You lose!", 250, 10, 2, RGB(0, 0, 0));
end;
TEXTOUT_P("Press any key.", 240, 22, 2, RGB(0, 0, 0));
ShowSolution(Solution);
break;
end;
G := G+1;
RECT_P(10, 230, 310, 235, RGB(142, 142, 142));
TEXTOUT_P("▲", (30 * (G-1)) + 20, 222, 5, RGB(207, 3, 124));
continue;
end;
if size(touch(1)) then
if touch(1, 5) == 0 then
X := B→R(touch(1, 1));
Y := B→R(touch(1, 2));
//color selector
if 110 <= X <= 310 and 43 <= Y <= 63 THEN
if 110 <= X <= 130 and choice == 1 then ColorX := 0 END; //unselected - brown
if 140 <= X <= 160 then ColorX := 1 END; //red
if 170 <= X <= 190 then ColorX := 2 END; //green
if 200 <= X <= 220 then ColorX := 3 END; //blue
if 230 <= X <= 250 then ColorX := 4 END; //yellow
if 260 <= X <= 280 then ColorX := 5 END; //black
if 290 <= X <= 310 then ColorX := 6 END; //white
BoxSelectedColor(ColorX);
continue;
END;
//placing pegs
if (30 * (G-1)) + 10 <= X <= (30 * (G-1)) + 40 and 80 <= Y <= 190 THEN
PegColor := colorList(ColorX+1);
if 80 <= Y <= 100 then
Guess(4) := ColorX;
FilledCircle((30 * (G-1)) + 25, 90, 9, PegColor);
continue;
END;
if 110 <= Y <= 130 then
Guess(3) := ColorX;
FilledCircle((30 * (G-1)) + 25, 120, 9, PegColor);
continue;
END;
if 140 <= Y <= 160 then
Guess(2) := ColorX;
FilledCircle((30 * (G-1)) + 25, 150, 9, PegColor);
continue;
END;
if 170 <= Y <= 190 then
Guess(1) := ColorX;
FilledCircle((30 * (G-1)) + 25, 180, 9, PegColor);
continue;
END;
END;
end;
end;
END;
key := -1;
while key = -1 do
wait(.1);
touch := mouse();
key := getkey;
end;
CHOOSE(choice, "Play Again?", "Yes", "No");
if choice < > 1 then done := 1; end;
END;
END;
FilledCircle(X, Y, Radius, Color)
begin
local I;
FOR I FROM 0 TO Radius DO
ARC_P(X, Y, I, Color);
END;
end;
ShowResult(I, Color, G)
begin
if I == 1 then FilledCircle(30*G-10, 205, 2, Color); end;
if I == 2 then FilledCircle(30*G, 205, 2, Color); end;
if I == 3 then FilledCircle(30*G-10, 215, 2, Color); end;
if I == 4 then FilledCircle(30*G, 215, 2, Color); end;
end;
BoxSelectedColor(ColorX)
begin
//clear old box wherever it may be
LINE_P(105, 40, 315, 40, RGB(142, 142, 142));
LINE_P(105, 66, 315, 66, RGB(142, 142, 142));
FOR I FROM 0 to 8 DO
LINE_P(105 + (I*30), 40, 105 + (I*30), 66, RGB(142, 142, 142));
END;
//draw box
LINE_P(105 + (ColorX*30), 40, 135 + (ColorX*30), 40, RGB(0, 0, 0));
LINE_P(105 + (ColorX*30), 66, 135 + (ColorX*30), 66, RGB(0, 0, 0));
LINE_P(105 + (ColorX*30), 40, 105 + (ColorX*30), 66, RGB(0, 0, 0));
LINE_P(135 + (ColorX*30), 40, 135 + (ColorX*30), 66, RGB(0, 0, 0));
end;
ShowSolution(Solution)
begin
FOR I FROM 1 to 4 DO
ARC_P(82 + (I*30), 20, 10, RGB(0, 0, 0));
FilledCircle(82 + (I*30), 20, 9, colorList(Solution(I) + 1));
END;
end;