请用C++制作一个switch游戏
时间: 2023-03-30 11:01:20 浏览: 130
好的,我可以回答这个问题。以下是一个简单的 switch 游戏的 C 代码示例:
#include <stdio.h>
int main() {
int choice;
printf("Welcome to the Switch Game!\n");
printf("Choose a number between 1 and 3:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
case 3:
printf("You chose option 3.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
return ;
}
这个程序会提示用户输入一个数字,然后根据用户的选择输出不同的结果。
相关问题
用c++制作一个五子棋小游戏
好的,以下是一个简单的C++五子棋小游戏代码示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
const int BOARD_SIZE = 15; // 棋盘大小
class ChessBoard {
public:
enum ChessType {
EMPTY = 0,
BLACK,
WHITE
};
ChessBoard() {
for (int i = 0; i < BOARD_SIZE; ++i) {
board_.push_back(vector<ChessType>(BOARD_SIZE, EMPTY));
}
}
bool PutChess(int x, int y, ChessType type) {
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board_[x][y] != EMPTY) {
return false;
}
board_[x][y] = type;
return true;
}
bool IsWin(int x, int y, ChessType type) {
int count = 0;
for (int i = x + 1; i < BOARD_SIZE && board_[i][y] == type; ++i) { // 横向
++count;
}
for (int i = x - 1; i >= 0 && board_[i][y] == type; --i) { // 横向
++count;
}
if (count >= 4) {
return true;
}
count = 0;
for (int i = y + 1; i < BOARD_SIZE && board_[x][i] == type; ++i) { // 纵向
++count;
}
for (int i = y - 1; i >= 0 && board_[x][i] == type; --i) { // 纵向
++count;
}
if (count >= 4) {
return true;
}
count = 0;
for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board_[i][j] == type; ++i, ++j) { // 正对角线
++count;
}
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board_[i][j] == type; --i, --j) { // 正对角线
++count;
}
if (count >= 4) {
return true;
}
count = 0;
for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board_[i][j] == type; ++i, --j) { // 反对角线
++count;
}
for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board_[i][j] == type; --i, ++j) { // 反对角线
++count;
}
if (count >= 4) {
return true;
}
return false;
}
void Print() {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
switch (board_[i][j]) {
case EMPTY:
cout << "+ ";
break;
case BLACK:
cout << "X ";
break;
case WHITE:
cout << "O ";
break;
}
}
cout << endl;
}
}
private:
vector<vector<ChessType>> board_;
};
int main() {
ChessBoard board;
ChessBoard::ChessType cur = ChessBoard::BLACK;
int x, y;
while (true) {
cout << "Player " << (cur == ChessBoard::BLACK ? "X" : "O") << " please input the position (x y): ";
cin >> x >> y;
if (board.PutChess(x, y, cur)) {
board.Print();
if (board.IsWin(x, y, cur)) {
cout << "Player " << (cur == ChessBoard::BLACK ? "X" : "O") << " win!" << endl;
break;
}
cur = (cur == ChessBoard::BLACK ? ChessBoard::WHITE : ChessBoard::BLACK);
} else {
cout << "Invalid input!" << endl;
}
}
return 0;
}
```
这个示例代码实现了一个简单的控制台五子棋小游戏。你可以根据自己的需要进行修改和优化。
用C++制作一个完整贪吃蛇代码
以下是一个贪吃蛇游戏的C语言代码,包含了基本的游戏逻辑和图形界面显示。建议在CodeBlocks等IDE中运行。
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define ROW 20 // 地图行数
#define COL 30 // 地图列数
#define FOOD_MAX 5 // 食物数量上限
typedef struct position {
int x;
int y;
} Position;
typedef struct snake {
Position body[ROW * COL]; // 蛇身体部位
int len; // 蛇长度
char direction; // 蛇所朝方向(w a s d)
} Snake;
typedef struct food {
Position pos[FOOD_MAX];
int num; // 当前食物数
} Food;
void initMap(char map[ROW][COL]) { // 初始化地图
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (i == 0 || i == ROW - 1 || j == 0 || j == COL - 1) {
map[i][j] = '#'; // 墙壁
} else {
map[i][j] = ' '; // 空地
}
}
}
}
void printMap(char map[ROW][COL]) { // 打印地图
system("cls"); // 清屏
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
}
void initSnake(Snake* snake) { // 初始化蛇
snake->len = 3;
snake->direction = 'd';
snake->body[0].x = ROW / 2;
snake->body[0].y = COL / 2;
for (int i = 1; i < snake->len; i++) {
snake->body[i].x = snake->body[i - 1].x - 1;
snake->body[i].y = snake->body[i - 1].y;
}
}
void printSnake(char map[ROW][COL], Snake* snake) { // 打印蛇
for (int i = 0; i < snake->len; i++) {
map[snake->body[i].x][snake->body[i].y] = '*';
}
}
void updateSnake(Snake* snake) { // 更新蛇
for (int i = snake->len - 1; i > 0; i--) {
snake->body[i].x = snake->body[i - 1].x;
snake->body[i].y = snake->body[i - 1].y;
}
switch (snake->direction) {
case 'w':
snake->body[0].x--;
break;
case 'a':
snake->body[0].y--;
break;
case 's':
snake->body[0].x++;
break;
case 'd':
snake->body[0].y++;
break;
}
}
void initFood(Food* food) { // 初始化食物
food->num = 0;
}
void addFood(Food* food) { // 添加食物
if (food->num < FOOD_MAX) {
int x, y;
do {
x = rand() % (ROW - 2) + 1;
y = rand() % (COL - 2) + 1;
} while (food->num > 0 && food->pos[food->num - 1].x == x && food->pos[food->num - 1].y == y); // 避免新食物出现在蛇身上
food->pos[food->num].x = x;
food->pos[food->num].y = y;
food->num++;
}
}
void printFood(char map[ROW][COL], Food* food) { // 打印食物
for (int i = 0; i < food->num; i++) {
map[food->pos[i].x][food->pos[i].y] = '$';
}
}
int checkGameOver(Snake* snake) { // 检测游戏是否结束
if (snake->body[0].x == 0 || snake->body[0].x == ROW - 1 || snake->body[0].y == 0 || snake->body[0].y == COL - 1) { // 撞墙
return 1;
}
for (int i = 1; i < snake->len; i++) {
if (snake->body[0].x == snake->body[i].x && snake->body[0].y == snake->body[i].y) { // 撞自己
return 1;
}
}
return 0;
}
int checkEatFood(Snake* snake, Food* food) { // 检测是否吃到食物
for (int i = 0; i < food->num; i++) {
if (snake->body[0].x == food->pos[i].x && snake->body[0].y == food->pos[i].y) {
return i; // 返回被吃食物的下标
}
}
return -1;
}
void updateFood(Food* food, int index) { // 更新食物
if (index >= 0 && index < food->num) {
for (int i = index; i < food->num - 1; i++) {
food->pos[i] = food->pos[i + 1];
}
food->num--;
}
}
void updateSpeed(int score) { // 根据得分更新游戏速度
int speed = 400 - score * 10;
if (speed < 50) {
speed = 50;
}
Sleep(speed);
}
int main() {
char map[ROW][COL]; // 地图
Snake snake; // 蛇
Food food; // 食物
int score = 0; // 得分
srand((unsigned) time(NULL)); // 初始化随机数种子
initMap(map); // 初始化地图
initSnake(&snake); // 初始化蛇
initFood(&food); // 初始化食物
addFood(&food); // 添加第一个食物
while (1) { // 游戏循环
printMap(map); // 打印地图
printSnake(map, &snake); // 打印蛇
printFood(map, &food); // 打印食物
printf("Score: %d\n", score); // 打印得分
updateSpeed(score); // 根据得分更新游戏速度
if (kbhit()) { // 监听键盘输入
char ch = getch();
if ((ch == 'w' && snake.direction != 's') || (ch == 'a' && snake.direction != 'd') || (ch == 's' && snake.direction != 'w') || (ch == 'd' && snake.direction != 'a')) { // 避免掉头
snake.direction = ch;
}
}
updateSnake(&snake); // 更新蛇
int eatIndex = checkEatFood(&snake, &food); // 检测是否吃到食物
if (eatIndex >= 0) { // 吃到食物
snake.body[snake.len].x = food.pos[eatIndex].x;
snake.body[snake.len].y = food.pos[eatIndex].y;
snake.len++;
updateFood(&food, eatIndex);
addFood(&food);
score++;
}
if (checkGameOver(&snake)) { // 游戏结束
printf("Game Over! Score: %d\n", score);
break;
}
}
return 0;
}
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)