Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URGENT! I need code help for the TO DO's for my AI class. It is due tonight please. So the requirements is to : Implement

URGENT! I need code help for the TO DO's for my AI class. It is due tonight please.

So the requirements is to : Implement missing features of a problem representation scenario for an

algorithmic search problem using a 15 piece sliding puzzle.

- location index of current empty slot (function blank_loc)

- number of pieces in the correct location (function board_status_pos)

- movement directions allowed for the current puzzle configuration

(function allowed_moves)

When done correctly, the BFS search can run on the slide puzzle model with the modifications made, and a small sample run can execute, with output being similar to that in file sp_out_test.txt. Here is the provided code for the functions.

/** * Slide puzzle * * A slide puzzle contains 15 pieces on a 4x4 grid with 16 positions, with * one position being marked as empty (identified as 0). The rows and columns * are occupied with pieces marked incrementally from 1 to 15 when solved. * * Slide puzzle (solved)(assume 0 "space" is on bottom right) * * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * 13 14 15 * * Pieces can be moved around, as shown in the example below. The empty * slot is identified with number 0. * * Slide puzzle movement step 1 of 5 (move left = piece 15 moves right) * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * 13 14 0 15 * * Slide puzzle movement step 2 of 5 (move up = piece 11 moves down) * 1 2 3 4 * 5 6 7 8 * 9 10 0 12 * 13 14 11 15 * * Slide puzzle movement step 3 of 5 (move up = piece 7 moves down) * 1 2 3 4 * 5 6 0 8 * 9 10 7 12 * 13 14 11 15 * * Slide puzzle movement step 4 of 5 (move right = piece 8 moves left) * 1 2 3 4 * 5 6 8 0 * 9 10 7 12 * 13 14 11 15 * * Slide puzzle movement step 4 of 5 (move down = piece 12 moves up) * 1 2 3 4 * 5 6 8 12 * 9 10 7 0 * 13 14 11 15 * */

/** Directions for movement for slide puzzle */ enum sp_move_t {UP, DOWN, LEFT, RIGHT};

/** * Location of blank or other slot for slide puzzle. * Layout of location is slots[a][b]. */ struct sp_loc_t { int a; int b;

sp_loc_t(): a(-1), b(-1) {}; // default set to -1, -1 sp_loc_t(int aa, int bb): a(aa), b(bb) {}; // set at creation time void reset() {a = -1; b = -1;}; // reset to invalid value void set(int aa, int bb) {a = aa; b = bb;}; // reset to invalid value bool valid() const { // NOLINT return clamp(a, 0, 3) == a && clamp(b, 0, 3) == b; }; };

class slide_puzzle_t { private: int slots[4][4]; // Current board configuration sp_loc_t blank_slot; // Position of blank slot

// Solved board configuration int slots_solved[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0} };

/** Copy board positions from provided configuration */ void copy_board(const int s[4][4]){ blank_slot.reset(); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { slots[i][j] = s[i][j]; if(slots[i][j] == 0) { blank_slot.set(i, j); } } } };

/** Check if this configuration is same as other configuration */ bool is_equal(const int s[4][4]) const { bool eq = true; for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { if(slots[i][j] != s[i][j]) { eq = false; } } } return eq; };

public:

/** Create a puzzle (default configuration is in solved state) */ slide_puzzle_t() { // NOLINT reset_board(); }

/** Create a puzzle as a copy of another puzzle */ slide_puzzle_t(const slide_puzzle_t &sp) { // NOLINT copy_board(sp.slots); }

/** Create a puzzle with a specific configuration provided as a 2D array */ explicit slide_puzzle_t(const int spa[4][4]) { // NOLINT copy_board(spa); }

/** * Determine if two slide puzzles have the same configuration. */ bool operator==(const slide_puzzle_t &sp) const { // NOLINT return is_equal(sp.slots); }

/** * Set the configuration of the puzzle to the same configuration of * a different puzzle */ slide_puzzle_t& operator=(const slide_puzzle_t &sp) { // NOLINT copy_board(sp.slots); return *this; }

/** * Set the configuration of the puzzle to the same configuration of * a provided slots array */ slide_puzzle_t& operator=(int slots_new[4][4]) { copy_board(slots_new); return *this; }

/** Reset board to the solved configuration */ void reset_board() { copy_board(slots_solved); }

/** Set the board to the provided configuration */ void set_board(int slots_new[4][4]) { copy_board(slots_new); }

/** * Identify array location of blank slot. * Minimum is a,b location 0,0 and maximum is a,b location 3,3. */ // TODO: implement blank slot location blank_loc() sp_loc_t blank_loc() const { sp_loc_t ll; ll.a = -1; ll.b = -1; // insert code here //TEST: assert(clamp(ll.a, 0, 3) == ll.a && clamp(ll.b, 0, 3) == ll.b); return ll; }

/** * Identify number of slots occupied by the correct puzzle piece. * Minimum is 0 (all out of place), and maximum is 15 (puzzle solved). */ // TODO: implement board correct positions board_status_pos() int board_status_pos() const { int num_good = 0; // ??? return num_good; }

/** * Identify allowed moves from current blank space. */ // TODO: implement allowed moves allowed_moves() vector allowed_moves() const { vector moves; // ??? return moves; }

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 Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

3. What might you have done differently?

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago