OIer 可以在 CSP 考场上做什么

· · 个人记录

背景介绍: 在几天前的某次 CSP 模拟赛时,本人在打完 T1 、 T2 后面对 T3 毫无办法后闲的没事,有玩腻了谷歌小恐龙,于是写了个贪吃蛇玩(甚至把它当 T3 交上去了,显然 CE )。 ::::info[当时写的代码(Dev-C++可运行)]

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <queue>
#define KEY_DOWN(VK_NONAME) (GetAsyncKeyState(VK_NONAME)==-32768?1:0)
using namespace std;
int mp0[10][10]={
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,3 ,3 ,3 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
},mp[10][10];
struct Node{
    int x,y;
};
deque<Node>snake,ept;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int dir,dirtmp;
int main() {
    srand(time(0));
    while(1){
        for(int i=0;i<10;i++)
            for(int j=0;j<10;j++)
                mp[i][j]=mp0[i][j];
        snake=ept;
        snake.push_back({4,3});
        snake.push_back({4,4});
        snake.push_back({4,5});
        dir=dirtmp=0;
        int x=rand()%8+1,y=rand()%8+1;
        while(mp[x][y]==3)
            x=rand()%8+1,y=rand()%8+1;
        mp[x][y]=2;
        int lstrfs=-300;
        while(1){
            if(KEY_DOWN(VK_LEFT)&&dir!=0)
                dirtmp=2;
            else if(KEY_DOWN(VK_RIGHT)&&dir!=2)
                dirtmp=0;
            else if(KEY_DOWN(VK_UP)&&dir!=1)
                dirtmp=3;
            else if(KEY_DOWN(VK_DOWN)&&dir!=3)
                dirtmp=1;
    //      MessageBox(NULL,ch,"0",MB_OK);
            if(clock()-lstrfs>=200){
                lstrfs=clock();
                system("cls");
                dir=dirtmp;
                int xx=snake.back().x+dx[dir],yy=snake.back().y+dy[dir];
                if(mp[xx][yy]==-1||mp[xx][yy]==3) break;
                else{
                    snake.push_back({xx,yy});
                    if(mp[xx][yy]==2){
                        x=rand()%8+1,y=rand()%8+1;
                        while(mp[x][y]==3||mp[x][y]==2)
                            x=rand()%8+1,y=rand()%8+1;
                        mp[x][y]=2;
                    }
                    else{
                        int xxx=snake.front().x,yyy=snake.front().y;
                        snake.pop_front(),mp[xxx][yyy]=0;
                    }
                    mp[xx][yy]=3;
                }
                for(int i=0;i<10;i++){
                    for(int j=0;j<10;j++){
                        if(mp[i][j]==-1) printf("[]");
                        else if(mp[i][j]==0) printf("  ");
                        else if(mp[i][j]==2) printf("<>");
                        else if(mp[i][j]==3) printf("()");
                    }
                    printf("\n");
                }
                if(snake.size()==64){
                    MessageBox(NULL,"You win!","Congratulations",MB_OK);
                    return 0;
                }
            }
        }
        printf("You're died.\n");
        printf("Your score is %d.\n",snake.size()-3);
        system("pause");
    }
    return 0;
}

