C++러시아 블록 소스 구현

19132 단어 C++러시아 블록
본 논문 의 사례 는 C+러시아 블록 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
먼저 효과 그림:
주 메뉴:

게임:

설정:

오류 처리:

코드:

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <fstream.h>
#include <time.h>
#include <cstring>
#pragma comment( lib,"winmm.lib" )
//  
    //  
#define NO 0
#define SQR 1
    //    
#define OK 0
#define CANTMOVE 1
    //  
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
    //   
#define no_enough_memory    0
#define set_no_found    1
#define dat_no_found    2
#define error_argument  3
//    
    //  
void play();//    
void sets();//  
void highscores();//   
void copyright();//  
    //  
void mapsetup();//    
bool newsqr();//    ,        
int move(int direction);//    ,     
void movetomap();//           
int wholeline();//         ,    ,-1    
void deleteline(int which);//    
void endup();//    ,    
    //  
void show();//    
void showmenu(char* menu);//    
    //  
void loadset();//    
void saveset();//    
void loadhs();//     
bool addscores(int score,char name[50]);//      ,       
void savehs();//     
    //    
int get(int x,int y);
void set(int x,int y,int date);
//  
    //  
struct{
    int xs,ys;//    
    int speed;//  
    char sqr[3],no[3],frame[3];//  、   、     
}gameset;
    //   
struct{
    char name[50];
    int score;
}rating[10];
//    
    //  
int* map=NULL;//  
bool now[4][4];//    
int xnow,ynow;//    
int guide;//  
    //  
const bool shap[7][4][4]={//  
{\
0,0,0,0,\
0,0,0,0,\
1,1,1,1,\
0,0,0,0,\
},\
{\
0,0,0,0,\
0,1,1,0,\
0,1,1,0,\
0,0,0,0,\
},\
{\
0,0,0,0,\
0,1,1,1,\
0,0,1,0,\
0,0,0,0,\
},\
{\
0,0,0,0,\
1,0,0,0,\
1,1,1,0,\
0,0,0,0,\
},\
{\
0,0,0,0,\
0,0,0,1,\
0,1,1,1,\
0,0,0,0,\
},\
{\
0,1,0,0,\
0,1,1,0,\
0,0,1,0,\
0,0,0,0,\
},\
{\
0,0,1,0,\
0,1,1,0,\
0,1,0,0,\
0,0,0,0,\
}\
};
const char errword[4][50]={"           ","            set.ini","             highscore.dat","            "};
    //   
