c++自制游戏(优化)
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
int n,s=1,x,d1,r=0,a,b,c,yuan;
int main()
{
cout<<"欢迎来到RYW游戏中心!"<<endl;
cout<<"1.猜数游戏"<<endl;
cout<<"2.bug赌场" <<endl;
cout<<"3.退出/未开发(-------------------------------- Process exited after 3.043 seconds with return value 0请按任意键继续. . .)"<<endl;
cout<<"4.五子棋"<<endl;
cout<<"5.?智力游戏"<<endl;
cout<<"请选择一个来玩!(只能玩1个且1次!)"<<endl;
cin>>n;
if(n==1)
{ cout<<"猜数游戏,现在开始!!!(数在1000内)"<<endl;
while(r=1)
{
d1=rand()%1000;
cin>>x;
if(x>d1)
{
cout<<"大了!"<<endl;
s++;
}
else
{
if(x<d1)
{
cout<<"小了!"<<endl;
}
else
{
if(x==d1)
{
cout<<"恭喜猜中!!共用"<<s<<"次!";
r=1;
}
}
}
}
}
else
{
if(n==2)
{ int t=rand()%9;
while(r=1)
{
cout<<"请输入1人名字(为数字/英文)"<<endl;
cin>>a;
cout<<a<<"有100元。"<<endl;
cout<<"请问"<<a<<"要做什么?"<<endl;
cout<<"1.用bug"<<endl;
cout<<"2.赌钱"<<endl;
cout<<"预言自己(今日运气)";
cout<<"1/2"<<endl;
cin>>x;
if(x==1)
{
cout<<"你好,首富,你还有30秒死亡!!!29,28,27,26,25,24,23,22,21....3,2,1"<<endl;
cout<<"你已死亡,剩余0元!"<<endl<<"--------------------------------"<<endl;
cout<<" Process exited after 3.043 seconds with return value 0请按任意键继续. . .";
return 0;
}
else
{
if(x==2)
{
while(c=1||yuan==0)
{
yuan=100;
cout<<"请输入3个数。";
cout<<endl;
cin>>a>>b>>c;
int t=rand()%9;
if(a==t)
{
s=s+1;
}
if(b==t)
{
s=s+1;
}
if(c==t)
{
s=s+1;
}
if(s==1)
{ yuan=yuan+15;
cout<<"恭喜获得3等奖!!!"<<endl;
cout<<"你有"<<yuan<<"元"<<endl;
}
else
{
if(s==3)
{ yuan=yuan+60;
cout<<"恭喜获得1等奖!!!"<<endl;
cout<<"你有"<<yuan<<"元"<<endl;
}
else
{
if(s==2)
{ yuan=yuan+30;
cout<<"恭喜获得2等奖!!!"<<endl;
cout<<"你有"<<yuan<<"元"<<endl;
}
}
}
break;
}
}
else
{
if(x==3)
{
cout<<"请问你要预言什么?"<<endl;
cout<<"1.预言运气"<<endl;
cout<<"2.预言SB朋友"<<endl<<"1/2"<<endl;
cin>>n;
if(n==1)
{
n=rand()%100;
cout<<n<<"为你的今日运气值(100制)"<<endl;
}
else
{if(n==2)
{
n=rand()%100;
cout<<n<<"为SB朋友的今日运气值(100制)"<<endl;
}
}
}
}
}
}
}
else
{
if(n==3)
{
cout<<"--------------------------------"<<endl;
cout<<" Process exited after 3.043 seconds with return value 0请按任意键继续. . .";
cout<<"△▽○◇□☆???▲▼●◆■★♀♂?√×?▃▂▁▄▆▆▇█∷※▓▏▎▍▌▋▊▉§◎¤??‖?卍卐〓℡?@㈱№↖↑↗←㊣→↙↓↘◤◥◣◢?¬▔??**";
}
else
{
if(n==4)
{
cout<<"五子棋即将开始!!!"<<endl;
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
const int N = 15; //15*15的棋盘
const char ChessBoard = ' '; //棋盘标志
const char flag1 = 'o'; //玩家1或电脑标志
const char flag2 = 'x'; //玩家2标志
typedef struct Position{ //坐标
int row; //行
int col; //列
}Position;
class GoBang{ //五子棋类
public:
GoBang(){
InitChessBoard(); //初始化棋盘
}
void Play(){ //下棋
Position Play1; //玩家1或电脑
Position Play2; //玩家2
while (1){
int mode = ChoiceMode();
while (1){
if (mode == 1){ //电脑VS玩家
ComputerChess(Play1, flag1); //电脑走
if (GetVictory(Play1, 0, flag1)){ //0代表电脑,为真则表示电脑获胜
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //2代表玩家2
break;
}
}
else{ //玩家1VS玩家2
PlayChess(Play1, 1, flag1); //玩家1走
if (GetVictory(Play1, 1, flag1)){ //玩家1赢
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //玩家2赢
break;
}
}
}
cout << "======再来一局=======" << endl;
cout << "yes or no :";
char s[] = "yes";
cin >> s;
if (strcmp(s, "no") == 0){
break;
}
}
}
protected:
void InitChessBoard(){ //初始化棋盘
for (int i = 0; i < N + 1; ++i){
for (int j = 0; j < N + 1; ++j){
_ChessBoard[i][j] = ChessBoard;
}
}
}
int ChoiceMode(){ //选择模式
system("cls");
//系统调用,清屏
InitChessBoard(); //重新初始化棋盘
cout << "*************************************************" << endl;
cout << "******************0、退出************************" << endl;
cout << "******************1、电脑VS玩家******************" << endl;
cout << "******************2、玩家VS玩家******************" << endl;
cout << "*************************************************" << endl;
while (1){
int i = 0;
cout << "请选择模式:";
cin >> i;
if (i == 0){ //退出
exit(1);
}
if (i == 1 || i == 2){
return i;
}
else{
cout << "非法输入,请重新输入!" << endl;
}
}
}
void PrintChessBoard(){ //打印棋盘
printf(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n");
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
for (int i = 1; i < N + 1; ++i)
{
printf("%2d ", i);
printf("| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", _ChessBoard[i][1], _ChessBoard[i][2], _ChessBoard[i][3], _ChessBoard[i][4], _ChessBoard[i][5], _ChessBoard[i][6], _ChessBoard[i][7], _ChessBoard[i][8], _ChessBoard[i][9], _ChessBoard[i][10], _ChessBoard[i][11], _ChessBoard[i][12], _ChessBoard[i][13], _ChessBoard[i][14], _ChessBoard[i][15]);
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
}
cout << endl;
}
void ComputerChess(Position& pos, char flag){ //电脑走
//PrintChessBoard(); //打印棋盘
int x = 0;
int y = 0;
while (1){ //循环查找空位置
x = (rand() % N) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
y = (rand() % N) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
if (_ChessBoard[x][y] == ChessBoard){ //如果这个位置为空(没有棋子),跳出循环,下棋
break;
}
}
pos.row = x;
pos.col = y;
_ChessBoard[pos.row][pos.col] = flag;
}
void PlayChess(Position& pos, int player, char flag){
PrintChessBoard(); //打印棋盘
while (1){
printf("请玩家%d输入坐标:", player);
cin >> pos.row >> pos.col;
if (JudgeValue(pos) == 1){ //判断坐标是否合法
break;
}
cout << "坐标不合法,请重新输入:" << endl;
}
_ChessBoard[pos.row][pos.col] = flag;
}
int JudgeValue(const Position& pos){ //判断坐标的合法性
//1.在棋盘上
if (pos.row > 0 && pos.row <= N && pos.col > 0 && pos.col <= N){
//2.所在位置为空(没有棋子)
if (_ChessBoard[pos.row][pos.col] == ChessBoard){
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Position pos, char flag){ //判断是否有玩家获胜(底层判断)
int begin = 0;
int end = 0;
//1.判断行是否满足条件
(pos.col - 4) > 0 ? begin = (pos.col - 4) : begin = 1;
(pos.col + 4) > N ? end = N : end = (pos.col + 4);
for (int i = pos.row, j = begin; j + 4 <= end; ++j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i][j + 1] == flag &&
_ChessBoard[i][j + 2] == flag && _ChessBoard[i][j + 3] == flag &&
_ChessBoard[i][j + 4] == flag)
return 1;
}
//2.判断列是否满足条件
(pos.row - 4) > 0 ? begin = (pos.row - 4) : begin = 1;
(pos.row + 4) > N ? end = N : end = (pos.row + 4);
for (int j = pos.col, i = begin ; i + 4 <= end; ++i){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j] == flag &&
_ChessBoard[i + 2][j] == flag && _ChessBoard[i + 3][j] == flag &&
_ChessBoard[i + 4][j] == flag)
return 1;
}
//3.判断主对角线是否满足条件
int len = 0; //相对长度
int start = 0;
int finish = 0;
pos.row > pos.col ? len = pos.col - 1 : len = pos.row - 1;
if (len > 4){
len = 4;
}
begin = pos.row - len; //横坐标起始位置
start = pos.col - len; //纵坐标起始位置
pos.row > pos.col ? len = N - pos.row : len = N - pos.col;
if (len > 4){
len = 4;
}
end = pos.row + len; //横坐标结束位置
finish = pos.col + len; //纵坐标结束位置
for (int i = begin, j = start; (i + 4 <= end) && (j + 4 <= finish); ++i, ++j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j + 1] == flag &&
_ChessBoard[i + 2][j + 2] == flag && _ChessBoard[i + 3][j + 3] == flag &&
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//4.判断副对角线是否满足条件
(pos.row - 1) > (N - pos.col) ? len = N - pos.col : len = pos.row - 1;
if (len > 4){
len = 4;
}
begin = pos.row - len; //横坐标起始位置
start = pos.col + len; //纵坐标起始位置
(N - pos.row) > (pos.col - 1) ? len = pos.col - 1 : len = N - pos.row;
if (len > 4){
len = 4;
}
end = pos.row + len; //横坐标结束位置
finish = pos.col - len; //纵坐标结束位置
for (int i = begin, j = start; (i + 4 <= end) && (j - 4 >= finish); ++i, --j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j - 1] == flag &&
_ChessBoard[i + 2][j - 2] == flag && _ChessBoard[i + 3][j - 3] == flag &&
_ChessBoard[i + 4][j - 4] == flag)
return 1;
}
//该位置并未下棋
for (int x = 1; x < N + 1; ++x){
for (int y = 1; y < N + 1; ++y){
if (_ChessBoard[x][y] == ChessBoard){
return 0; //未下棋
}
}
}
return -1; //和局
}
bool GetVictory(Position& pos, int player, char flag){ //判断具体哪位玩家赢
if (JudgeVictory(pos, flag) != 0){ //判断有无人获胜
if (JudgeVictory(pos, flag) == 1){ //判断是否有人获胜,1表示获胜
PrintChessBoard(); //打印棋盘
if (player == 0){
cout << "电脑获胜!" << endl;
}
else{
printf("恭喜玩家%d获胜!\n", player);
}
}
else{
printf("和局!\n");
}
return true; //有人获胜
}
return false; //没人获胜
}
private:
char _ChessBoard[N + 1][N + 1]; //棋盘
};
GoBang g;
g.Play();
system("pause");
}
else
{
if(n==5)
{
cout<<"请选择一种玩法。"<<endl;
cout<<"1.速算游戏(10000以内)"<<endl;
cout<<"2.速算平方(10000以内)" <<endl;
cin>>n;
if(n==1)
{s=0;
for(int i=1;;i++)
{
a=rand()%10000;
b=rand()%10000;
cout<<a<<"+"<<b<<"= ?"<<endl;
cin>>n;
if(n==a+b)
{
s++;
cout<<"太棒了!共有"<<s<<"分"<<endl;
}
else
{
cout<<"失败了!共有"<<s<<"分"<<endl;
c=1;
break;
}
}
}
else
{
if(n==2)
{s=0;
for(int i=1;;i++)
{
a=rand()%10000;
cout<<a<<"*"<<a<<"= ?"<<endl;
cin>>n;
if(n==a*a)
{
s++;
cout<<"太棒了!共有"<<s<<"分"<<endl;
}
else
{
cout<<"失败了!共有"<<s<<"分"<<endl;
c=1;
break;
}
}
}
}
}
}
}
}
}
return 0;
}
原文地址:https://blog.csdn.net/comands7/article/details/143781579
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!