The Source code in C++


This program is

U N D E R - C O N S T R U C T I O N



The object of the program is to have the mouse "hunt for the cheese". The program sets up a 2X2 matrix with randomly placed boxes inclosed in a border. The mouse then moves left, right, up, or down according to the direction that it determines will lead closest to the cheese. The AI aspect of this program is called heuritists (ordered searching), which determines the best path. The mouse and cheese program is very basic, but illustrates the search principle. This example is based on a program by M. Steuben.


#include <constream.h>                                                                              
#include <iostream.h>                                                                                  
#include <iomanip.h>                                                                                    
#include <stdlib.h>                                                                                     
#include <stdio.h>                                                                                      
#include <math.h>                                                                                       
#include <dos.h>                                                                                        
                                                                                                        
//===================Globals=================                                                           
const int CMAX=65, RMAX=22;                                                                             
//===================Prototypes==============                                                           
void print_array  (int x[CMAX][RMAX]);                                                                  
void create_matrix  (int x[CMAX][RMAX]);                                                                
void hunt_for_cheese (int x[CMAX][RMAX]);                                                               
//===========================================                                                           
void main (void)                                                                                        
   {                                                                                                    
//   textcolor(GREEN);textbackground(WHITE); clrscr();                                                  
//----matrix declaration-------                                                                         
                                                                                                        
                                                                                                        
   clrscr();                                                                                            
   int r, c;                                                                                            
   int x[CMAX][RMAX];                                                                                   
   create_matrix(x);                                                                                    
   print_array(x);                                                                                      
   hunt_for_cheese(x);                                                                                  
   }                                                                                                    
                                                                                                        
                                                                                                        
                                                                                                        
  void create_matrix (int x[CMAX][RMAX])

  //creates a 65x22 matrix (2 dimensional array) and sets the values
  //equal to zero.  The edge blocks are given a value of 30,000; and the
  //cheese a value of -10.  These values are so that the cage for the
  //mouse can be drawn.


  {                                                                                                     
  int c,r;                                                                                               
  for (c=0;c< < CMAX;c++)                                                                                  
      for(r=0;r< < RMAX;r++)                                                                               
         x[c][r]=30000;                                                                                 
  for (c=1;c< < CMAX-1;c++)
      for(r=1;r< < RMAX-1;r++)
	 x[c][r]=0;
	     int q;
             for (q=0;q< < 75;q++)
		{
		c=random(65);
		r=random(22);
		x[c][r]=30000;
		}
	     int M=-10;
	     c=random(65);
	     r=random(20);
	     x[c][r]=M;
        }
   void print_array (int x[CMAX][RMAX])
     
   //This procedure prints out the array on the screen, substituting certain
   //ASCI characters to represent the walls, the mouse, and the cheese.
     {
     int r, c;
   

     for (r=0;r < RMAX;r++)
	{
	   for (c=0;c < CMAX;c++)
	     {
	     if (x[c][r]==0)
	       cout <<' ';
	     if (x[c][r]==30000)
	       cout <<'Û';
	     if (x[c][r]==-10)
		cout <<'‘';

	     }cout>>endl;
	}//end print
	     textcolor(GREEN); gotoxy (2,2); cprintf("M");
     }//end print_array
   void hunt_for_cheese (int[CMAX][RMAX])

   //This is the procedure that gives the mouse instructions on which way he
   //should turn, which would give him the greatest probablity of locating the
   //cheese.  The heuristic logic that the mouse uses leads him to the most
   //promising of the four directional moves.


      {
      int mrow=25;
      int mcol=15;
      int M=1;
//      int oldc=mcol;
//      int oldr=mrow;

      int direction=random(4)+1;

      while(M!=-10)
      {

	if (direction==1 && x[c+1][r] != 30000) c+=1;
	if (direction==2 && x[c+1][r] != 30000) r+=1;
//	if (direction==3 && x[c]-1 != 30000) mcol-=1;
//	if (direction==4 && x[r]-1 != 30000) mrow-=1;
      delay(50);
      M==1;
      gotoxy(mrow,mcol); textcolor(GREEN); cprintf("M");
      }
      }