Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The main objective of this assignment is to assess students ability to apply the stepwise refinement process to develop a new algorithm and carry that

The main objective of this assignment is to assess students ability to apply the stepwise refinement process to develop a new algorithm and carry that through to the implementation of the program. Implementation must follow the top-down design approach, where the solution starts by describing the general functionality of a game. Next, more details are provided in successive steps to refine the implementation.

Problem Description: You are to write an A.I. only prototype of an old-school dungeon-crawler video game, that involves a player fighting having to reach the end of the grid by fighting monsters.

The Grid consists of two parallel corridors connected by a single vertical bridge.

Each corridor is populated by a monster with the following graphic (^).

The A.I. controlled player, graphic is {}, spawns in the left-most part of the top corridor and must fight the monsters in order to reach the left-most part of the bottom corridor.

Your goal is to implement the MonstersMove function, which contains the behavior for both monsters: movement, collision detection, updating graphics, and fight animation. You can look for guidance in the PlayerMoves function.

The structure that contains the graphics, is the grid 2D array. That is what you need to constantly update and print as the game unfolds. Throughout the game, the player and monsters will move until a fight happens. An example of a proper trigger for a fight is illustrated below. The player and monster must have a single spot between them, for the fight to occur. That is because, that spot will be used to draw dashes (representing swords) and animate the fight.

If the player wins a fight, the monster must disappear from the grid, allowing the player to continue down his path. If the player loses, he will respawn in the beginning. If the player dies, any monster he has killed will not respawn.

Movements are based on randomness, both monsters and players can move both right and left, though the player has been given a bias depending on which corridor he is in, to make sure that he eventually reaches his goal and is not stuck in an endless loop.

You need to write the stepwise refinement for the MonstersMove function prior to implementing it. You can use the lecture notes to read more about stepwise refinement. The game should start when the user presses any key.

General Instructions:

You are given a code that compiles with no errors. Without the functionality that you are required to implement, the grid will be printed and the player will move, but monsters will not.

You need to keep the function definitions unchanged.

You cannot use any function inside MostersMove, except PrintGrid.

Print the grid, by updating the grid array. Dont forget to delete previous positions by inserting the empty character

Task: Use a text editor (e.g., the Code::Blocks editor or Notepad) to write the first pseudo-code statement of the problem. Next, add more statements that support the implementation of that first pseudo-code statement. Each time you refine a step, copy and paste the current version of the program design to the end of the text file, then make the change within that pasted version. Stick to the instructor's convention of using a different number of *s to indicate the level of expansion applicable to each statement (see example below). When the design is complete, the text file will contain a complete record of the design process used to reach the final design. The text file you submit may be quite lengthy and should show the entire process. Do not remove anything and do not just submit the final iteration. Below is a partial example of stepwise refinement (Note it is only a partial example and has nothing to do with the program for this assignment.):

image text in transcribed

main.cpp

#include  #include  #include  #include  #include  #include  using namespace std; //utility functions void MakeGrid(char grid[][38], int x, int y); void PrintGrid(char grid[][38], int x, int y); int GetRandomBetween(int ceiling); //hybrid functions void PlayerMoves(char grid[][38], int rows, int columns, int &m1_x, int &m1_y, int &m2_x, int &m2_y, int &p_x, int &p_y); void MonstersMove(char grid[][38], int rows, int columns, int &m1_x, int &m1_y, int &m2_x, int &m2_y, int &p_x, int &p_y); //graphics-independent A.I. functions void MoveHorizontaly(int &x, int xDim, bool right); void MoveVertically(int &x, int xDim); void PlayerMove(int &x, int &y, int xDim, int yDim); void MonsterMove(int &x, int &y, int xDim); int main() { char grid[8][38]; int m1_x = 15, m1_y = 1; //spawn coordinates of monster 1 int m2_x = 25, m2_y = 6; //spawn coordinates of monster 2 int p_x = 0, p_y = 1; //spawn coordinates of A.I. player //set the graphics MakeGrid(grid, 8, 38); //creates the grid, with two corridors grid[m1_y][m1_x]='('; grid[m1_y][m1_x+1]='^'; grid[m1_y][m1_x+2]=')'; grid[m2_y][m2_x]='('; grid[m2_y][m2_x+1]='^'; grid[m2_y][m2_x+2]=')'; grid[p_y][p_x]='{'; grid[p_y][p_x+1]='}'; PrintGrid(grid, 8, 38); srand(time(NULL)); bool start = false; cin>>start; while(true) { PrintGrid(grid, 8, 38);//print grid in beginning of each iteration PlayerMoves(grid, 8, 38, m1_x, m1_y, m2_x, m2_y, p_x, p_y); //check if fight will happen prior to moving MonstersMove(grid, 8, 38, m1_x, m1_y, m2_x, m2_y, p_x, p_y); //check if fight will happen after moving if(p_x7 )//smaller chance { if(x>1 && right == true)//can't move beyond the grid x--; else if(x1 && right == false) x--; } } void MoveVertically(int &y, int yDim) { if(GetRandomBetween(10)>7) { if(y>1)//can't move beyond the grid y--; } else //move right { if(y= columns-3)//else move vertically MoveVertically(p_y, rows); //update graphics grid[m1_y][m1_x]='('; grid[m1_y][m1_x+1]='^'; grid[m1_y][m1_x+2]=')'; grid[m2_y][m2_x]='('; grid[m2_y][m2_x+1]='^'; grid[m2_y][m2_x+2]=')'; grid[p_y][p_x]='{'; //update graphic values for player grid[p_y][p_x+1]='}'; if(p_x == (m1_x-3) && p_y == 1 && m1_x != -1)//if no distance between player and monster 1 { for(int i=0; i5)//player wins { m1_x=-1; m1_y=-1; grid[m1_y][m1_x]=' '; grid[m1_y][m1_x+1]=' '; } else //player loses { p_x=0; p_y=1; } } else if(p_x ==(m2_x+4) && p_y == 6 && m2_x != -1) //or monster 2 respectively { for(int i=0; i6)//player wins { m2_x=-1; m2_y=-1; grid[m2_y][m2_x]=' '; grid[m2_y][m2_x+1]=' '; } else //player loses { p_x=0; p_y=1; } } PrintGrid(grid, 8, 38);//print grid in beginning of each iteration }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions