Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

condensing code

This topic is 2330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have done research on condensing code, all I found was that I could use a List statement instead of using if statements.I am using c#.there has to be a way to condense the following code.

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] j = 3; j < 6; ++j)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (pX >= squares[j].X && pX <= squares[j].Y && pY >= squares[j].Width && pY <= squares[j].Height)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[1, j-3] = 1;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (board[1, 0] == 1 && board[1, 1] == 1 && board[1, 2] == 1)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 125, 200, 125);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
Not much... it depends exactly on what you're doing, but it's not going to get a lot simpler than that. If squares had a contains() method where you could pass it a point and it returns true or false, that would make the if statement a little shorter...

By the way, why are you comparing pX to squares[j].Y? And why are you comparing pY to squares[j].Width? Normally, (X goes with X and width) and (Y goes with Y and height)...

Share this post


Link to post
Share on other sites
It looks like you are trying to determine which square has been clicked on, or something like that. You can simply take the coordinates of the click, subtract that coordinates of the top left corner, divide by the square size and round down.

int board_x = mouse_x - board_min_x;
int board_y = mouse_y - board_min_y;

int square_x = board_x / square_size;
int square_y = board_y / square_size;


The logic to detect three in a row in the game of tic-tac-toe can be made unbelievably compact with a clever bit trick, but it's sort of confusing for beginners so I won't post it.

Share this post


Link to post
Share on other sites

The logic to detect three in a row in the game of tic-tac-toe can be made unbelievably compact with a clever bit trick, but it's sort of confusing for beginners so I won't post it.


Well man, if you can please post what you have in mind. I'm losing my sleep here ... tongue.png

Share this post


Link to post
Share on other sites

The idea is to keep one magic sum per player. Every time a player moves, you add a magic number that encodes the square that was just played, according to this table:
unsigned magic_code[9] = {
0x10010010, 0x10001000, 0x10000101,
0x01010000, 0x01001011, 0x01000100,
0x00110001, 0x00101000, 0x00100110
};


Each four bits (one hex digit) encode one way of winning, and the magic number for a square is just indicating which ways of winning that square participates in. When one of the hex digits reaches the value 3, we know that the player has won. As one final little twist, you can initialize the sums to 0x11111111 instead of 0, and then the victory condition is making a hex digit reach 4, which can be detected like this:
if (magic_sum[player] & 0x44444444) { /* Victory! */ }

Un-f***ing-believeable, brother! This is a piece of code I want to hang on my wall. You have my respect.... (but you had that already anyways.) biggrin.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!