博主主页:Yu·仙笙🦄
专栏:C++游戏game
视频:C++井字棋坤坤版
如果不想看代码制作过程及原理,想直接play者,请自行划到文末取源码
目录
井字棋(Tic Tac Toe),又称井字游戏、"连城"游戏、OX棋,是一种供两人玩的纸笔游戏。两个玩家轮流在九个空格中画上代表自己的O或X,谁先将自己的符号连成一线(横连、竖连、斜连皆可),即获得胜利。倘若在游戏过程中,双方都采取最佳策略,那么游戏往往会以平局告终。井字棋也被应用于人工智能与博弈论的研究。
蔡徐坤(KUN),1998年8月2日出生,中国内地男歌手、演员。
2012年4月,蔡徐坤因参加综艺节目《向上吧!少年》进入全国200强而进入娱乐圈;8月,参演个人首部偶像剧《童话二分之一》。2014年3月,参演个人首部电影《完美假妻168》;2015年7月,蔡徐坤参加真人秀节目《星动亚洲》,并成功进入全国前十五强;2016年10月,蔡徐坤通过10人男子组合SWIN正式出道;2018年1月,参加爱奇艺重点打造的中国首档偶像男团竞演养成类真人秀《偶像练习生》。2018年4月6日,在《偶像练习生》决赛中,蔡徐坤最终排名第一,作为Nine Percent组合成员正式出道。6月15日,参加的纪录片《NINEPERCENT花路之旅》在爱奇艺上线。2018年8月2日,入选福布斯2018年中国“30位30岁以下精英”榜单。8月18日,随组合参加2018微博粉丝嘉年华线下活动。12月18日,担任2019年北京卫视春晚代言人。同年12月,获第十二届音乐盛典咪咕汇年度“最佳彩铃销量歌手”、年度十大金曲《WaitWaitWait》、搜狐时尚盛典“年度人气男明星”以及今日头条年度盛典“年度偶像人物”。2019年2月,首登北京台春晚便包揽词曲,为其创作歌曲《那年春天》。2019年2月19日,蔡徐坤合约案胜诉,与原公司经纪合约解除。3月22日,发布海外公演主题曲《Bigger》。4月19日,发布单曲《Hard To Get》。12月25日,加盟《青春有你第二季》担任青春制作人代表。
---------------------------------------------------------------------------------------------------------------------------------
如果不想看代码制作过程及原理,想直接play者,请自行划到文末取源码
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- char play[9] = { '1','2','3','4','5','6','7','8','9' };
- void system()
- {
- system("color FD");
- system("date/t");
- system("time/t");
- system("title 井字棋 Ver:1.0.0");
- }
- void CSH()
- {
- cout << "######井字棋小游戏######" << endl;
- cout << "(*游戏规则:先掷骰子选出优先下子者,然后轮流下子,任一方若有三子连成一线,该方即获胜)" << endl;
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n"<
- cout<<"鸡你太美"<
- cout<<"你干嘛,哎呦"<
- cout<<"-------------------------------------------------------"<
- }
- void return_CSH()
- {
- memset(play,0,9);
- play[0] = '1';
- play[1] = '2';
- play[2] = '3';
- play[3] = '4';
- play[4] = '5';
- play[5] = '6';
- play[6] = '7';
- play[7] = '8';
- play[8] = '9';
- }
四、玩家信息录入
- class Player {
- public:
- Player() {}
- void Name(int i)
- {
- cout << "请为玩家" << i << "设置玩家名称:";
- cin >> name;
- }
- void Get_Name()
- {
- cout << name;
- }
五、玩家投掷骰子确定出手先后
- int Order()
- {
- cout << "请玩家 " << name << " 掷骰子:";
- system("pause");
- srand((unsigned)time(NULL));
- a = rand() % 6 + 1;
- cout << a << endl;
- return a;
- }
- int PD()
- {
- return a;
- }
六、玩家选择棋子样式
- void XQ_1()
- {
- cout << "******游戏设置******" << endl;
- cout << " 1.O 2.X " << endl;
- cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
- while (xq == 0)
- {
- cin >> xq;
- if (xq == 1)
- q1 = 'O';
- else if (xq == 2)
- q1 = 'X';
- else
- cout << "命令错误!请重新输入合法选项:";
- }
- }
- void XQ_2()
- {
- cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
- while (xq == 0)
- {
- cin >> xq;
- if (xq == 1)
- q2 = 'O';
- else if (xq == 2)
- q2 = 'X';
- else
- cout << "命令错误!请重新输入合法选项:";
- }
- }
六、游戏正文执行部分
- void XS_Play()
- {
- int n;
- cout << "请玩家 " << name << " 输入下棋位置:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
- {
- play[n - 1] = q1;
- }
- else
- {
- cout << "位置不合法!请重新选择:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q1;
- }
- else
- {
- cout << "我感觉你是故意的";
- exit(1);
- }
- }
- system("cls");
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- }
- void HS_Play()
- {
- int n;
- cout << "请玩家 " << name << " 输入下棋位置:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q2;
- }
- else
- {
- cout << "位置不合法!请重新选择:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q2;
- }
- else
- {
- cout << "你又找茬是吧?";
- exit(1);
- }
- }
- system("cls");
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- }
- friend int Get_Q1(Player& p1);
- friend int Get_Q2(Player& p2);
- private:
- string name;
- int a = 0;
- int xq = 0;
- char q1, q2;
- };
-
-
- int Get_Q1(Player& p1)
- {
- return p1.q1;
- }
- int Get_Q2(Player& p2)
- {
- return p2.q2;
- }
-
- int End()
- {
- if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
- {
- if (play[3] == 'X')
- {
- return 1;
- }
- if (play[3] == 'O')
- {
- return 2;
- }
- }
- if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
- {
- if (play[6] == 'X')
- {
- return 1;
- }
- if (play[6] == 'O')
- {
- return 2;
- }
- }
-
- if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
- {
- if (play[1] == 'X')
- {
- return 1;
- }
- if (play[1] == 'O')
- {
- return 2;
- }
- }
- if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
- {
- if (play[1] == 'X')
- {
- return 1;
- }
- if (play[1] == 'O')
- {
- return 2;
- }
- }
- if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
- {
- if (play[2] == 'X')
- {
- return 1;
- }
- if (play[2] == 'O')
- {
- return 2;
- }
- }
- return 0;
- }
-
- int main()
- {
- system();
- CSH();
- Player p1;
- Player p2;
- int xz;
- int j = 0;
- int xs = 0;
- p1.Name(1);
- p2.Name(2);
- while (xs == 0)
- {
- p1.Order();
- p2.Order();
- if (p1.PD() > p2.PD())
- cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
- else if (p1.PD() < p2.PD())
- cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
- else
- cout << "双方点数一样大,请重新掷点数:" << endl;
- }
- if (xs == 1)
- {
- p1.XQ_1(), p2.XQ_2();
- }
- else if (xs == 2)
- {
- p2.XQ_1(), p1.XQ_2();
- }
- CSH();
- while (j < 5)
- {
- if (xs == 1)
- {
- p1.XS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- p2.HS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- }
- if (xs == 2)
- {
- p2.XS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- p1.HS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- }
- j++;
- }
- system("cls");
- cout<<"******游戏结束******"<
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- if (End() == 1)
- {
- if (Get_Q1(p1) == 'x')
- cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<
- else
- cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<
- }
- if (End() == 2)
- {
- if (Get_Q1(p1) == 'O')
- cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<
- else
- cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<
- }
七、游戏后期处理
- cout<<"------------------------------"<
- cout<<"********是否继续游戏?********"<
- cout<<"1.重新开始 2.退出游戏"<
- cin>>xz;
- switch(xz)
- {
- case 1:return_CSH(),main();break;
- case 2:return 0;break;
- default:cout<<"指令错误,游戏终止!";break;
- }
- }
源码:
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- char play[9] = { '1','2','3','4','5','6','7','8','9' };
- void system()
- {
- system("color FD");
- system("date/t");
- system("time/t");
- system("title 井字棋 Ver:1.0.0");
- }
-
- void CSH()
- {
- cout << "######井字棋小游戏######" << endl;
- cout << "(*游戏规则:先掷骰子选出优先下子者,然后轮流下子,任一方若有三子连成一线,该方即获胜)" << endl;
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n"<
- cout<<"鸡你太美"<
- cout<<"你干嘛,哎呦"<
- cout<<"-------------------------------------------------------"<
- }
- void return_CSH()
- {
- memset(play,0,9);
- play[0] = '1';
- play[1] = '2';
- play[2] = '3';
- play[3] = '4';
- play[4] = '5';
- play[5] = '6';
- play[6] = '7';
- play[7] = '8';
- play[8] = '9';
- }
- class Player {
- public:
- Player() {}
- void Name(int i)
- {
- cout << "请为玩家" << i << "设置玩家名称:";
- cin >> name;
- }
- void Get_Name()
- {
- cout << name;
- }
- int Order()
- {
- cout << "请玩家 " << name << " 掷骰子:";
- system("pause");
- srand((unsigned)time(NULL));
- a = rand() % 6 + 1;
- cout << a << endl;
- return a;
- }
- int PD()
- {
- return a;
- }
- void XQ_1()
- {
- cout << "******游戏设置******" << endl;
- cout << " 1.O 2.X " << endl;
- cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
- while (xq == 0)
- {
- cin >> xq;
- if (xq == 1)
- q1 = 'O';
- else if (xq == 2)
- q1 = 'X';
- else
- cout << "命令错误!请重新输入合法选项:";
- }
- }
- void XQ_2()
- {
- cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
- while (xq == 0)
- {
- cin >> xq;
- if (xq == 1)
- q2 = 'O';
- else if (xq == 2)
- q2 = 'X';
- else
- cout << "命令错误!请重新输入合法选项:";
- }
- }
- void XS_Play()
- {
- int n;
- cout << "请玩家 " << name << " 输入下棋位置:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
- {
- play[n - 1] = q1;
- }
- else
- {
- cout << "位置不合法!请重新选择:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q1;
- }
- else
- {
- cout << "我感觉你是故意的";
- exit(1);
- }
- }
- system("cls");
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- }
- void HS_Play()
- {
- int n;
- cout << "请玩家 " << name << " 输入下棋位置:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q2;
- }
- else
- {
- cout << "位置不合法!请重新选择:";
- cin >> n;
- if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
- {
- play[n - 1] = q2;
- }
- else
- {
- cout << "你又找茬是吧?";
- exit(1);
- }
- }
- system("cls");
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- }
- friend int Get_Q1(Player& p1);
- friend int Get_Q2(Player& p2);
- private:
- string name;
- int a = 0;
- int xq = 0;
- char q1, q2;
- };
-
-
- int Get_Q1(Player& p1)
- {
- return p1.q1;
- }
- int Get_Q2(Player& p2)
- {
- return p2.q2;
- }
-
- int End()
- {
- if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
- {
- if (play[3] == 'X')
- {
- return 1;
- }
- if (play[3] == 'O')
- {
- return 2;
- }
- }
- if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
- {
- if (play[6] == 'X')
- {
- return 1;
- }
- if (play[6] == 'O')
- {
- return 2;
- }
- }
-
- if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
- {
- if (play[1] == 'X')
- {
- return 1;
- }
- if (play[1] == 'O')
- {
- return 2;
- }
- }
- if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
- {
- if (play[1] == 'X')
- {
- return 1;
- }
- if (play[1] == 'O')
- {
- return 2;
- }
- }
- if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
- {
- if (play[0] == 'X')
- {
- return 1;
- }
- if (play[0] == 'O')
- {
- return 2;
- }
- }
- if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
- {
- if (play[2] == 'X')
- {
- return 1;
- }
- if (play[2] == 'O')
- {
- return 2;
- }
- }
- return 0;
- }
-
- int main()
- {
- system();
- CSH();
- Player p1;
- Player p2;
- int xz;
- int j = 0;
- int xs = 0;
- p1.Name(1);
- p2.Name(2);
- while (xs == 0)
- {
- p1.Order();
- p2.Order();
- if (p1.PD() > p2.PD())
- cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
- else if (p1.PD() < p2.PD())
- cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
- else
- cout << "双方点数一样大,请重新掷点数:" << endl;
- }
- if (xs == 1)
- {
- p1.XQ_1(), p2.XQ_2();
- }
- else if (xs == 2)
- {
- p2.XQ_1(), p1.XQ_2();
- }
- CSH();
- while (j < 5)
- {
- if (xs == 1)
- {
- p1.XS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- p2.HS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- }
- if (xs == 2)
- {
- p2.XS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- p1.HS_Play();
- End();
- if (End() != 0)
- {
- break;
- }
- }
- j++;
- }
- system("cls");
- cout<<"******游戏结束******"<
- cout << "+---+---+---+\n"
- "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
- "+---+---+---+\n"
- "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
- "+---+---+---+\n"
- "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
- "+---+---+---+\n";
- if (End() == 1)
- {
- if (Get_Q1(p1) == 'x')
- cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<
- else
- cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<
- }
- if (End() == 2)
- {
- if (Get_Q1(p1) == 'O')
- cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<
- else
- cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<
- }
- cout<<"------------------------------"<
- cout<<"********是否继续游戏?********"<
- cout<<"1.重新开始 2.退出游戏"<
- cin>>xz;
- switch(xz)
- {
- case 1:return_CSH(),main();break;
- case 2:return 0;break;
- default:cout<<"指令错误,游戏终止!";break;
- }
- }
-
相关阅读:
基于樽海鞘群算法的线性规划求解matlab程序
计算系统概论ics第五章习题
.NET RulesEngine(规则引擎)
拖尾渲染器-Unity拖尾渲染器的使用
(八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
VS编译.cu文件源文件无法打开matrix.h和mex.h问题
新渲染引擎、自定义设计和高质量用户体验的样例应用 Wonderous 现已开源
VSC-HVDC直流输电matlab仿真模型
2024 年(第 12 届)“泰迪杯”数据挖掘挑战赛—— C 题:竞赛论文的辅助自动评阅完整思路与源代码分享
-
原文地址:https://blog.csdn.net/djfihhfs/article/details/127683917