How to Develop an iPad Board Game App: Part 1/2

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

How to Develop an iPad Board Game App: Part 1/2

Postby rwenderlich » Tue Feb 19, 2013 11:00 am

This is the official thread to discuss the following blog post: How to Develop an iPad Board Game App: Part 1/2
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Site Admin
 
Posts: 2268
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 382 times

Re: How to Develop an iPad Board Game App: Part 1/2

Postby wdflu » Tue Feb 19, 2013 6:28 pm

Finally! I've been waiting for this tutorial for a long time! :D

Thanks a lot!
wdflu
Hacker
 
Posts: 10
Joined: Sun Nov 27, 2011 12:06 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How to Develop an iPad Board Game App: Part 1/2

Postby PenguinGab » Sat Feb 23, 2013 5:26 am

Very nice! I'm sorry, but could you elaborate more on how the navigation functions work, and more about the moveSurroundsCountersForColumn: method? My C-programming kung-fu is weak as I came to Objective-C from a Ruby background, so I don't really fully comprehend typedefs and blocks.. But fantastic tutorial anyway! Learnt loads. By the way I'm @sg_gabriel here

------
Get your app on the App Store in the shortest time. No designers needed.
Beautiful iPhone design templates with reusable sliced image assets and PSD files you can customize. Focus on your coding, leave the design to us.
http://www.getappninja.com
Twitter: https://twitter.com/sg_gabriel
User avatar
PenguinGab
Baby Hacker
 
Posts: 7
Joined: Sat Apr 07, 2012 7:43 pm
Has thanked: 0 time
Been thanked: 1 time

Re: How to Develop an iPad Board Game App: Part 1/2

Postby ColinEberhardt » Sat Feb 23, 2013 7:47 am

PenguinGab wrote:Very nice! I'm sorry, but could you elaborate more on how the navigation functions work, and more about the moveSurroundsCountersForColumn: method? My C-programming kung-fu is weak as I came to Objective-C from a Ruby background, so I don't really fully comprehend typedefs and blocks.. But fantastic tutorial anyway! Learnt loads. By the way I'm @sg_gabriel here


Let's see if I can make it a bit more clear.

Blocks are an Obj-C language feature that allow you to define distinct segments of code that can be passed around just like variables. A block has a 'signature'just like any other method, which details its return value and any arguments that are passed to it. The typedef is simply used to create an alias for a particular signature. This is all that typedefs do - alias things.

The moveSurroundsCountersForColumn takes a BoardNavigationFunction, which is a block. In other words, you can pass a segment of code to this method which dictates how it moves from one cell to the next. I practice, this is one of the 8 navigation functions declared at the top of the file.

Within moveSurroundsCountersForColumn the passed BoardNavigationFunction is invoked on each iteration. As an example, let's say BoardNavigationFunctionRight was passed to this function. When invoked, this function will increment the value of the column variable that was passed to it. This results in one move to the function moving one cell to the right. Note, this also makes use of C-style programming where the address of a variable is passed then de-referenced in order to increment / decrement it. You can probably ignore this!

I hope this makes sense. If I were you I would read a few tutorials and just give blocks a go!

Colin E.
User avatar
ColinEberhardt
iOS Tutorial Team Member
 
Posts: 141
Joined: Sat Nov 03, 2012 2:17 pm
Has thanked: 10 times
Been thanked: 21 times

Re: How to Develop an iPad Board Game App: Part 1/2

Postby nafu » Tue Feb 26, 2013 10:18 am

Thank you for the very good tutorial!!!

I have a question on 'moveSurroundsCountersForColumn' unit testing.
I have no idea how I can ensure that I have full branch coverage.
Is there any suggestions how to think about algorithms?
nafu
n00b
 
Posts: 1
Joined: Tue Feb 26, 2013 10:06 am
Has thanked: 0 time
Been thanked: 0 time

Re: How to Develop an iPad Board Game App: Part 1/2

Postby ColinEberhardt » Tue Feb 26, 2013 9:54 pm

nafu wrote:I have a question on 'moveSurroundsCountersForColumn' unit testing.
I have no idea how I can ensure that I have full branch coverage.
Is there any suggestions how to think about algorithms?


Good question! in the absence of dedicated code coverage tools, this basically just takes discipline. In the past I have added comments in my code that indicate the various code paths then added unit test that refer to each of these. This makes it easier to audit your code visually in order to test for coverage.

However, this is pretty tedious!

