Tutorial 7: Using arrays

In this Wii programming tutorial we will quickly cover how we can utilise arrays for holding information about games like in card games or other games were the positions of objects remains static. You can grab the PDF of this tutorial here: codemii-tutorial-7

An array as you should know can hold multiple elements. By utilising these elements we can arrange them however we like. For example, if we were to do a tic tac toe game, we know there are 9 cells in 3 x 3. We can declare an array with 8 elements and set all those elements to 0 as shown below.

int cells[] =  {0, 0, 0,
				0, 0, 0, 
				0, 0, 0};

So we have our 9 cells, 0 is for empty, 1 could stand for a cross and 2 could stand for a circle. As you can see we haven’t defined how many elements are in the array cells, because we are initialising the array whilst declaring it. Then all we need to do is display the crosses, circles and empty cells. I won’t be providing the code but rather just providing some quick pseudocode.

Pseudocode is a way of writing code but in a briefer level where you don’t need to care about writing the exact code, but rather paraphrasing what it will do.

Something like this will do:

For all elements of the cells array
	If element equals 1, display a cross
	Else if element equals 2, display a circle

Our code could then look something like this:

int x = 0;
for (x = 0; x <= 8; x++) { 
	if (cells[x] == 1) { 
		display_cells (x, 1); 
	}
	else if (cells[x] == 2) { 
		display_cells (x, 2); 
	}
}

It’s up to you how you derive the code from the pseudocode. So we now have some example code, although what I haven’t shown you is the display_cells function.

The display_cells function could take the position in the array which is denoted by x and whether it’s a cross or circle (1 or 2).

Our pseudocode could look like:

Initialise a temporary x and y position
If array position divided by 3 is more or equal to 1, add 100 to y position
	Use that divided figure and multiply by 100 to x position
Else multiply 100 to x position by the array position
If element equals 1, use x and y position and draw a circle
Else if element equals 2, use x and y position and draw a cross

What we are doing above is converting a number from 0 to 9 (the array position) to a certain position on the screen. Say we have the number 5 as the array position; we divide 5 by 3 which is about 1.67. As this figure is more than 1, we add 100 to the y position to move it down to the next row and then multiply the remaining result (2) by 100 so it will end up in the centre of the screen. We then check to see if the element is a 1 or 2 and then display the cross or circle.

The next thing you would have to do is how to select cells with the Wiimote. From our previous tutorials we know how to do this. So we just need to split all 9 cells into buttons. As the cells positions remains the same, we can apply a formula to working out which cell we are clicking on.

It’s similar to the formula above. If we moved our cursor to say 130 x and 160 y, it would fall right in the centre. All we need to do is divide both the x and y by 100 and we have a single digit value with our x and y values which would give us 1 x and 1 y (remember 0 x and 0 y is the top left cell). So then we just need to do y * 3 + x and we have our position in the array which would be 4.

Now all you would need is the graphics, the 3 way cross/circle detection, simple AI, a function to restart the game and you should be set.

