This program is a simple bejewel game... i need to enter the matrix pos[alpabet][number]...eg(a1 or b7)
so if i error input for example:
11 (cout error) this can work
aa (cout error) but this will infinite loop...

please help me... besides some one can tell me what can i do with c++???? i use it to make game like this but it look boring... so what can i use c++ in more advance???

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime> 
#include <cstring>
using namespace std;

void introduce (char pos[][10]);
void random_board (char pos[][10]);
void show_board (char pos[][10]);
int change (char x);
void setAsZero (char a[][10]);
void SetSame (char pos[][10],char same[][10],char checked[][10],int x, int y);
void explode (char pos[][10],char same[][10]);
int checkSpace (char pos[][10]);
void arrangement (char pos[][10],int largest_space);
int gameover(char pos[][10],int x, int y);
int check_score (char same[][10]);
int check_skip (char pos[][10],int x,int y);
void initialize (string name[],int score[]);
void sort (string new_name,int new_score, string name[], int score[], int tot_list);
void score_board (string name[], string temp_name, int score_list[], int Total_score,int tot_list);

main()
{
      string name[10];
      char pos[10][10],x_char;
      int i,x,y,largest_space,error=0;
      int GAMEOVER=1,score=0,Total_score=0,skip=0,score_list[10],tot_list=0;
      char same[10][10],checked[10][10];

      random_board(pos);
      introduce(pos);
      setAsZero (checked);
      setAsZero (same);
      
      while(true)
      {
      if(GAMEOVER==0)                                          //end the game
      break;
      show_board(pos);                                    //show board
      cout<<"Your score is "<<Total_score<<endl;
      cout<<"Enter the position: ";
      cin>>x_char>>y;
      x_char=tolower(x_char);                             //to lower case
      if(x_char=='q' && y==0)
      break;
      
      if(x_char!='a' && x_char!='b' && x_char!='c' && x_char!='d' && x_char!='e' && x_char!='f' && x_char!='g' && x_char!='h' && x_char!='i' && x_char!='j')
      {cout<<"Error input... Pls try again...\n";
      system("pause");
      error=1;}
      if(isalpha(y))
      {cout<<"Error input... Pls try again...\n";
      system("pause");
      error=1;}
      
      if(error==0)
      x=change(x_char);                                   //change char to int
      skip=check_skip (pos,x,y);                          //skip step if pos is ' '
      if(error==1)
      skip=1;
      if(skip==0)                                         //below are the steps of whole game
      {
          SetSame (pos,same,checked,x,y);                     //Set same and checked   
          explode (pos,same);                                 //explode
          largest_space=checkSpace(pos);                      //check largest space at y axis
          arrangement (pos,largest_space);                    //arrange
          score=check_score (same);                           //check score of 1 round
          setAsZero (checked);                                //reset checked
          setAsZero (same);                                   //reset same
          Total_score=Total_score+score;
          GAMEOVER= gameover (pos,x,y);                       //c end game d o nt
      }
      skip=0;
      error=0;
      system("cls");
      }      
      initialize (name,score_list);
      cout<<"Game Over!!!\nYour Score is "<<Total_score<<endl;
      cout<<"Enter your name: ";
      string temp_name;
      cin>>temp_name;
      score_board (name, temp_name, score_list, Total_score, tot_list);
      
      system("pause");
      return EXIT_SUCCESS;
      
      }
      
void introduce (char pos[][10])
{
     cout<<"  **************************\n";
     cout<<"  * WELCOME TO BUBBLE BOOM *\n";
     cout<<"  **************************\n";
     cout<<"\n\n";
     show_board(pos);
     cout<<"\n\n";
     cout<<"Rule:\n1)explode the same symbol which gather around.\n2)At first u hv 1 point.\n3)if u explode 2 bubbles has 2 points,3 bubbles 4 point, 4 bubbles 8 points and so on.\n4)At least explode 2 bubbles.\n5)Enter your position as in chess,eg e5 ,b7 and etc.\n";
     system("pause");
     system("cls");
}

void random_board (char pos[][10])
{
     srand((unsigned)time(0)); 
     for(int i=0;i<10;i++)
     for(int j=0;j<10;j++)
             pos[i][j]=rand()%4+35;
}

void show_board (char pos[][10])
{
     for(int i=0;i<10;i++)
     {
     cout<<i<<"    ";
     for(int j=0;j<10;j++)
     cout<<pos[j][i]<<" ";
     cout<<endl;
     }
     cout<<"\n     a b c d e f g h i j \n";
}

int change (char x)
{
    for(int i=0;i<10;i++)
    if (x==97+i)
       return i;
}

void setAsZero (char a[][10])
{
     for(int i=0;i<10;i++)
     for(int j=0;j<10;j++)
     a[i][j]=' ';
}

