驗分code

 avatar
unknown
c_cpp
a year ago
11 kB
28
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<conio.h>
#include <limits.h>

#define RED "\033[48;2;180;0;0m\033[38;5;255m %s%d   \033[0;0m"
#define YELLOW "\033[48;2;230;220;0m\033[38;2;0;0;0m %s%d   \033[0;0m"
#define ORANGE "\033[48;2;200;110;0m\033[38;2;0;0;0m %s%d   \033[0;0m"
#define WHITE "\033[48;5;255m\033[38;5;55m %s%d   \033[0;0m"
#define ARROW "\033[48;5;255m\033[38;5;255m   %c   \033[0;0m"
#define CURRENT "\033[48;2;0;255;0m\033[38;2;0;0;0m | %c | \033[0;0m"
#define RESET "\033[48;2;0;0;0m"

#define REDBAR "\033[48;2;180;0;0m\033[38;5;255m       \033[0;0m"
#define YELLOWBAR "\033[48;2;230;220;0m\033[38;2;0;0;0m       \033[0;0m"
#define ORANGEBAR "\033[48;2;200;110;0m\033[38;2;0;0;0m       \033[0;0m"
#define WHITEBAR "\033[48;5;255m\033[38;5;55m       \033[0;0m"
#define GREENBAR "\033[48;2;0;255;0m\033[38;2;0;0;0m       \033[0;0m"

