Question

Lab 6.3.1 Polymorphism: part 2 Objectives Familiarie the student with: . polymorphism, or using objocts of different types th
solve in c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include <cstdlib>
#include<string>
using namespace std;
  
class Pieces // abstract class
{
public:
virtual bool checkvalidmove(string start,string last) = 0;//virtual function
};
class Men: public Pieces
{
public:
bool checkvalidmove(string start,string last) // override function
{
int a=start[0];
int b=last[0];
int aa=start[1];
int bb=last[1];
a=a-b;
a=abs(a);
aa=aa-bb;
aa=abs(aa);
if(a<=1 && aa<=1) // if row and column doesn't excedd one block
{
if(a==0 && aa==0) // if they doesn't move than not valid
{
return false;
}
return true;
}
return false;
}
};
  
class King: public Pieces
{
public:
bool checkvalidmove(string start,string last) {
int a=start[0];
int b=last[0];
int aa=start[1];
int bb=last[1];
if(a==b) // if they have same row after the move than valid
{
if(aa==bb) // if they doesn't move than not valid
{
return false;
}
return true;
}else if(aa==bb) // if they have same column after the move than valid
{
if(a==b) // if they doesn't move than not valid
{
return false;
}
return true;
}else{
a=a-b;
a=abs(a);
aa=aa-bb;
aa=abs(aa);
if(a==aa) /* if distance between start row and last row and distance between start column and last column same than its diagonal move*/
{
return true;
}
}
return false;
}
};
  
int main(void)
{
Men ob;
King obj;
string s;
getline (cin, s);
string s1;
s1="Man";
int count=-1;
for (int i = 0; i <=s.length()-s1.length(); i++) // finding men substring
{
int j;
for (j = 0; j <s1.length(); j++)
{ if (s[i + j] != s1[j])
{
break;
}
}
if (j==s1.length())
{
count=i;
}
}
string start;
string last;
last+=s[s.length()-2];
last+=s[s.length()-1];
start+=s[s.length()-8];
start+=s[s.length()-7];
if(count>=1) // if men exit
{
bool b=ob.checkvalidmove(start,last);// call men valid move
if(b==1)
{
cout<<"true"<<endl;
}else
{
cout<<"false"<<endl;
}

}
else
{
bool bb=obj.checkvalidmove(start,last);// call king valid move
if(bb==1)
{
cout<<"true"<<endl;
}else
{
cout<<"false"<<endl;
}
}
return 0;
}

/*

case 1:-

Input:- call a method to check(King) can be solved from b1 to f5

Output :- true

case 2:-

Input :- call a method to check(Man) can be solved from b1 to f5

Output:- false

case 3:-

case 3:-

Input:- call a method to check(Man) can be solved from b1 to a2

Output :- true

case 4:-

Input:- call a method to check(King) can be solved from b1 to b7

Output :- true

*/

Add a comment
Know the answer?
Add Answer to:
Lab 6.3.1 Polymorphism: part 2 Objectives Familiarie the student with: . polymorphism, or using o...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the classes from Lab 1, app and student, create a solution for implementing some kind...

    Using the classes from Lab 1, app and student, create a solution for implementing some kind of random behavior for your student. Suppose the student can be reading, surfing the web, or interacting with other students. You will need to check now and then to see what the student is doing. You will be asking the instance of the student class and what it is doing at certain time intervals. You Will Need To: Create a method in the class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT