Jun 12, 2013

Cow and Bull In Salesforce - Let's Brainstorm

Hope may of you are aware of the game Cow & Bull, but those who are not here are the rules

1) Computer will generate a 4 digit number which you need to guess in less than 10 chances
2) This number will not start from 0
3) All digits of the number will be unique (no digit will repeat)

For example:

1234 - is correct
0234 - is wrong
3445 - is wrong

Now user will input a 4 digit number which doesn't starts with 0 and all digits are unique. Computer will then compare with it's generated number and tell you the count of cows and bulls.

What is Cow and Bull

1) Cow: When digits of your number matches digits of computer's generated number on same index

For example: Computer generated > 4580

Your guess > 4619
Count of cows will be one as only one digit matches on same index.

If your guess is > 4689
Then count of cows will be 2 as "4" and "8" both matches at same index

2) Bull: When digit of your number matches digits of computer's generated number but not on same index

For example: Computer generated > 4580

Your guess > 6419
Count of bulls will be one as only one digit ("4") matches (but on same index).

If your guess is > 4689
Then count of bulls will be 2 as "4" and "8" both matches (but on same index)

So if you guessed 4851, then there is ONE cow ("4") and TWO bulls ("8" and "5")



Now it's time to use the brain, here is the site link: http://bmprojects-developer-edition.ap1.force.com/CB_CowAndBull


VFP:


<apex:page controller="CB_CowAndBullController" showHeader="false" sidebar="false">
    <apex:form id="FRM">
        <apex:pageBlock id="PB" title="Cow And Bull">
            
            <apex:pageMessage severity="INFO" escape="false">
                1) Enter 4 digit number which have all digits unique and is not starting from 0 <br/><br/>
                2) Cows: Digits from your number matches computer's number at same index <br/>
                e.g: You Enter -> 1234 and Computer Number -> 1960 <br/>
                1 matches on same index so it will be counted as cow <br/><br/>
                3) Bulls: Digits from your number matches computer's number but not on same index <br/>
                e.g: You Enter -> 1234 and Computer Number -> 9120 <br/>
                1 and 2 matches but not on same index so it will be counted as bull
            </apex:pageMessage>
            <apex:pageMessages id="PM"/>
            
            <apex:pageBlockButtons rendered="{!IF(showButton , true , false)}">
                <apex:commandButton value="Check C&B" action="{!CheckCandB}"/>
            </apex:pageBlockButtons>
            
            <!-- <apex:outputText value="Computer Generated Number : {!CompNumber}"/> -->
            
            <apex:pageBlockSection id="PBS1">
                <apex:pageBlockSectionItem id="PBSI1">
                    <apex:outputLabel value="Please Enter Your Number:"/>
                    <apex:inputText size="4" value="{!userEnteredNUmber}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            
            <apex:pageBlockTable rendered="{!IF(toDisplayWrapperLst.size > 0 , true, false)}" value="{!toDisplayWrapperLst}" var="wrap">
                <apex:column headerValue="Your Input" value="{!wrap.userInput}"/>
                <apex:column headerValue="Cows" value="{!wrap.cow}"/>
                <apex:column headerValue="Bulls" value="{!wrap.bull}"/>
            </apex:pageBlockTable>
        
        </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Code:


public class CB_CowAndBullController
{
    public Integer CompNumber {get; set;}
    public Integer userEnteredNUmber {get; set;}
    //To maintain the user inputs
    public List<userInputResults> toDisplayWrapperLst {get; set;}
    //Flag to show the button which compare the user input with computer's generated number
    public boolean showButton {get; set;}
    
    public CB_CowAndBullController()
    {
        //Number entered by user
        userEnteredNUmber = 0 ;
        
        showButton = true ;
        
        toDisplayWrapperLst = new List<userInputResults>() ;
        
        //Generating 4 digit number which doesn't start with 0 and no number is repeated
        boolean flag = false ;
        while(!flag)
        {
            Integer tempVar = Integer.valueOf(Math.Random() * 10000) ;
            if(checkNumber(tempVar))
            {
                flag = true ;
                CompNumber = tempVar ;
            }
        }
    }
    
    private Boolean checkNumber(Integer num)
    {
        //Pattern for 4 digit number which doesn't start with 0 and no number is repeated
        Pattern nonZeroPattern = Pattern.compile('([1-9])(?!.*\\1)([0-9])(?!.*\\2)([0-9])(?!.*\\3)([0-9])(?!.*\\4)');
        Matcher nonZeroMatcher = nonZeroPattern.matcher(''+num);
        if(nonZeroMatcher.matches())
             return true;
        else
            return false ;
    }
    