//------------------------------------------------------------------------------
void SetSame (char pos[][10],char same[][10],char checked[][10],int x, int y)
{
     char temp;
     
     if(checked[x][y]!='c')
         temp=pos[x][y];
             
     if(pos[x-1][y]==temp && x!=0 && checked[x-1][y]!='c' && temp!=' ')    //check left is same or nt? and left checked or nt?
     {
         same[x][y]='s';
         checked[x][y]='c';
         SetSame (pos,same,checked,x-1,y);
     }    
     if(pos[x+1][y]==temp && x!=9 && checked[x+1][y]!='c' && temp!=' ')
     {
         same[x][y]='s';
         checked[x][y]='c';
         SetSame (pos,same,checked,x+1,y);
     }             
     if(pos[x][y-1]==temp && y!=0 && checked[x][y-1]!='c' && temp!=' ')
     {
         same[x][y]='s';
         checked[x][y]='c';
         SetSame (pos,same,checked,x,y-1);
     }             
     if(pos[x][y+1]==temp && y!=9 && checked[x][y+1]!='c' && temp!=' ')
     {
         same[x][y]='s';
         checked[x][y]='c';
         SetSame (pos,same,checked,x,y+1);
     }
     if((pos[x][y+1]==temp && y!=9) || (pos[x][y-1]==temp && y!=0) || (pos[x+1][y]==temp && x!=9) || (pos[x-1][y]==temp && x!=0))
     {
         same[x][y]='s';
         checked[x][y]='c';
     }
}

void explode (char pos[][10],char same[][10])
{
     for(int i=0;i<10;i++)
     for(int j=0;j<10;j++)
     {
         if (same[i][j]=='s')
            pos[i][j]=' ';
     }
}

int gameover (char pos[][10],int x, int y)
{
     int GAMEOVER=0;
     for(int i=0;i<10;i++)
     for(int j=0;j<10;j++)
     {
             char temp=pos[i][j];
             if(pos[i-1][j]==temp && i!=0 && temp!=' ')
             {
             GAMEOVER=1;
             break;
             }
             if(pos[i+1][j]==temp && i!=9 && temp!=' ')
             {
             GAMEOVER=1;   
             break;
             }
             if(pos[i][j-1]==temp && j!=0 && temp!=' ')
             {
             GAMEOVER=1;
             break;
             }
             if(pos[i][j+1]==temp && j!=9 && temp!=' ')
             {
             GAMEOVER=1;
             break;
             }
     }
     return GAMEOVER;
}

int checkSpace (char pos[][10])
{
    int largest_space=0,space=0;
    for(int i=9;i>=0;i--)
    {
         for(int j=9;j>=0;j--)
         {
             if(pos[i][j]==' ')
             space++;
         }
         if(largest_space<space)
         largest_space=space;
         space=0;
     }
     return largest_space;
}

void arrangement (char pos[][10],int largest_space)
{
     for(int k=0;k<largest_space;k++)
     for(int i=9;i>=0;i--)
     for(int j=9;j>0;j--)
         if(pos[i][j]==' ')
         {
             pos[i][j]=pos[i][j-1];
             pos[i][j-1]=' ';
         }
}

int check_score (char same[][10])
{
    int score=1;
    for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)
    if(same[i][j]=='s')
    score=score*2;
    if(score!=1)
    return score;
    else
    return 0;
}

int check_skip (char pos[][10],int x,int y)
{
     if(pos[x][y]==' ')
     return 1;
     else
     return 0;
}

void initialize (string name[],int score[])
{
     for(int i=0;i<10;i++)
     {
     name[i]="   ";
     score[i]=0000;
     }
}

void sort (string new_name,int new_score, string name[], int score[], int tot_list)
{
    int position,temp_score;
    string temp_name;
    for(int i=0;i<10;i++)
    if(new_score>score[i])
        {   position=i;
            for(int j=9;j>position;j--)
            {
            score[j]=score[j-1];
            name[j]=name[j-1];}
            score[position]=new_score;
            name[position]=new_name;
            break;
        }
}

void score_board (string name[], string temp_name, int score_list[], int Total_score,int tot_list)
{
     
      ifstream input;
      input.open("score.txt");  
      int i=0;
      if(input)
      while(!input.eof())
      {
        input>>name[i];
        input.get();
        input>>score_list[i];
        input.get();
        i++;
        tot_list++;
      }
      input.close();
      sort (temp_name,Total_score,name,score_list,tot_list);
      //output
      cout<<"  ***************\n";
      cout<<"  * SCORE BOARD *\n";
      cout<<"  ***************\n";      
      cout<<"\n\nName\t\tScore\n";
      for(i=0;i<10;i++)
      cout<<setw(3)<<left<<i+1<<setw(10)<<name[i]<<"\t"<<score_list[i]<<endl;
      
      ofstream output;
      output.open("score.txt");
      for(i=0;i<10;i++)
      {
      output<<name[i];
      output<<endl;
      output<<score_list[i];
      output<<endl;
      }
      output.close();
}

Recommended Answers

All 3 Replies

>>what can i do with c++????
You can use vector instead of arrays.

line 5: should be <string> to get std::string class

line 24: use int main()

line 129: you need to make that function return a value if the loop never finds what it is looking for.

Spell out the words in the instructions -- don't use chatroom speak, which make your program look childish.

But the infinite loop error still haven't solve... I will learn better english and re-write it.. and i learning c++ so i'm not very concentrate on the english... sorry~~~~~~~~~~~~~~~~~~

line 44: you can't enter two values without spaces like that. Here is a suggested alternate way to do that: Get the 2 characters as a string then split them apart. See this thread for explaination of the cin.ignore() line

#include <limits>
...
<snip>
      x_char = 0;
      y = 0;
      string input;
      cin >> input;
      cin.ignore ( std::numeric_limits<std::streamsize>::max(), cin.widen ( '\n' ) );
      if( input.length() == 2)
      {
        x_char = input[0];
        if(!isdigit(input[1]) )
        {
            cout << "Error -- second character must be a digit 1-9\n";
            cin.get();
        }
        else
            y = input[1] - '0';
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.