If you really care about getting full coverage of branches or conditions, you are going to need tools. I've used code coverage tools within Eclipse and Visual Studio before - ones the visually highlight the code that has been covered as well as report metrics are the best.

I haven't tried this in Xcode yet, but it does appear that it is possible:

http://www.infinite-loop.dk/blog/2011/0 ... it-all-up/

Regards, Colin E.
User avatar
ColinEberhardt
iOS Tutorial Team Member
 
Posts: 141
Joined: Sat Nov 03, 2012 2:17 pm
Has thanked: 10 times
Been thanked: 21 times

Re: How to Develop an iPad Board Game App: Part 1/2

Postby jieuryli » Thu Feb 28, 2013 3:15 pm

I think you have the "r" variable increment/decrement for BoardNavigationFunctionLeftUp and BoardNavigationFunctionLeftDown reversed. For BoardNavigationFunctionLeftUp, shouldn't it be (*r)--, and BoardNavigationFunctionLeftDown (*r)++? As in...

Code: Select all
BoardNavigationFunction BoardNavigationFunctionLeftUp = ^(NSInteger* c, NSInteger* r) {
    (*c)--;
    (*r)--;
};
 
BoardNavigationFunction BoardNavigationFunctionLeftDown = ^(NSInteger* c, NSInteger* r) {
    (*c)--;
    (*r)++;
};
jieuryli
n00b
 
Posts: 1
Joined: Sat Dec 31, 2011 4:19 am
Has thanked: 0 time
Been thanked: 1 time

Re: How to Develop an iPad Board Game App: Part 1/2

Postby zenwar » Thu Mar 07, 2013 3:05 am

First off: great tutorial.

I have a question about your use of blocks.

I tried changing the block signature from NSInteger to int, so that rather than defining it as you did:

Code: Select all
 BoardNavigationFunction BoardNavigationFunctionRight = ^(NSInteger* c, NSInteger* r) {
    (*c)++;
};


I did this:

Code: Select all
BoardNavigationFunction BoardNavigationFunctionRight = ^(int c, int r) {
    c++;
};


I changed the typedef accordingly, got everything to compile and run and found that it did not work. The columns and rows do not get altered as they did with the objects. I am still new to blocks, so I wanted to know if this was a scoping issue? It actually seems to me that the int behavior makes more sense since the values passed in are supposed to be copied into the block and should not change the originals. Are you circumventing that behavior in your code by the deference?

What am I missing?

Thanks.

P.S. I ran some coding tests. It looks like this has nothing to do with primitives vs. objects but rather about the fact that you are doing a deference in the block. That is what allows you to change the values of column and row. This seems to go against the idea that blocks have their own protected scope -- on the other hand it does work...:) I guess technically you are not changing the value of the parameters, they always point to the same thing, you just change the value that is being pointed at. So I think I have answered my own question.

Thanks again!
zenwar
n00b
 
Posts: 1
Joined: Thu Mar 07, 2013 2:55 am
Has thanked: 0 time
Been thanked: 0 time

Re: How to Develop an iPad Board Game App: Part 1/2

Postby ColinEberhardt » Fri Mar 08, 2013 9:42 am

jieuryli wrote:I think you have the "r" variable increment/decrement for BoardNavigationFunctionLeftUp and BoardNavigationFunctionLeftDown reversed.


Ooops - well spotted! Fortunately this doesn't actually have an impact on the gameplay.

Colin E.
User avatar
ColinEberhardt
iOS Tutorial Team Member
 
Posts: 141
Joined: Sat Nov 03, 2012 2:17 pm
Has thanked: 10 times
Been thanked: 21 times

Re: How to Develop an iPad Board Game App: Part 1/2

Postby ColinEberhardt » Fri Mar 08, 2013 9:46 am

zenwar wrote:I tried changing the block signature from NSInteger to int


NSInteger and int are interchangeable, however ... you have swapped an NSInteger* for int - the first (NSInteger*) is a pointer to an integer, whereas the second (int) is an integer.

Primitive / value types are passed by value rather than passed by reference. In order to allow the block to update the rows / columns, you have to pass a pointer to the value, which is then de-referenced and updated.

Hope that helps!
Colin E.
User avatar
ColinEberhardt
iOS Tutorial Team Member
 
Posts: 141
Joined: Sat Nov 03, 2012 2:17 pm
Has thanked: 10 times
Been thanked: 21 times

Next

Return to Official Tutorials

Who is online

Users browsing this forum: Bing [Bot] and 5 guests