• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
black_darkness

Could someone give me feedback on my algorithm?

24 posts in this topic

[quote name='Luis Guimaraes' timestamp='1355439217' post='5010365']
I don't understand much of what you're doing or working with, but it looks like, without changing the logic of how you wanna paint it, you could use a for loops instead of repeating 8,16,24...64 you could make it 8 * i where i = 1,2,3...8
[/quote]

Here is an explanation of the goal.

I have this information for my algorithm to work with.

* current square (or k)
* j is a counter. So it changes from even to odd every turn, letting me know if current square is even or odd. (I could use k to do this and make simpler though I think)
* I have the rects.size() (This is the size of the vector)

I want to use this information to paint a checkerboard pattern. In order to do this the first 8 must have odds painted white, and evens painted red. The second 8 must have odds painted red and evens painted white. This will alternate line by line until I get to the bottom.

I couldn't think of how to do this without manually switching every time k increases by 8.
1

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1355439959' post='5010374']
I think you could immediately shorten that to:
[code]
// This if statement might seem kind of magical
if (((rects.size() / 8) % 2) == 0) // if <= 16, 32, 48, 64
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else // <= 8, 24, 40, 56
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
[/code]
[/quote]
Yeah I kind of want to avoid another solution like this.
[quote name='Cornstalks' timestamp='1355439959' post='5010374']
However, I wouldn't actually recommend the above, and if I were you, I'd do something more like:
[code]
for (int i = 0; i < rects.size(); ++i)
{
if ((i % 2) == 0)
{
rects[i]->set_fill_color(Color::red);
}
else
{
rects[i]->set_fill_color(Color::white);
}
}
[/code]
[/quote]
I tried that at first but since the first square on the left is always even it made a red and white stripe pattern. So that is why I was forced to switch every time k increased by 8.
1

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1355440233' post='5010379']
First step is to obtain the 2-dimensional coordinate of a square. For example, if k is what you call the current square is a 1-dimensional sequential index (for example you number the squares sequentially along a row, and column by column), then you can obtain the [i](x,y)[/i] coordinate as [i]x=k%8[/i] and [i]y=k/8[/i].

Once you have a 2-dimensional coordinate into the checker board, you can color it according to:
[code]
if(x%2 == y%2)
red square
else
white square
[/code]
[/quote]

This seems like it will work thank you. I will try to implement a 2d system.
1

Share this post


Link to post
Share on other sites
[quote name='black_darkness' timestamp='1355440371' post='5010381']
*snip*
I tried that at first but since the first square on the left is always even it made a red and white stripe pattern. So that is why I was forced to switch every time k increased by 8.
[/quote]
Sorry, yeah, I forgot to watch out for that. I've edited that post just barely to fix that and add another alternative way of doing it.

[b]Edit:[/b] Holy ninja'ing fest... We're all replying at once ha Edited by Cornstalks
2

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1355439959' post='5010374']

[/quote]

Hey Cornstalks I wrote a test and it works but It is switching 1 square too soon.

[img]http://s10.postimage.org/t6xdarce1/test_1.jpg[/img]