18 Responses to “Tutorial 7: Using arrays”

  1. Deozaan says:

    I just found your tutorials tonight and have been going through them all. I feel you’ve jumped too quickly from telling us exactly what steps to take into letting us figure everything out by ourselves from pseudocode.

    This one is really confusing…

  2. Deozaan says:

    Just realized that maybe I should mention: If your purpose was to just introduce arrays, I think it would have been better to explain arrays and give a few examples instead of making us feel like this is actually a Tic-Tac-Toe tutorial.

  3. harry says:

    Personally i think the exact opposite.. These tutorials should focus more on GC and Wii specific subjects and methods. Basic programming tutorials can be found all over the web, but there are no Wii/GC tuts out there.

    So IMHO it would be better to continue explaining pointer / button functionality etc etc. This array tutorial, for me, was just useless and could have been found with ease via Google.

  4. teknecal says:

    I guess this is what happens when I rush things 😛 (didn’t want to not put up a tutorial for a 2nd week). It wasn’t really meant to introduce arrays (should have learnt a little in tutorial 2) but rather show how they can be used, but I agree you could have learnt it elsewhere but I’m trying to apply it to the Wii console.

    To be honest, I’ve kind of run out of things with the Wii that I personally know, except for some basics of grrlib, so am moving on to showing people how to use what we’ve learned.

    I’ll try thinking of other things, anyone have any suggestions?

  5. Rogue says:

    @Deozaan, stop complaining, as harry has said, if you cant understand this then theres tons of tutorials online for basic coding.

    @teknecal man i think uve done a very good job of introducing the core concepts. So thanks :D. It would be cool to maybe move more towards wii specific tutorials.

    Thanks alot!

    p.s. ive only started looking at wii coding(done java and c# before so c is basically the same) about 10 minutes ago(quickly read ur tuts). Could you just explain one thing, technically is it possible to use standard c code for example to connect to a sql database or to ping a host, or will all of this require wii libraries?

  6. Pvt Ryan says:

    As far as I can tell you would at least need the wii network library. But for the rest std c should be fine.

  7. Ether2802 says:

    ok i came up with an idea to make an app, and the source was fine, I mean it makes the .dol but the wii doesn’t do anything, is based on the guitardemo that you press green and move the strum bar and prints on the TV “Green is pressed”, but I want to do the same but with sound, can you help me or it’s just to much information that I would need??

  8. teknecal says:

    So when you press green you want a sound to play?
    I think I might make the next tutorial about playing sounds.

  9. ether2802 says:

    Exactly! thanks again for doing this tutorials

  10. DCelso says:

    How i can create a log file in the wii sd?, Im using lastest cvs libogc and when I use:

    The program crash dropping and fatal error that I must have to reboot the wii.

  11. DCelso says:


    f o p e n(file_id,'/temp/debug.txt','w');

    f p r i n t f(file_id,"hello\n");
    f p r i n t f (file_id,"cells[0]:%d\n",cells[0]);
    f c l o s e(file_id);

  12. DCelso says:

    sorry i have said
    file_id=f o p e n(‘/temp/debug.txt’,’w’);

  13. teknecal says:

    I’m assuming that you have declared file_id as a file. If not it should be:
    FILE *file_id;

    The problem is that you are using single quotes (‘) which is only to be used when you have one character in that string (for example char test = ‘a’;).

    You actually need to use double quotes (“) for strings and when passing arguments in text to functions.
    file_id = fopen(“/temp/debug.txt”,”w”)

  14. DCelso says:

    oh, thanks, but this was because the blog submit failed and I changed it erroneusly.
    waninkoko solved my problem. Actualy the solution to write in sd wii is:

    fatInitDefault ( ) ;
    FILE* file_id = f o p e n(“fat3:/temp/debug.txt”,”w”);
    f p r i n t f(file_id,”hello\n”);
    f p r i n t f (file_id,”cells[0]:%d\n”,cells[0]);
    f c l o s e(file_id)

  15. Lewiis says:

    G’day Teknecal,

    Thank you HEAPS for your tutorials, i’m starting to code a very simple game. What I would really appreciate seeing in a tutorial is how to layer images on top of each other (i’m having trouble with that at the moment, i want a button that when clicked changes colour or from yes -> no.). Sounds would also be nice in a tutorial.

    Once again thank you for your tute’s, i think you are the only person on the net at the moment who has them for the wii and PLEASE continue them

  16. teknecal says:

    To layer images, all you need to do is display the new image either over the top of the other image or have a if statement which can decide which image to display. So you could just have a variable which keeps track of this and then if the user clicks, it sets the variable to say true and then displays the other image.

    This is quite easy and I should be able to do a quick tutorial on it this weekend.

  17. Lewiis says:

    Thanks Tek, that’d be great. I downloaded your Simon program but haven’t had a chance to look through the source yet. It’s good to see some well documented source though and I’m hoping it’ll help me get going again with my game. I’ll hopefully have a beta version released in a few weeks.

  18. sean says:

    Up until this point, these have been among the best “intro to programming” tutorials i’ve found, despite being Wii tutorials.

    I’ve tried to learn C from online tutorials, but up until I found these, every single one i’ve come accross has assumed I already knew a previous programming language – and used lingo that I didn’t “get.” Maybe i’m just bad at searching for tutorials.

    At least I have a basic idea of how C works now, and i can read SOME source codes and tell what is going on, but i’ve got a long way to go.

Leave a Reply