FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Mastermind tutorial

 
Post new topic   Reply to topic     Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
DonAman



Joined: 27 Nov 2011
Posts: 4

PostPosted: Thu Jan 26, 2012 9:29 am    Post subject: Mastermind tutorial Reply with quote

Here is a small tutorial to develop a 'Mastermind' game in D language.

To do this, we will use the Phobos library.

D is an object-oriented language, so we create a class 'Mastermind' that
will contain all the necessary to model the game:

mastermind class's source code :

Code:
import std.stdio;
import std.random;
import std.outbuffer;

/**
*   Class which contains all the methods to model a mastermind game.
*/
class Mastermind {
private:
   
   /**
    *    Used to define the length of the secret code.
    */
   uint _width;
      
   /**
    *   Used to define the amount of tries allowed to crack the code.
    */
   uint _height;
      
   /**
    *   The range of available values in the code.
    */
   uint _range;
      
   /**
    *   Contains the secret code.
    */
   int _code[];
      
   /**
    *   Contains the current try code.
    */
   int _try[];
      
public:
   /**
    *   Default constructor.
    */
   this() {
      _width = 0;
      _height = 0;
      _range = 0;
      _code[]=null;
   }
      
   /**
    *   Initialize the mastermind with the given width, height and range.
    */
   this( uint width, uint height, uint range ) {
      _width = width;
      _height = height;
      _range = range;
      _code = new int[_width];
      _try = new int[_width];
   }
      
   /**
    *   Generates a random secret code for the mastermind game
    */
   void secretGen(){
      for( uint i=0; i < _width; i++ ){
    _code[i] = ( rand() )%_range;/**gives a random int between 0 and _range-1*/
      }
   }
         
   /**
    *   Display the current try of the player.
    *   Returns an outBuffer which contains the code.
    */   
   OutBuffer display() {
      OutBuffer buffer = new OutBuffer();
      /** a specific out buffer where we write the try code.*/
      foreach ( int x; _try ){/**x are the values in the try code.*/
    buffer.printf("%d ", x);/**We can do a printf like in C in a outbuffer.*/
      }
      return buffer;
   }
      
   /**
    *   Display the secret code of the player.
    */
   void displaySecret() {
      foreach ( int x; _code ){
    writef("%d ", x);/**Directly write in out stream.*/
      }
      writefln("\n");
   }
      
   /**
    *   Copy the values in the board oneTry[] given by the player in the try board.
    */
   void giveTry( int oneTry[] ) {      
      foreach (int i, int x; oneTry){
    /**i is the index of the case in the board and x is its value.*/
    _try[i] = x;
      }
   }
      
   /**
    *   Check if the player won. Return true if it is the case.
    */
   bool winner() {
      foreach (int i,int x; _try) {
    if( x != _code[i] ) {/**Tests if the current try matches with the secret code.*/
       return false;
    }
      }
      return true;
   }
      
   /**
    *   The algorithm count how many numbers are in the right place in the try code
    *   and how many numbers are at the wrong place.
    *   Returns the results in an outBuffer.
    */
   OutBuffer giveStatus() {
      int good=0;/**the amount of numbers right placed.*/
      int bad=0;/**the amount of numbers wrong placed.*/
      int[] tmpTry = _try.dup;/**performs a temporary copy.*/
      int[] tmpCode = _code.dup;
      OutBuffer buffer = new OutBuffer();
      foreach ( int i,int x; tmpTry) {
    if( x == tmpCode[i] ){
       good++;
       /**
        *   When a value is checked as a right value, we set this value
        *   in the 2 temporary boards on -1 to avoid conflicts with other values.
        */
       tmpCode[i] = -1;
       tmpTry[i] = -1;
    }
      }
      foreach ( int i,int x; tmpTry) {
    if( x > 0 ){
       foreach( int j, int y; tmpCode ) {
          /** Here we search a match with the values in the secret board.*/
          if( y>0 && x==y ) {
        bad++;
        tmpCode[j] = -1;
        tmpTry[i] =-1;
          }
       }
    }
      }
      buffer.printf( "   Right placed: %d Wrong placed: %d\n", good, bad );
      return buffer;
   }
}

And here is the main class code :

import std.stdio;
import Mastermind;
import std.outbuffer;

/**
*   The main function which runs the mastermind game.
*/
int main(){
   uint width = 0;
   uint height = 0;
   uint range = 0;
   int[] code;
   OutBuffer buffer = new OutBuffer();
   writefln("Welcome to the Mastermind game");
   /**
    *   We ask the player the parameters ha want for the game.
    */
   writefln("What is the length of the secret code you want to crack?");
   scanf("%d",&width);
   code = new int[width];
   writefln("What is the amount of tries allowed to crack the code?");
   scanf("%d",&height);
   writefln("What is the range of possible values in the code?");
   scanf("%d",&range);
   Mastermind m = new Mastermind( width, height, range );/**Creation of mastermind*/
   m.secretGen();/**Secret code generation.*/
   writefln( "Let's play" );
   for( int j=height; j>0; j--){
      writefln( "%d tries left", j);
      /**
       *   we ask the player a guess of the secret code.
       */
      writefln("Please enter your code (between 0 and %d)", (range-1));
      for( int i=0; i<width; i++ ){
       scanf( "%d",code.ptr + i );
      }
      m.giveTry( code );/**copy of the try code.*/
      buffer.write( m.display() );/**Displays the try code.*/
      buffer.write( m.giveStatus() );/**Give the status of the try code.*/
      writefln( "---------------------" );
      writefln( "%s", buffer );/**The game status.*/
      writefln( "---------------------" );
      if( m.winner() ){/**Check if the player won.*/
    m.displaySecret();
    writefln("You won");
    return 0;
      }
   }
   /**
    *   If the amount of tries is 0 the player lost.
    */
   writefln( "You lost");
   m.displaySecret();
   return 0;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group