:::: ::::info[后来修改的代码]

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <fstream>
#include <time.h>
#include <queue>
#define made using
#define by namespace
#define hwq std
#define KEY_DOWN(VK_NONAME) (GetAsyncKeyState(VK_NONAME)==-32768?1:0)
made by hwq;
int mp0[10][10]={
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,3 ,3 ,3 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
},mp[10][10];
int mxscore;
void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void HideCursor(){
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void SetConsoleFontSize(int fontSize) {
    HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize=sizeof(CONSOLE_FONT_INFOEX);
    if (GetCurrentConsoleFontEx(hConsole,FALSE,&cfi)) {
        cfi.dwFontSize.Y=fontSize;
        wcscpy_s(cfi.FaceName,L"Consolas");
        SetCurrentConsoleFontEx(hConsole,FALSE,&cfi);
    } 
    else MessageBox(GetConsoleWindow(),"Cannot fetch console font info!","Error",MB_OK|MB_ICONERROR);
}
struct Node{
    int x,y;
};
deque<Node>snake,ept;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int dir,dirtmp;
int Game(){
    while(1){
        system("cls");
        fstream fio("snakerecord.nwdat",std::ios::out);
        bool f=1;
        for(int i=0;i<10;i++)
            for(int j=0;j<10;j++)
                mp[i][j]=mp0[i][j];
        snake=ept;
        snake.push_back({4,3});
        snake.push_back({4,4});
        snake.push_back({4,5});
        dir=dirtmp=0;
        int x=rand()%8+1,y=rand()%8+1;
        while(mp[x][y]==3)
            x=rand()%8+1,y=rand()%8+1;
        mp[x][y]=2;
        int lstrfs=-333;
        while(1){
            if(KEY_DOWN(VK_UP)&&dir!=0)
                dirtmp=2;
            else if(KEY_DOWN(VK_DOWN)&&dir!=2)
                dirtmp=0;
            else if(KEY_DOWN(VK_LEFT)&&dir!=1)
                dirtmp=3;
            else if(KEY_DOWN(VK_RIGHT)&&dir!=3)
                dirtmp=1;
            else if(KEY_DOWN(VK_CONTROL)){
                gotoxy(0,11);
                printf("paused");
                while(!KEY_DOWN(VK_RETURN)&&!KEY_DOWN('Z'));
                printf("\b\b\b\b\b\b      ");
            }
            if(clock()-lstrfs>=333){
                lstrfs=clock();
                dir=dirtmp;
                int xx=snake.back().x+dx[dir],yy=snake.back().y+dy[dir];
                mp[snake.back().x][snake.back().y]=3;
                if(mp[xx][yy]==2){
                    snake.push_back({xx,yy});
                    if(snake.size()^64){
                        x=rand()%8+1,y=rand()%8+1;
                        while(mp[x][y]==3||mp[x][y]==2)
                            x=rand()%8+1,y=rand()%8+1;
                        mp[x][y]=2;
                    }
                    mp[xx][yy]=1;
                }
                else{
                    int xxx=snake.front().x,yyy=snake.front().y;
                    snake.pop_front(),mp[xxx][yyy]=0;
                    if(mp[xx][yy]==-1||mp[xx][yy]==3){
                        snake.push_front({xxx,yyy});
                        mp[xxx][yyy]=3;
                        break;
                    }
                    else{
                        snake.push_back({xx,yy});
                        mp[xx][yy]=1;
                    }
                }
                for(int i=0;i<10;i++){
                    for(int j=0;j<10;j++){
                        gotoxy(i<<1,j);
                        if(mp[i][j]==-1) printf("[]");
                        else if(mp[i][j]==0) printf("  ");
                        else if(mp[i][j]==1) printf("{}");
                        else if(mp[i][j]==2) printf("<>");
                        else if(mp[i][j]==3) printf("()");
                    }
                }
                for(int j=0;j<10;j++){
                    for(int i=0;i<10;i++){
                        if(mp[i][j]==-1) fio<<"[]";
                        else if(mp[i][j]==0) fio<<"  ";
                        else if(mp[i][j]==1) fio<<"{}";
                        else if(mp[i][j]==2) fio<<"<>";
                        else if(mp[i][j]==3) fio<<"()";
                    }
                    fio<<"\n";
                }
                gotoxy(0,10);
                printf("Score:%d",snake.size()-3);
                if(snake.size()==64){
                    f=0;
                    MessageBox(NULL,"You won!","Congratulations",MB_OK);
                    break;
                }
            }
        }
        system("cls");
        if(f) printf("You're died.\n");
        else printf("You won!\n");
        printf("Your score is %d.\n",snake.size()-3);
        fio.close();
        Sleep(100);
        if((int)snake.size()-3>mxscore){
            printf("Score refreshed!\n");
            system("del snakerecord.dat");
            system("ren snakerecord.nwdat snakerecord.dat");
            Sleep(100);
            fstream fo("mxscore.dat",std::ios::out);
            fo<<(int)snake.size()-3;
            fo.close();
            mxscore=snake.size()-3;
        }
        else system("del snakerecord.nwdat");
        printf("Press R to play again.\n");
        printf("Press C to quit.\n");
        while(!KEY_DOWN('R')&&!KEY_DOWN('C'));
        if(KEY_DOWN('C')) break;
    }
    return 0;
}
void PrtMenu(){
    system("cls");
    gotoxy(0,0);
    for(int i=1;i<=16;i++) printf("-");
    gotoxy(3,1);
    printf("Snake Game");
    gotoxy(0,2);
    for(int i=1;i<=16;i++) printf("-");
    gotoxy(3,3);
    printf("Start!");
    gotoxy(3,4);
    printf("Max Score Record");
    gotoxy(3,5);
    printf("Delete Record");
    gotoxy(3,6);
    printf("Control");
    gotoxy(3,7);
    printf("Exit");
}
void ReadRecord(){
    system("cls");
    fstream fio("snakerecord.dat");
    if(!fio.is_open()){
        printf("ERR:Record doesn't exist.\n");
        fio.close();
        system("pause");
        PrtMenu();
        return;
    }
    Sleep(100);
    char str[80];
    while(!fio.eof()){
        gotoxy(0,0);
        for(int i=0;i<10;i++){
            fio.getline(str,80);
            printf("%s\n",str);
        }
        Sleep(300);
    }
    fio.close();
    system("pause");
}
void DelRecord(){
    system("cls");
    gotoxy(0,0);
    printf("Are you sure that\nyou want to delete your record?\n (Y/N)");
    bool f=false;
    while(!KEY_DOWN(VK_RETURN)){
        gotoxy(0,3);
        while(!KEY_DOWN('Y')&&!KEY_DOWN('N')&&!KEY_DOWN(VK_RETURN));
        if(KEY_DOWN('Y')) f=1,printf("Y");
        else if(KEY_DOWN('N')) f=0,printf("N");
    }
    if(f) mxscore=0,system("del snakerecord.dat mxscore.dat");
    else Sleep(300);
}
void PrtControl(){
    system("cls");
    printf("Use ↑&↓&←&→ to move.\nUse CTRL(Control) to pause.\nUse Enter(Return) or Z to select or end pausing.\n");
    system("pause");
}
void MainMenu(){
    PrtMenu();
    int lstpos=3,pos=3;
    int lstrfs=clock();
    int con=0;
    while(1){
        if(KEY_DOWN(VK_DOWN)&&pos^7) con=1;
        else if(KEY_DOWN(VK_UP)&&pos^3) con=-1;
        else if(KEY_DOWN(VK_RETURN)||KEY_DOWN('Z')) con=2;
        if(clock()-lstrfs>=300){
            lstrfs=clock();
            gotoxy(0,lstpos);
            printf("   ");
            gotoxy(0,pos);
            printf(" ->");
            if(con^2){
                lstpos=pos;
                pos+=con;
                con=0;
            }
            else if(con){
                switch(pos){
                    case 3: 
                        Game();
                        break;
                    case 4:
                        ReadRecord();
                        break;
                    case 5:
                        DelRecord();
                        break;
                    case 6:
                        PrtControl();
                        break;
                    case 7:
                        gotoxy(0,10);
                        system("taskkill -f -im main.exe");
                        break;
                }
                PrtMenu();
                con=0;
            }
        }
    }
}
int main(){
    srand(time(0));
    fstream fio("mxscore.dat",std::ios::in);
    if(!fio.eof()) fio>>mxscore;
    fio.close();
    SetConsoleFontSize(36);
    SetWindowPos(GetConsoleWindow(),NULL,128,128,512,512,0);
    HideCursor();
    MainMenu();
    return 0;
}

:::: ::::info[Dev-C++能运行的]

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <fstream>
#include <cstring>
#include <time.h>
#include <queue>
#include <tchar.h>
#define made using
#define by namespace
#define hwq std
#define KEY_DOWN(VK_NONAME) (GetAsyncKeyState(VK_NONAME)==-32768?1:0)
made by hwq;
int mp0[10][10]={
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,3 ,3 ,3 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
},mp[10][10];
int mxscore;
void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void HideCursor(){
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void SetConsoleFontSize(int fontSize) {
    HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize=sizeof(CONSOLE_FONT_INFOEX);
    if (GetCurrentConsoleFontEx(hConsole,FALSE,&cfi)) {
        cfi.dwFontSize.Y=fontSize;
        wcscpy(cfi.FaceName,L"Consolas");
        SetCurrentConsoleFontEx(hConsole,FALSE,&cfi);
    } 
    else MessageBox(GetConsoleWindow(),"Cannot fetch console font info!","Error",MB_OK|MB_ICONERROR);
}
struct Node{
    int x,y;
};
deque<Node>snake,ept;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int dir,dirtmp;
int Game(){
    while(1){
        system("cls");
        fstream fio("snakerecord.nwdat",std::ios::out);
        bool f=1;
        for(int i=0;i<10;i++)
            for(int j=0;j<10;j++)
                mp[i][j]=mp0[i][j];
        snake=ept;
        snake.push_back({4,3});
        snake.push_back({4,4});
        snake.push_back({4,5});
        dir=dirtmp=0;
        int x=rand()%8+1,y=rand()%8+1;
        while(mp[x][y]==3)
            x=rand()%8+1,y=rand()%8+1;
        mp[x][y]=2;
        int lstrfs=-333;
        while(1){
            if(KEY_DOWN(VK_UP)&&dir!=0)
                dirtmp=2;
            else if(KEY_DOWN(VK_DOWN)&&dir!=2)
                dirtmp=0;
            else if(KEY_DOWN(VK_LEFT)&&dir!=1)
                dirtmp=3;
            else if(KEY_DOWN(VK_RIGHT)&&dir!=3)
                dirtmp=1;
            else if(KEY_DOWN(VK_CONTROL)){
                gotoxy(0,11);
                printf("paused");
                while(!KEY_DOWN(VK_RETURN)&&!KEY_DOWN('Z'));
                printf("\b\b\b\b\b\b      ");
            }
            if(clock()-lstrfs>=333){
                lstrfs=clock();
                dir=dirtmp;
                int xx=snake.back().x+dx[dir],yy=snake.back().y+dy[dir];
                mp[snake.back().x][snake.back().y]=3;
                if(mp[xx][yy]==2){
                    snake.push_back({xx,yy});
                    if(snake.size()^64){
                        x=rand()%8+1,y=rand()%8+1;
                        while(mp[x][y]==3||mp[x][y]==2)
                            x=rand()%8+1,y=rand()%8+1;
                        mp[x][y]=2;
                    }
                    mp[xx][yy]=1;
                }
                else{
                    int xxx=snake.front().x,yyy=snake.front().y;
                    snake.pop_front(),mp[xxx][yyy]=0;
                    if(mp[xx][yy]==-1||mp[xx][yy]==3){
                        snake.push_front({xxx,yyy});
                        mp[xxx][yyy]=3;
                        break;
                    }
                    else{
                        snake.push_back({xx,yy});
                        mp[xx][yy]=1;
                    }
                }
                for(int i=0;i<10;i++){
                    for(int j=0;j<10;j++){
                        gotoxy(i<<1,j);
                        if(mp[i][j]==-1) printf("[]");
                        else if(mp[i][j]==0) printf("  ");
                        else if(mp[i][j]==1) printf("{}");
                        else if(mp[i][j]==2) printf("<>");
                        else if(mp[i][j]==3) printf("()");
                    }
                }
                for(int j=0;j<10;j++){
                    for(int i=0;i<10;i++){
                        if(mp[i][j]==-1) fio<<"[]";
                        else if(mp[i][j]==0) fio<<"  ";
                        else if(mp[i][j]==1) fio<<"{}";
                        else if(mp[i][j]==2) fio<<"<>";
                        else if(mp[i][j]==3) fio<<"()";
                    }
                    fio<<"\n";
                }
                gotoxy(0,10);
                printf("Score:%d",snake.size()-3);
                if(snake.size()==64){
                    f=0;
                    MessageBox(NULL,"You won!","Congratulations",MB_OK);
                    break;
                }
            }
        }
        system("cls");
        if(f) printf("You're died.\n");
        else printf("You won!\n");
        printf("Your score is %d.\n",snake.size()-3);
        fio.close();
        Sleep(100);
        if((int)snake.size()-3>mxscore){
            printf("Score refreshed!\n");
            system("del snakerecord.dat");
            system("ren snakerecord.nwdat snakerecord.dat");
            Sleep(100);
            fstream fo("mxscore.dat",std::ios::out);
            fo<<(int)snake.size()-3;
            fo.close();
            mxscore=snake.size()-3;
        }
        else system("del snakerecord.nwdat");
        printf("Press R to play again.\n");
        printf("Press C to quit.\n");
        while(!KEY_DOWN('R')&&!KEY_DOWN('C'));
        if(KEY_DOWN('C')) break;
    }
    return 0;
}
void PrtMenu(){
    system("cls");
    gotoxy(0,0);
    for(int i=1;i<=16;i++) printf("-");
    gotoxy(3,1);
    printf("Snake Game");
    gotoxy(0,2);
    for(int i=1;i<=16;i++) printf("-");
    gotoxy(3,3);
    printf("Start!");
    gotoxy(3,4);
    printf("Max Score Record");
    gotoxy(3,5);
    printf("Delete Record");
    gotoxy(3,6);
    printf("Control");
    gotoxy(3,7);
    printf("Exit");
}
void ReadRecord(){
    system("cls");
    fstream fio("snakerecord.dat");
    if(!fio.is_open()){
        printf("ERR:Record doesn't exist.\n");
        fio.close();
        system("pause");
        PrtMenu();
        return;
    }
    Sleep(100);
    char str[80];
    while(!fio.eof()){
        gotoxy(0,0);
        for(int i=0;i<10;i++){
            fio.getline(str,80);
            printf("%s\n",str);
        }
        Sleep(300);
    }
    fio.close();
    system("pause");
}
void DelRecord(){
    system("cls");
    gotoxy(0,0);
    printf("Are you sure that\nyou want to delete your record?\n (Y/N)");
    bool f=false;
    while(!KEY_DOWN(VK_RETURN)){
        gotoxy(0,3);
        while(!KEY_DOWN('Y')&&!KEY_DOWN('N')&&!KEY_DOWN(VK_RETURN));
        if(KEY_DOWN('Y')) f=1,printf("Y");
        else if(KEY_DOWN('N')) f=0,printf("N");
    }
    if(f) mxscore=0,system("del snakerecord.dat mxscore.dat");
    else Sleep(300);
}
void PrtControl(){
    system("cls");
    printf("Use ^&v&<&> to move.\nUse CTRL(Control) to pause.\nUse Enter(Return) or Z to select or end pausing.\n");
    system("pause");
}
void MainMenu(){
    PrtMenu();
    int lstpos=3,pos=3;
    int lstrfs=clock();
    int con=0;
    while(1){
        if(KEY_DOWN(VK_DOWN)&&pos^7) con=1;
        else if(KEY_DOWN(VK_UP)&&pos^3) con=-1;
        else if(KEY_DOWN(VK_RETURN)||KEY_DOWN('Z')) con=2;
        if(clock()-lstrfs>=300){
            lstrfs=clock();
            gotoxy(0,lstpos);
            printf("   ");
            gotoxy(0,pos);
            printf(" ->");
            if(con^2){
                lstpos=pos;
                pos+=con;
                con=0;
            }
            else if(con){
                switch(pos){
                    case 3: 
                        Game();
                        break;
                    case 4:
                        ReadRecord();
                        break;
                    case 5:
                        DelRecord();
                        break;
                    case 6:
                        PrtControl();
                        break;
                    case 7:
                        gotoxy(0,10);
                        system("taskkill -f -im main.exe");
                        break;
                }
                PrtMenu();
                con=0;
            }
        }
    }
}
int main(){
    srand(time(0));
    fstream fio("mxscore.dat",std::ios::in);
    if(!fio.eof()) fio>>mxscore;
    fio.close();
    SetConsoleFontSize(36);
    SetWindowPos(GetConsoleWindow(),NULL,128,128,512,512,0);
    HideCursor();
    MainMenu();
    return 0;
}

:::: 我认为当时写的还是挺好理解的,可以在 CSP 考场上写出来玩(