Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science Please turn this code from c++ to java. #include #include #include #include #include using namespace std; const char UP = 0x1; const char

Computer Science

Please turn this code from c++ to java.

#include #include #include #include #include

using namespace std;

const char UP = 0x1; const char DOWN = 0x2; const char LEFT = 0x4; const char RIGHT = 0x8; const char VISITED = 0x10;

void set_flag(char &cell, const char flag) { cell |= flag; }

bool has_flag(const char &cell, const char flag) { return static_cast(cell & flag); }

int rand_int(int a, int b) { double N = (double) abs(a - b); int x = a + (int) (N * rand()/(RAND_MAX + 1.0)); return x; }

void dump_maze(const char (&maze)[12][12]) { cout << " _ _ _ _ _ _ _ _ _ _ _ _" << endl; for(int i=0; i<12; ++i) { for(int j=0; j<12; ++j) { cout << (has_flag(maze[i][j], LEFT) ? ' ' : '|'); cout << (has_flag(maze[i][j], DOWN) ? ' ' : '_'); } cout << "|" << endl; } }

void generate_maze(char (&maze)[12][12], pair current) {

static stack< pair > stk; int i = current.first; int j = current.second; set_flag(maze[i][j], VISITED); vector< pair > unvisited_neighbours; vector< char > flags; if((i > 0) && !has_flag(maze[i-1][j], VISITED)) { unvisited_neighbours.push_back(make_pair(i-1, j)); flags.push_back(UP); } if((i < 11) && !has_flag(maze[i+1][j], VISITED)) { unvisited_neighbours.push_back(make_pair(i+1, j)); flags.push_back(DOWN); } if((j > 0) && !has_flag(maze[i][j-1], VISITED)) { unvisited_neighbours.push_back(make_pair(i, j-1)); flags.push_back(LEFT); } if((j < 11) && !has_flag(maze[i][j+1], VISITED)) { unvisited_neighbours.push_back(make_pair(i, j+1)); flags.push_back(RIGHT); } int len = unvisited_neighbours.size(); if(len > 0) { int tmp = rand_int(0, len); int k = unvisited_neighbours[tmp].first; int l = unvisited_neighbours[tmp].second; char t1 = flags[tmp]; char t2 = (t1 == UP) ? DOWN : (t1 == LEFT) ? RIGHT : (t1 == DOWN) ? UP : LEFT; set_flag(maze[i][j], t1); set_flag(maze[k][l], t2); stk.push(current); generate_maze(maze, unvisited_neighbours[tmp]); }else if(stk.size() == 0) { return; }else{ pair tmp = stk.top(); stk.pop(); generate_maze(maze, tmp); } }

int main(int argc, char *argv[]) { srand(time(NULL)); char maze[12][12] = {0}; generate_maze(maze, make_pair(0,0)); set_flag(maze[0][0], LEFT); set_flag(maze[11][11], DOWN); dump_maze(maze); return 0; }

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions