EvilZone
Programming and Scripting => C - C++ => : Code.Illusionist December 04, 2013, 03:52:38 PM
-
I have task to calculate how many combinations there are to set on chess board two rooks in way they don't att each other. So I solved that part with this loop:
for(int i= 0; i <7; i++)
for(int j = 0; j < 8; j++) {
if(i==0) rezultat += 49;
if(i==1) rezultat += 42;
if(i==2) rezultat += 35;
if(i==3) rezultat += 28;
if(i==4) rezultat += 21;
if(i==5) rezultat += 14;
if(i==6) rezultat += 7;
}
parametar = rezultat ;
But, not I'd like to make graphic preview of all combinations (1568 in total) . For example I'd like first image to be like this:
(http://www.dodaj.rs/f/1V/u3/26Mt9380/picture-1.png)
Second image would be:
(http://www.dodaj.rs/f/30/8x/3pcP9qwj/picture-2.png)
As you see the images will move that rook to the right until he reach last field, then he will move to next line and to all that again and again till he reach last bottom field in the right. That's totaly 49 pictures. After that, first rook will move one step to right and then second rook will be set in first column and repeat all this moving to right avoiding second column (because there is another rook) . After first rook reach end of row, he will move to second row and everything will be totaly same, same moving etc. How can I make that graphicly with console application? I was thinking a lot about it but only way I could think of is tooooo many if statements. Is there any shorter way?
-
Are you sure there are only 1568 combination to place two rocks(?). Are the rocks "different/unique", as in: Is rock A on position 1 and rock B on position 2 the same as rock A on position 2 and rock B on position 1? If so you have to do something like this:
[gist]anonymous/7789257[/gist]
Also for the "graphical" preview, I would go ASCII art like this:
+----+----+----+----+----+----+----+----+
| | A | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | B | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
| | | | | | | | |
+----+----+----+----+----+----+----+----+
-
They are same rooks. Not black and white, but both white or just black. And what you made there is one amazing graphical drawing, just problem is I don't know how to fill it with loops. It would be crazy to draw each chess board for so much combinations. :D
-
They are same rooks. Not black and white, but both white or just black. And what you made there is one amazing graphical drawing, just problem is I don't know how to fill it with loops. It would be crazy to draw each chess board for so much combinations. :D
You could do something like this:
[gist]anonymous/7790285[/gist]
EDIT: Eheh, clearly I have been doing too much PHP lately :P Change "function" to void or something.
-
You could do something like this:
[gist]anonymous/7790285[/gist]
EDIT: Eheh, clearly I have been doing too much PHP lately :P Change "function" to void or something.
(http://weknowmemes.com/wp-content/uploads/2012/02/dude-what-if-matrix.jpg)
-
This one is tested and works:
[gist]anonymous/7790347[/gist]
-
I was trying to understand what you did here but no chance. I see you have function parameters, but I guess they are changeable , and in this example I don't see how they are changed? =/
-
Hope this makes more sense:
[gist]anonymous/7797593[/gist]
PS: I got 2016 possible combinations. Not 1000% sure I am correct, but going through the first three Y lines X by X it looks like its calculating correctly.
-
Thanks for this. The reason why you get that number is because you set first for loop to repeat 8 times. That just shouldn't happen. Why? Well, when you have rook in top left position, and he slowly progress down, he will make other rook to move down to and don't allow him to go above. Since they are both same color and it has no sence one of them come again on the top (combinations will repeat). So, as he move down, he will reach row 7 and when he is done with moving there, it has no sense again to go to last row. because again, the other rook just did that combinations, it will be repeat again. First loop should happen 7 times, second one 8 times, third one 8 times and last one is dynamic. it's dynamic because when first loop increase, the field for second rook will decrease slowly. From 7 possible rows to move, to 6, then to 5 etc.
-
Update: I just realised how foolish I am. I told you that rooks are from the same color while they are not. OMG. Now this loops won't do the job that should be done xXD
-
Update: I just realised how foolish I am. I told you that rooks are from the same color while they are not. OMG. Now this loops won't do the job that should be done xXD
The loops I supplied will do that as well. The only think you will need to do is remove some of the if statement stuff.
EDIT: Just do something like: if((rockaPosX != rockbPosX || rockaPosY != rockbPosY))