Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Colored checkerboard' topicStudent Support Forum > General > Archives > "Colored checkerboard"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
01/22/13 10:30pm

I am assuming you mean you do not want two adjacent squares to have the same color.

This generates a board of size*size and then checks to see that no two adjacent are the same number. If two adjacent are the same it generates a new board and tries again.

In[1]:= size=2;count=0;
While[True,
t=Table[RandomInteger[3,{size,size}];
success=True;count++;
For[i=1,i≤size,i++,
For[j=1,j≤size,j++,
If[i>1&&t[[i,j]]==t[[i-1,j]]||
i<size&&t[[i,j]]==t[[i+1,j]]||
j>1&&t[[i,j]]==t[[i,j-1]]||
j<size&&t[[i,j]]==t[[i,j+1]],success=False]
]
];
If[success,Break[]]
];
Print[t,count]

From In[3]:= {{2,1},{3,2}} 3

As you make size (slowly) smaller you can see that it takes more and more tries before it finds a board without adjacent squares the same color. For size=10 this will be very very very slow.

Perhaps you could consider how to write this yourself by starting in one square, generating a color, moving to an adjacent square, choosing a color that does not conflict with any previous squares and repeating. You may find that you reach a square where there is no color that does not conflict. Then you could backtrack to the previous square, make another choice for color and continue. You may find you must backtrack again and again in some cases. You would need to think how to keep track of all the history to be able to move forward and back until you find a complete solution.

URL: ,

Subject (listing for 'Colored checkerboard')
Author Date Posted
Colored checkerboard Savage 01/22/13 11:21am
Re: Colored checkerboard Bill Simpson 01/22/13 10:30pm
Re: Re: Colored checkerboard Savage 01/24/13 1:41pm
Re: Re: Re: Colored checkerboard Bill Simpson 01/24/13 10:41pm
Re: Colored checkerboard yehuda 01/23/13 06:46am
Re: Colored checkerboard Bill Simpson 01/25/13 3:25pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic