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

February 13th, 2012, 10:42 AM
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|

February 13th, 2012, 11:13 PM
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
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 11:17 PM..
|

February 14th, 2012, 03:10 PM
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
|

February 14th, 2012, 04:21 PM
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
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
|

February 15th, 2012, 10:13 AM
|
Registered User
|
|
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 15th, 2012, 12:22 PM
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
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
|

February 21st, 2012, 11:01 PM
|
Registered User
|
|
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 21st, 2012, 11:25 PM
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
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
|

February 21st, 2012, 11:31 PM
|
Registered User
|
|
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you I understand the error now
|

March 10th, 2012, 05:42 PM
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
|
|
 |