小游戏分享
目录
小游戏分享
1.井字棋游戏
2.简单计算器游戏
3.猜单词
4.石头剪刀布游戏
5.猜数字游戏
1.井字棋游戏
「3×3 棋盘上的思维博弈!与好友轮流落子,抢占先机,连成一线即可获胜。简单规则蕴含无限策略,展现你的战术智慧!」
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
int currentPlayer = 1; // 1=玩家X,2=玩家Ovoid printBoard() {printf("\n");printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);printf("---+---+---\n");printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);printf("---+---+---\n");printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);printf("\n");
}bool checkWin() {// 检查行for (int i = 0; i < 3; i++)if (board[i][0] == board[i][1] && board[i][1] == board[i][2])return true;// 检查列for (int i = 0; i < 3; i++)if (board[0][i] == board[1][i] && board[1][i] == board[2][i])return true;// 检查对角线if (board[0][0] == board[1][1] && board[1][1] == board[2][2])return true;if (board[0][2] == board[1][1] && board[1][1] == board[2][0])return true;return false;
}bool checkDraw() {for (int i = 0; i < 3; i++)for (int j = 0; j < 3; j++)if (board[i][j] != 'X' && board[i][j] != 'O')return false;return true;
}void makeMove() {int position;char mark = (currentPlayer == 1) ? 'X' : 'O';printf("玩家 %d (%c) 的回合,请选择位置 (1-9): ", currentPlayer, mark);scanf("%d", &position);int row = (position - 1) / 3;int col = (position - 1) % 3;if (position >= 1 && position <= 9 && board[row][col] != 'X' && board[row][col] != 'O') {board[row][col] = mark;currentPlayer = (currentPlayer == 1) ? 2 : 1;} else {printf("无效的位置,请重试!\n");}
}int main() {printf("井字棋游戏!\n");while (1) {printBoard();makeMove();if (checkWin()) {printBoard();printf("玩家 %d 获胜!\n", (currentPlayer == 1) ? 2 : 1);break;}if (checkDraw()) {printBoard();printf("平局!\n");break;}}return 0;
}
2.简单计算器游戏
「数学天才的试炼场!输入数字与运算符,挑战心算极限。快速验证计算能力,在数字世界中突破自我,成为计算大师!」
#include <stdio.h>int main() {char operation;double num1, num2, result;printf("简单计算器游戏!\n");printf("请输入操作 (+, -, *, /): ");scanf(" %c", &operation);printf("请输入两个数字: ");scanf("%lf %lf", &num1, &num2);switch (operation) {case '+':result = num1 + num2;printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);break;case '-':result = num1 - num2;printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);break;case '*':result = num1 * num2;printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);break;case '/':if (num2 != 0) {result = num1 / num2;printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);} else {printf("错误:除数不能为零!\n");}break;default:printf("错误:无效的操作符!\n");}return 0;
}
3.猜单词
「词汇探险,智破谜题!根据提示字母,逐步揭开隐藏单词的神秘面纱。考验英语词汇量,每猜对一个单词都将解锁新的挑战!」
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>int main() {char *words[] = {"apple", "banana", "cherry", "grape", "orange"};int numWords = sizeof(words) / sizeof(words[0]);char guess[50];int attempts = 5;int i, len, correct = 0;char letter;bool found;srand(time(0));int randomIndex = rand() % numWords;char *word = words[randomIndex];len = strlen(word);char hidden[len + 1];for (i = 0; i < len; i++) {hidden[i] = '_';}hidden[len] = '\0';printf("猜单词游戏!你有%d次机会猜出这个单词。\n", attempts);while (attempts > 0 && strcmp(hidden, word) != 0) {printf("当前猜测: %s\n", hidden);printf("剩余尝试次数: %d\n", attempts);printf("请输入一个字母: ");scanf(" %c", &letter);letter = tolower(letter);found = false;for (i = 0; i < len; i++) {if (word[i] == letter) {hidden[i] = letter;found = true;}}if (!found) {attempts--;printf("错误!这个字母不在单词中。\n");} else {printf("正确!这个字母在单词中。\n");}}if (strcmp(hidden, word) == 0) {printf("恭喜你,猜对了!单词是: %s\n", word);} else {printf("游戏结束!正确的单词是: %s\n", word);}return 0;
}
4.石头剪刀布游戏
「指尖对决,策略至上!石头、布、剪刀,三选一的永恒谜题。击败 AI 对手,成为不败王者,体验经典游戏的全新魅力!」
#include <stdio.h>
#include <stdlib.h>
#include <time.h>int main() {int player, computer;srand(time(0));computer = rand() % 3 + 1; // 1=石头,2=布,3=剪刀printf("石头剪刀布游戏!\n");printf("请选择: 1=石头, 2=布, 3=剪刀: ");scanf("%d", &player);printf("你选择了: ");switch (player) {case 1: printf("石头\n"); break;case 2: printf("布\n"); break;case 3: printf("剪刀\n"); break;default: printf("无效选择\n"); return 1;}printf("电脑选择了: ");switch (computer) {case 1: printf("石头\n"); break;case 2: printf("布\n"); break;case 3: printf("剪刀\n"); break;}// 判断胜负if (player == computer)printf("平局!\n");else if ((player == 1 && computer == 3) || (player == 2 && computer == 1) || (player == 3 && computer == 2))printf("你赢了!\n");elseprintf("你输了!\n");return 0;
}
5.猜数字游戏
「数字迷踪,智胜挑战!我已锁定 1-100 之间的神秘数字,每轮猜测都将获得精准提示。用最少的尝试揭开谜底,展现你的逻辑推理天赋!」
#include <stdio.h>
#include <stdlib.h>
#include <time.h>int main() {int number, guess, attempts = 0;srand(time(0));number = rand() % 100 + 1; // 生成1-100之间的随机数printf("猜数字游戏!我已经想好了一个1到100之间的数字,你可以开始猜了...\n");do {printf("请输入你的猜测: ");scanf("%d", &guess);attempts++;if (guess > number)printf("猜的数字太大了!再试一次。\n");else if (guess < number)printf("猜的数字太小了!再试一次。\n");elseprintf("恭喜你,猜对了!你用了%d次尝试。\n", attempts);} while (guess != number);return 0;
}