void print_map(int x,int y,int dir,int total,int energy,int map[9][9], int score[9][9]){
    for(int i=0;i<9;i++){
        for(int r=0;r<3;r++){
            for(int j=0;j<9;j++){
                if(i == x && j == y){
                    if(r == 0 || r == 2) printf(GREENBAR);
                    else printf(CURRENT," ^<V>"[dir+1]);
                    printf(RESET);
                }
                else{
                    switch(map[i][j]){
                        case 0:
                            if(r == 0 || r == 2) printf(WHITEBAR);
                            else printf(WHITE, (score[i][j] <10?"  ":" "), score[i][j]);
                            printf(RESET);
                            break;
                        case 1: //RED
                            if(r == 0 || r == 2) printf(REDBAR);
                            else printf(RED, (score[i][j] <10?"  ":" "), score[i][j]);
                            printf(RESET, score[i][j]);
                            break;
                        case 2: //YELLOW
                            if(r == 0 || r == 2) printf(YELLOWBAR);
                            else printf(YELLOW, (score[i][j] <10?"  ":" "), score[i][j]);
                            printf(RESET);
                            break;
                        case 3: //ORANGE
                            if(r == 0 || r == 2) printf(ORANGEBAR);
                            else printf(ORANGE, (score[i][j] <10?"  ":" "), score[i][j]);
                            printf(RESET);
                            break;
                        case 10:
                        case 20:
                        case 30:
                        case 40:
                            if(r == 0 || r == 2) printf(WHITEBAR);
                            else printf(ARROW, " ^<V>"[map[i][j]/10]);
                            printf(RESET);
                            break;
                    }
                }
            }
            printf(RESET);
            printf("\n");
        }
    }
}
int main(){
    int score[9][9] = {
        { 5,60, 2, 0,10, 0, 7, 5, 1},
        {45,10, 5, 0,15, 0,15,25, 6},
        { 0, 0, 1, 7, 8, 0, 0, 9, 5},
        {10,15, 3,15, 4, 0, 7, 0, 3},
        { 0, 0,20,90,25,20, 0, 0, 2},
        { 8, 1,15,20, 2, 7, 6, 8,40},
        { 7, 0, 0, 3,30, 3,20, 5, 6},
        { 4, 0, 0, 1,45, 3,25, 5, 4},
        {75, 9,30, 5, 1, 4, 9, 8, 3}
    };
    int map[9][9] = {
        { 3, 2, 1,40, 1,20, 1, 3, 1},
        { 2, 2, 1,40, 3,20, 2, 2, 1},
        {40,30, 1, 1, 1,30,20, 0, 1},
        { 3, 1, 1, 2, 1,30, 1,30, 0},
        {10,20, 2, 2, 2, 2,20,20, 1},
        { 1, 0, 2, 2, 1, 1, 0, 1, 1},
        { 1,40,10, 1, 1, 1, 2, 3, 1},
        { 1,10,20, 1, 1, 1, 2, 3, 0},
        { 2, 1, 2, 2, 1, 1, 0, 1, 1}
    };

    // print_map(-1, -1, 1, 0, 75, map, score);
    
    int dx[4] = {-1,0,1,0};
    int dy[4] = {0,-1,0,1};
    char info[5] = "NWSE";
    int dir_clock[4] = {0, 3, 2, 1};

    int max = INT_MIN;

    for(int R=0;R<3;R++){
        
        bool task1 = false;
        bool task2 = false;
        bool task3 = false;
        bool task4 = false;
        bool mission = false;

        int dt[9][9] = {0};

        int x,y;
        int dir;
        printf("Enter x,y,dir (1-based):");
        scanf("%d%d%d",&x,&y,&dir);
        x--;y--;dir--;
        x %= 9;
        y %= 9;
        dir %= 4;
        if(x == 1 && y == 1){
            task4 = true;
        }
        int op_c;
        printf("Enter op count:\n");
        scanf("%d",&op_c);
        printf("Enter operation (0~4):\n");
        int op[op_c];
        for(int i=0;i<op_c;i++){
            scanf("%d",&op[i]);
        }
        int total = 0;
        int energy = 75;
        int rc = 0;
        int el[2*op_c];
        int last_op = -1;
        for(int i=0;i<op_c;i++){
            system("cls");
            printf("[ROUND %d]:\n",i+1);
            if((x == 0 && dir == 0) || (y == 0 && dir == 1) || (x == 8 && dir == 2) || (y == 8 && dir == 3)){
                printf("  Turn to %c from %c.\n",info[(dir+2) % 4],info[dir]);
                dir = (dir+2) % 4;
            }else{
                printf("  Move from (%d,%d) to (%d,%d).\n",x+1,y+1,x+1+dx[dir],y+1+dy[dir]);
                x += dx[dir];
                y += dy[dir];
            }
            switch(map[x][y]){
                case 1: //RED
                    total += 3;
                    rc += 1;
                    task2 = (rc >= 12);
                    printf("  Step on \033[31;1;4mRED\033[0m at (%d,%d), get treasure valued 3.\n",x+1,y+1);
                    break;
                case 2: //YELLOW
                    printf("  Step on \033[93;1;4mYELLOW\033[0m at (%d,%d), move to ",x+1,y+1);
                    if(!((x == 0 && dir == 0) || (y == 0 && dir == 1) || (x == 8 && dir == 2) || (y == 8 && dir == 3))){
                        x += dx[dir];
                        y += dy[dir];
                    }
                    printf("(%d,%d)\n",x+1,y+1);
                    break;
                case 3: //ORANGE
                    energy += 2;
                    printf("  Step on \033[1;4m\033[38;5;166mORANGE\033[0m at (%d,%d), get 2 energy.\n",x+1,y+1);
                    if(energy >= 0 && el[2*i-1] < 0 && el[2*i-2] >= 0){
                        mission = true;
                    }
                    break;
                case 10:
                case 20:
                case 30:
                case 40:
                    dir = map[x][y] / 10 - 1;
                    printf("  Step on \033[97;1;4mARROW\033[0m at (%d,%d), turn to %c.\n",x+1,y+1,info[dir]);
                    break;
            }
            el[2*i] = energy;
            if(op[i] != 0){
                if(last_op != -1 && op[last_op] < 0 && energy >= 0){
                    mission = true;
                }
            }
            switch(op[i]){
                case 0:
                    break;
                case 1:
                    dir = (dir+3) % 4;
                    if(energy < 0){
                        total -= 50;
                        printf("  Execute op %d, turn to %c, cost 2 energy.(Run out of energy)\n",op[i],info[dir]);
                    }else{
                        printf("  Execute op %d, turn to %c, cost 2 energy.\n",op[i],info[dir]);
                    }
                    energy -= 2;
                    break;
                case 2:
                    dir = (dir+1) % 4;
                    if(energy < 0){
                        total -= 50;
                        printf("  Execute op %d, turn to %c, cost 2 energy.(Run out of energy)\n",op[i],info[dir]);
                    }else{
                        printf("  Execute op %d, turn to %c, cost 2 energy.(Run out of energy)\n",op[i],info[dir]);
                    }
                    energy -= 2;
                    break;
                case 3:
                    dir = (dir+2) % 4;
                    if(energy < 0){
                        total -= 50;
                        printf("  Execute op %d, turn to %c, cost 3 energy.(Run out of energy)",op[i],info[dir]);
                    }else{
                        printf("  Execute op %d, turn to %c, cost 3 energy.\n",op[i],info[dir]);
                    }
                    energy -= 3;
                    break;
                case 4:
                    if(energy < 0){
                        total -= 50;
                        printf("  Execute op %d, found treasure valued %d, cost %d energy.(Run out of energy)\n",op[i],score[x][y],5+dt[x][y]);
                    }else{
                        printf("  Execute op %d, found treasure valued %d, cost %d energy.\n",op[i],score[x][y],5+dt[x][y]);
                    }
                    total += score[x][y];
                    energy -= (5+dt[x][y]);
                    dt[x][y] += 2;
                    if(dt[x][y] >= 8){
                        task3 = true;
                    }
                    break;
            }
            el[2*i+1] = energy;
            if(op[i] != 0){
                last_op = 2*i+1;
            }
            printf("  End at (%d,%d) facing %c.\n",x+1,y+1,info[dir]);
            printf("  [Status] Energy Remaining: %d, Accumulated Value: \033[93;1;4m%d\033[0m\n\n",energy,total);
            print_map(x,y,dir,total,energy,map,score);
            while(1){
                if(kbhit()){
                    char c = getch();
                    break;
                }
            }
        }
        task1 = (energy == 0);
        if(task1){
            total += 100;
            printf("Game %d finish task 1.\n",R+1);
        }
        if(task2){
            total += 200;
            printf("Game %d finish task 2.\n",R+1);
        }
        if(task3){
            total += 100;
            printf("Game %d finish task 3.\n",R+1);
        }
        if(task4){
            total += 100;
            printf("Game %d finish task 4.\n",R+1);
        }
        if(mission){
            total += 200;
            printf("Mission complete.\n");
        }
        if(energy > 10){
            // total = total * 9 / 10;
            printf("Game %d end with %d energy and treasure valued \033[93;1;4m%d\033[0m totally.(After Energy Penalty)\n",R+1,energy,total);
        }else{
            printf("Game %d end with %d energy and treasure valued \033[93;1;4m%d\033[0m totally.\n",R+1,energy,total);
        }
        max = max>total? max:total;
        while(1){
            if(kbhit()){
                char c = getch();
                if(c == 'n'){
                    system("cls");
                    break;
                }
            }
        }
    }
    max = max<800? max:800;
    max = max>0? max:0;
    printf("Final Score: %d\n",max);
    return 0;
}
Editor is loading...
Leave a Comment