Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 13th, 2012, 09:42 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3, pg 68 - Shuffling the Deck of Cards

I've put the code into my xcode program and I'm getting two errors that I can't figure out.

Here is how I have the code typed:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
char* name;
char* suit;
int value;
};

// Seed the random number generator

srandom ( time( NULL ) );

int randomA, randomB;
struct card tempCard;

int i=0;
do {
// Generate 2 random numbers to determine which cards to swap
randomA = random() % 52;
randomB = random() % 52;

if (randomA == randomB)
{
continue;
}

// Swap slots A and B
tempCard = deck[randomA];
deck[randomA] = deck[randomB];
deck[randomB] = tempCard;

// Increment the counter
i++;

}
while (i<1000000);

I get errors on "srandom" telling me Type specifier missing. And also on "do" telling me expecting identifier.

Can anyone help with this?

Thanks!
Reply With Quote
  #2 (permalink)  
Old February 13th, 2012, 10:13 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

It would be helpful to see all of your code, but the question that arises from what you included is "Is your shuffle code as you presented it, inside the main function?" It appears to be after the #include statements but not in main. This will not work unless it is part of a function implementation. Step 3 on page 68 states to put this code in main. The downloadable source code has the shuffle code as a separate function, which is a better approach.
To eliminate the warning in the srandom call you should cast time to unsigned as follows
srandom((unsigned) time(NULL));
also randomA and randomB should be long not int, or type cast
randomA = (int)random() % 52;
randomB = (int)random() % 52;

If this is not clear post your entire main.c
Bob

Last edited by thepianoguy; February 13th, 2012 at 10:17 PM.
Reply With Quote
  #3 (permalink)  
Old February 14th, 2012, 02:10 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Complete copy of code

Ok, I went back and re-did the "TRY IT OUT Shuffling the Deck of Cards" exercise and I still get errors, so I'm posting the entire code I have. Please take and look and see what I'm doing wrong.


//
// main.c
// Cards
//
// Created by on 2/14/12.
// Copyright (c) 2012. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
char* name;
char* suit;
int value;
};



int main (int argc, const char * argv[])
{

struct card deck[] =
{
{"ace", "spades",1}, {"two", "spades",2}, {"three", "spades",3},
{"four", "spades",4}, {"five", "spades",5}, {"six", "spades",6},
{"seven", "spades",7}, {"eight", "spades",8}, {"nine", "spades",9},
{"ten", "spades",10}, {"jack", "spades",11}, {"queen", "spades",12},
{"king", "spades",13},
{"ace", "clubs",1}, {"two", "clubs",2}, {"three", "clubs",3},
{"four", "clubs",4}, {"five", "clubs",5}, {"six", "clubs",6},
{"seven", "clubs",7}, {"eight", "clubs",8}, {"nine", "clubs",9},
{"ten", "clubs",10}, {"jack", "clubs",11}, {"queen", "clubs",12},
{"king", "clubs",13},
{"ace", "hearts",1}, {"two", "hearts",2}, {"three", "hearts",3},
{"four", "hearts",4}, {"five", "hearts",5}, {"six", "hearts",6},
{"seven", "hearts",7}, {"eight", "hearts",8}, {"nine", "hearts",9},
{"ten", "hearts",10}, {"jack", "hearts",11}, {"queen", "hearts",12},
{"king", "hearts",13},
{"ace", "diamonds",1}, {"two", "diamonds",2}, {"three", "diamonds",3},
{"four", "diamonds",4}, {"five", "diamonds",5}, {"six", "diamonds",6},
{"seven", "diamonds",7}, {"eight", "diamonds",8}, {"nine", "diamonds",9},
{"ten", "diamonds",10}, {"jack", "diamonds",11}, {"queen", "diamonds",12},
{"king", "diamonds",13},

};

// Seed the random number generator
srandom( time( NULL ) );

int randomA, randomB;
struct card tempCard;

int i=0;
do {
// Generate 2 random numbers to determine which cards to swap
randomA = random() % 52;
randomB = random() % 52;

if (randomA == randomB)
{
continue;
}

// Swap slots A and B
tempCard = deck[randomA];
deck[randomA] = deck[randomB];
deck[randomB] = tempCard;

// Increment the counter
i++;

}
while (i<1000000);

return 0;
}
Reply With Quote
  #4 (permalink)  
