Does any can me to add one more class to this program to let this project run au
ID: 3919701 • Letter: D
Question
Does any can me to add one more class to this program to let this project run automatically.
#include"stdafx.h"
#include<time.h>
#include<stdlib.h>
#include<vector>
#include<string>
#include<iostream>
usingnamespacestd;
classp15{
public:
voidplay()
{
boolp = true;
stringa;
while(p)
{
createBrd();
while(!isDone())
{
drawBrd();
getMove();
}
drawBrd();
cout <<" Congratulations! Play again (Y/N)?";
cin >>a; if(a !="Y"&& a !="y") break;
}
}
private:
voidcreateBrd()
{
vector<int> v;
inti = 1;
for(inti=0; i < 16; i++)
{
brd[i - 1] = i;
}
brd[15] = 0;
x = y = 3;
for(i = 0; i < 1000; i++)
{
getCandidates(v);
move(v[rand() % v.size()]);
v.clear();
}
}
voidmove(intd)
{
intt = x + y * 4;
switch(d) {
case1: y--; break;
case2: x++; break;
case4: y++; break;
case8: x--;
}
brd[t] = brd[x + y * 4];
brd[x + y * 4] = 0;
}
voidgetCandidates(vector<int>& v) {
if(x < 3) v.push_back(2); if(x > 0) v.push_back(8);
if(y < 3) v.push_back(4); if(y > 0) v.push_back(1);
}
voiddrawBrd()
{
intr; cout <<" ";
for(inty = 0; y < 4; y++) {
cout <<"+----+----+----+----+ ";
for(intx = 0; x < 4; x++) {
r = brd[x + y * 4];
cout<<"| ";
if(r < 10) cout <<" ";
if(!r) cout <<" ";
elsecout <<r <<" ";
}
cout <<"| ";
}
cout <<"+----+----+----+----+ ";
}
voidgetMove()
{
vector<int> v;
getCandidates(v);
vector<int> p;
getTiles(p, v);
unsignedinti;
while(true)
{
cout <<" Possible moves: ";
for(i = 0; i < p.size(); i++)
{
cout<<p[i]<<" "<<endl;
intz;
cin>>z;
for(i = 0; i < p.size(); i++)
if(z == p[i])
{
move(v[i]); return;
}
}
}
}
voidgetTiles(vector<int>& p, vector<int>& v)
{
for(unsignedintt = 0; t < v.size(); t++)
{
intxx = x, yy = y;
switch(v[t]) {
case1: yy--; break;
case2: xx++; break;
case4: yy++; break;
case8: xx--;
}
p.push_back(brd[xx + yy * 4]);
}
}
boolisDone() {
for(inti = 0; i < 15; i++) {
if(brd[i] != i + 1)
returnfalse;
}
returntrue;
}
intbrd[16], x, y;
};
intmain(intargc, char* argv[]) {
srand((unsigned)time(0));
p15p; p.play(); return0;
}
Explanation / Answer
here is your program : ---------------->>>>>>>>>>>
//#include"stdafx.h"
#include<time.h>
#include<stdlib.h>
#include<vector>
#include<string>
#include<windows.h>
#include<iostream>
using namespace std;
class p15{
public:
void play(){
bool p = true;
string a;
while(p){
createBrd();
while(!isDone()){
drawBrd();
getMove();
}
drawBrd();
cout <<" Congratulations! Play again (Y/N)?";
cin >>a; if(a !="Y"&& a !="y") break;
}
}
protected:
void createBrd(){
vector<int> v;
int i = 1;
for(int i=0; i < 16;i++){
brd[i - 1] = i;
}
brd[15] = 0;
x = y = 3;
for(i = 0; i < 1000; i++){
getCandidates(v);
move(v[rand() % v.size()]);
v.clear();
}
}
void move(int d){
int t = x + y * 4;
switch(d) {
case 1: y--; break;
case 2: x++; break;
case 4: y++; break;
case 8: x--;
}
brd[t] = brd[x + y * 4];
brd[x + y * 4] = 0;
}
void getCandidates(vector<int>& v) {
if(x < 3) v.push_back(2);
if(x > 0) v.push_back(8);
if(y < 3) v.push_back(4);
if(y > 0) v.push_back(1);
}
void drawBrd(){
int r;
cout <<" ";
for(int y = 0; y < 4; y++) {
cout <<"+----+----+----+----+ ";
for(int x = 0; x < 4; x++) {
r = brd[x + y * 4];
cout<<"| ";
if(r < 10)
cout <<" ";
if(!r)
cout <<" ";
else
cout <<r <<" ";
}
cout <<"| ";
}
cout <<"+----+----+----+----+ ";
}
void getMove(){
vector<int> v;
getCandidates(v);
vector<int> p;
getTiles(p, v);
unsigned int i;
while(true){
cout <<" Possible moves: ";
for(i = 0; i < p.size(); i++){
cout<<p[i]<<" "<<endl;
int z;
cin>>z;
for(i = 0; i < p.size(); i++)
if(z == p[i]){
move(v[i]);
return;
}
}
}
}
void getTiles(vector<int>& p, vector<int>& v){
for(unsigned int t = 0; t < v.size(); t++){
int xx = x, yy = y;
switch(v[t]) {
case 1: yy--; break;
case 2: xx++; break;
case 4: yy++; break;
case 8: xx--;
}
p.push_back(brd[xx + yy * 4]);
}
}
bool isDone() {
for(int i = 0; i < 15; i++) {
if(brd[i] != i + 1)
return false;
}
return true;
}
int brd[16], x, y;
};
class automatic:public p15{
private:
void getMove(){
vector<int> v;
getCandidates(v);
vector<int> p;
getTiles(p, v);
unsigned int i;
int z;
while(true){
z = rand()%15 + 1;
for(i = 0;i<p.size();i++){
if(p[i] == z){
move(v[i]);
return;
}
}
}
}
public:
automatic(){
}
void play(){
bool p = true;
string a;
while(p){
createBrd();
while(!isDone()){
drawBrd();
getMove();
Sleep(2000);
system("cls");
}
drawBrd();
cout <<" Congratulations! Play again (Y/N)?";
cin >>a; if(a !="Y"&& a !="y") break;
}
}
};
int main(int argc, char* argv[]) {
srand((unsigned)time(0));
automatic p;
p.play();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.