I wrote a simple bubble game... but it look long and complicated... so any idea to short it down? i heard about class.. but i dunno how to use it.. so is my program can use class? or any idea to short it down?
#include <iostream>
#include <ctime>
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);
main()
{
char pos[10][10],x_char;
int x,y,largest_space;
int end=1,score=0,Total_score=0,skip=0;
char same[10][10],checked[10][10];
random_board(pos);
introduce(pos);
setAsZero (checked);
setAsZero (same);
while(true)
{
if(end==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=change(x_char); //change char to int
skip=check_skip (pos,x,y); //skip step if pos is ' '
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;
end= gameover (pos,x,y); //c end game d o nt
}
skip=0;
system("cls");
}
cout<<"Game Over!!!\nYour Score is "<<Total_score<<endl;
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.\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 end=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!=' ')
{
end=1;
break;
}
if(pos[i+1][j]==temp && i!=9 && temp!=' ')
{
end=1;
break;
}
if(pos[i][j-1]==temp && j!=0 && temp!=' ')
{
end=1;
break;
}
if(pos[i][j+1]==temp && j!=9 && temp!=' ')
{
end=1;
break;
}
}
return end;
}
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;
}