    public PageReference CheckCandB()
    {
        if(checknumber(userEnteredNUmber))
        {
            //Logic to compare user input and computer's generated number
            Integer cow = 0 ;
            Integer bull = 0 ;
            String UEN = string.valueOf(userEnteredNUmber) ;
            String CGN = string.valueOf(CompNumber) ;
            
            for(Integer i = 0 ; i < UEN.length() ; i++)
            {   
                for(Integer j = 0 ; j < CGN.length() ; j++)
                {
                    if(UEN.subString(i , i+1) == CGN.subString(j , j+1))
                    {
                        if(i == j)
                            cow = cow + 1 ;
                        else
                            bull = bull + 1 ;
                    }
                }
            }
            toDisplayWrapperLst.add(new userInputResults(cow, bull, userEnteredNUmber)) ;

            if(cow == 4)
            {
                showButton = false ;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Congratulations! You guessed it right in ' + toDisplayWrapperLst.size() + ' chances.'));
            }
            else
            {
                if(toDisplayWrapperLst.size() > 9)
                {
                    showButton = false ;
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'You failed to guess the number in 10 chances. Better luck next time.'));
                }
            }
        }
        else
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a 4 digit number which does not start with 0 and no number is repeated' ));
        }
        return null ;
    }
    
    //Wrapper to maintain number of cows, bulls and user input
    public class userInputResults
    {
        public Integer cow {get; set;}
        public Integer bull {get; set;}
        public Integer userInput {get; set;}
        
        public userInputResults(Integer c, Integer b, Integer ui)
        {
            cow = c ;
            bull = b;
            userInput = ui ;
        }
    }
}


Would love to hear the feedback.

Apr 30, 2013

How Can I Become A MVP (Force.com)

Recently my MVP title is renewed (Extra! Announcing our newest and renewed Force.com MVPs) and this question bothered many from time to time "How can I become a MVP"? 

Don't focus on how to become a MVP, focus only on the motive behind the program. It is stated that "Force.com MVP program recognizes outstanding contributors and technological leaders in the Force.com cloud platform ecosystems. Force.com MVPs are being called out for willingly sharing their expertise with others, demonstrating stewardship of the community in which they play an integral part, advancing the community body of knowledge and strengthening the developer network." 

Now there are multiple ways to do this, let's talk about some of them: 

1) Contribution via discussion boards, which is the heart of community. We've multiple discussion boards where you can share your knowledge with other community members.


2) Evangelism of the technology through independent publication such as blogs/whitepapers/FB Page 

3) Contribution to open sources. If you've ever thought about sharing some code samples or some big projects so don't let it be restricted to only your personal blogs. Make them live here:


4) Get Social and stay connected with other peers on social sites like

  • Twitter : Follow official accounts @forcedotcom @salesforce and use #askforce if you need any help or you want to help the community
  • Facebook : Like the official page and stay connected with the latest news and events
  • LinkedIn etc.

5) Participate as much as you can via attending live webinars (you can get the information from social sites or here), online events (where you get chance to win some cool stuff), online challenges like this 

6) Participate in local developer user group meetings. You can get the list of all developer user groups here.

7) Participate in big events like Dreamforce, Cloudstock etc.

List never ends and it's benefits too, like by doing all this

  • Your knowledge will be increased.
  • Your networking will be strong
  • Will be acknowledged globally
  • Significant increase in offers from companies
  • Domain will not be restricted any more
  • After actively participating in events your wardrobe will be full of some cool stuff like Jackets, T-Shirts, Caps, Bags, Trophies, Books, Hoodies


Above benefits are for sure, and if you are entitled as MVP then there are some additional benefits to above:

  • You'll get sponsored trips to SF for events like MVP Summit
  • You'll get free passes to Dreamforce
  • Highlighted community profile, (here is mine)
  • Early access to feature previews and releases, with direct feedback to the PMs on these products
  • Access to chatter groups where all MVPs jam together



I don't know where to stop...so now you have the links and I hope you know what to do. GOOD LUCK!

Make our community rock!!


Mar 16, 2013

Jaipur DUG Cloud Trivia Winners

Yes! It's true, winners list of Jaipur DUG Cloud Trivia is out.
Thank you all who participated. After getting such an awesome response, I hope I'll be able to arrange some more contest like this. It is just to motivate you guys to stay updated with the latest release, and I must say you guys did a great job.

Here is the winners list :-



Amit Jain
Deepali Jain
Pankaj Kabra
Er Deepak Sheoran
Kumawat Deshraj
Jinesh Goyal
Ranu Jain
Mohit Chawla
Ashish Agarwal






Please drop me an email on "ankit.salesforce@gmail.com" so we can co-ordinate how you can collect your prize (T-Shirt/Workbook/CheatSheets/Salesforce Touch Platform Book).

Stay connected with us to know info about more contest like this :-
3) Meetup