Here is the test I wrote.
[CODE]
#include <iostream>
int main()
{
using namespace std;
int ooo_test = (1+1/8) % 2;
cout<<"square 1 is: "<<ooo_test<<"\n";
ooo_test = (2+2/8) % 2;
cout<<"square 2 is: "<<ooo_test<<"\n";
ooo_test = (3+3/8) % 2;
cout<<"square 3 is: "<<ooo_test<<"\n";
ooo_test = (4+4/8) % 2;
cout<<"square 4 is: "<<ooo_test<<"\n";
ooo_test = (5+6/8) % 2;
cout<<"square 5 is: "<<ooo_test<<"\n";
ooo_test = (6+6/8) % 2;
cout<<"square 6 is: "<<ooo_test<<"\n";
ooo_test = (7+7/8) % 2;
cout<<"square 7 is: "<<ooo_test<<"\n";
ooo_test = (8+8/8) % 2;
cout<<"square 8 is: "<<ooo_test<<"\n";
cin>>ooo_test;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1355442443' post='5010394']
In C++, indices start at zero, not one, so your first one should be [font=courier new,courier,monospace]0 + 0 / 8[/font], your second [font=courier new,courier,monospace]1 + 1 / 8[/font], third [font=courier new,courier,monospace]2 + 2 / 8[/font], etc.
[/quote]

Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?

anyways look how elegant you made it look. I am very pleased.

[CODE]
if((k+k/8)%2 == 0) {
(*rects[(k)]).set_fill_color(Color::red);
}
else if((k+k/8)%2 == 1) {
(*rects[(k)]).set_fill_color(Color::white);
}
k++;
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1355442542' post='5010396']
[quote name='black_darkness' timestamp='1355441798' post='5010392']Hey Cornstalks I wrote a test and it works but It is switching 1 square too soon.[/quote]
The test actually works, but you are misinterpreting the results. Square 1 is the second square of the first row, and square 8 is the first square of the second row. Both square 1 and square 8 should be red. Square 0, the first square of the first row, on the other hand, should be (and is) white.
[/quote]


I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.

[CODE]
#include <iostream>;
int main()
{
using namespace std;
for(int i=0;i<5;i++)
{
cout<<i<<"\n";
}
return 0;
}
[/CODE]

and this was the output.
[img]http://s13.postimage.org/6e6geq5tj/Untitled.png[/img]
1

Share this post


Link to post
Share on other sites
Well then at least remove the "!" by switching the colors around [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]

[CODE]
rects[k]->set_fill_color((k+k/8)%2 ? Color::white : Color::red);
[/CODE]
2

Share this post


Link to post
Share on other sites
[quote name='black_darkness' timestamp='1355442771' post='5010398']
Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?
[/quote]
Yes, the formal name is the Cornstalks Algorithm [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
Just kidding, there isn't a formal name for it. It might be possible to generalize it to a larger, more complex algorithm that has a formal name, but I just came up with this off the top of my head. Years of programming will do that to you...

[quote name='black_darkness' timestamp='1355443264' post='5010401']
I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.
[/quote]
I'll break down a for loop like this:
[code]
for (A; B; C)
D;
[/code]
Step 1: A is done (A is usually creating and setting a variable, like [font=courier new,courier,monospace]int [/font][font=courier new,courier,monospace]i = 0[/font]). Step 2: B is checked (B is the looping condition, and as long as it's true the loop is run). Step 3: D is run. Step 4: C is run (which is usually what updates the loop counter). Then it goes back to step 2 and repeats until B is false.

[quote name='Stroppy Katamari' timestamp='1355450276' post='5010430']
Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code.
[/quote]
One liner? (Added the loop [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img])
[code]
// This:
for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) rects[x + y * 8]->set_fill_color(((x + y) % 2) ? Color::white : Color::red);

// Or:
for (int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color(((i + i / 8) % 2) ? Color::white : Color::red);

// But seriously, I hope no one ever does this in real life. Use *at least* two lines...
[/code] Edited by Cornstalks
2

Share this post


Link to post
Share on other sites
To test for odd vs even don't use modulation. Just binary-and with 1.

0 & 1 = 0
1 & 1 = 1
2 & 1 = 0
3 & 1 = 1
4 & 1 = 0
5 & 1 = 1 Edited by Khatharr
2

Share this post


Link to post
Share on other sites
This is true. Personally I [i]always[/i] use parens to avoid ambiguity and in this case since you're feeding it to a ? operator you can just swap the result order and drop the explicit comparison to zero.
0

Share this post


Link to post
Share on other sites
Lol @ the article.

[source lang="cpp"]for(int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color((i & 1) ? Color::white : Color::red);[/source] Edited by Khatharr
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0