- 分享
c++答案
- @ 2026-4-21 16:48:14
看下面
14 条评论
-
-
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #include <windows.h> #include <string.h> // 方向宏定义 #define UP 1 #define DOWN 2 #define LEFT 3 #define RIGHT 4 #define ESC 5 #define FOOD 10 // 蛇身链表结构体 typedef struct snake{ int x; int y; struct snake *next; }snake; // 全局变量 int score = 0; int speed = 200; int status; snake *tail, *head; snake *food, *q; HANDLE hOUT; // 函数声明 void gotoxy(int x, int y); int choice(void); int color(int c); void printGame(void); void printSnake(void); void printFood(void); void printTips(void); void snakeMove(void); int biteSelf(void); int encounterWall(void); void keyboardControl(void); void speedUp(void); void speedDown(void); int endGame(void); char *s_gets(char *st, int n); void frees(snake *); int main(int argc, char *argv[]){ while (1) { if (choice() == 1) keyboardControl(); else { gotoxy(5, 15); printf("按任意键返回"); getchar(); while (1) { if (getchar()) { system("cls"); break; } } } } frees(head); return 0; } // 控制台光标跳转函数 void gotoxy(int x, int y) { COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); } // 主菜单 int choice(void) { int yourchoice; gotoxy(35, 5); color(11); printf("\t贪吃蛇大作战\n"); printf("\n\n"); color(13); printf("\t\t★★★★★★★★ Snake!"); printf("\t\t★★★★★★★★ Snake!"); gotoxy(25, 15); color(12); printf("1.进入游戏\t2.查看说明\t3.退出游戏\n"); color(11); printf("请选择:"); scanf("%d", &yourchoice); switch (yourchoice) { case 1: system("cls"); printGame(); printSnake(); printFood(); break; case 2: system("cls"); printTips(); break; case 3: system("cls"); gotoxy(30, 10); color(11); printf("Bye!"); exit(0); default: system("cls"); printf("没有此序号,请输入1,2或3\n"); Sleep(2000); system("cls"); } return yourchoice; } // 控制台字体颜色设置 int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); return 0; } // 绘制游戏边界、背景 void printGame() { int i, j; gotoxy(5, 5); printf("游戏载入中...请稍后"); Sleep(2000); system("cls"); // 上下围墙 for (i = 0; i <= 50; i += 2) { gotoxy(i, 0); printf("□"); gotoxy(i, 25); printf("□"); } // 左右围墙 for (i = 0; i <= 25; i += 1) { gotoxy(0, i); printf("□"); gotoxy(50, i); printf("□"); } // 内部游戏网格 for (i = 1; i <= 24; i += 1) { for (j = 2; j <= 48; j += 2) { gotoxy(j, i); color(11); printf("■"); } } // 右侧信息提示栏 gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); gotoxy(60, 15); printf("用↑ ↓ ← →分别控制蛇的移动\n"); gotoxy(60, 18); printf("每次获取食物加10分 F1加速,F2减速,空格暂停\n"); gotoxy(60, 20); printf("不能撞墙和咬到自己!"); gotoxy(60, 22); printf("速度不低于100,不高于300"); } // 初始化蛇身 void printSnake(void) { int i; tail = (snake*)malloc(sizeof(snake)); tail->x = 16; tail->y = 13; tail->next = NULL; // 初始蛇长度4节 for (i = 1; i <= 4; i++) { head = (snake*)malloc(sizeof(snake)); head->next = tail; head->x = 16 + 2 * i; head->y = 13; tail = head; } // 打印初始蛇身 while (tail->next) { gotoxy(tail->x, tail->y); color(14); printf("★"); tail = tail->next; } } // 随机生成食物 void printFood(void) { srand((unsigned)time(NULL)); food = (snake*)malloc(sizeof(snake)); food->x = 1; while (food->x % 2 && food->x) { food->x = rand() % 46 + 2; } food->y = rand() % 23 + 1; q = head; while (q->next) { if (q->x == food->x && q->y == food->y) { free(food); printFood(); } else { gotoxy(food->x, food->y); color(12); printf("●"); break; } } } // 游戏说明界面 void printTips(void) { color(11); printf("***********Tips************\n"); printf("1.采用合理的速度可以获得更高的分数哦!\n"); printf("2.一定不要撞到自己或者两边的墙!\n"); printf("3.游戏过程中按ESC退出游戏!\n"); } // 蛇移动核心函数 void snakeMove(void) { snake *snakenext; snakenext = (snake*)malloc(sizeof(snake)); if (biteSelf()) { gotoxy(60, 11); printf("咬到自己啦!"); free(snakenext); Sleep(1500); system("cls"); exit(0); } else if (encounterWall()) { gotoxy(60, 11); printf("撞到墙啦!"); free(snakenext); Sleep(1500); system("cls"); exit(0); } else { Sleep(350 - speed); if (status == UP) { snakenext->x = head->x; snakenext->y = head->y - 1; snakenext->next = head; head = snakenext; q = head; if (snakenext->x == food->x && snakenext->y == food->y) { while (q) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } score += FOOD; gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); printFood(); } else { while (q->next->next) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } gotoxy(q->next->x, q->next->y); color(11); printf("■"); free(q->next); q->next = NULL; } } else if (status == DOWN) { snakenext->x = head->x; snakenext->y = head->y + 1; snakenext->next = head; head = snakenext; q = head; if (snakenext->x == food->x && snakenext->y == food->y) { while (q) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } score += FOOD; gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); printFood(); } else { while (q->next->next) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } gotoxy(q->next->x, q->next->y); color(11); printf("■"); free(q->next); q->next = NULL; } } else if (status == LEFT) { snakenext->x = head->x - 2; snakenext->y = head->y; snakenext->next = head; head = snakenext; q = head; if (snakenext->x == food->x && snakenext->y == food->y) { while (q) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } score += FOOD; gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); printFood(); } else { while (q->next->next) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } gotoxy(q->next->x, q->next->y); color(11); printf("■"); free(q->next); q->next = NULL; } } else if (status == RIGHT) { snakenext->x = head->x + 2; snakenext->y = head->y; snakenext->next = head; head = snakenext; q = head; if (snakenext->x == food->x && snakenext->y == food->y) { while (q) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } score += FOOD; gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); printFood(); } else { while (q->next->next) { gotoxy(q->x, q->y); color(14); printf("★"); q = q->next; } gotoxy(q->next->x, q->next->y); color(11); printf("■"); free(q->next); q->next = NULL; } } } } // 判断蛇是否咬到自身 int biteSelf(void) { int x = 0; q = head->next; while (q->next) { if (q->x == head->x && q->y == head->y) { x = 1; } q = q->next; } return x; } // 判断蛇是否撞墙 int encounterWall(void) { int x = 0; if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25) x = 1; return x; } // 键盘按键控制 void keyboardControl(void) { status = RIGHT; while (1) { if (GetAsyncKeyState(VK_UP) && status != DOWN) { status = UP; } else if (GetAsyncKeyState(VK_DOWN) && status != UP) { status = DOWN; } else if (GetAsyncKeyState(VK_LEFT) && status != RIGHT) { status = LEFT; } else if (GetAsyncKeyState(VK_RIGHT) && status != LEFT) { status = RIGHT; } // 空格暂停 if (GetAsyncKeyState(VK_SPACE)) { while (1) { Sleep(300); if (GetAsyncKeyState(VK_SPACE)) { break; } } } // ESC退出 else if (GetAsyncKeyState(VK_ESCAPE)) { status = ESC; if (endGame()) { Sleep(500); system("cls"); break; } } // F1加速 else if (GetAsyncKeyState(VK_F1)) { speedUp(); gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); } // F2减速 else if (GetAsyncKeyState(VK_F2)) { speedDown(); gotoxy(60, 13); printf("当前分数:%d分,当前速度%d", score, speed); } snakeMove(); } } // 加速函数 void speedUp(void) { if (speed <= 280) speed += 20; } // 减速函数 void speedDown(void) { if (speed >= 120) speed -= 20; } // 游戏退出确认 int endGame(void) { char x = 0; char judge[5]; getchar(); gotoxy(60, 9); printf("确定退出吗?(Yes/No)"); gotoxy(60, 11); s_gets(judge, 5); if (strcmp(judge, "Yes") == 0) { Sleep(250); system("cls"); gotoxy(40, 11); printf("\tBye!"); x = 1; } else x = 0; return x; } // 安全字符串读取 char *s_gets(char *st, int n) { char *ret_val; char *find; gotoxy(60, 11); ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, '\n'); if (find) *find = '\0'; else while (getchar() != '\n') continue; } return ret_val; } // 链表内存释放,防止内存泄漏 void frees(snake *s) { snake *current = s; while (current) { current = s; s = current->next; free(current); } } -
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdlib> #include <ctime> #include <windows.h> using namespace std; #define MAX_ROW 3 #define MAX_COL 3 // 初始化棋盘 void init(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { chessBoard[row][col] = ' '; } } } // 打印棋盘 void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]) { cout << "+---+---+---+" << endl; for (int row = 0; row < MAX_ROW; row++) { cout << "| " << chessBoard[row][0] << " | " << chessBoard[row][1] << " | " << chessBoard[row][2] << " |" << endl; cout << "+---+---+---+" << endl; } } // 玩家落子 void playerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (true) { int row = 0; int col = 0; cout << "请输入坐标(行 列,范围 0~2):"; cin >> row >> col; // 越界判断 if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) { cout << "坐标超出范围!请重新输入!" << endl; continue; } // 重复落子判断 if (chessBoard[row][col] != ' ') { cout << "该位置已有棋子!请重新输入!" << endl; continue; } chessBoard[row][col] = 'X'; break; } } // 电脑落子 void computerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (true) { int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (chessBoard[row][col] == ' ') { chessBoard[row][col] = 'O'; break; } } } // 判断棋盘是否满了 bool isFull(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row < MAX_ROW; row++) { for (int col = 0; col < MAX_COL; col++) { if (chessBoard[row][col] == ' ') { return false; } } } return true; } // 判断胜负 // 返回值:'X'玩家赢 'O'电脑赢 'Q'平局 ' '继续 char isWin(char chessBoard[MAX_ROW][MAX_COL]) { // 行判断 for (int row = 0; row < MAX_ROW; row++) { if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) { return chessBoard[row][0]; } } // 列判断 for (int col = 0; col < MAX_COL; col++) { if (chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col]) { return chessBoard[0][col]; } } // 对角线 if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2]) { return chessBoard[0][0]; } if (chessBoard[2][0] != ' ' && chessBoard[2][0] == chessBoard[1][1] && chessBoard[2][0] == chessBoard[0][2]) { return chessBoard[2][0]; } // 平局 if (isFull(chessBoard)) { return 'Q'; } return ' '; } // 游戏主逻辑 void game() { char chessBoard[MAX_ROW][MAX_COL] = { 0 }; init(chessBoard); char winner = ' '; while (true) { system("cls"); print_chessBoard(chessBoard); // 玩家 playerMove(chessBoard); winner = isWin(chessBoard); if (winner != ' ') break; // 电脑 computerMove(chessBoard); winner = isWin(chessBoard); if (winner != ' ') break; } system("cls"); print_chessBoard(chessBoard); if (winner == 'X') { cout << "?? 恭喜你,你赢了!" << endl; } else if (winner == 'O') { cout << "?? 电脑获胜!你连人机都下不过~" << endl; } else { cout << "?? 平局!" << endl; } } // 菜单 int menu() { cout << "============================" << endl; cout << "===== 井字棋(五子棋) =====" << endl; cout << "===== 1. 开始游戏 ======" << endl; cout << "===== 0. 退出游戏 ======" << endl; cout << "============================" << endl; int choice = 0; cout << "请输入选择:"; cin >> choice; return choice; } int main() { srand((unsigned int)time(NULL)); // 随机数种子 while (true) { system("cls"); int choice = menu(); if (choice == 1) { game(); } else if (choice == 0) { cout << "退出游戏,再见!" << endl; break; } else { cout << "输入错误,请重新输入!" << endl; } system("pause"); } return 0; } -
#include<iostream> #include<math.h> #include<Windows.h> #include<conio.h> #include<ctime> using namespace std; enum DIR { UP, RIGHT, DOWN, LEFT }; time_t start = 0, finish = 0; int _x = 6, _y = 1; int map[30][16] = { 0 }; int sharp[20][8] = { {0,0,0,0,0,0,0,0}, {0,0,0,1,0,2,0,3}, {0,0,1,0,2,0,3,0}, {0,0,1,0,0,1,1,1}, {0,0,0,1,0,2,1,2}, {0,0,0,1,1,0,2,0}, {0,0,1,0,1,1,1,2}, {0,1,1,1,2,0,2,1}, {0,2,1,0,1,1,1,2}, {0,0,0,1,1,1,2,1}, {0,0,0,1,0,2,1,0}, {0,0,1,0,2,0,2,1}, {0,0,1,0,1,1,2,1}, {0,1,0,2,1,0,1,1}, {0,1,1,0,1,1,2,0}, {0,0,0,1,1,1,1,2}, {0,1,1,0,1,1,2,1}, {0,0,0,1,0,2,1,1}, {0,0,1,0,1,1,2,0}, {0,1,1,0,1,1,1,2} }; class Game { public: int score; int _id; int top; int speed; Game(); void showMenu(); void showGround(); void gameOver(); void Run(); void sharpDraw(int id, bool show = false); void keyControl(); bool move(int dir, int id); bool downSet(int id); void Turn(int id); void clean(); }; void SetPos(int i, int j) { COORD pos = { i,j }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } int main() { CONSOLE_CURSOR_INFO cursor; GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); cursor.bVisible = 0; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); srand((unsigned)time(NULL)); Game game; game.showMenu(); return 0; } Game::Game() { score = 0; _id = 0; top = 58; speed = 1000; } void Game::showMenu() { for (int i = 0; i < 30; i++) { for (int j = 0; j < 26; j++) { if ((i == 0 || i == 29) || (j == 0 || j == 25)) { cout << "■"; } else { cout << " "; } } cout << endl; } SetPos(17, 8); cout << "俄 罗 斯 方 块" << endl; SetPos(13, 12); cout << "↑旋转方块 ↓加速下滑" << endl; SetPos(12, 14); cout << "← →左右移动 空格 暂停" << endl; SetPos(15, 20); cout << "0 退出 Enter 开始" << endl; while (1) { int select = _getch(); if (select == 13) { system("cls"); this->Run(); } else if (select == 48) // 修复BUG:== 而不是 = { system("cls"); exit(0); } } } void Game::showGround() { for (int i = 0; i < 30; i++) { for (int j = 0; j < 26; j++) { if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15)) { cout << "■"; } else if (i == 15 && j > 15) { cout << "■"; } else { cout << " "; } } cout << endl; } SetPos(31, 2); cout << "下 个图形" << endl; SetPos(31, 17); cout << "当 前得分" << endl; for (int i = 0; i < 30; i++) { for (int j = 0; j < 16; j++) { if ((i == 0 || i == 29) || (j == 0 || j == 15)) { map[i][j] = 1; } else { map[i][j] = 0; } } } } void Game::gameOver() { for (int i = 5; i < 15; i++) { SetPos(1, i); cout << " " << endl; } SetPos(8, 7); cout << "G a m e O v e r" << endl; SetPos(3, 10); cout << "0 退出 Enter 重新开始" << endl; while (1) { int select = _getch(); if (select == 13) { system("cls"); this->Run(); } else if (select == 48) { system("cls"); exit(0); } } } void Game::Run() { score = 0; _id = 0; top = 58; _x = 6; _y = 1; showGround(); start = clock(); int new_id = rand() % 19 + 1; while (1) { sharpDraw(_id); keyControl(); if (downSet(_id)) { sharpDraw(-new_id, 1); _id = new_id; new_id = rand() % 19 + 1; sharpDraw(new_id, 1); clean(); } SetPos(34, 20); cout << score << endl; } } void Game::sharpDraw(int id, bool show) { int x, y; if (show == true) { if (id > 0) { for (int i = 0; i < 4; i++) { x = 19 + sharp[id][2 * i]; y = 6 + sharp[id][2 * i + 1]; SetPos(2 * x, y); cout << "■"; } } else { for (int i = 0; i < 4; i++) { x = 19 + sharp[-id][2 * i]; y = 6 + sharp[-id][2 * i + 1]; SetPos(2 * x, y); cout << " "; } } return; } if (id > 0) { for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; SetPos(2 * x, y); cout << "■"; } } else { for (int i = 0; i < 4; i++) { x = _x + sharp[-id][2 * i]; y = _y + sharp[-id][2 * i + 1]; SetPos(2 * x, y); cout << " "; } } return; } bool Game::downSet(int id) { if (id == 0) return true; finish = clock(); if (finish - start < speed) { return false; } start = clock(); if (!move(DOWN, _id)) { int x, y; for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; map[y][x] = 1; if (y < top) { top = y; } if (top <= 1) { gameOver(); } } _x = 6; _y = 1; return true; } sharpDraw(-id); _y++; sharpDraw(id); return false; } bool Game::move(int dir, int id) { int x, y; switch (dir) { case UP: for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; if (map[y][x] == 1) { return false; } } break; case DOWN: { for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; if (map[y + 1][x] == 1) { return false; } } } break; case RIGHT: { for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; if (map[y][x + 1] == 1) { return false; } } } break; case LEFT: { for (int i = 0; i < 4; i++) { x = _x + sharp[id][2 * i]; y = _y + sharp[id][2 * i + 1]; if (map[y][x - 1] == 1) { return false; } } } break; default: break; } return true; } void Game::Turn(int id) { switch (id) { case 1:id++; break; case 2:id--; break; case 3: break; case 4:id++; break; case 5:id++; break; case 6:id++; break; case 7:id -= 3; break; case 8:id++; break; case 9:id++; break; case 10:id++; break; case 11:id -= 3; break; case 12:id++; break; case 13:id--; break; case 14:id++; break; case 15:id--; break; case 16:id++; break; case 17:id++; break; case 18:id++; break; case 19:id -= 3; break; default: break; } if (!move(UP, id)) { return; } sharpDraw(-_id); _id = id; } void Game::keyControl() { if (!_kbhit()) return; int key = _getch(); switch (key) { case 72: Turn(_id); break; case 80: if (move(DOWN, _id)) { sharpDraw(-_id); _y++; } break; case 75: if (move(LEFT, _id)) { sharpDraw(-_id); _x--; } break; case 77: if (move(RIGHT, _id)) { sharpDraw(-_id); _x++; } break; case 32: { for (int i = 5; i < 15; i++) { SetPos(1, i); cout << " " << endl; } SetPos(10, 7); cout << "游 戏 暂 停" << endl; SetPos(3, 10); cout << "0 返回菜单 回车 继续游戏" << endl; while (1) { int select = _getch(); if (select == 13) { for (int i = 5; i < 15; i++) { SetPos(1, i); cout << " " << endl; } break; } else if (select == 48) { system("cls"); showMenu(); } } } default: break; } } void Game::clean() { int n = -1; int line = -1; while (1) { for (int i = 28; i > 0; i--) { for (int j = 1; j < 15; j++) { line = i; if (map[i][j] == 0) { line = -1; break; } } if (line != -1) break; } if (line == -1) break; for (int i = line; i > 0; i--) { for (int j = 1; j < 15; j++) { if (i == 1) map[i][j] = 0; else { map[i][j] = map[i - 1][j]; SetPos(2 * j, i); if (map[i][j] == 1) cout << "■"; else cout << " "; } } } top++; n++; } if (n >= 0) { score += n * n * 100 + 100; if (speed > 100) speed = 1000 - score / 10; } } -
#include <iostream> #include <cstdlib> // 随机数 #include <ctime> // 时间种子 using namespace std; int main() { // 初始化随机数 srand(time(0)); int secretNum = rand() % 100 + 1; // 1~100随机数 int guess; cout << "==== 猜数字游戏 ====" << endl; cout << "我已经想好了一个1-100的数字,开始猜吧!\n" << endl; while (true) { cout << "请输入你的猜测:"; cin >> guess; if (guess > secretNum) { cout << "太大了!再试试\n"; } else if (guess < secretNum) { cout << "太小了!再试试\n"; } else { cout << "\n恭喜你猜对了!答案就是:" << secretNum << endl; break; } } system("pause"); return 0; }
- 1