* * *

Author Topic: Mastermind code  (Read 2282 times)

wareospike

  • New member
  • *
  • Posts: 6
Mastermind code
« on: August 29, 2017, 10:48:09 am »
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!
« Last Edit: August 29, 2017, 01:36:35 pm by wareospike »

Thaddy

  • Hero Member
  • *****
  • Posts: 5970
Re: Mastermind code
« Reply #1 on: August 29, 2017, 11:02:40 am »
Try the randomfrom function in math....
recommends {$macro on}{$define Silly:=ObjFpc}

wareospike

  • New member
  • *
  • Posts: 6
Re: Mastermind code
« Reply #2 on: August 29, 2017, 01:29:48 pm »
Try the randomfrom function in math....

Hey, that randomfunction I know. But I am more struggling with the code itself to be honest. :)

howardpc

  • Hero Member
  • *****
  • Posts: 2669
Re: Mastermind code
« Reply #3 on: August 29, 2017, 03:06:22 pm »
Is this homework?
What code do you have so far?

wareospike

  • New member
  • *
  • Posts: 6
Re: Mastermind code
« Reply #4 on: August 29, 2017, 03:32:06 pm »
Is 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

  • Hero Member
  • *****
  • Posts: 2345
Re: Mastermind code
« Reply #5 on: August 29, 2017, 03:48:37 pm »
It would be easier if you would just put up the complete question.

Not 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.

Quote
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!
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.

To 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.
« Last Edit: August 29, 2017, 03:53:19 pm by molly »

RAW

  • Hero Member
  • *****
  • Posts: 662
Re: Mastermind code
« Reply #6 on: September 01, 2017, 06:53:46 am »
Guess Number
MY FIRST GAME EVER...  :)

(maybe there is something you can use... maybe not...  :D
Code: Pascal  [Select]
  1. PROGRAM GuessNumber;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  USES
  5.   Crt, SysUtils, Keyboard;
  6.  
  7.  
  8.  Function KeyInput: Char;
  9.    Var
  10.     Key: TKeyEvent;
  11.   Begin
  12.    InitKeyBoard;
  13.     Try
  14.      Key:= GetKeyEvent;
  15.      Key:= TranslateKeyEvent(Key);
  16.      Result:= GetKeyEventChar(Key);
  17.     Finally
  18.      DoneKeyBoard;
  19.     End;
  20.   End;
  21.  
  22.  
  23.  Function IsValidNumber(str: String): Boolean;
  24.    Var
  25.     iNum: Integer;
  26.   Begin
  27.    Result:= False;
  28.  
  29.    If Length(str) > 4 Then Exit;
  30.  
  31.    If TryStrToInt(str, iNum) And (Length(str) = 4)
  32.    Then Result:= True;
  33.   End;
  34.  
  35.  
  36.  Function WaitForEscOrEnter: Char;
  37.   Begin
  38.    Repeat
  39.     Result:= KeyInput;
  40.    Until (Result = #27) Or (Result = #13);
  41.   End;
  42.  
  43.  
  44.  Procedure Play;
  45.    Var
  46.     booQuit, booGameOver: Boolean;
  47.     strNum, strFound, strRandomNum, strRightFalse: String;
  48.     i, ix, iCount, iRound, iFound: Integer;
  49.   Begin
  50.    booQuit:= False;
  51.    RANDOMIZE;
  52.  
  53.    While Not booQuit // Main Menu Loop
  54.    Do
  55.     Begin
  56.      ClrScr;
  57.  
  58.      WriteLn('==============');
  59.      WriteLn(' GUESS NUMBER');
  60.      WriteLn('==============');
  61.      WriteLn;
  62.      WriteLn('ESC   = QUIT');
  63.      WriteLn('ENTER = START');
  64.  
  65.      If WaitForEscOrEnter = #27 // ESC
  66.      Then Break
  67.      Else
  68.       Begin
  69.        iRound:= 0;
  70.        strRandomNum:= IntToStr(1000+Random(8999));
  71.        booGameOver := False;
  72.  
  73.        While Not booGameOver // Game Loop
  74.        Do
  75.         Begin
  76.          ClrScr;
  77.  
  78.          WriteLn('==============');
  79.          WriteLn(' GUESS NUMBER');
  80.          WriteLn('==============');
  81.          WriteLn;
  82.          WriteLn('Input A Valid 4Digit-Number:');
  83.  
  84.          ReadLn(strNum);
  85.  
  86.          If IsValidNumber(strNum)
  87.          Then
  88.           Begin
  89.            Inc(iRound);
  90.  
  91.            If SameText(strNum, strRandomNum)
  92.            Then
  93.             Begin
  94.              ClrScr;
  95.              booGameOver:= True;
  96.  
  97.              WriteLn('==============');
  98.              WriteLn(' GUESS NUMBER');
  99.              WriteLn('==============');
  100.              WriteLn;
  101.              WriteLn('>> SUCCESS <<'+sLineBreak+
  102.                      IntToStr(iRound)+' Round(s)');
  103.              WriteLn('Right Number: '+strRandomNum);
  104.              WriteLn;
  105.              WriteLn;
  106.             End
  107.            Else
  108.             Begin
  109.              strRightFalse:= '';
  110.              strFound     := '';
  111.  
  112.              iCount:= 0;
  113.              iFound:= 0;
  114.  
  115.              For i:= 1 To 4
  116.              Do
  117.               Begin
  118.                If strNum[i] = strRandomNum[i]
  119.                Then strRightFalse:= strRightFalse+' RIGHT '
  120.                Else strRightFalse:= strRightFalse+' FALSE ';
  121.               End;
  122.  
  123.  
  124.              For i:= 1 To strNum.Length
  125.              Do
  126.               For ix:= 1 To strRandomNum.Length
  127.               Do
  128.                Begin
  129.                 Inc(iCount);
  130.  
  131.                 If strNum[i] = strRandomNum[ix]
  132.                 Then Inc(iFound);
  133.  
  134.                 If iCount = 4
  135.                 Then
  136.                  Begin
  137.                   iCount  := 0;
  138.                   strFound:= strFound+' '+IntToStr(iFound)+'x    ';
  139.                   iFound  := 0;
  140.                  End;
  141.                End;
  142.  
  143.              WriteLn;
  144.              //WriteLn(strRandomNum); // CheatMode On/Off :-)
  145.              WriteLn('ROUND '+IntToStr(iRound));
  146.              WriteLn;
  147.              WriteLn(' '+strNum[1]+'      '+strNum[2]+'      '+
  148.                      strNum[3]+'      '+strNum[4]);
  149.              WriteLn(strRightFalse);
  150.              WriteLn;
  151.              WriteLn(' FOUND  FOUND  FOUND  FOUND');
  152.              WriteLn(strFound);
  153.              WriteLn;
  154.              WriteLn;
  155.             End;
  156.  
  157.            If Not booGameOver
  158.            Then
  159.             Begin
  160.              WriteLn('ESC   = MAIN MENU');
  161.              WriteLn('ENTER = TRY AGAIN');
  162.  
  163.              If WaitForEscOrEnter = #27 // ESC
  164.              Then Break;
  165.             End
  166.            Else
  167.             Begin
  168.              WriteLn('ENTER Or ESC = MAIN MENU');
  169.  
  170.              If WaitForEscOrEnter = #27 // ESC
  171.              Then Break
  172.              Else Break;
  173.             End;
  174.           End; // IsValidNumber
  175.         End; // Game Loop
  176.       End; // Start Game
  177.     End; // MainMenu Loop
  178.   End;
  179.  
  180.  
  181. BEGIN
  182.  Play;
  183. END.

EDIT: Improved version  :)
« Last Edit: March 18, 2018, 12:25:39 am by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.2 FPC 3.0.4 // 1.7 FPC 3.1.1

wareospike

  • New member
  • *
  • Posts: 6
Re: Mastermind code
« Reply #7 on: September 01, 2017, 07:47:35 am »
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]
  1. program MasterMind;
  2.  
  3. var
  4.   { TODO: check and accomplish variable declarations }
  5.   uit2: char;
  6.   uit1: char;
  7.   pogingen: Longint;
  8.   gokD: Longint;
  9.   gokC: Longint;
  10.   gokB: Longint;
  11.   gokA: Longint;
  12.   cijferD: Longint;
  13.   cijferC: Longint;
  14.   cijferB: Longint;
  15.   cijferA: Longint;
  16.  
  17. begin
  18.   RANDOMIZE;
  19.   cijferA := RANDOM(4);
  20.   cijferB := RANDOM(4);
  21.   cijferC := RANDOM(4);
  22.   cijferD := RANDOM(4);
  23.   writeln(cijferA,' ',cijferB,' ',cijferC,' ',cijferD);
  24.   uit1 := 'R';
  25.   uit2 := 'W';
  26.   pogingen := 1;
  27.   writeln('Raad de code. Geef 4 cijfers iedere keer met een spatie.');
  28.   readln(gokA, gokB, gokC, gokD);
  29.   while (cijferA<>gokA) or (cijferB<>gokB) or (cijferC<>gokC) or (cijferD<>gokD) do
  30.   begin
  31.     pogingen := pogingen+1;
  32.     if (cijferA=gokA) then
  33.     begin
  34.       writeln(uit1);
  35.     end
  36.     else
  37.     begin
  38.       if (cijferA=gokB) or (cijferA=gokC) or (cijferA=gokD) then
  39.       begin
  40.         writeln(uit2);
  41.       end;
  42.     end;
  43.     if (cijferB=gokB) then
  44.     begin
  45.       writeln(uit1);
  46.     end
  47.     else
  48.     begin
  49.       if (cijferB=gokA) or (cijferB=gokC) or (cijferB=gokD) then
  50.       begin
  51.         writeln(uit2);
  52.       end;
  53.     end;
  54.     if (cijferC=gokC) then
  55.     begin
  56.       writeln(uit1);
  57.     end
  58.     else
  59.     begin
  60.       if (cijferC=gokA) or (cijferC=gokB) or (cijferC=gokD) then
  61.       begin
  62.         writeln(uit2);
  63.       end;
  64.     end;
  65.     if (cijferD=gokD) then
  66.     begin
  67.       writeln(uit1);
  68.     end
  69.     else
  70.     begin
  71.       if (cijferD=gokA) or (cijferD=gokB) or (cijferD=gokC) then
  72.       begin
  73.         writeln(uit2);
  74.       end;
  75.     end;
  76.     writeln('Raad de code. Geef 4 cijfers iedere keer met een spatie.');
  77.     readln(gokA, gokB, gokC, gokD);
  78.   end;
  79.   writeln('Proficiat u hebt de code juist in', pogingen, ' pogingen.');
  80.   writeln;
  81.   writeln('Druk op <ENTER> om het programma te sluiten.');
  82.   readln();
  83. end.        

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Mastermind code
« Reply #8 on: September 01, 2017, 09:50:43 pm »
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:
Quote
Random 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.
« Last Edit: September 02, 2017, 02:13:03 am by molly »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus