• 21年51假期(打字母游戏 -> 数组之间的映射)



    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> #include<thread> #include<assert.h> #include<windows.h> #if 0 using namespace std; #define ROWSIZE 25 #define COLSIZE 40 #define LETSIZE 26 //26个英文字母的长度 #define LENSIZE 26 //对26个字母进行映射,防止开局出现重复字母 #define MAXSIZE 10 //游戏开局出现10个 //GridType类型即代表了具有行25和列40 的元素的字符数组 typedef char GridType[ROWSIZE][COLSIZE]; typedef enum { NULLLET = 0, UPLET = 1, LOWLET = 2 } LetType; struct LetterNode { LetType tag; //0表示空 1是大写字母 2是小写字母 int row; int col; }; //这是一个结构体类型的数组 //还是全局数组,用来表示26个字母 //用typedef简写成了LeTable,要定义它的时候直接上LeTable typedef struct LetterNode LeTable[LENSIZE]; void Init_Let(LeTable plet) { assert(plet!=nullptr); int num = 0; int table[COLSIZE] = { 0 };// 0~39 srand(time(nullptr)); while (num<MAXSIZE) { int tag = rand() % 2 + 1; //1 2 int pos = rand() % LETSIZE; if (plet[pos].tag==NULLLET) { plet[pos].tag = (tag == 1) ? UPLET : LOWLET; plet[pos].row = 0; while (1) { int col = rand() % (COLSIZE - 1); if (0==table[col]) { table[col] = 1; plet[pos].col = col; break; } } num++; } } } void Init_Grid(GridType grid) { for (int i=0;i<ROWSIZE;i++) { memset(grid[i],0x20,sizeof(char)*COLSIZE); grid[i][COLSIZE - 1] = ''; } } void Print_Grid(GridType grid,LeTable plet) //这里的grid是 (*grid)[CLOWSIZE] { assert(grid!=nullptr && plet!=nullptr); Init_Grid(grid); //刷新屏幕 system("cls"); for (int i=0;i< LENSIZE;i++) { if (plet[i].tag!=NULLLET) { char ch = (plet[i].tag == UPLET) ? 'A' + i : 'a' + i; grid[plet[i].row][plet[i].col] = ch; } } for (int i=0;i<ROWSIZE;i++) { printf("%s ",grid[i]); } } bool DownLet(LeTable plet) { assert(plet!=nullptr); bool res = false; for (int i=0;i<LENSIZE;++i) { if (plet[i].tag!=NULLLET) { plet[i].row += 1; if (plet[i].row>=ROWSIZE) { res = true; break; } } } return res; } bool Remove_Inc(LeTable plet, char ch) { assert(plet != nullptr); bool res = false; LetType tag = isupper(ch) ? UPLET : LOWLET; //这是一个枚举类型tag,判断是大写小写 int pos = (tag == UPLET) ? ch - 'A' : ch - 'a'; //这个是下标定位 if (plet[pos].tag == tag) //判断映射表里面有没有字母,有的话继续 { plet[pos].tag = NULLLET; plet[pos].row = plet[pos].col = 0; res = true; //上面的if删除了我一个字母,下面再加一个 //同样是不重复列,不重复字母 while (1) { tag = (rand() % 2 + 1 == 1) ? UPLET : LOWLET; pos = rand() % LETSIZE; if (plet[pos].tag == NULLLET) { plet[pos].tag = tag; plet[pos].row = 0; plet[pos].col = rand() % (COLSIZE - 1); break; } } } return res; } int main() { GridType grid = {}; LeTable xLet = {}; //这两个一定要初始化,不然Init_Let里无法比较 Init_Let(xLet); int num = 0;//分数 while (1) { Print_Grid(grid, xLet); Sleep(800); if (DownLet(xLet)) { printf("over! "); break; } if (_kbhit()) { char ch = _getch(); if (Remove_Inc(xLet, ch)) { num++; } } } return 0; } #endif
    Linux学习笔记
  • 相关阅读:
    二逼平衡树(线段树套平衡树)
    AtCoder Beginner Contest 237 G Range Sort Query
    带修改莫队/【2011集训队出题】数颜色
    6.视图
    8.SpringBoot集成MongoDB
    5.聚合管道
    2.MongoDB安装
    3.MongoDB权限
    4.MongoDB命令语法基本使用
    7.索引
  • 原文地址:https://www.cnblogs.com/zealwang/p/14736479.html
Copyright © 2020-2023  润新知