By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Mousebot Program (two-dimentional array with extras)

P: 5
Hello,

I am making a program that runs off of a two dimentional array but I have made it into a game called MouseBot. I am trying to make the game so that you can move the mouse until you get the cheese. It has four different difficulty levels and you have obsticles in your way (the amount depends on the difficulty level).

The problem I am having is that when I move the 'M' (mousebot) into the array space that the cheese ('C') is in it doesn't give the message and end the game. It does the same thing with the obsticels the M just covers it up and continues the game.

Here is the code for the game (I know it is long but I am just in Computer Science I but I don't think there were any shortcuts....oh well can someone please help me with this...

Thanks
Erik

BTW, I am using Bloodshed Dev C++ version 4.9.9.2 and I am running my programs in the command prompt on Windows XP...I hope this helps.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cctype>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. enum Key{ LEFT = 75, RIGHT = 77, UP = 72, DOWN = 80 };
  13. const int numColms = 10;
  14. const int numRows = 10;
  15.  
  16. void centerText(string str, int numCols, bool printendl);
  17. void Introduction();
  18. void StartScreen();
  19. void DifficultyLevel(int& obsticles);
  20. void StartTable(int numColms, int numRows, char table[][numRows]);
  21. void CheesePosition(int numColms, int numRows, char table[][numRows], int& cheesePosX, int& cheesePosY);
  22. void ObsticalPositions(int obsticles, int numColms, int numRows, char table[][numRows]);
  23. void MouseBotPosition(int numColms, int numRows, char table[][numRows], int& x, int& y);
  24. void Movement(int numColms, int numRows, char table[][numRows], int x, int y, int& newMousePositionX, int& newMousePositionY, int cheesePosX, int cheesePosY);
  25. void checkMovement(int newMousePositionX, int newMousePositionY, char table[][numRows], int numColms, int numRows, int x, int y, int& cheesePosX, int& cheesePosY);
  26. void PrintNewScreen(char table[][numRows]);
  27.  
  28. int main()
  29. {
  30.     int obsticles, numCols, x, y, newMousePositionX, newMousePositionY, cheesePosX, cheesePosY;
  31.     string str;
  32.     bool printendl;
  33.     char table[numColms][numRows];
  34.  
  35.  
  36.     Introduction();
  37.     StartScreen();
  38.     DifficultyLevel(obsticles);
  39.     StartTable(numColms, numRows, table);
  40.     CheesePosition(numColms, numRows, table, cheesePosX, cheesePosY);
  41.     ObsticalPositions(obsticles, numColms, numRows, table);
  42.     MouseBotPosition(numColms, numRows, table, x, y);
  43.     Movement(numColms, numRows, table, x, y, newMousePositionX, newMousePositionY, cheesePosX, cheesePosY);
  44.  
  45.     system("pause");
  46.  
  47.     return 0;
  48. }
  49.  
  50. void centerText(string str, int numCols, bool printendl)
  51. {
  52.      // center the string str over the number of cols specified in numCols
  53.      // if printendl is true then an "endl" is printed after str is printed
  54.      int len;
  55.  
  56.      len = str.length();
  57.      cout << setw((numCols-len)/2) << " " << str << setw((numCols-len)/2) << " ";
  58.      if (printendl == true)
  59.         cout << endl;
  60. }
  61.  
  62. void Introduction()
  63. {
  64.      cout << "Erik Lindow" << endl;
  65.      cout << "Computer Science I" << endl;
  66.      cout << "Dr. Craig Varnell" << endl;
  67.      cout << endl;
  68.      centerText("Programming Assignment #6", 80, true);
  69.      cout << endl;
  70.      cout << "This program is a game where a mousebot is after a piece of cheese.  The" << endl; 
  71.      cout << "mousebot and the cheese are randomly placed on a 10x10 grid along with" << endl; 
  72.      cout << "obsticals depending on difficulty level.  The user then will control a" << endl; 
  73.      cout << "mousebot and move it around obsticals in order to get the cheese.  If" << endl; 
  74.      cout << "the mousebot moves too many spaces up, down, left, or right it will fall off" << endl;
  75.      cout << "the table and the game will be over." << endl;
  76.      system("pause");
  77. }
  78.  
  79. void StartScreen()
  80. {
  81.      system("cls");
  82.      cout << setw(77) << left << "MMMM        MMMM       OOOOO       UUU      UUU     SSSSSS     EEEEEEEEEEE" << endl;
  83.      cout << setw(77) << left << "MMMMM      MMMMM     OOO   OOO     UUU      UUU   SSS    SSS   EEEEEEEEEEE" << endl;
  84.      cout << setw(77) << left << "MMM MM      MM MMM    OOO     OOO    UUU      UUU   SSS     SS   EEE" << endl;
  85.      cout << setw(77) << left << "MMM  MM  MM  MMM    OOO     OOO    UUU      UUU    SSS         EEE" << endl;
  86.      cout << setw(77) << left << "MMM   MMMM   MMM    OOO     OOO    UUU      UUU      SSSS      EEEEEEE" << endl;
  87.      cout << setw(77) << left << "MMM    MM    MMM    OOO     OOO    UUU      UUU         SSS    EEEEEEE" << endl;
  88.      cout << setw(77) << left << "MMM          MMM    OOO     OOO     UUU    UUU    SS     SSS   EEE" << endl;
  89.      cout << setw(77) << left << "MMM          MMM     OOO   OOO       UUU  UUU     SSS    SSS   EEEEEEEEEEE" << endl;
  90.      cout << setw(77) << left << "MMM          MMM       OOOOO          UUUUUU        SSSSSS     EEEEEEEEEEE" << endl;
  91.      cout << endl << endl;
  92.      cout << setw(77) << "BBBBBBBBB        OOOOO      TTTTTTTTTTTTT" << endl; 
  93.      cout << setw(77) << "BBBB   BBBB    OOO   OOO    TTTTTTTTTTTTT" << endl;
  94.      cout << setw(77) << "BBB    BBBB   OOO     OOO        TTT" << endl;
  95.      cout << setw(77) << "BBBB   BBBB   OOO     OOO        TTT" << endl;
  96.      cout << setw(77) << "BBBBBBBB      OOO     OOO        TTT" << endl;
  97.      cout << setw(77) << "BBBB   BBBB   OOO     OOO        TTT" << endl;
  98.      cout << setw(77) << "BBB    BBBB   OOO     OOO        TTT" << endl;
  99.      cout << setw(77) << "BBBB   BBBB    OOO   OOO         TTT" << endl;
  100.      cout << setw(77) << "BBBBBBBBB        OOOOO           TTT" << setw(80) << right << "Programmed by Erik Lindow" << endl;
  101.      cout << endl;
  102.      system("pause");
  103. }
  104.  
  105.  
  106. void DifficultyLevel(int& obsticles)
  107. {
  108.      char level;
  109.      bool test;
  110.  
  111.      test = false;
  112.      while(test == false)
  113.      {
  114.                 system("cls");
  115.                 cout << "What difficulty level would you like to start out on?" << endl;
  116.                 cout << "1. (E)asy" << endl;
  117.                 cout << "2. (N)ormal" << endl;
  118.                 cout << "3. (H)ard" << endl;
  119.                 cout << "4. E(X)treme (Warning: Not all maps on Exteme level will have a solution!!!)" << endl;
  120.                 cout << "Please enter your answer using E, N, H, or X.  Thank you." << endl;
  121.                 cin >> level;
  122.                 cout << endl;
  123.  
  124.                 if (toupper(level) == 'E')
  125.                 {
  126.                       obsticles = 2;
  127.                       cout << "You have chosen the Easy difficulty level." << endl; 
  128.                       cout << "There will be two obsticals on the map." << endl;
  129.                       test = true;
  130.                 }
  131.                 else if (toupper(level) == 'N')
  132.                 {
  133.                       obsticles = 8;
  134.                       cout << "You have chosen the Normal difficulty level." << endl;
  135.                       cout << "There will be eight obsticals on the map." << endl;
  136.                       test = true;
  137.                 }
  138.                 else if (toupper(level) == 'H')
  139.                 {
  140.                       obsticles = 15;
  141.                       cout << "You have chosen the Hard difficulty level." << endl;
  142.                       cout << "There will be fifteen obsticals on the map." << endl;
  143.                       test = true;
  144.                 }
  145.                 else if (toupper(level) == 'X')
  146.                 {
  147.                       obsticles = 30;
  148.                       cout << "You have chosen the Extreme difficulty level." << endl;
  149.                       cout << "There will up to thirty obsticals on the map." << endl;
  150.                       cout << "WARNING!!!! -- Remember not all maps on Extreme difficulty level" << endl;
  151.                       cout << "will have solutions." << endl;
  152.                       test = true;
  153.                 }
  154.                 else
  155.                 {
  156.                       cout << "You entered an incorrect choice.  Please enter again." << endl;
  157.                       test = false;
  158.                 }
  159.                 cout << endl;
  160.                 system("pause");
  161.      } 
  162. }
  163.  
  164. void StartTable(int numColms, int numRows, char table[][numRows])
  165. {
  166.      int i, j;
  167.  
  168.      system("cls");
  169.      for(i = 0; i < numColms; i++)
  170.      {
  171.            for(j = 0; j < numRows; j++)
  172.            {
  173.                  table[i][j] = '.';
  174.            }
  175.      }
  176. }
  177.  
  178. void ObsticalPositions(int obsticles, int numColms, int numRows, char table[][numRows])
  179. {
  180.     int i, x, y;
  181.  
  182.     cout << obsticles << endl;
  183.     i = 0;
  184.     srand (time(NULL));
  185.     while(i < obsticles)
  186.     {
  187.  
  188.          x = rand() % 10;
  189.          y = rand() % 10;
  190.          table[x][y] = 219;
  191.          i++;
  192.     }
  193.  
  194. }
  195.  
  196. void CheesePosition(int numColms, int numRows, char table[][numRows], int& cheesePosX, int& cheesePosY)
  197. {
  198.     int x, y;
  199.  
  200.     srand (time(NULL));
  201.     x = rand() % 12 - 3;
  202.     y = rand() % 12 - 3;
  203.     table[x][y] = 'C';
  204.     cheesePosX = x;
  205.     cheesePosY = y;     
  206. }   
  207.  
  208. void MouseBotPosition(int numColms, int numRows, char table[][numRows], int& x, int& y)
  209. {
  210.  
  211.      srand (time(NULL));
  212.      x = rand() % 8;
  213.      y = rand() % 8;
  214.      table[x][y] = 'M';
  215.      for(int i = 0; i < numColms; i++)
  216.      {
  217.           for(int j = 0; j < numRows; j++)
  218.           {
  219.                 cout << setw(3) << table[i][j] << setw(3) << " ";
  220.           }
  221.           cout << endl << endl;
  222.      }
  223.      system("pause");
  224. }
  225.  
  226.  
The rest of the code is following............
Dec 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: 5
Here is the rest of the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Movement(int numColms, int numRows, char table[][numRows], int x, int y, int& newMousePositionX, int& newMousePositionY, int cheesePosX, int cheesePosY)
  3. {
  4.      system("cls");
  5.      PrintNewScreen(table);
  6.  
  7.      int kbd_key;
  8.      while(table[x][y] != 'C')
  9.      {
  10.           table[x][y] = ' ';
  11.           cout << "To move the MouseBot use the arrows keys to move the mouse up, down, right, or left." << endl;
  12.           kbd_key = getch();
  13.           if( kbd_key == 0 || kbd_key == 224) 
  14.           {
  15.              kbd_key = getch();
  16.              switch(kbd_key)
  17.             {
  18.                 case UP: if( x > 0) x--; break;
  19.                 case DOWN: if( x < 24) x++; break;
  20.                 case LEFT: if( y > 0) y--; break;
  21.                 case RIGHT: if( y < 75) y++; break;
  22.  
  23.             }
  24.              table[x][y] = 'M';
  25.              newMousePositionX = x;
  26.              newMousePositionY = y;
  27.              checkMovement(newMousePositionX, newMousePositionY, table, numColms, numRows, x, y, cheesePosX, cheesePosY);
  28.           }      
  29.      }
  30. }
  31.  
  32. void checkMovement(int newMousePositionX, int newMousePositionY, char table[][numRows], int numColms, int numRows, int x, int y, int& cheesePosX, int& cheesePosY)
  33. {
  34.      int x1, y1;
  35.      cout << "Getting Here" << endl;
  36.      system("pause");
  37.  
  38.      x1 = newMousePositionX;
  39.      y1 = newMousePositionY;
  40.      if (table[x1][y1] ==  char(219))
  41.      {
  42.             cout << "This move cannot be made because there is a wall in the MouseBot's way!!!!" << endl;
  43.             cout << "Please try again." << endl;
  44.             Movement(numColms, numRows, table, x, y, newMousePositionX, newMousePositionY, cheesePosX, cheesePosY);
  45.      }
  46.      else if (table[x1][y1] == 'C')
  47.      {
  48.           //WinScreen();
  49.           cout << "Congratulations you Win!!!!" << endl;
  50.           system("pause");
  51.           exit(1);
  52.      }
  53.      else
  54.          PrintNewScreen(table);
  55.          Movement(numColms, numRows, table, x, y, newMousePositionX, newMousePositionY, cheesePosX, cheesePosY);
  56. }   
  57.  
  58. void PrintNewScreen(char table[][numRows])
  59. {
  60.  
  61.      for(int i = 0; i < numColms; i++)
  62.      {
  63.           for(int j = 0; j < numRows; j++)
  64.           {
  65.                 cout << setw(3) << table[i][j] << setw(3) << " ";
  66.           }
  67.           cout << endl << endl;
  68.      }  
  69.  
Dec 2 '06 #2

P: 5
does anyone know what I am doing wrong?
Dec 2 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.