
用c++做简易的游戏
·
一、石头剪刀布
1.主要功能
和电脑进行猜拳游戏,并进行统计输赢次数
2.代码
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0)); // 初始化随机种子
std::cout << "欢迎来到剪刀石头布游戏!" << std::endl;
int playerWins = 0;
int computerWins = 0;
while (true) {
int playerChoice;
std::cout << "请选择你的出拳(1-剪刀,2-石头,3-布;输入其他数字结束游戏):";
std::cin >> playerChoice;
if (playerChoice < 1 || playerChoice > 3) {
break;
}
int computerChoice = rand() % 3 + 1; // 生成1到3之间的随机数,代表电脑的出拳
std::cout << "你选择了:";
switch (playerChoice) {
case 1:
std::cout << "剪刀" << std::endl;
break;
case 2:
std::cout << "石头" << std::endl;
break;
case 3:
std::cout << "布" << std::endl;
break;
default:
std::cout << "无效的选择" << std::endl;
return 0;
}
std::cout << "电脑选择了:";
switch (computerChoice) {
case 1:
std::cout << "剪刀" << std::endl;
break;
case 2:
std::cout << "石头" << std::endl;
break;
case 3:
std::cout << "布" << std::endl;
break;
}
if (playerChoice == computerChoice) {
std::cout << "平局!" << std::endl;
} else if ((playerChoice == 1 && computerChoice == 3) || (playerChoice == 2 && computerChoice == 1) || (playerChoice == 3 && computerChoice == 2)) {
std::cout << "你赢了!" << std::endl;
playerWins++;
} else {
std::cout << "你输了!" << std::endl;
computerWins++;
}
std::cout << "得分情况:" << std::endl;
std::cout << "玩家:" << playerWins << " 电脑:" << computerWins << std::endl;
std::cout << std::endl;
}
std::cout << "游戏结束!" << std::endl;
return 0;
}
二、扫雷
1.主要功能
输入坐标,进行扫雷
2.代码
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
const int BOARD_SIZE = 10;
const int NUM_MINES = 10;
const char MINE_SYMBOL = '*';
const char HIDDEN_SYMBOL = '#';
// 初始化游戏板
void initializeBoard(std::vector<std::vector<char>>& board) {
// 初始化所有格子为隐藏状态
for (int i = 0; i < BOARD_SIZE; i++) {
std::vector<char> row(BOARD_SIZE, HIDDEN_SYMBOL);
board.push_back(row);
}
}
// 显示游戏板
void displayBoard(const std::vector<std::vector<char>>& board) {
std::cout << " ";
for (int i = 0; i < BOARD_SIZE; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
for (int i = 0; i < BOARD_SIZE; i++) {
std::cout << i << " ";
for (int j = 0; j < BOARD_SIZE; j++) {
std::cout << board[i][j] << " ";
}
std::cout << std::endl;
}
}
// 放置地雷
void placeMines(std::vector<std::vector<char>>& board) {
int count = 0;
while (count < NUM_MINES) {
int x = rand() % BOARD_SIZE;
int y = rand() % BOARD_SIZE;
// 如果该位置没有地雷,则放置地雷
if (board[x][y] != MINE_SYMBOL) {
board[x][y] = MINE_SYMBOL;
count++;
}
}
}
// 获取指定位置周围地雷的数量
int getAdjacentMinesCount(const std::vector<std::vector<char>>& board, int x, int y) {
int count = 0;
// 检查该位置周围的8个方向
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int newX = x + i;
int newY = y + j;
// 跳过超出边界的位置以及当前位置本身
if (newX < 0 || newX >= BOARD_SIZE || newY < 0 || newY >= BOARD_SIZE || (i == 0 && j == 0)) {
continue;
}
// 如果周围位置有地雷,则计数加一
if (board[newX][newY] == MINE_SYMBOL) {
count++;
}
}
}
return count;
}
// 揭示指定位置的格子
void revealCell(std::vector<std::vector<char>>& board, std::vector<std::vector<bool>>& visited, int x, int y) {
// 跳过已经访问过的格子和已经揭示出来的格子
if (visited[x][y] || board[x][y] != HIDDEN_SYMBOL) {
return;
}
visited[x][y] = true;
// 获取该位置周围地雷的数量
int adjacentMines = getAdjacentMinesCount(board, x, y);
// 如果该位置周围有地雷,则将该数量显示在格子上
if (adjacentMines > 0) {
board[x][y] = '0' + adjacentMines;
}
// 否则递归揭示周围的格子
else {
board[x][y] = ' ';
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int newX = x + i;
int newY = y + j;
if (newX >= 0 && newX < BOARD_SIZE && newY >= 0 && newY < BOARD_SIZE) {
revealCell(board, visited, newX, newY);
}
}
}
}
}
// 扫雷游戏主循环
void gameLoop() {
std::vector<std::vector<char>> board;
std::vector<std::vector<bool>> visited;
initializeBoard(board);
placeMines(board);
for (int i = 0; i < BOARD_SIZE; i++) {
std::vector<bool> row(BOARD_SIZE, false);
visited.push_back(row);
}
bool gameOver = false;
while (!gameOver) {
displayBoard(board);
int x, y;
std::cout << "请输入要揭示的格子坐标 (x, y): ";
std::cin >> x >> y;
// 检查输入是否合法
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
std::cout << "无效的坐标!" << std::endl;
continue;
}
// 如果该位置是地雷,游戏结束
if (board[x][y] == MINE_SYMBOL) {
gameOver = true;
std::cout << "很遗憾,踩到地雷了!游戏结束。" << std::endl;
break;
}
// 如果该位置周围没有地雷,则递归揭示周围的格子
if (board[x][y] == HIDDEN_SYMBOL) {
revealCell(board, visited, x, y);
}
// 检查是否胜利
int hiddenCount = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == HIDDEN_SYMBOL || board[i][j] == MINE_SYMBOL) {
hiddenCount++;
}
}
}
if (hiddenCount == NUM_MINES) {
gameOver = true;
std::cout << "恭喜您,成功找到所有的地雷!游戏胜利。" << std::endl;
}
}
}
int main() {
srand(time(0)); // 初始化随机种子
std::cout << "欢迎来到扫雷游戏!" << std::endl;
std::cout << "请输入要揭示的格子坐标以避免踩到地雷。" << std::endl;
std::cout << "揭示一个格子将会显示出周围地雷的数量。找到所有的地雷即可胜利!" << std::endl;
gameLoop();
return 0;
}
更多推荐
所有评论(0)