Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream>
#include<windows.h>
#include <conio.h>

using namespace std;

int main()
{
    int posx = 1;
    int posy = 0;
    char wall = '#';
    char eat1 = '*';
    char eat2 = '*';
    char eat3 = '*';
    char space = ' ';
    char goal = 'G';
    char gate = '|';
    int star=3;
    int p1=1, p2=1, p3=1;
        while (true){
    char map [10] [10] {    //0     1    2     3     4     5   6     7   8      9
                            {wall, ' ', wall, wall,wall, wall,wall, wall,wall,wall}, /*0*/
                            {wall,' ', wall,' ', ' ',' ',' ', ' ', ' ',wall},        /*1*/
                            {wall,' ', wall,' ',wall,wall,wall,wall, ' ', wall},    /*2*/
                             {wall,' ',wall,' ',wall,' ',' ',' ',' ',wall},         /*3*/
                             {wall,' ',' ',' ',wall,wall,' ', wall,eat1 ,wall},     /*4*/
                             {wall,wall,wall,wall,wall,' ',' ',wall,wall,wall},     /*5*/
                             {wall,eat3,wall,' ',' ',' ',' ', ' ',eat2,wall },      /*6*/
                             {wall,' ',wall,' ',wall,' ',wall,wall,wall,wall},      /*7*/
                             {wall,' ',' ',' ',wall,' ',gate,' ',goal,wall},          /*8*/
                             {wall,wall,wall,wall,wall,wall,wall,wall,wall,wall}    /*9*/

                     };

                char player = 'P';

                cout<<"Star Remain : "<<star<<endl;

                     map[posy][posx] = player;


                     for (int i=0; i<=9;i++)
                     {
                         for (int j=0; j<=9;j++)
                         {
                             cout<<map[i][j];

                         }
                     cout<<endl;}



    //action
    if(posx != 8 || posy != 8)
    {char control = getch();
    switch (control)
    {case 's':
    if (map [posy+1] [posx] != wall)
    {posy++;}
   break;

   case 'w':
    if (map [posy-1] [posx] != wall && posy >0)
    {posy--;}
   break;

   case 'a':
   if (map [posy] [posx-1] != wall)
   {posx--;}
   break;

   case 'd':
    if (map [posy] [posx+1] != wall && map [posy] [posx+1] != '|')
    {
        posx++;
    }
    break;
    }

    if (map [posy] [posx] == map [4][8]  && eat1 != space)
    {

        star = star-1;
        eat1 = space;
    }
    else if (map [posy] [posx] == map [6] [8]  && eat2 !=space)

    {
        star = star-1;
        eat2 = space;
    }
    else if (map [posy] [posx] == map [6] [1] && eat3 !=space)

    {
        star = star-1;
        eat3 = space;
    }

    if (star == 0)

    {
        gate = space;
    }
    }
    else cout<<"you win";



        system ("cls");}




    return 0;
}
Posted
Comments
Richard MacCutchan 17-Oct-15 9:36am    
Please edit your question and add some proper detail about your problem.

If I undestand you right, you want to eat the stars. Then simply replace a star by a space inside the map when at a star:
C++
if (map[posy][posx] == '*')
{
    star--; // similar to star = star - 1
    map[posy][posx] = space;
}
 
Share this answer
 
This code map [posy] [posx] == map [4][8] check that the contain of your position is the same as the contain of map [4][8] and this code eat1 != space that first star is not eaten.
The problem is that your actual position is not necessary 4,8
This code correct for first star.
C++
if (posy==4 && posx == 8 && eat1 != space)

Another problem with your code, if you need 20 stars, you also need 20 if, and since the stars positions are hard coded, you can't handle random positions.

The trick is to use directly map and clear eaten stars on the map directly.
The code in solution 1 handle any number of stars and any position (random).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900