Same GNOME
(C++ Game)



Same GNOME is a part of GNOME games.
/* ##################################### # Same GNOME # # tested in Turbo C++,version 3.0 # #############15/10/2008############## */ #include <dos.h> #include <math.h> #include <time.h> #include <conio.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <graphics.h> #define TEN 10 #define ROWS 10 #define COLS 15 #define MIDX 320 #define MIDY 240 #define CELLSIZE 41 #define BOARDX1 12 #define BOARDY1 60 #define TOATALLINES 19 #define NICKNAMELEN 10 #define BOXBORDER YELLOW #define LIGHT 8 #define BGCOLOR BLACK #define BORDERCOLOR WHITE int score; int colors[]={RED,GREEN,BLUE}; enum bool {false,true}; char ts[40]; // temporary string. void InitMouse() { union REGS r; r.x.ax=0x0, int86(0x33, &r, &r); } void ShowMouse() { union REGS r; r.x.ax = 0x1, int86(0x33, &r, &r); } void HideMouse() { union REGS r; r.x.ax =0x2, int86(0x33, &r, &r); } void MouseBoundary() { union REGS r; r.x.cx =BOARDX1, r.x.dx=BOARDX1+COLS*CELLSIZE-1; r.x.ax=0x7; int86(0x33, &r, &r); r.x.cx =BOARDY1, r.x.dx=BOARDY1+ROWS*CELLSIZE-1; r.x.ax=0x8; int86(0x33, &r, &r); } int GetMouse(int *x, int *y) { union REGS r; r.x.ax=0x3, int86(0x33,&r,&r); *x=(int)r.x.cx, *y=(int)r.x.dx; return (r.x.bx); } int ReadMouse(int *x, int *y) { int button; button = GetMouse(x,y); *x=(*x-BOARDX1)/CELLSIZE, *y=(*y-BOARDY1)/CELLSIZE; return button; } class tiles { private: int color, row, col; bool selected, destroyed; public: void DrawTile(); bool AdjustCol(); bool AdjustRow(int col2); void SetSelected(bool b); void Destroy(bool repaint); bool Selected() { return (selected==true) ? true:false; } bool Destroyed() { return (destroyed==true) ? true:false; } int GetColor() { return color; } void SetRC(int r, int c) { row = r; col = c; selected= destroyed = false; } void SetColor(int c) { color = c; DrawTile(); } } tile[ROWS][COLS]; void tiles::DrawTile() { int i, fgColor; if(destroyed) fgColor= BGCOLOR; else if(selected) fgColor= LIGHT+color; else fgColor= color; setcolor(fgColor); HideMouse(); for(i=3; i<=38; i++) line(BOARDX1+col*CELLSIZE+3,BOARDY1+row*CELLSIZE+i,BOARDX1+col*CELLSIZE+38,BOARDY1+row*CELLSIZE+i); ShowMouse(); } void tiles::SetSelected(bool b) { bool haveBuddy=false; if(selected==b) return; selected=b; if(b==true) { if(row!=0&&tile[row-1][col].GetColor()==color&&!tile[row-1][col].Destroyed()) { tile[row-1][col].SetSelected(true); haveBuddy=true; } if(col!=0&&tile[row][col-1].GetColor()==color&&!tile[row][col-1].Destroyed()) { tile[row][col-1].SetSelected(true); haveBuddy=true; } if(col!=14&&tile[row][col+1].GetColor()==color&&!tile[row][col+1].Destroyed()) { tile[row][col+1].SetSelected(true); haveBuddy=true; } if(row!=ROWS-1&&tile[row+1][col].GetColor()==color&&!tile[row+1][col].Destroyed()) { tile[row+1][col].SetSelected(true); haveBuddy=true; } } if(haveBuddy||selected==false) DrawTile(); else selected = false; } bool tiles::AdjustRow(int col2) { destroyed= false; SetColor(tile[row][col2].GetColor()); tile[row][col2].Destroy(false); return true; } void SetRGB() { int i, j, rTiles=0, gTiles=0, bTiles=0; for(i=0; i<ROWS; i++) for(j=0; j<COLS; j++) if(tile[i][j].Destroyed()==false) switch(tile[i][j].GetColor()) { case RED: rTiles++; break; case GREEN: gTiles++; break; case BLUE: bTiles++; } textcolor(YELLOW); gotoxy(64,3), printf("R:%-2d G:%-2d B:%-2d",rTiles,gTiles,bTiles); } void InitTiles() { int i, j, k, colorIndex[3]; for(i=0; i<ROWS; i++) { for(j=0; j<COLS; j+=3) { colorIndex[0]=random(3); do colorIndex[1]=random(3); while(colorIndex[1]==colorIndex[0]); colorIndex[2]=3-colorIndex[0]-colorIndex[1]; for(k=0; k<3; k++) { tile[i][j+k].SetRC(i,j+k); tile[i][j+k].SetColor(colors[colorIndex[k]]); } } } SetRGB(); } void InitGraph() { int gDriver = DETECT, gmode, errorCode; initgraph(&gDriver, &gmode, ""); if ((errorCode=graphresult()) != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorCode)); getch(); exit(1); } } void DrawBorder() { HideMouse(); setcolor(BORDERCOLOR); for(int i=0; i<=ROWS; i++) line(BOARDX1,BOARDY1+CELLSIZE*i,BOARDX1+COLS*CELLSIZE,BOARDY1+CELLSIZE*i); for(i=0; i<=COLS; i++) line(BOARDX1+CELLSIZE*i,BOARDY1,BOARDX1+CELLSIZE*i,BOARDY1+ROWS*CELLSIZE); ShowMouse(); } int SelectedTiles() { int i, j, total=0; for(i=0; i<ROWS; i++) for(j=0; j<COLS; j++) if(tile[i][j].Selected()) total++; return total; } void Select() { int sel= SelectedTiles(); if(sel==0) sprintf(ts,"No tiles selected."); else if(sel==2) sprintf(ts,"Two tiles selected. No points."); else if(sel==3) sprintf(ts,"Three tiles selected. One point."); else sprintf(ts,"%d tiles selected. %d points.",sel,(int)pow(sel-2,2)); gotoxy(3,3), printf("%-33s",ts); } void Score() { score+=(int)pow(SelectedTiles()-2,2); gotoxy(43,3), printf("Score: %-5d",score) ; } void Destroy() { int c, r; Score(); for(r=0; r<ROWS; r++) for(c=0; c<COLS; c++) { if(tile[r][c].Selected()==false) continue; tile[r][c].Destroy(true); } } bool tiles::AdjustCol() { int i; if(destroyed==false) return false; for(i=row-1; i>=0; i--) if(tile[i][col].Destroyed()==false) { tile[i][col].Destroy(true); destroyed=false; SetColor(tile[i][col].GetColor()); break; } return true; } void tiles::Destroy(bool repaint) { destroyed=true; selected=false; if(repaint==true) DrawTile(); } void RemoveSelection() { int i, j; if(SelectedTiles()==0) return; for(i=0; i<ROWS; i++) for(j=0; j<COLS; j++) tile[i][j].SetSelected(false); Select(); } bool GameOver() { int i, j; for(i=ROWS-1; i>=0; i--) for(j=0; j<COLS-1; j++) { if(tile[i][j].Destroyed()||tile[i][j+1].Destroyed()) continue; if(tile[i][j].GetColor()==tile[i][j+1].GetColor()) return false; } for(j=0; j<COLS; j++) for(i=ROWS-1; i>0; i--) { if(tile[i][j].Destroyed()||tile[i-1][j].Destroyed()) continue; if(tile[i][j].GetColor()==tile[i-1][j].GetColor()) return false; } return true; } void AdjustRows() { int i, j, c1, c2, r; for(c1=0; c1<COLS-1; c1++) { if(!tile[ROWS-1][c1].Destroyed()) continue; for(c2=c1+1; c2<COLS; c2++) { if(tile[ROWS-1][c2].Destroyed()) continue; for(r=ROWS-1; r>=0; r--) { if(tile[r][c2].Destroyed()) break; tile[r][c1].AdjustRow(c2); } break; } } for(j=0 ; j<COLS; j++) for(i=0; i<ROWS; i++) if(tile[i][j].Destroyed()) tile[i][j].DrawTile(); } void AdjustCols() { int c, r; for(c=0; c<COLS; c++) for(r=ROWS-1; r>=0; r--) tile[r][c].AdjustCol(); } void GetName(char *name) { HideMouse(); cleardevice(); setcolor(WHITE); outtextxy(MIDX,200,"Congratulations! Welcome You to Top ten list."); outtextxy(MIDX,220,"Please enter your Nick Name:"); char ch; int len=0; name[0]=NULL; while(kbhit()) getch(); while(1) { ch = getch(); setcolor(BLACK); outtextxy(MIDX,MIDY+40,name); if(len!=0&&ch=='\b') { len--; name[len]=NULL; } else if(ch==13&&name[0]!=NULL) break; else if(isalpha(ch)&&len<TEN) { name[len]=ch; len++; name[len]=NULL; } setcolor(WHITE); outtextxy(MIDX,MIDY+40,name); } ShowMouse(); } void WaitInput() { int x, y; while(1) { if(GetMouse(&x,&y)) { while(GetMouse(&x,&y)!=0); break; } if(kbhit()) { while(kbhit()) getch(); break; } } } struct topTen { char name[NICKNAMELEN+1]; int score; }; void NewTopTen() { topTen top[TEN]= { "Unni" , 3000, "Akhil" , 2900, "Ippu " , 2800, "sappu" , 2700, "Nisha" , 2600, "Jose" , 2500, "Noori" , 2400, "Neethu", 2300, "Reshma", 2200, "Anju" , 2000 }; FILE *writeFile= fopen("topten.sg","w"); if(!writeFile) exit(0); for(int i=0; i<TEN; i++) fprintf(writeFile,"%-12s %5d \n",top[i].name,top[i].score); fclose(writeFile); } void TopTen(bool update,bool display) { FILE *readFile=fopen("topten.sg","r"); if(!readFile) { NewTopTen(); readFile= fopen("topten.sg","r"); } topTen top[TEN]; for(int i=0; i<TEN; i++) { if(fscanf(readFile,"%s %d \n",top[i].name,&top[i].score)!=2) { fclose(readFile); NewTopTen(); readFile= fopen("topten.sg","r"); for(i=0; i<TEN; i++) fscanf(readFile,"%s %d \n",top[i].name,&top[i].score); break; } } fclose(readFile); for(i=0; i<TEN; i++) { if(strlen(top[i].name)>NICKNAMELEN||top[i].score>top[i-1].score&&i!=0) { NewTopTen(); readFile= fopen("topten.sg","r"); for(i=0; i<TEN; i++) fscanf(readFile,"%s %d \n",top[i].name,&top[i].score); fclose(readFile); break; } } if(update&&score>top[TEN-1].score) { display=true; char name[NICKNAMELEN+1]; GetName(name); for(i=0; i<TEN; i++) if(score>top[i].score) break; for(int j=ROWS-1; j>i; j--) top[j] = top[j-1]; top[i].score=score; strcpy(top[i].name,name); FILE *writeFile= fopen("topten.sg","w"); if(!writeFile) exit(0); for(i=0; i<TEN; i++) fprintf(writeFile,"%-12s %5d \n",top[i].name,top[i].score); fclose(writeFile); } if(display==false) return; HideMouse(); cleardevice(); setcolor(LIGHTGREEN); setfillstyle(SOLID_FILL,BLUE); bar3d(200,100,440,380,10,1); outtextxy(MIDX,138,"_________________"); outtextxy(MIDX,140,"_________________"); setcolor(YELLOW); outtextxy(MIDX,130,"TOP TEN PLAYERS"); setcolor(WHITE); for(i=0; i<TEN; i++) { sprintf(ts,"%-10s %5d",top[i].name,top[i].score); outtextxy(MIDX,170+20*i,ts); } ShowMouse(); WaitInput(); } void NewGame() { int i, j; int button=0, x, y; score= 0; HideMouse(); cleardevice(); gotoxy( 3,3), printf("No tiles selected."); gotoxy(64,3), printf("R:50 G:50 B:50"); gotoxy(43,3), printf("Score: none") ; ShowMouse(); while(kbhit()) getch(); setbkcolor(BLACK); DrawBorder(); InitTiles(); while(1) { button = ReadMouse(&x,&y); if(tile[y][x].Selected()==false&&tile[y][x].Destroyed()==false) { RemoveSelection(); tile[y][x].SetSelected(true); if(SelectedTiles()>1) Select(); } else if(tile[y][x].Destroyed()==true&&SelectedTiles()>1) RemoveSelection(); if(button==1&&tile[y][x].Selected()) { Destroy(); SetRGB(); delay(250); AdjustCols(); AdjustRows(); delay(250); Select(); if(GameOver()) { HideMouse(); cleardevice(); setcolor(WHITE); if(tile[ROWS-1][0].Destroyed()) { outtextxy(MIDX,MIDY,"Bonus 1000 points"); score+=1000; delay(1500); cleardevice(); } outtextxy(MIDX,MIDY,"GAME OVER!"); delay(1500); cleardevice(); sprintf(ts,"Score: %d",score); outtextxy(MIDX,MIDY,ts); delay(1500); ShowMouse(); while(ReadMouse(&x,&y)); TopTen(true,false); return; } while(GetMouse(&x,&y)); } if(kbhit()&&getch()==27) break; } } void Help() { HideMouse(); cleardevice(); setcolor(LIGHTGREEN); setfillstyle(SOLID_FILL,BLUE); bar3d(50,50,590,430,0,0); outtextxy(MIDX,95,"__________________"); outtextxy(MIDX,97,"__________________"); setcolor(YELLOW); outtextxy(MIDX,87,"Playing Same GNOME"); char *para[TOATALLINES]= { " Same GNOME is a part of GNOME games. The goal is to remove", "as many tiles as possible in as few moves as possible. Tiles that", "are adjacent to each other get removed as a group. The remaining", "tiles then collapse to fill in the gaps and new groups are formed.", "You cannot remove single tiles. ", "", " The playing area contains three types of tiles. Each type is", "a different color. Tiles of the same type in squares that border", "each other are grouped together. If you click on one tile in a", "group, you remove all of the tiles in that group. The more tiles", "in the group, the more points you will score.", "", " The game is over when no groups of tiles remain. If there are", "no tiles left, you receive a bonus 1000 points.", "", " Scoring is based on the number of tiles you delete. if you", "remove N tiles, then you will get [N-2]\xFD points. If you clear the", "board, there is a 1000 point bonus." }; settextjustify(LEFT_TEXT, TOP_TEXT); setcolor(WHITE); for(int i=0; i<TOATALLINES; i++) outtextxy(59,130+15*i,para[i]); ShowMouse(); settextjustify(CENTER_TEXT, CENTER_TEXT); WaitInput(); } void DrawMainWindow() { HideMouse(); cleardevice(); setbkcolor(BLACK); settextstyle(0,HORIZ_DIR,3); setcolor(WHITE); outtextxy(MIDX,100,"SAME GNOME"); settextstyle(0,HORIZ_DIR,1); setfillstyle(SOLID_FILL,RED); setcolor(BOXBORDER); bar3d(245, 190, 395, 340, 15, 1); floodfill(260,188,BOXBORDER); floodfill(400,330,BOXBORDER); setcolor(LIGHTGREEN); outtextxy(MIDX,400,"http://programs.on.googlepages.com"); ShowMouse(); } void MainMenu() { int i, x, y; char *menu[]={"NEW GAME","TOP TEN","HELP","EXIT"}; DrawMainWindow(); int selected=-2, index; while(1) { if(GetMouse(&x,&y)==1) { while(GetMouse(&x,&y)); while(kbhit()) getch(); switch(selected) { case 0 : NewGame(); break; case 1 : TopTen(false,true); break; case 2 : Help(); break; case 3 : closegraph(); exit(0); default: continue; } selected=-2; DrawMainWindow(); } if(280<x&&x<360&&MIDY-30<y&&y<MIDY+120) { index=(y-MIDY+30)/30; if(selected==index) continue; selected=index; } else if(selected!=-1) selected=-1; else continue; HideMouse(); for(i=0; i<4; i++) { setcolor((i==selected)?WHITE:LIGHTGREEN); outtextxy(MIDX,MIDY+i*30-20,menu[i]); } ShowMouse(); } } void main() { randomize(); InitGraph(); InitMouse(); ShowMouse(); MouseBoundary(); settextjustify(CENTER_TEXT, CENTER_TEXT); MainMenu(); } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report bugs to: programs.on@gmail.com * ********************************************740*lines**/

            Click here to download the executable file and source file of this program. I am not responsible for this program if it causes any damages.