MATLAB Answers

0

Word Search Words Overlapping

Asked by Nydea Ortega on 3 Apr 2019
Latest activity Edited by Are Mjaavatten on 4 Apr 2019
We are making a code for a word search and do not want the words to overlap one another (unless the letters overlapping are similar). I have attached our code to see if anyone can help us. We have already managed so that the first letter of the words don't overlap but do not want the rest of the word to overlap with another. Thank you.
clear all
clc
close all
%Creates the grid
xlim([0 10]);
ylim([0 10]);
grid('on');
% creates alphabet array
alpha26 = {'A','B','C','D','E','F','G','H','I','J','K','L',...
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
%First Word
word1 = {'A','L','P','A','C','A'};
%Second Word
word2 = {'A','R','M','A','D','I','L','L','O'};
%Third Word
word3 = {'B','A','B','O','O','N'};
%Fourth Word
word4 = {'S','N','A','K','E'};
%For word 1
lo = randi(10)-0.5;
po = randi(5) - 0.5;
%For word 2
lo1 = randi(10) - 0.5;
po1 = randi(2) - 0.5;
%So word 2 does not overlap with word 1
if lo1 == lo
lo1 = randi(10) - 0.5;
elseif po1 == po
po1 = randi(2) - 0.5;
end
%For word 3
lo2 = randi(10) - 0.5;
po2 = randi(5) - 0.5;
%For Word 4
lo3 = randi(10)-0.5;
po3 = randi(5) - 0.5;
%Test to get word 3 not to overlap (Needs to be fixed)
if lo2 == lo1 || lo
lo2 = randi(10) - 0.5;
elseif po2 == po1 || po
po2 = randi(5) - 0.5;
end
chance= randi(2);
chance1 = chance;
chance2 = chance;
for i = 1:10
for j = 1:10
ws{i,j} = alpha26{randi([1,26],1)};
if chance==1
%Word 1
for mo=po+0.5:po + length(word1)
ws{(lo+0.5),mo}=('');
%word 2
for mo1=po1+0.5:po1 + length(word2)
ws{(lo1+0.5),mo1}=('');
% Word 3
for mo2 = po2+0.5: po2 + length(word3)
ws{mo2,(lo2+0.5)} = ('');
%Word 4
for mo3 = po3+0.5: po3 + length(word4)
ws{mo3,(lo3+0.5)} = ('');
end
end
end
end
elseif chance==2
%Word 1
for mo=po+0.5:po + length(word1)
ws{mo,(lo+0.5)}=('');
%Word 2
for mo1=po1+0.5:po1 + length(word2)
ws{mo1,(lo1+0.5)}=('');
%Word 3
for mo2 = po2 + 0.5: po2 + length(word3)
ws{(lo2 + 0.5),mo2} = ('');
%Word 4
for mo3 = po3 + 0.5: po3 + length(word4)
ws{(lo3 + 0.5),mo3} = ('');
end
end
end
end
end
end
end
search = 10;
% puts random letters in the grid
for p=1:search
for n = 1:search
te1 = text(p-0.5, n-0.5 , ws{p,n}, ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
end
search = 10;
% puts letters of first word in the grid
for p=1:search
for n = 1:search
te1 = text(p-0.5, n-0.5 , ws{p,n}, ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
end
search = 10;
% puts letters of first word in the grid
for p = 1:1 %1 row
for n = 1:length(word1) % 6
if chance==1
te1 = text(lo,po,word1(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
elseif chance==2
te1 = text(po,lo,word1(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
po = po + 1;
end
end
%Placing word 2 into the grid
for p = 1:1 %1 row
for n = 1:length(word2) %9
if chance1==1
te1 = text(lo1,po1,word2(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
elseif chance1==2
te1 = text(po1,lo1,word2(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
po1 = po1 + 1;
end
end
%Placing word 3 into the grid
for p = 1:1 %1 row
for n = 1:length(word3) %6
if chance1==2
te1 = text(lo2,po2,word3(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
elseif chance1==1
te1 = text(po2,lo2,word3(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
po2 = po2 + 1;
end
end
for p = 1:1 %1 row
for n = 1:length(word4) %5
if chance1==2
te1 = text(lo3,po3,word4(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
elseif chance1==1
te1 = text(po3,lo3,word4(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
po3 = po3 + 1;
end
end

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Are Mjaavatten on 4 Apr 2019
Edited by Are Mjaavatten on 4 Apr 2019

I was fascinated by your problem, so I wrote my own version. This gave me some insights:
I recommend that you first create an array of random characters, for instance by
ws = char(randi(26,side)+64);
Next, fill in your words by overwriting pre-existing characters in this array. Finally display ws. This way there is only one character per square.
Note that if you write
words = {'ARMADILLO','ALPACCA','SNAKE','BABOON'}
you may use a loop instead of separate sections for each word.
My version is a function that takes the side length and a word list as inputs. Sample calls:
wordsearch(10,{'ARMADILLO','ALPACCA','SNAKE','BABOON'})
wordsearch(6,{'LUCKY','UGLY','SCREAM','MAYBE','SAD','PLAIN','YUCK'})
The challenge in the general case is to find a valid solution, where words do not overlap unless the overlapping characters are equal. I used a dumb brute force approach, where i tried with random positions and orientations until I found a valid one. To save execution time, trap any unlawful overlaps as early as possible.
Good luck!

  0 Comments

Sign in to comment.