* * *

Author Topic: Mastermind code  (Read 1131 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: 4617
Re: Mastermind code
« Reply #1 on: August 29, 2017, 11:02:40 am »
Try the randomfrom function in math....
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

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: 2388
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: 1949
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: 561
Re: Mastermind code
« Reply #6 on: September 01, 2017, 06:53:46 am »
Guess My Number
MY FIRST GAME EVER...  :)

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

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: 1949
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