Sign in to follow this  

condensing code

This topic is 2115 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[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] j = 3; j < 6; ++j)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/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)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]board[1, j-3] = 1;[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (board[1, 0] == 1 && board[1, 1] == 1 && board[1, 2] == 1)[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawLine(pn, 0, 125, 200, 125);[/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
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.

[code] 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;[/code]

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
[quote name='alvaro' timestamp='1330228561' post='4916650']
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.
[/quote]

Well man, if you can please post what you have in mind. I'm losing my sleep here ... [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1330438404' post='4917383']
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:
[code]unsigned magic_code[9] = {
0x10010010, 0x10001000, 0x10000101,
0x01010000, 0x01001011, 0x01000100,
0x00110001, 0x00101000, 0x00100110
};[/code]

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:
[code] if (magic_sum[player] & 0x44444444) { /* Victory! */ }[/code]
[/quote]
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.) [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


Link to post
Share on other sites

This topic is 2115 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.

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