Mastermind code lazarus / 61-Beginners ---------------------------------------------------------------------- wareospike · 2017-08-29 08:48 Hey folks! I am on a simple project, but still hard to achieve for me. So, I could use some useful information or tips. I should code a similar Mastermind program (only with function and procedures) Not yet with arrays etc..: - 4 RANDOM numbers between 1 and 4  to be guessed. - In the result of the guess there will be 2 pins. * A red one (R) when the number is on the right position. * A white one (W) when the number is in it, but on the wrong place. - It will be a loop so the user can continue as many times he needs, and when he succeed there should be a result of the attempts he needed. I am wondering what the best way would be for this. A procedure or a function and what all the variables would be. Anything can help me on the right way! Thanks in advance! ---------------------------------------------------------------------- Thaddy · 2017-08-29 09:02 Try the randomfrom function in math.... ---------------------------------------------------------------------- wareospike · 2017-08-29 11:29 Quote from: Thaddy on August 29, 2017, 11:02:40 amTry the randomfrom function in math.... Hey, that randomfunction I know. But I am more struggling with the code itself to be honest. ---------------------------------------------------------------------- howardpc · 2017-08-29 13:06 Is this homework? What code do you have so far? ---------------------------------------------------------------------- wareospike · 2017-08-29 13:32 Quote from: howardpc on August 29, 2017, 03:06:22 pmIs this homework? What code do you have so far? This is self learning by a school course To be honest, I almost have nothing because it's quite hard to see the structure in this one. I've been coding similar things, but with this one I am kind of confused on which things to use and how many functions I should use. And what all the variables are. I will only learn to code this by myself, but would need some advice in this one. ---------------------------------------------------------------------- molly · 2017-08-29 13:48 It would be easier if you would just put up the complete question. Quote from: wareospike on August 29, 2017, 10:48:09 amNot yet with arrays etc..: To vague. what means etc ? what does array mean in their context (mind that indexing a string or pointer is also an array). Quote- In the result of the guess there will be 2 pins. * A red one (R) when the number is on the right position. * A white one (W) when the number is in it, but on the wrong place. Can these pins be duplicated ? your output shows not but the question does not mention it. QuoteI am wondering what the best way would be for this. A procedure or a function and what all the variables would be. Anything can help me on the right way! Well, you can use whatever you like. I did  it in 25 lines in main, no subroutines. And i consider that a bad result. I did use strings though. Quote from: wareospike on August 29, 2017, 03:32:06 pmTo be honest, I almost have nothing because it's quite hard to see the structure in this one. Then write down the steps in plain (native tongue or) English (or pseudo-code) and if a single step is too complicated then try to break it apart into smaller pieces. Have you already got something in code that asks and wait for input from the user for example ? if not how would you approach that. If you have no idea then think about it for a moment and show us where exactly you get stuck by showing some (pseudo) code. ---------------------------------------------------------------------- RAW · 2017-09-01 04:53 Guess Number MY FIRST GAME EVER...  (maybe there is something you can use... maybe not...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROGRAM GuessNumber;{$MODE OBJFPC}{$H+}{$J-}  USES  Crt, SysUtils, Keyboard;   Function KeyInput: Char;   Var    Key: TKeyEvent;  Begin   InitKeyBoard;    Try     Key:= GetKeyEvent;     Key:= TranslateKeyEvent(Key);     Result:= GetKeyEventChar(Key);    Finally     DoneKeyBoard;    End;  End;   Function IsValidNumber(str: String): Boolean;   Var    iNum: Integer;  Begin   Result:= False;    If Length(str) > 4 Then Exit;    If TryStrToInt(str, iNum) And (Length(str) = 4)   Then Result:= True;  End;   Function WaitForEscOrEnter: Char;  Begin   Repeat    Result:= KeyInput;   Until (Result = #27) Or (Result = #13);  End;   Procedure Play;   Var    booQuit, booGameOver: Boolean;    strNum, strFound, strRandomNum, strRightFalse: String;    i, ix, iCount, iRound, iFound: Integer;  Begin   booQuit:= False;   RANDOMIZE;    While Not booQuit // Main Menu Loop   Do    Begin     ClrScr;      WriteLn('==============');   WriteLn(' GUESS NUMBER');     WriteLn('==============');     WriteLn;     WriteLn('ESC   = QUIT');     WriteLn('ENTER = START');      If WaitForEscOrEnter = #27 // ESC     Then Break     Else      Begin       iRound:= 0;       strRandomNum:= IntToStr(1000+Random(8999));       booGameOver := False;        While Not booGameOver // Game Loop       Do        Begin         ClrScr;        WriteLn('==============');         WriteLn(' GUESS NUMBER');       WriteLn('==============');         WriteLn;         WriteLn('Input A Valid 4Digit-Number:');          ReadLn(strNum);          If IsValidNumber(strNum)         Then          Begin           Inc(iRound);            If SameText(strNum, strRandomNum)           Then            Begin             ClrScr;             booGameOver:= True;              WriteLn('==============');             WriteLn(' GUESS NUMBER');             WriteLn('==============');             WriteLn;             WriteLn('>> SUCCESS <<'+sLineBreak+                   IntToStr(iRound)+' Round(s)');             WriteLn('Right Number: '+strRandomNum);             WriteLn;             WriteLn;          End           Else            Begin             strRightFalse:= '';             strFound     := '';              iCount:= 0;           iFound:= 0;              For i:= 1 To 4             Do              Begin               If strNum[i] = strRandomNum[i]               Then strRightFalse:= strRightFalse+' RIGHT '               Else strRightFalse:= strRightFalse+' FALSE ';              End;             For i:= 1 To strNum.Length             Do              For ix:= 1 To strRandomNum.Length              Do               Begin              Inc(iCount);                 If strNum[i] = strRandomNum[ix]              Then Inc(iFound);                 If iCount = 4                Then                 Begin                  iCount  := 0;                strFound:= strFound+' '+IntToStr(iFound)+'x    ';                iFound  := 0;                 End;               End;              WriteLn;             //WriteLn(strRandomNum); // CheatMode On/Off :-)             WriteLn('ROUND '+IntToStr(iRound));             WriteLn;             WriteLn(' '+strNum[1]+'      '+strNum[2]+'      '+                     strNum[3]+'      '+strNum[4]);             WriteLn(strRightFalse);             WriteLn;             WriteLn(' FOUND  FOUND  FOUND  FOUND');             WriteLn(strFound);           WriteLn;             WriteLn;            End;            If Not booGameOver           Then            Begin             WriteLn('ESC = MAIN MENU');             WriteLn('ENTER = TRY AGAIN');              If WaitForEscOrEnter = #27 // ESC             Then Break;            End           Else            Begin             WriteLn('ENTER Or ESC = MAIN MENU');              If WaitForEscOrEnter = #27 // ESC           Then Break             Else Break;            End;          End; // IsValidNumber        End; // Game Loop      End; // Start Game    End; // MainMenu Loop  End;  BEGIN Play;END. EDIT: Improved version ---------------------------------------------------------------------- wareospike · 2017-09-01 05:47 Hey you all! I coded this program alone after all. I wrote it all out, but now I am wondering about to create a subroutine. Anyone who can explain me how to start and see which function I should create in this project? Thanks! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program MasterMind; var  { TODO: check and accomplish variable declarations }  uit2: char;  uit1: char;  pogingen: Longint;  gokD: Longint;  gokC: Longint;  gokB: Longint;  gokA: Longint;  cijferD: Longint;  cijferC: Longint;  cijferB: Longint;  cijferA: Longint; begin  RANDOMIZE;  cijferA := RANDOM(4);  cijferB := RANDOM(4);  cijferC := RANDOM(4);  cijferD := RANDOM(4);  writeln(cijferA,' ',cijferB,' ',cijferC,' ',cijferD);  uit1 := 'R';  uit2 := 'W';  pogingen := 1;  writeln('Raad de code. Geef 4 cijfers iedere keer met een spatie.');  readln(gokA, gokB, gokC, gokD);  while (cijferA<>gokA) or (cijferB<>gokB) or (cijferC<>gokC) or (cijferD<>gokD) do  begin    pogingen := pogingen+1;    if (cijferA=gokA) then    begin      writeln(uit1);    end    else    begin      if (cijferA=gokB) or (cijferA=gokC) or (cijferA=gokD) then      begin        writeln(uit2);      end;    end;    if (cijferB=gokB) then    begin      writeln(uit1);    end    else    begin      if (cijferB=gokA) or (cijferB=gokC) or (cijferB=gokD) then      begin        writeln(uit2);      end;    end;    if (cijferC=gokC) then    begin      writeln(uit1);    end    else    begin      if (cijferC=gokA) or (cijferC=gokB) or (cijferC=gokD) then      begin        writeln(uit2);      end;    end;    if (cijferD=gokD) then    begin      writeln(uit1);    end    else    begin      if (cijferD=gokA) or (cijferD=gokB) or (cijferD=gokC) then      begin        writeln(uit2);      end;    end;    writeln('Raad de code. Geef 4 cijfers iedere keer met een spatie.');    readln(gokA, gokB, gokC, gokD);  end;  writeln('Proficiat u hebt de code juist in', pogingen, ' pogingen.');  writeln;  writeln('Druk op om het programma te sluiten.');  readln();end. ---------------------------------------------------------------------- molly · 2017-09-01 19:50 You can't really aid your code with additional subroutines except perhaps for the comparisons. tbh, the first thing what comes to mind when reading your code is "what a waste of longints"... 3 perfectly good bytes gone to waste  If you create the same program using an array, and add the possibility to be able to guess between more different digits and longer numbers then a) you have completely other code to look at and 2) are more likely to see what functions/procedure might be able to aid such code. f you then re-write back that code without using an array, things become even more clear. Not really part of the assignment but you could learn some additional things from that. BTW: you might want to read up on random function: QuoteRandom returns a random number larger or equal to 0 and strictly less than L. If the argument L is omitted, a Real number between 0 and 1 is returned (0 included, 1 excluded). As your code uses it right now, it generates numbers from 0 to 3.