Skip to content

Instantly share code, notes, and snippets.

@elizabet92
Created October 31, 2015 20:02
Show Gist options
  • Save elizabet92/973cd06f83c8292fdd72 to your computer and use it in GitHub Desktop.
Save elizabet92/973cd06f83c8292fdd72 to your computer and use it in GitHub Desktop.
Problem2 - Word Game
#include <iostream>
using namespace std;
int main()
{
int i, j, k, n, m, word_len=0, br=0;
char arr1[100];
char arr2[100][100];
cout<<"Enter a word"<<endl;
cin>>arr1;
cout<<"Enter number of rows: "<<endl;
cin>>n;
cout<<"Enter number of coulumns: "<<endl;
cin>>m;
cout<<"Enter letters: "<<endl;
for (i=0; i<n; i++)
for (j=0; j<m; j++)
cin >> arr2[i][j];
//looks for straight horizontal word
for(; arr1[word_len]!='\0'; word_len++);
for(i=0; i<n; i++)
{
j=0;
for(k=0; k<word_len; k++)
{
if (arr2[i][j]==arr1[k])
j++;
else break;
if(j==word_len-1) {br++; }
}
}
//looks for reverse horizontal word
for(i=0; i<n; i++)
{
j=word_len-1;
if(arr2[i][j]==arr1[0])
for(k=0; k<word_len; k++)
{
if (arr2[i][j]==arr1[k])
j--;
else break;
if(j==0) {br++;}
}
}
//looks for straight vertical word
for(j=0; j<m; j++)
{
i=0;
for(k=0; k<word_len; k++)
{
if (arr2[i][j]==arr1[k])
i++;
else break;
if(i==word_len-1) {br++; }
}
}
//looks for reverse vertical word
for(j=0; j<m; j++)
{
i=word_len-1;
for(k=0; k<word_len; k++)
{
if (arr2[i][j]==arr1[k])
i--;
else break;
if(i==0) {br++; }
}
}
int oldi, oldj;
//looks for main diagonal word
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(arr2[i][j]==arr1[0]){
oldi=i+1;
oldj=j+1;
for(k=1; k<word_len; k++)
{
if ((arr2[oldi++][oldj++]==arr1[k]));
else break;
if(oldi-i==word_len&&oldj-word_len==j) {br++; }
}
}
}
}
//looks for reversed main diagonal word
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(arr2[i][j]==arr1[0]){
oldi=i-1;
oldj=j-1;
for(k=1; k<word_len; k++)
{
if ((arr2[oldi--][oldj--]==arr1[k]));
else break;
if(oldi+word_len==i&&oldj+word_len==j) {br++; }
}
}
}
}
cout<<br;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment