Wrox Programmer Forums
|
BOOK: Beginning iOS Game Development
This is the forum to discuss the Wrox book Beginning iOS Game Development by Patrick Alessi ; ISBN: 978-1-1181-0732-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS Game Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2012, 08:15 PM
JML JML is offline
Registered User
 
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default MagicMind game - faulty code

I'm pretty convinced that the code in the book for the MagicMind game doesn't work correctly. It compiles all right, but the checkGuess method in MindPlayer.m is faulty. Consider, for example, a rack of Red, Green, Black and Yellow, which is rgky. If the player guesses Black, Green, Black, Yellow, which is kgky, then the checkGuess method returns correctColorAndPosition: 2 and correctColorOnly: 1, which is wrong. The correct answer is correctColorAndPosition: 3, correctColorOnly: 0. The problem is the first 'k' in the guess is matched up via color only with the 'k' in the rack, which is immediately set to 'x', so by the time the loop gets to the second 'k' in the guess, there is no 'k' in the rack anymore.

The code should be amended to first loop through all pegs to check for correct color and position, and then afterwards double-loop through separately to check for correct color only. The book code tries to do this all in one outer loop. Code for the correct implementation of correctGuess is below:

Code:
// Check the guess against the rack
-(NSString*) checkGuess:(NSString*) guess
{
    // Declare two ints to keep track of response criteria
    int correctColorAndPosition = 0;
    int correctColorOnly = 0;
    
    // Declare a string to temporarily hold the guess peg letter
    char tempGuessPeg;
    
    // Build a temporary rack
    char tempRack[4];
    for (int i=0; i<=3; i++)
    {
        tempRack[i] = rack[i].color;
    }
    
    // Loop through each guess peg to check for color and position match
    for (int i=0; i<=3; i++)
    {
        tempGuessPeg = [guess characterAtIndex:i];
        
        // If pegs are identical, increment correctColorAndPosition
        if (tempGuessPeg == tempRack[i])
        {
            correctColorAndPosition++;
            
            // Change the temp rack color to 'x' to indicate
            // that it has already been matched to a guess peg
            tempRack[i] = 'x';
        }
    }
    
    // Loop through each guess peg to check for color only match
    for (int i=0; i<=3; i++)
    {
        tempGuessPeg = [guess characterAtIndex:i];

        for (int j=0; j<=3; j++)
        {
            // Is there a peg this color in the rack?
            if (tempGuessPeg == tempRack[j])
            {
                correctColorOnly++;
                
                // Change the temp rack color to 'x' to indicate
                // that the color has been matched to a guess peg
                tempRack[j] = 'x';
                
                break;
            }
        }
    }
    
    
    // Determine if the user won
    if (correctColorAndPosition == 4)
    {
        return @"win";
    }
    else 
    {
        return [NSString stringWithFormat:
                @"\nCorrect Color And Position: %i \nCorrect Color Only: %i",
                correctColorAndPosition, correctColorOnly];
    }
}
 
Old July 22nd, 2013, 02:15 PM
Registered User
 
Join Date: Jul 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thank you

Thank you! I saw there was a bug and used your code to fix it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch4 MagicMind Glorysaber BOOK: Beginning iOS Game Development 3 March 17th, 2012 01:38 PM
Source code of Mario Game using C (API) phuc_tran C++ Programming 0 May 4th, 2010 10:43 AM
how do i get source code from online game? xzaverax Need help with your homework? 3 October 4th, 2007 12:57 PM
Buggy and Faulty code in Vb sun21170 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 September 18th, 2006 08:59 PM
small C++ game code PLEASE HELP!!! ands122 C++ Programming 2 June 18th, 2005 04:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.