Jump to content



condensing code

  • You cannot reply to this topic
6 replies to this topic

#1 phil67rpg   Members   -  Reputation: 94

Like
0Likes
Like

Posted 25 February 2012 - 05:49 PM

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.

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

Ad:

#2 Cornstalks   Members   -  Reputation: 1217

Like
0Likes
Like

Posted 25 February 2012 - 07:57 PM

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)...
[ Realistic Rendering ] [ School + Dublin = Boom ] [ I've been ninja'd 70 times ] [ f.k.a. MikeTacular ] [ My Blog ]

#3 phil67rpg   Members   -  Reputation: 94

Like
0Likes
Like

Posted 25 February 2012 - 08:01 PM

thanks corn I was thinking about using list instead of arrays

#4 alvaro   Members   -  Reputation: 1698

Like
0Likes
Like

Posted 25 February 2012 - 09:56 PM

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.

#5 Marvel Magnum   Members   -  Reputation: 143

Like
0Likes
Like

Posted 28 February 2012 - 07:03 AM

View Postalvaro, on 25 February 2012 - 09:56 PM, said:

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 ... Posted Image

#6 alvaro   Members   -  Reputation: 1698

Like
1Likes
Like

Posted 28 February 2012 - 08:13 AM

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! */ }


#7 Marvel Magnum   Members   -  Reputation: 143

Like
0Likes
Like

Posted 28 February 2012 - 04:42 PM

View Postalvaro, on 28 February 2012 - 08:13 AM, said:

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.) Posted Image






We are working on generating results for this topic
PARTNERS