Thread: need help with two player mode on racecar problem

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    need help with two player mode on racecar problem

    Code:
    ok the first player works but the second player doesnt work. would someone please explain thank you. I think its the check collision function thats the problem.
    
    #include <iostream>
    using namespace std;
    
    const int nRows = 52;
    const int nCols = 72;
    char grid[nRows][nCols];
    
    
    void initRace(char grid[][nCols])
    {
       for(int i = 0; i < nRows; i++)
       {  for(int j = 0; j < nCols; j ++)
          {  
             if (i == 0)
                grid[i][j] = 'X';  // initializes first row of array
             else if ((i >= 1)&& (i < 35) && (j > 10) && (j <=30))
                grid[i][j] = 'X';  // initializes left boundary
             else if ((i >= 16) && (i <= 50) && (j >= 40) && (j <= 64))
                grid[i][j] = 'X';  // initializes right boundary
             else if((j == 0) || (j == 71))
                grid[i][j] = 'X';  // initializes sides
             else if ((i == 51)) 
                {
                   if ((j > 64) && ( j <= 71))
                      grid[i][j] = 'F';  // initializes finish line
                   else
                      grid[i][j] = 'X';
                }
    	 else if ((i == 1) && (j == 1))   
                grid[i][j] = 'O';   // car
    	 else
                grid[i][j] = ' ';  // fills with spaces
          }
       }
    }
    
    void carPosition1(int& vx, int& vy, int& xIndex, int& yIndex, bool& crash)
    {
       int ax, ay;
       cout<< "Enter horizontal and vertical accelerations (-1,0,1): ";
       cin>> ax >> ay;
       
       if((ax > 1) || (ay > 1) || (ax < -1) || (ay < -1))
          crash = true;
       else
       {
          vx = vx + ax;
          vy = vy + ay;
          xIndex = xIndex + vx;
          yIndex = yIndex + vy;
       }
    }
    
    
    
    void carPosition2(int& vx, int& vy, int& xIndex, int& yIndex, bool& crash)
    {
       int ax, ay;
       cout<< "Enter horizontal and vertical accelerations (-1,0,1): ";
       cin>> ax >> ay;
       
       if((ax > 1) || (ay > 1) || (ax < -1) || (ay < -1))
          crash = true;
       else
       {
          vx = vx + ax;
          vy = vy + ay;
          xIndex = xIndex + vx;
          yIndex = yIndex + vy;
       }
    }
    
    void printRace(char grid[][nCols])
    { 
       for(int i = 0; i < nRows; i ++)
       {  for(int j = 0; j < nCols; j ++)
             cout<< grid[i][j];
       cout<< endl;
       }
    }   
    
    void checkCrash(char grid[][nCols], int xIndex, int yIndex, bool& crash, bool&
    finish)
    {  
       if( xIndex < 0) //x out of bounds
          {
          xIndex = 0;
          crash = true;
          }
       if(xIndex >= nCols) // x out of bounds
          {
          xIndex = nCols -1;
          crash = true;
          }   
       if(yIndex < 0) // y out of bounds     
          {
          yIndex = 0;
          crash = true;
          } 
       if(yIndex >= nRows )
          {
          yIndex = nRows -1;
          crash = true;
          }
       
       if(grid[yIndex][xIndex] == 'X')
       {  
          grid[yIndex][xIndex] = 'O';      
          crash = true;
       }
       else if( grid[yIndex][xIndex] == 'F')
       {
          grid[yIndex][xIndex] = 'O'; 
          finish = true;  
       }  
       else // grid[y][x] == ' ';  
          grid[yIndex][xIndex] = 'O';
    }         
    
    void checkCrash2(char grid[][nCols], int xIndex2, int yIndex2, bool& crash,
    bool& finish)
    {  
       if( xIndex2 < 0) //x out of bounds
       {
          xIndex2 = 0;
          crash = true;
       }
       if(xIndex2 >= nCols) // x out of bounds
       {
          xIndex2 = nCols -1;
          crash = true;
       }
       if(yIndex2 < 0) // y out of bounds     
       {
          yIndex2 = 0;
          crash = true;
       }
       if(yIndex2 >= nRows)
       {
          yIndex2 = nRows -1;
          crash = true;
       }
       
       
       if(grid[yIndex2][xIndex2] == 'X') // if car in walls or barrier
       {  
          grid[yIndex2][xIndex2] = 'S';      
          crash = true;
       }
       else if( grid[yIndex2][xIndex2] == 'F')  // if car finished
       {
          grid[yIndex2][xIndex2] = 'S'; 
          finish = true;  
       }  
       else // grid[y][x] == ' ';  
          grid[yIndex2][xIndex2] = 'S';
           
    }
     
    void checkCollision(int xIndex1,int yIndex1, int xIndex2, int yIndex2,
    bool&crash)
    {
       if((xIndex1 == xIndex2)|| (yIndex1 == yIndex2))
          crash = true;
    }      
                      
    
    
    int main(void)   
    {  
       int vx1 = 0, vx2 = 0, vx = 0;
       int vy1 = 0, vy2 = 0, vy = 0;
       int xIndex1 = 1, xIndex2 = 2, xIndex = 1;
       int yIndex1 = 1, yIndex2 = 1, yIndex = 1;
       bool finish = false;
       bool crash = false;
       bool multiPlayer = true;
       int time = 0;
       int numOfPlayers;
       
       initRace(grid);
       
       cout<< "Enter number of players: \n";
       cin>> numOfPlayers;
       if(numOfPlayers != 1 && numOfPlayers != 2)
       {
           cout<< "Invalid input.\n"; 
           return 200;
       }   
       else
       {
          if(numOfPlayers == 1)
          {
             do
    	 {
    	    printRace(grid);
    	    carPosition1(vx, vy, xIndex, yIndex, crash);
    	    checkCrash(grid, xIndex, yIndex, crash, finish);
    	    time ++;
    	    printRace(grid);
    	    
    	 }while(crash == false && finish == false);
          	 
    	 
          }
          else // 2 players
          {
             do
    	 {
    	    if(multiPlayer) // first player turn
    	    {  
    	       
    	       grid[1][2] = 'S';
    	       printRace(grid);
    	       cout<< "Player 1: ";
    	       carPosition1(vx1,vy1, xIndex1, yIndex1, crash);
    	       checkCrash(grid, xIndex1, yIndex1, crash, finish);
    	       checkCollision(xIndex1, yIndex1, xIndex2, yIndex2, crash);  
    	       printRace(grid);
    	       multiPlayer = false;
    	    
    	      
    	    }
    	    else // 2nd player turn   	 
    	    {
    	       
    	       cout<< "Player 2: ";
    	       carPosition2(vx2, vy2, xIndex2, yIndex2, crash);
    	       checkCrash2(grid, xIndex2, yIndex2, crash, finish);
    	       checkCollision(xIndex1, yIndex1, xIndex2, yIndex2, crash);
    	       printRace(grid);
    	       multiPlayer = true;
    	       
                   time ++;
    	    }
    	 }while(crash == false && finish == false);
          }
    	 
          if(crash == true)
          {
             cout<< "Crashed after " << time << " seconds!\n";
             return 200;
          }
          else // finished
          {
             cout<< "Finished after " << time << " seconds!\n";
             return time;
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void checkCollision(int xIndex1,int yIndex1, int xIndex2, int yIndex2,
    bool&crash)
    >{
    >   if((xIndex1 == xIndex2)|| (yIndex1 == yIndex2))
    Shouldn't this be:
    if((xIndex1 == xIndex2) && (yIndex1 == yIndex2))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Opengl masking problem
    By kaptenkraek in forum Game Programming
    Replies: 1
    Last Post: 03-14-2008, 12:26 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. media player problem
    By gooddevil in forum Tech Board
    Replies: 0
    Last Post: 05-19-2004, 01:43 PM
  5. problem going into mode 13h
    By ArseMan in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2001, 04:53 PM