Old February 14th, 2012, 03:21 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

I just put the code you posted into a new project and it compiled and ran with 3 warnings and no errors.

The way to silence the warnings is addressed in my previous post.

I added a print function and it ran fine.

Are you getting errors or warnings? I am assuming that what you pasted into the post is the complete code. I simply copy and pasted it into a new command line tool, changing nothing and aside from the warning everything was fine. If the warnings you are getting are implicit conversion warnings see my other post about how to silence them.

Bob
Reply With Quote
  #5 (permalink)  
Old February 15th, 2012, 09:13 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Appreciate the help

I appreciate the help.

I'm new to the world of iOS and jumped in head first using this book.

It's a little frustrating not knowing all the terminology.

Some pages of the book show what the complete code should look like and some parts show just a snippet. So the placement of the snippets sometimes slows me down if I get them in the wrong places.

I downloaded the code from the website and it basically shows the completed code from the entire chapter.

I'd rather learn it a little at a time as it is presented in the book.
Reply With Quote
  #6 (permalink)  
Old February 15th, 2012, 11:22 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

What is your programming background? The examples in this chapter really have nothing to do with iOS, they are demonstrations using C. iOS frameworks are used in Chapter 2 and in the subsequent chapters. If you are trying to get into iOS programming without a background in Objective-C or a least some C the road is going to be tough. (Speaking from personal experience, trying to dive into Cocoa 10 years ago) This book has introductory chapters on these languages, but if you are finding them difficult to follow I would strongly recommend strengthening your understanding of Objective-C by going through Stephen Kochan's Objective-C 2.0 4th edition. Excellent book and a forum that the author actively follows and contributes to. If worked through diligently including all the exercises (which I strongly recommend) the book can be completed in 1-2 months. You will know Objective-C, object-oriented concepts, Xcode to a certain extent, and the foundation framework. At that point, iOS will be much easier to learn. In the end your path will be easier and ultimately faster, even with the detour.

Any way, the downloads are always valuable to make comparisons to. Keep in mind, sometimes there may be some structural and code discrepancies between the downloads and the book code. They should both be valid, with the exception of uncaught errors, for which hopefully an errata page is kept.

Good luck.

Bob
Reply With Quote
  #7 (permalink)  
Old February 21st, 2012, 10:01 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default wrong output

I'm having a problem with this exercise, here is my code and its output:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
char* name;
char* suit;
int value;
};

int main (int argc, const char * argv[])
{
struct card deck[] =
{
{"ace", "spades",1}, {"two", "spades",2}, {"three", "spades",3},
{"four", "spades",4}, {"five", "spades",5}, {"six", "spades",6},
{"seven", "spades",7}, {"eight", "spades",8}, {"nine", "spades",9},
{"ten", "spades",10}, {"jack", "spades",11}, {"queen", "spades",12},
{"king", "spades",13},
{"ace", "clubs",1}, {"two", "clubs",2}, {"three", "clubs",3},
{"four", "clubs",4}, {"five", "clubs",5}, {"six", "clubs",6},
{"seven", "clubs",7}, {"eight", "clubs",8}, {"nine", "clubs",9},
{"ten", "clubs",10}, {"jack", "clubs",11}, {"queen", "clubs",12},
{"king", "clubs",13},
{"ace", "hearts",1}, {"two", "hearts",2}, {"three", "hearts",3},
{"four", "hearts",4}, {"five", "hearts",5}, {"six", "hearts",6},
{"seven", "hearts",7}, {"eight", "hearts",8}, {"nine", "hearts",9},
{"ten", "hearts",10}, {"jack", "hearts",11}, {"queen", "hearts",12},
{"king", "hearts",13},
{"ace", "diamonds",1}, {"two", "diamonds",2}, {"three", "diamonds",3},
{"four", "diamonds",4}, {"five", "diamonds",5}, {"six", "diamonds",6},
{"seven", "diamonds",7}, {"eight", "diamonds",8}, {"nine", "diamonds",9},
{"ten", "diamonds",10}, {"jack", "diamonds",11}, {"queen", "diamonds",12},
{"king", "diamonds",13},

};

// Seed the random number generator
srandom(time(NULL));

int randomA, randomB;
struct card tempCard;

int i = 0;
do {
//Generate 2 random numbers to determine which cards to swap
randomA = random() % 52;
randomB = random() % 52;

if (randomA == randomB)
{
continue;
}

// Swap slots A and B
tempCard = deck[randomA];
deck[randomA] = deck[randomB];
deck[randomB] = tempCard;

// Increment the counter
i++;
}
while (i<1000000);
printf("The card is %s of %s\n", deck[i].name, deck[i].suit);

return 0;

}