HANDLE    hout;//     
COORD curpos={0,0};//    
//   
int main()
{
start1:
    try
    {
    hout = GetStdHandle(STD_OUTPUT_HANDLE);//       ,      
    srand(time(0));//              
    loadset();//  
    loadhs();
start2:
    while(1)
    {
        showmenu("     

1.
2.
3.
4.
5.
"); switch(getch()) { case '1': system("cls");//play , play(); break; case '2': sets(); break; case '3': highscores(); break; case '4': copyright(); break; case '5': savehs();// saveset(); return 0; } } } catch(int errnum)// { system("cls"); printf("o(>n<)o !
, :%d(%s)

",errnum,errword[errnum]); printf("

1.
2.
3.
4.
"); switch(getch()) { case '1': goto start1; case '2': gameset.xs=20; gameset.ys=20; gameset.speed=100; strcpy(gameset.sqr,"[]");// strcpy(gameset.no," "); strcpy(gameset.frame,"::"); int i; for(i=0;i<10;i++) strcpy(rating[i].name," "),rating[i].score=0; goto start2; case '3': { ofstream fout; fout.open("set.ini"); fout<<"20
20
100[]

::
"; fout.close(); fout.clear(); fout.open("highscore.dat"); int j; for(j=0;j<10;j++) fout<<"
0
"; goto start1; } default: return -1;// } } return 0; } void play() { mapsetup();// /*for(int i=0;i<20;i++) set(i,19,SQR);*/ while(newsqr())// , NO { while(move(DOWN)!=CANTMOVE)// , { guide+=1;// 1 show();// while(kbhit())// , { switch(getch())// { case 'w': move(UP); break; case 's': move(DOWN); break; case 'a': move(LEFT); break; case 'd': move(RIGHT); break; } } Sleep(gameset.speed);// } movetomap();// , int line; while((line=wholeline())!=-1);// , deleteline(line);// } endup();// , return;// } // void mapsetup() { map=new int[gameset.xs*gameset.ys];// if(!map)// 0 throw no_enough_memory;// // int i,j; for(i=0;i<gameset.xs;i++) { for(j=0;j<gameset.ys;j++) { set(i,j,NO); } } guide=0;// return; } int get(int x,int y) { if(y<0)// return NO; if(x>=0&&x<gameset.xs&&y>=0&&y<gameset.ys)// return *(map+y*gameset.xs+x);// else return SQR;// } void set(int x,int y,int date) { if(x>=0&&x<gameset.xs&&y>=0&&y<gameset.ys)//if(x>0&&x<gameset.xs&&y>0&&y<gameset.ys)// *(map+y*gameset.xs+x)=date;// return; } bool newsqr(){ int i,j; for(i=0;i<4;i++)// if(get(gameset.xs/2+i,0)==SQR) return false;// , int which=rand()%7;// for(i=0;i<4;i++) { for(j=0;j<4;j++) { now[i][j]=shap[which][i][j];// } } for(i=rand()%4;i>0;i--)// 0-3 move(UP); xnow=gameset.xs/2;// ynow=-4; return true; } int move(int direction){ int x,y;// int i,j; switch(direction) { case UP:// bool newshap[4][4];// for(i=0;i<4;i++) { for(j=0;j<4;j++) { newshap[i][j]=now[j][3-i];// } } for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(newshap[i][j]==true&&get(xnow+i,ynow+j)==SQR)// return CANTMOVE;// } } for(i=0;i<4;i++) { for(j=0;j<4;j++) { now[i][j]=newshap[i][j];// , } } return OK; case DOWN:// , x=0,y=1; break; case LEFT: x=-1;y=0; break; case RIGHT: x=1,y=0; break; } for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(now[i][j]==true&&get(i+x+xnow,j+y+ynow)==SQR)// ( get SQR, )//if(get(i+x,j+y)==SQR)//if(now[i+x][j+y]==SQR) { return CANTMOVE;// } } } xnow+=x;// , ynow+=y; return OK; } void movetomap(){ guide+=10;// , 10 int i,j; for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(now[i][j]==true) set(xnow+i,ynow+j,SQR);// } } return; } int wholeline(){ int i,j; bool whole;// for(j=0;j<gameset.ys;j++)//for(i=0;i<gameset.ys;i++) { whole=true;// for(i=0;i<gameset.xs;i++)//for(j=0;j<gameset.xs;j++) { if(get(i,j)==NO) whole=false;// , } if(whole) return j;// , } return -1;// , } void deleteline(int which){ int i,j; guide+=1000;// , for(i=which;i>=0;i--) { for(j=0;j<gameset.xs;j++) { set(j,i,get(j,i-1));// , 。 , } } return; } void endup(){ delete map;// system("cls"); while(kbhit())// getchar(); showmenu(" , :"); char name[50]="noname"; scanf("%s",&name[0]);// char word[1000];// sprintf(&word[0]," !

(%s) :%d

%s

・・・
",name,guide,((addscores(guide,name))?" ":" ")); showmenu(&word[0]); getch(); highscores();// savehs();// return; } void show(){ int i,j; SetConsoleCursorPosition(hout,curpos);//system("cls");// , printf(" :%d
",guide); for(i=0;i<gameset.xs+2;i++)// printf(gameset.frame); printf("
"); for(j=0;j<gameset.ys;j++) { printf(gameset.frame);// for(i=0;i<gameset.xs;i++) { if(i>=xnow&&i<(xnow+4)&&j>=ynow&&j<(ynow+4))//if(i>=xnow&&i<(xnow+1)&&j>=ynow&&j<(ynow+1))// { if(now[i-xnow][j-ynow]==true)// printf(gameset.sqr); else if(get(i,j)==SQR)// printf(gameset.sqr); else// , printf(gameset.no); } else// , { if(get(i,j)==SQR)// printf(gameset.sqr); else// , printf(gameset.no); } } printf("::
");// } for(i=0;i<gameset.xs+2;i++)// printf(gameset.frame); printf("
"); return; } void showmenu(char* menu) { int i,j; char output[100];// system("cls"); for(i=0;i<gameset.xs;i++) printf(gameset.frame);// printf("
"); i=0,j=0; while(*menu!='\0') { printf(gameset.frame);// for(i=0,j=0;*(menu+i)!='
'&&*(menu+i)!='\0';i++,j++)// { if(*(menu+i)=='\t')// , 6 { for(;j%6!=5;j++) output[j]=gameset.no[0]; j--; } else output[j]=*(menu+i);// } menu=menu+i+1;// for(;j<gameset.xs*2-6;j++)// output[j]=gameset.no[0]; output[j]='\0';// printf(gameset.no);// , printf(output);// printf(gameset.frame);// printf("
");// Sleep(100);// , } for(i=0;i<gameset.xs;i++) printf(gameset.frame);// printf("
"); return; } void sets() { char word[1000];// while(1) {// '\' sprintf(&word[0],"\
\ :
\ :
\ \t1. :%d
\ \t2. :%d
\ :
\ \t3. :%d
\ :
\ \t4. :\"%s\"
\ \t5. :\"%s\"
\ \t6. :\"%s\"
\ 7.
\ ",gameset.xs,gameset.ys,gameset.speed,gameset.sqr,gameset.no,gameset.frame); showmenu(&word[0]); char choice=getch(); showmenu(" :");// switch(choice)// { case '1': scanf("%d",&gameset.xs); if(gameset.xs<15||gameset.xs>70) throw error_argument; break; case '2': scanf("%d",&gameset.ys); if(gameset.ys<15||gameset.ys>70) throw error_argument; break; case '3': scanf("%d",&gameset.speed); if(gameset.speed<0) throw error_argument; break; case '4': cin.getline(&gameset.sqr[0],3);//scanf("%s",&gameset.sqr[0]); cout<<endl; //gameset.sqr[3]='\0'; break; case '5': cin.getline(&gameset.no[0],3);//scanf("%s",&gameset.no[0]); cout<<endl; //gameset.no[3]='\0'; break; case '6': cin.getline(&gameset.frame[0],3);//scanf("%s",&gameset.frame[0]); cout<<endl; //gameset.frame[3]='\0'; break; case '7': saveset();// return; } } } void highscores() { int i; char word[1000]="
\t \t
\0";// for(i=0;i<10;i++) { sprintf(&word[0],"%s%d\t%s\t%d
",&word[0],i+1,rating[i].name,rating[i].score);// } sprintf(&word[0],"%s ・・・
",&word[0]);// showmenu(&word[0]); getch();// return; } void copyright() { showmenu("\ a,s,d,w ,
\ a,s,d
\ ,
\ w
\
\
\
\ ・・・
\ "); getch(); return; } void loadset() { ifstream fin; fin.open("set.ini",ios::in|ios::nocreate);// if(!fin) throw set_no_found; fin>>gameset.xs>>gameset.ys>>gameset.speed; fin.getline(gameset.sqr,4);// , fin.getline(gameset.no,4); fin.getline(gameset.frame,4); return; } void saveset() { ofstream fout;// fout.open("set.ini",ifstream::out|ios::nocreate); if(!fout) throw set_no_found; fout<<gameset.xs<<'
'<<gameset.ys<<'
'<<gameset.speed<<gameset.sqr<<'
'<<gameset.no<<'
'<<gameset.frame<<'
'; return; } void loadhs() { int i; ifstream fin; fin.open("highscore.dat",ifstream::in|ios::nocreate);// , if(!fin)// throw dat_no_found; for(i=0;i<10;i++)// fin>>rating[i].name>>rating[i].score; return; } bool addscores(int score,char name[50]) { int i,j; for(i=0;i<10;i++)// { if(rating[i].score<score)// { for(j=9;j>i;j--)// { for(int k=0;k<50;k++) rating[j].name[k]=rating[j-1].name[k]; rating[j].score=rating[j-1].score; } rating[i].score=score;// strcpy(rating[i].name,name); return true;// } } return false;// } void savehs() { int i; ofstream fout; fout.open("highscore.dat",ifstream::out|ios::nocreate); if(!fout) throw dat_no_found; for(i=0;i<10;i++) fout<<rating[i].name<<'
'<<rating[i].score<<'
'; return; }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기