- 取石子
小游戏中心1.0
- 2025-10-4 11:10:21 @
这是一个原创C++代码,大家可以免费使用,以后也会经常更新,但是禁止抄袭、搬运等,原创@,大家可以提出意见,发讨论,在讨论里@我就行
目前代码功能:
1.登录,注册
2.猜数字
3.石头剪刀布
4.计算器
5.AI五子棋模式
6.双人五子棋模式
注:登录,注册功能直接保存到本地,可以永久使用!!!
代码如下,直接Ctrl+C,Ctrl+V就行
/*
开发者:故渊(才译翔)
协助开发:(想成为者@才译翔,UID:2430)
*/
#include <iostream>
#include <fstream>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;
map<string, string> users;
void load_users() {
ifstream fin("users.txt");
string username, password;
while (fin >> username >> password) {
users[username] = password;
}
fin.close();
}
void save_users() {
ofstream fout("users.txt");
for (auto &p : users) {
fout << p.first << " " << p.second << endl;
}
fout.close();
}
void guess_number() {
srand((unsigned)time(0));
int secret = rand() % 100 + 1, guess, cnt = 0;
cout << "猜数字游戏:我想了一个1~100的数字。\n";
while (true) {
cout << "请输入你的猜测:";
cin >> guess;
cnt++;
if (guess == secret) {
cout << "恭喜你,猜对了!共猜了" << cnt << "次。\n";
break;
} else if (guess < secret) {
cout << "太小了。\n";
} else {
cout << "太大了。\n";
}
}
}
void rock_paper_scissors() {
srand((unsigned)time(0));
string choice[3] = {"石头", "剪刀", "布"};
int user, computer;
cout << "石头剪刀布游戏(0-石头,1-剪刀,2-布):";
cin >> user;
computer = rand() % 3;
cout << "你出的是:" << choice[user] << endl;
cout << "电脑出的是:" << choice[computer] << endl;
if (user == computer) cout << "平局!\n";
else if ((user == 0 && computer == 1) || (user == 1 && computer == 2) || (user == 2 && computer == 0))
cout << "你赢了!\n";
else cout << "你输了!\n";
}
void calculator() {
double a, b;
char op;
cout << "计算器:请输入表达式(如 2 + 3):";
cin >> a >> op >> b;
if (op == '+') cout << "结果:" << a + b << endl;
else if (op == '-') cout << "结果:" << a - b << endl;
else if (op == '*') cout << "结果:" << a * b << endl;
else if (op == '/') {
if (b == 0) cout << "除数不能为0!\n";
else cout << "结果:" << a / b << endl;
} else cout << "不支持的运算符!\n";
}
// 五子棋相关
const int SIZE = 15;
char board[SIZE][SIZE];
void print_board() {
cout << " ";
for (int i = 0; i < SIZE; ++i) cout << (i < 10 ? " " : "") << i << " ";
cout << endl;
for (int i = 0; i < SIZE; ++i) {
cout << (i < 10 ? " " : "") << i << " ";
for (int j = 0; j < SIZE; ++j) {
cout << (board[i][j] == 0 ? '.' : board[i][j]) << " ";
}
cout << endl;
}
}
bool check_win(int x, int y, char c) {
int dx[] = {1, 0, 1, 1}, dy[] = {0, 1, 1, -1};
for (int d = 0; d < 4; ++d) {
int cnt = 1;
for (int k = 1; k < 5; ++k) {
int nx = x + dx[d] * k, ny = y + dy[d] * k;
if (nx < 0 || nx >= SIZE || ny < 0 || ny >= SIZE) break;
if (board[nx][ny] == c) cnt++;
else break;
}
for (int k = 1; k < 5; ++k) {
int nx = x - dx[d] * k, ny = y - dy[d] * k;
if (nx < 0 || nx >= SIZE || ny < 0 || ny >= SIZE) break;
if (board[nx][ny] == c) cnt++;
else break;
}
if (cnt >= 5) return true;
}
return false;
}
void init_board() {
for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
board[i][j] = 0;
}
void gomoku_pvp() {
init_board();
cout << "五子棋-双人模式,玩家1(X) 玩家2(O)\n";
int turn = 0;
while (true) {
print_board();
int x, y;
cout << "玩家" << (turn % 2 + 1) << "(" << (turn % 2 == 0 ? 'X' : 'O') << ")请输入落子坐标(行 列):";
cin >> x >> y;
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y]) {
cout << "无效落子,请重新输入。\n";
continue;
}
board[x][y] = (turn % 2 == 0 ? 'X' : 'O');
if (check_win(x, y, board[x][y])) {
print_board();
cout << "玩家" << (turn % 2 + 1) << "获胜!\n";
break;
}
turn++;
if (turn == SIZE * SIZE) {
print_board();
cout << "平局!\n";
break;
}
}
}
void gomoku_ai() {
init_board();
cout << "五子棋-AI模式,你是X,AI是O\n";
int turn = 0;
while (true) {
print_board();
int x, y;
if (turn % 2 == 0) {
cout << "请你输入落子坐标(行 列):";
cin >> x >> y;
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y]) {
cout << "无效落子,请重新输入。\n";
continue;
}
board[x][y] = 'X';
if (check_win(x, y, 'X')) {
print_board();
cout << "你赢了!\n";
break;
}
} else {
// 简单AI:随机落子
do {
x = rand() % SIZE;
y = rand() % SIZE;
} while (board[x][y]);
board[x][y] = 'O';
cout << "AI落子:" << x << " " << y << endl;
if (check_win(x, y, 'O')) {
print_board();
cout << "AI赢了!\n";
break;
}
}
turn++;
if (turn == SIZE * SIZE) {
print_board();
cout << "平局!\n";
break;
}
}
}
void game_menu() {
while (true) {
cout << "\n请选择游戏:\n1. 猜数字\n2. 石头剪刀布\n3. 计算器\n4. 五子棋-双人\n5. 五子棋-AI\n6. 退出\n";
int sel;
cin >> sel;
if (sel == 1) guess_number();
else if (sel == 2) rock_paper_scissors();
else if (sel == 3) calculator();
else if (sel == 4) gomoku_pvp();
else if (sel == 5) gomoku_ai();
else if (sel == 6) break;
else cout << "无效选择。\n";
}
}
int main() {
// 显示版权信息
cout << "开发者:故渊(才译翔)\n";
cout << "协助开发:(想成为者@才译翔,UID:2430)\n";
cout << "协助开发人员:曹莫凡(UID:1619)\n\n";
load_users();
int choice;
cout << "1. 注册\n2. 登录\n请选择:";
cin >> choice;
string username, password;
bool login = false;
if (choice == 1) {
cout << "请输入用户名:";
cin >> username;
if (users.count(username)) {
cout << "用户名已存在!\n";
} else {
cout << "请输入密码:";
cin >> password;
users[username] = password;
save_users();
cout << "注册成功!\n";
login = true;
}
} else if (choice == 2) {
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
if (users.count(username) && users[username] == password) {
cout << "登录成功!\n";
login = true;
} else {
cout << "用户名或密码错误!\n";
}
} else {
cout << "无效选择。\n";
}
if (login) {
game_menu();
}
return 0;
}
代码到此结束,谢谢!!!
0 条评论
目前还没有评论...
信息
- ID
- 116
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 56
- 已通过
- 9
- 上传者