• YU-NO中的拼图小程序


    受同学的启发写了个YU-NO中的拼图小程序,控制台版。

    功能:画出图形,就能在旁边显示对应的数字。

    有三个问题:①是代码里面有很多重复语句,我懒得改成函数了。②是每次重新打印的东西太多了,很慢,不美观,其实可以只打印变化的部分。③是最好在顶部和左边加上行列标号,免得数,但是我懒,要改的地方太多了,就没加。

      1 #include <bits/stdc++.h>
      2 #include <windows.h>
      3 
      4 /**
      5  -> x
      6 |
      7 y
      8 □■
      9 */
     10 
     11 const int N = 40;
     12 
     13 int map[N][N], lastx[N], lasty[N], last_bottom, n, m;
     14 std::vector<int> x[N], y[N];
     15 
     16 int max(int x, int y) {
     17     return x > y ? x : y;
     18 }
     19 
     20 void set_position(int x,int y) {
     21     HANDLE winHandle;//句柄
     22     COORD pos = {x, y};
     23     winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
     24     //设置光标位置
     25     SetConsoleCursorPosition(winHandle, pos);
     26 }
     27 
     28 void cal() {
     29     for(int i = 0; i < n; i++) {
     30         int t = 0;
     31         x[i].clear();
     32         for(int j = 0; j < m; j++) {
     33             if(map[i][j]) {
     34                 ++t;
     35             }
     36             else {
     37                 if(t) {
     38                     x[i].push_back(t);
     39                     t = 0;
     40                 }
     41             }
     42         }
     43         if(t) {
     44             x[i].push_back(t);
     45         }
     46     }
     47 
     48     for(int j = 0; j < m; j++) {
     49         y[j].clear();
     50         int t = 0;
     51         for(int i = 0; i < n; i++) {
     52             if(map[i][j]) {
     53                 ++t;
     54             }
     55             else {
     56                 if(t) {
     57                     y[j].push_back(t);
     58                     t = 0;
     59                 }
     60             }
     61         }
     62         if(t) {
     63             y[j].push_back(t);
     64         }
     65     }
     66 
     67     return;
     68 }
     69 
     70 inline void output() {
     71     set_position(0, 0);
     72     for(int i = 0; i < n; i++) {
     73         for(int j = 0; j < m; j++) {
     74             if(j) printf(" ");
     75             if(map[i][j]) printf("");
     76             else printf("");
     77         }
     78         while(lastx[i]--) {
     79             printf(" ");
     80         }
     81         set_position(m * 3 - 1, i);
     82         for(int j = 0; j < x[i].size(); j++) {
     83             printf("%3d", x[i][j]);
     84         }
     85         lastx[i] = x[i].size() * 3;
     86         puts("");
     87     }
     88 
     89     for(int i = 0; i < last_bottom; i++) {
     90         for(int j = 0; j < m * 3 - 1; j++) {
     91             printf(" ");
     92         }
     93         puts("");
     94     }
     95     for(int i = 0; i < 45; i++) printf(" ");
     96     puts("");
     97     for(int i = 0; i < 10; i++) {
     98         for(int j = 0; j < 20; j++) printf(" ");
     99         puts("");
    100     }
    101     set_position(0, n);
    102     last_bottom = 0;
    103     for(int j = 0; j < m; j++) {
    104         last_bottom = max(last_bottom, y[j].size());
    105     }
    106     for(int i = 0; i < last_bottom; i++) {
    107         for(int j = 0; j < m; j++) {
    108             if(j) printf(" ");
    109             if(i >= y[j].size()) {
    110                 printf("  ");
    111             }
    112             else {
    113                 printf("%2d", y[j][i]);
    114             }
    115         }
    116         puts("");
    117     }
    118     printf("whick block do you want to change(row first):
    ");
    119     return;
    120 }
    121 
    122 int main() {
    123     printf("please input width(less then 31) and height(less than 16):
    ");
    124     scanf("%d%d", &m, &n);
    125     while(n <= 0 || m <= 0 || m > 30 || n > 15) {
    126         set_position(0, 0);
    127         for(int i = 0; i < 58; i++) printf(" ");
    128         puts("");
    129         for(int i = 0; i < 58; i++) printf(" ");
    130         set_position(0, 0);
    131         printf("NAIVE!!please input again:
    ");
    132         scanf("%d%d", &m, &n);
    133     }
    134     set_position(0, 0);
    135     for(int i = 0; i < 58; i++) printf(" ");
    136     puts("");
    137     for(int i = 0; i < 10; i++) {
    138         for(int j = 0; j < 20; j++) printf(" ");
    139         puts("");
    140     }
    141     set_position(0, 0);
    142     while(1) {
    143         output();
    144         int x, y;
    145         scanf("%d%d", &x, &y);
    146         --x, --y;
    147         while(x < 0 || y < 0 || x >= n || y >= m) {
    148             set_position(0, n + last_bottom);
    149             for(int i = 0; i < 45; i++) printf(" ");
    150             puts("");
    151             for(int i = 0; i < 10; i++) {
    152                 for(int j = 0; j < 20; j++) printf(" ");
    153                 puts("");
    154             }
    155             set_position(0, n + last_bottom);
    156             printf("NAIVE!!please input again:
    ");
    157             scanf("%d%d", &x, &y);
    158             --x, --y;
    159         }
    160         map[x][y] ^= 1;
    161         cal();
    162     }
    163     return 0;
    164 }
    代码
  • 相关阅读:
    如何使用观测者模式实现监控和推送
    oracle在desc表时,name 和type列不能格式化问题(占位过长)
    [置顶] Vim用正则表达式进行批量修改
    Eclipse扩展点
    写给C语言新手的话
    QQ圈子降级为“应用”后应关注其隐私设置
    win8vs2012创建自带sqlServer数据库出错
    JQuery 选择器
    SINGLETON(单例模式)---(孤独的人)
    C++中的常对象和常对象成员
  • 原文地址:https://www.cnblogs.com/huyufeifei/p/12662530.html
Copyright © 2020-2023  润新知