Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C or C++ program to solve a sliding block problem. I was provided the following link as a sample: https://www.youtube.com/watch?v=vx25e_9Z3ok I need to write a

C or C++ program to solve a sliding block problem.

I was provided the following link as a sample: https://www.youtube.com/watch?v=vx25e_9Z3ok

I need to write a program in C or C++. Problem is I have not written in this language and still new to coding in general. I need to solve a sliding block problem. I need to import from a file a the variable size grid and of l and w. the first line will have the w and l in that order separated by commas. 0 means the cell is empty, 1 means it has wall, 2 means the moveable primary brick is in the cell, and 3 represents the goal. four through 9 can be any other brick. I need to have a function that says when the goal is reached when the goal is reached. I need a function that allows moving. The move function is accomplished through 5,d which moves block 5 down. I tried to solve this in pieces. Started with move up and down mini functions.

#include #include #include #include #include

// This part reads data in.

main() {

FILE *myFile; myFile = fopen("datafile.txt", "r");

//Find null if (myFile == NULL) { printf("File open failed"); return -1 ; }

//read file into array

string[] Line = sr.ReadLine().Split(','); if (Row == 0) { data = new string[Line.Length, Line.Length]; } for (int column = 0; column < Line.Length; column++) { data[Row, column] = Line[column]; } Row++; Console.WriteLine(Row); }

//MOVING FUNCTION where x = h and w = y. void up () { setArray(); take (); box [x][y+1]; result = box [x][y+1]; showupdatedpuzzle (); }

void down () { setArray(); take (); box [x][y-1]; result = box [x][y-1]; }

void left () { setArray(); take(); box [x-1][y]; result = box [x-1] [y]; }

void right () { setArray(); take(); box [x+1][y]; result = box [x+1][y]; }

THIS is how I think I select the type of move.

char choice; cout << "How would you like to move the blank? (select #)"<< endl; cout << "u: Up" << endl; cout << "d: Down" << endl; cout << "l: Left" << endl; cout << "r: Right" << endl; cin>> choice; cout< switch (choice) { case 'u': up(); break; case 'd': down(); break; case 'l': left (); break; case 'r': right (); break;

}

So I am having a hard time bringing it all together and printing each move to the screen to help the user (me) visualize. I still need to also determine a function to determine the goal state. the block I am moving may exist in more than one row/column but will always be attached so if I have 2x1 block and the goal state is 2x1, both parts of the block must be in the goal state. Any help would be useful. Also please confirm if what I have so far is correct.

A text file that can be used for sample input is:

5,4, 1,3,3,1,1, 1,0,5,4,1, 1,0,2,2,1, 1,1,1,1,1,

So to sum up the problem, I am not a C coder in anyway but I must complete it in one of the two c languages mentioned above.

I need to help make connections between what I have written and what I still need to do so code is missing, but it is because I am too inept to currently write. I am trying to build it one piece at a time, but this is above my skill set currently. The code I have may be wrong but I am trying to get it to work as well and not being a C coder makes it harder. I am hoping to get someone who can help me get the missing pieces and solve the pieces that are wrong. Here is how I think it should work.

I call in the text file using the sample I showed above (could not find a way to attach the text file). That will then layout the board. I need to know how to correct the reading in of the file to the proper type which is most likely an array. I know I am not properly calling in the file so I will definitely need help there. Then I will have the user prompted to move (code is above) but I also need to include and have not down a # of spaces so u5 moves up 5 spaces. When the user moves the block, it moves the whole block (which may be any size) to the direction indicated which I think I have the correct moving code. The brick cannot move through or into the wall (which may be some check before each move.). When the goal blocks all have the desired moving block in it, we are considered it a win. I have not written that code. I also need to recorder the number of moves. I have not written that in either. So I know the code is wrong and incomplete, but I NEED help to complete it. There is more to the assignment, but getting this gets me to the 80% marker. The rest I think I can handle once I get past this. Even if someone helps me with one or two pieces at a time it would be a win for me. And FYI the includes typically do not keep correctly when I post the updates but I have , ,< string.h>, , ,< queue>.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

3. What strategies might you use?

Answered: 1 week ago