When I run it, here is the output:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
sharedlibrary apply-load-rules all
[Switching to process 732 thread 0x0]
Current language: auto; currently minimal
(gdb)

I don't know what I'm doing wrong, but I'm clearly not getting the randomized deck arrangement that I otherwise should be getting.
Reply With Quote
  #8 (permalink)  
Old February 21st, 2012, 10:25 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

electrcb

Your printf needs to been inside a loop

for (i = 0; i < 52; i++) {

printf("The card is %s of %s\n", deck[i].name, deck[i].suit);
}

Bob
Reply With Quote
  #9 (permalink)  
Old February 21st, 2012, 10:31 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you I understand the error now
Reply With Quote
  #10 (permalink)  
Old March 10th, 2012, 04:42 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had a problem with this as well. I figured it out: I was working according to directions initially testing a 'for' loop. Then changed code to 'while' loop. Then I got to page68 and added a 'do while' loop to existing 'while' loop. I came to the forums for help and learned that I have to add a 'for' loop after the current 'do while' loop. Thank you 'THEPIANOGUY', great help. I hope this "working" code helps next person. I think this is what the book expects us to have to this point.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
char* name;
char* suit;
int value;
};



int main (int argc, const char * argv[])
{

struct card deck[] =
{
{"ace", "spades",1}, {"two", "spades",2}, {"three", "spades",3},
{"four", "spades",4}, {"five", "spades",5}, {"six", "spades",6},
{"seven", "spades",7}, {"eight", "spades",8}, {"nine", "spades",9},
{"ten", "spades",10}, {"jack", "spades",11}, {"queen", "spades",12},
{"king", "spades",13},
{"ace", "clubs",1}, {"two", "clubs",2}, {"three", "clubs",3},
{"four", "clubs",4}, {"five", "clubs",5}, {"six", "clubs",6},
{"seven", "clubs",7}, {"eight", "clubs",8}, {"nine", "clubs",9},
{"ten", "clubs",10}, {"jack", "clubs",11}, {"queen", "clubs",12},
{"king", "clubs",13},
{"ace", "hearts",1}, {"two", "hearts",2}, {"three", "hearts",3},
{"four", "hearts",4}, {"five", "hearts",5}, {"six", "hearts",6},
{"seven", "hearts",7}, {"eight", "hearts",8}, {"nine", "hearts",9},
{"ten", "hearts",10}, {"jack", "hearts",11}, {"queen", "hearts",12},
{"king", "hearts",13},
{"ace", "diamonds",1}, {"two", "diamonds",2}, {"three", "diamonds",3},
{"four", "diamonds",4}, {"five", "diamonds",5}, {"six", "diamonds",6},
{"seven", "diamonds",7}, {"eight", "diamonds",8}, {"nine", "diamonds",9},
{"ten", "diamonds",10}, {"jack", "diamonds",11}, {"queen", "diamonds",12},
{"king", "diamonds",13},

};

// Seed the random number generator
srandom( time( NULL ) );

int randomA, randomB;
struct card tempCard;

int i=0;
do {
// Generate 2 random numbers to determine which cards to swap
randomA = random() % 52;
randomB = random() % 52;

if (randomA == randomB)
{
continue;
}

// Swap slots A and B
tempCard = deck[randomA];
deck[randomA] = deck[randomB];
deck[randomB] = tempCard;

// Increment the counter
i++;

}
while (i<1000000);
for (i = 0; i < 52; i++) {

printf("The card is %s of %s\n", deck[i].name, deck[i].suit);
}

return 0;
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling Edit Errors (page 68) Mossyman BOOK: Professional ASP.NET MVC 2 3 October 11th, 2010 04:33 PM
Card shuffling problem! the Real Newman Java Basics 2 January 30th, 2006 11:57 AM
Chapter 2, pages 68/69 moviesite.php ruairiglenn04 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 August 1st, 2004 05:27 AM



All times are GMT -4. The time now is 09:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.