ox得分

#include<iostream>
#include<string.h>
using namespace std;
char a[80];
int sc[80];
int main()
{
    int i, sum = 0;
    while (scanf("%s", a) == 1)//注意s前无&用这种方法碰到空格tab等就会结束 用getchar/fgetc(stdin)
    {
        for (i = 0; i < strlen(a); i++) {
            if (a[i] == 'X')
                sc[i] = 0;
            else if (a[i] == 'O') {
                sc[i] = 0;
                for (int n = i; n >= 0; n--) {//这里不能i=i了,否则i的值会被修改
                    if (a[n] == 'O')sc[n]++;
                    else break;
                }
            }
        }
        for (int i = 0; i < strlen(a); i++)cout << sc[i];
        for (int i = 0; i < strlen(a); i++)sum += sc[i];
        cout << sum;
    }
    
    return 0;
}

在这里插入图片描述

#include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <deque> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; const int WIDTH = 40; // 游戏区域宽度 const int HEIGHT = 20; // 游戏区域高度 // 定义方向枚举 enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 障碍物结构体 struct Obstacle { int x, y; // 位置 int size; // 大小(1为1x1,2为2x2,3为3x3等) char symbol; // 显示的符号 int lifetime; // 剩余存在时间(0为永久) }; // 游戏状态结构体 struct GameState { int snakeX, snakeY; // 蛇头位置 int fruitX, fruitY; // 水果位置 int chestX, chestY; // 宝箱位置 int score; // 得分 Direction direction; // 当前方向 bool gameOver; // 游戏结束标志 deque<int> tailX; // 蛇身X坐标队列 deque<int> tailY; // 蛇身Y坐标队列 int tailLength; // 蛇身长度 bool chestActive; // 宝箱是否激活 int chestTimer; // 宝箱刷新计时器 const int CHEST_SPAWN_INTERVAL = 50; // 宝箱刷新间隔 bool mapFull = false; // 标志地图是否被填满 int speedLevel = 0; // 速度等级 (0-初始速度, 1-加快, 2-更快等) vector<Obstacle> obstacles; // 障碍物列表 int obstacleTimer; // 障碍物生成计时器 const int OBSTACLE_SPAWN_INTERVAL = 80; // 障碍物生成间隔 const int MAX_OBSTACLES = 5; // 同时存在的最大障碍物数 }; // 设置控制台光标位置 void SetCursorPosition(short x, short y) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = {x, y}; SetConsoleCursorPosition(hConsole, pos); } // 隐藏控制台光标 void HideCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = false; SetConsoleCursorInfo(hConsole, &cursorInfo); } // 检查位置是否被占用 bool IsPositionOccupied(const GameState& state, int x, int y) { // 如果是地图满状态,所有位置都被占用 if (state.mapFull) return true; // 检查蛇头 if (state.snakeX == x && state.snakeY == y) return true; // 检查水果 if (state.fruitX == x && state.fruitY == y) return true; // 检查宝箱 if (state.chestActive && state.chestX == x && state.chestY == y) return true; // 检查蛇身 for (int i = 0; i < state.tailLength; i++) { if (state.tailX[i] == x && state.tailY[i] == y) return true; } // 检查障碍物 for (const auto& obs : state.obstacles) { for (int oy = obs.y; oy < obs.y + obs.size && oy < HEIGHT; oy++) { for (int ox = obs.x; ox < obs.x + obs.size && ox < WIDTH; ox++) { if (ox == x && oy == y) return true; } } } return false; } // 检查地图是否被完全占据 bool IsMapFull(const GameState& state) { // 总格子数 int totalCells = WIDTH * HEIGHT; // 如果蛇身长度+1(蛇头)+障碍物数量*平均大小^2 小于总格子数,地图可能没满 int estimatedOccupied = state.tailLength + 1 + state.obstacles.size() * 4; if (estimatedOccupied < totalCells) return false; // 创建一个虚拟地图,标记所有占据位置 bool occupied[HEIGHT][WIDTH] = {false}; // 标记蛇头位置 occupied[state.snakeY][state.snakeX] = true; // 标记蛇身位置 for (int i = 0; i < state.tailLength; i++) { occupied[state.tailY[i]][state.tailX[i]] = true; } // 标记水果位置 occupied[state.fruitY][state.fruitX] = true; // 标记宝箱位置(如果存在) if (state.chestActive) { occupied[state.chestY][state.chestX] = true; } // 标记障碍物位置 for (const auto& obs : state.obstacles) { for (int oy = obs.y; oy < obs.y + obs.size && oy < HEIGHT; oy++) { for (int ox = obs.x; ox < obs.x + obs.size && ox < WIDTH; ox++) { if (ox < WIDTH && oy < HEIGHT) { occupied[oy][ox] = true; } } } } // 遍历所有格子,如果发现任何空位,返回false for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (!occupied[y][x]) return false; // 发现空位,地图没满 } } return true; // 所有格子都被占满 } // 初始化游戏状态 void Setup(GameState& state) { state.gameOver = false; state.direction = STOP; state.snakeX = WIDTH / 2; state.snakeY = HEIGHT / 2; // 随机生成水果位置 state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; // 初始化宝箱 state.chestActive = false; state.chestTimer = 0; state.score = 0; state.tailLength = 0; state.mapFull = false; // 初始状态地图不满 state.speedLevel = 0; // 初始速度等级 state.tailX.clear(); state.tailY.clear(); state.obstacles.clear(); // 清空障碍物 state.obstacleTimer = 0; // 重置障碍物计时器 } // 生成新水果(带安全机制) void GenerateFruit(GameState& state) { // 如果地图被填满,跳过生成水果 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; attempts++; // 防止无限循环,达到最大尝试次数后停止 if (attempts >= MAX_ATTEMPTS) { // 如果尝试了很多次仍失败,标记地图已满 state.mapFull = IsMapFull(state); break; } } while (IsPositionOccupied(state, state.fruitX, state.fruitY)); } // 生成新宝箱(带安全机制) void GenerateChest(GameState& state) { // 如果地图被填满,跳过生成宝箱 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.chestX = rand() % WIDTH; state.chestY = rand() % HEIGHT; attempts++; // 防止无限循环 if (attempts >= MAX_ATTEMPTS) { state.chestActive = false; // 放弃生成宝箱 state.chestTimer = state.CHEST_SPAWN_INTERVAL / 2; // 设置半程计时器 state.mapFull = IsMapFull(state); // 检查地图是否已满 break; } } while (IsPositionOccupied(state, state.chestX, state.chestY)); if (attempts < MAX_ATTEMPTS) { state.chestActive = true; } } // 生成新障碍物(带安全机制) void GenerateObstacle(GameState& state) { // 如果障碍物数量已达上限或地图已满,不生成新障碍物 if (state.obstacles.size() >= state.MAX_OBSTACLES || state.mapFull) { return; } Obstacle newObstacle; const int MAX_ATTEMPTS = 50; // 最大尝试次数 int attempts = 0; bool positionValid = false; do { // 随机决定障碍物大小 (1-3),80%概率1x1,15%概率2x2,5%概率3x3 int randSize = rand() % 100; if (randSize < 80) newObstacle.size = 1; else if (randSize < 95) newObstacle.size = 2; else newObstacle.size = 3; // 随机决定位置(考虑障碍物大小) newObstacle.x = rand() % (WIDTH - newObstacle.size); newObstacle.y = rand() % (HEIGHT - newObstacle.size); // 随机决定符号 char symbols[] = {'#', 'X', '*', '+'}; newObstacle.symbol = symbols[rand() % 4]; // 随机决定持续时间(0-50%永久,50-85%中等时间,85-100%短期) int durationChance = rand() % 100; if (durationChance < 50) { newObstacle.lifetime = 0; // 永久 } else if (durationChance < 85) { newObstacle.lifetime = 200; // 中等时间 } else { newObstacle.lifetime = 100; // 短期 } // 检查位置是否有效(不与现有元素冲突) positionValid = true; for (int oy = newObstacle.y; oy < newObstacle.y + newObstacle.size; oy++) { for (int ox = newObstacle.x; ox < newObstacle.x + newObstacle.size; ox++) { if (ox < WIDTH && oy < HEIGHT) { if (IsPositionOccupied(state, ox, oy)) { positionValid = false; break; } } else { positionValid = false; break; } } if (!positionValid) break; } attempts++; if (attempts >= MAX_ATTEMPTS) { break; // 达到最大尝试次数,放弃生成 } } while (!positionValid); // 如果找到有效位置,添加障碍物 if (positionValid) { state.obstacles.push_back(newObstacle); } } // 绘制障碍物 void DrawObstacles(const vector<Obstacle>& obstacles) { for (const auto& obs : obstacles) { for (int y = 0; y < obs.size; y++) { for (int x = 0; x < obs.size; x++) { if (obs.y + y < HEIGHT && obs.x + x < WIDTH) { SetCursorPosition(obs.x + x + 1, obs.y + y + 1); cout << obs.symbol; } } } } } // 绘制游戏界面 void Draw(const GameState& state) { // 设置控制台位置到左上角 SetCursorPosition(0, 0); // 绘制上边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 绘制游戏区域 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { // 左边界 if (j == 0) cout << "#"; // 绘制蛇头 if (i == state.snakeY && j == state.snakeX) cout << "O"; // 绘制水果 else if (i == state.fruitY && j == state.fruitX) cout << "@"; // 绘制宝箱 else if (state.chestActive && i == state.chestY && j == state.chestX) cout << "$"; else { bool isTail = false; // 绘制蛇身 for (int k = 0; k < state.tailLength; k++) { if (state.tailX[k] == j && state.tailY[k] == i) { cout << "o"; isTail = true; break; } } // 如果不是蛇身,检查是否是障碍物的一部分 if (!isTail) { bool isObstacle = false; for (const auto& obs : state.obstacles) { for (int oy = obs.y; oy < obs.y + obs.size; oy++) { for (int ox = obs.x; ox < obs.x + obs.size; ox++) { if (ox == j && oy == i) { isObstacle = true; break; } } if (isObstacle) break; } if (isObstacle) break; } if (!isObstacle) cout << " "; } } // 右边界 if (j == WIDTH - 1) cout << "#"; } cout << endl; } // 绘制下边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 显示游戏信息 cout << "得分: " << state.score << " "; cout << "蛇身长度: " << state.tailLength << " "; cout << "速度: " << state.speedLevel + 1 << "级 "; cout << "障碍物: " << state.obstacles.size() << "/" << state.MAX_OBSTACLES << " "; if (state.mapFull) { cout << "地图已满! "; } else if (state.chestActive) { cout << "宝箱: ($) "; } else { int timeLeft = (state.CHEST_SPAWN_INTERVAL - state.chestTimer) / 5; cout << "下个宝箱: " << max(0, timeLeft) << " "; } // 显示下一个障碍物的出现时间 int obstacleTime = (state.OBSTACLE_SPAWN_INTERVAL - state.obstacleTimer) / 5; cout << "障碍物倒计时: " << max(0, obstacleTime) << " "; cout << "按 'x'退出游戏" << endl; // 在游戏下方显示障碍物信息 SetCursorPosition(0, HEIGHT + 3); cout << "障碍物类型:"; for (const auto& obs : state.obstacles) { string type; if (obs.lifetime == 0) type = "永久"; else type = "临时"; cout << obs.size << "x" << obs.size << "(" << obs.symbol << "," << type << ") "; } // 清除可能存在的旧内容(修复第79行) cout << " "; } // 处理用户输入 void ProcessInput(GameState& state) { // 检查是否有按键输入 if (_kbhit()) { switch (_getch()) { case 'a': case 'A': if (state.direction != RIGHT) state.direction = LEFT; break; case 'd': case 'D': if (state.direction != LEFT) state.direction = RIGHT; break; case 'w': case 'W': if (state.direction != DOWN) state.direction = UP; break; case 's': case 'S': if (state.direction != UP) state.direction = DOWN; break; case 'x': case 'X': state.gameOver = true; break; case 'p': case 'P': // 暂停功能 while (true) { if (_kbhit() && _getch() == 'p') break; Sleep(100); } break; } } } // 更新速度等级(基于蛇身长度) void UpdateSpeedLevel(GameState& state) { // 每5个长度提升一个速度等级 int newLevel = state.tailLength / 5; // 仅当等级提升时才显示提示 if (newLevel > state.speedLevel) { state.speedLevel = newLevel; // 在游戏界面显示速度提升提示 SetCursorPosition(0, HEIGHT + 4); cout << "蛇身长度达到 " << (state.speedLevel * 5) +上述代码中第80行代码出现报错报错内容为1,80 28 C:\Users\lenovo\Desktop\C++游戏.cpp [Error] range-based 'for' loops are not allowed in C++98 mode 2,81 27 C:\Users\lenovo\Desktop\C++游戏.cpp [Error] request for member 'y' in 'obs', which is of non-class type 'const int' 3,81 39 C:\Users\lenovo\Desktop\C++游戏.cpp [Error] request for member 'y' in 'obs', which is of non-class type 'const int'
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值