小游♂戏
Arc0_FishyFool · · 个人记录
游戏非原创:
手撕写实现,开源,普天同庆:
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
char huge[4][4],small[12][12],cmd;
int x,y,pl,xx[9]={1,1,1,5,5,5,9,9,9},yy[9]={1,5,9,1,5,9,1,5,9},rst[9],tem,xam,yam;
void pre(){
cout<<"--=== ===== --=== ---== /|\n ___ / |\n / _ \\ ___ _ /\\ ___ /\\ ____ / |\n / /_> > / _ \\ \\ \\ / / / _ \\ \\ \\/ __ \\ / / |\n / ___ / |<_> \\ \\ \\/ / | |_>_> | / | | \\/_/ \n/_/ \\_\\ \\__/\\_\\ \\__/ \\____/ /_/ \\_| / \n-=- =-== -= =- ===== ====- ---=== ===== ~~~~~~v\n";
cout<<"欢迎来到Raven用c++实现的简易方位井字棋游戏!\n";
cout<<"按任意字母/数字键以查看规则。\n";
cmd=getch();
system("cls");
cout<<"规则如下,请您细品(每次打开程序只显示一次!!!):\n";
cout<<"开始游戏后,将出现一个类似九宫数独的棋盘,一个小棋盘,表示每宫的占有情况,初始状态下,先手(X)现在中央宫落子。\n";
cout<<"落子:Q为左上,W为中上,E为右上,A为中左,S为中央,D为中右,Z为左下,X为中下,C为右下(必须小写)。\n";
cout<<"对于正常键盘,九键排列成九宫格,极易分辨。\n";
cout<<"每次落子在该宫的位置,将决定对手下一次落子的地方。\n";
cout<<"(如当X该回合落在该宫左上角,下回合O将被限定在左上宫落子)\n";
cout<<"特别地,当该回合落子范围内全满,系统将提示您,您可以在任一宫中任一无子处落子。\n";
cout<<"当一人落子后宫内同类子三字成线(不得越界)时,该方占有此格,小棋盘上对应位置更新!\n";
cout<<"注意!!!除非宫满,否则若另一方在宫内三子成线,则该宫所有权被另一方抢夺!!!\n";
cout<<"获胜条件:若一方占有的宫三子成线,则该方获胜!!!\n";
cout<<"特别鸣谢:\n";
cout<<"Raven:没有这个B你玩不了他c++实现的游戏。\n";
cout<<"Raven某同桌:没有他Raven不知道这个井字棋的改良版。\n";
cout<<"“方位井字棋”的所有发明者:感谢他(们)把原本无聊(存疑)的井字棋改良成这个更好玩的版本!!!\n";
cout<<"井字棋的所有发明者:感谢他(们)创造了井字棋,提供了改良空间!!!\n";
cout<<"找好对手后按任意字母/数字键以开始游戏。\n";
cmd=getch();
system("cls");
}
void init(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
huge[i][j]='.';
}
}
for(int i=0;i<11;i++){
small[i][3]=small[i][7]='|';
small[3][i]=small[7][i]='-';
}
small[3][7]=small[7][3]=small[7][7]=small[3][3]='+';
for(int i=0;i<11;i++){
for(int j=0;j<11;j++){
if(small[i][j]!='|'&&small[i][j]!='-'&&small[i][j]!='+') small[i][j]='.';
}
}
for(int i=0;i<9;i++) rst[i]=9;
}
void print_board(){
cout<<"微观战局:\n";
for(int i=0;i<11;i++){
for(int j=0;j<11;j++) cout<<small[i][j];
cout<<endl;
}
cout<<"宏观战局:\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++) cout<<huge[i][j];
cout<<endl;
}
}
void hint(){
if(pl==0) cout<<"您须在左上宫落子\n";
if(pl==1) cout<<"您须在中上宫落子\n";
if(pl==2) cout<<"您须在右上宫落子\n";
if(pl==3) cout<<"您须在中左宫落子\n";
if(pl==4) cout<<"您须在中央宫落子\n";
if(pl==5) cout<<"您须在中右宫落子\n";
if(pl==6) cout<<"您须在左下宫落子\n";
if(pl==7) cout<<"您须在中下宫落子\n";
if(pl==8) cout<<"您须在右下宫落子\n";
}
void mark(){
if(small[xx[pl]-1][yy[pl]-1]==small[xx[pl]-1][yy[pl]]&&small[xx[pl]-1][yy[pl]]==small[xx[pl]-1][yy[pl]+1]&&small[xx[pl]-1][yy[pl]+1]!='.'&&small[xx[pl]-1][yy[pl]+1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]-1][yy[pl]+1];
if(small[xx[pl]][yy[pl]-1]==small[xx[pl]][yy[pl]]&&small[xx[pl]][yy[pl]]==small[xx[pl]][yy[pl]+1]&&small[xx[pl]][yy[pl]+1]!='.'&&small[xx[pl]][yy[pl]+1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]][yy[pl]];
if(small[xx[pl]+1][yy[pl]-1]==small[xx[pl]+1][yy[pl]]&&small[xx[pl]+1][yy[pl]]==small[xx[pl]+1][yy[pl]+1]&&small[xx[pl]+1][yy[pl]+1]!='.'&&small[xx[pl]+1][yy[pl]+1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]+1][yy[pl]+1];
if(small[xx[pl]-1][yy[pl]-1]==small[xx[pl]][yy[pl]-1]&&small[xx[pl]][yy[pl]-1]==small[xx[pl]+1][yy[pl]-1]&&small[xx[pl]+1][yy[pl]-1]!='.'&&small[xx[pl]+1][yy[pl]-1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]+1][yy[pl]-1];
if(small[xx[pl]-1][yy[pl]]==small[xx[pl]][yy[pl]]&&small[xx[pl]][yy[pl]]==small[xx[pl]+1][yy[pl]]&&small[xx[pl]+1][yy[pl]]!='.'&&small[xx[pl]+1][yy[pl]]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]][yy[pl]];
if(small[xx[pl]-1][yy[pl]+1]==small[xx[pl]][yy[pl]+1]&&small[xx[pl]][yy[pl]+1]==small[xx[pl]+1][yy[pl]+1]&&small[xx[pl]+1][yy[pl]+1]!='.'&&small[xx[pl]+1][yy[pl]+1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]+1][yy[pl]+1];
if(small[xx[pl]-1][yy[pl]-1]==small[xx[pl]][yy[pl]]&&small[xx[pl]][yy[pl]]==small[xx[pl]+1][yy[pl]+1]&&small[xx[pl]+1][yy[pl]+1]!='.'&&small[xx[pl]+1][yy[pl]+1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]][yy[pl]];
if(small[xx[pl]-1][yy[pl]+1]==small[xx[pl]][yy[pl]]&&small[xx[pl]][yy[pl]]==small[xx[pl]+1][yy[pl]-1]&&small[xx[pl]+1][yy[pl]-1]!='.'&&small[xx[pl]+1][yy[pl]-1]!=huge[pl/3][pl%3]) huge[pl/3][pl%3]=small[xx[pl]][yy[pl]];
}
bool win(){
char winner;
if(huge[0][0]==huge[0][1]&&huge[0][2]==huge[0][1]&&huge[0][0]!='.'){
winner=huge[0][0];
goto awm;
}
else if(huge[1][0]==huge[1][1]&&huge[1][2]==huge[1][1]&&huge[1][1]!='.'){
winner=huge[1][1];
goto awm;
}
else if(huge[2][0]==huge[2][1]&&huge[2][2]==huge[2][1]&&huge[2][2]!='.'){
winner=huge[2][2];
goto awm;
}
else if(huge[0][0]==huge[1][0]&&huge[2][0]==huge[1][0]&&huge[0][0]!='.'){
winner=huge[0][0];
goto awm;
}
else if(huge[0][1]==huge[1][1]&&huge[1][1]==huge[2][1]&&huge[1][1]!='.'){
winner=huge[1][1];
goto awm;
}
else if(huge[0][2]==huge[1][2]&&huge[2][2]==huge[1][2]&&huge[2][2]!='.'){
winner=huge[2][2];
goto awm;
}
else if(huge[0][0]==huge[1][1]&&huge[1][1]==huge[2][2]&&huge[0][0]!='.'){
winner=huge[0][0];
goto awm;
}
else if(huge[0][2]==huge[1][1]&&huge[1][1]==huge[2][0]&&huge[1][1]!='.'){
winner=huge[1][1];
goto awm;
}
else return 0;
awm:cout<<"游戏结束!!!获胜者是"<<winner<<"方!!!";
return 1;
}
void go(){
char chx,goo;
if(tem%2==1) chx='X';
else chx='O';
if(!rst[pl]){
ak47:cout<<"此宫已满!!!输入以选择一未满的宫落子!!!(键位与一般情况下落子时相同,因为懒写,此次选择无法悔改)\n";
char gong;
gong=getch();
if(gong=='q'){
if(!rst[0]) goto ak47;
pl=0;
}
if(gong=='w'){
if(!rst[1]) goto ak47;
pl=1;
}
if(gong=='e'){
if(!rst[2]) goto ak47;
pl=2;
}
if(gong=='a'){
if(!rst[3]) goto ak47;
pl=3;
}
if(gong=='s'){
if(!rst[4]) goto ak47;
pl=4;
}
if(gong=='d'){
if(!rst[5]) goto ak47;
pl=5;
}
if(gong=='z'){
if(!rst[6]) goto ak47;
pl=6;
}
if(gong=='x'){
if(!rst[7]) goto ak47;
pl=7;
}
if(gong=='c'){
if(!rst[8]) goto ak47;
pl=8;
}
hint();
}
al:goo=getch();
if(goo=='q'){
if(small[xx[pl]-1][yy[pl]-1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar0:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]-1][yy[pl]-1]=chx;
rst[pl]--;
mark();
pl=0;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar0;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='w'){
if(small[xx[pl]-1][yy[pl]]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar1:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]-1][yy[pl]]=chx;
rst[pl]--;
mark();
pl=1;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar1;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
if(goo=='e'){
if(small[xx[pl]-1][yy[pl]+1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar2:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]-1][yy[pl]+1]=chx;
rst[pl]--;
mark();
pl=2;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar2;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='a'){
if(small[xx[pl]][yy[pl]-1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar3:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]][yy[pl]-1]=chx;
rst[pl]--;
mark();
pl=3;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar3;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='s'){
if(small[xx[pl]][yy[pl]]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar4:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]][yy[pl]]=chx;
rst[pl]--;
mark();
pl=4;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar4;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='d'){
if(small[xx[pl]][yy[pl]+1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar5:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]][yy[pl]+1]=chx;
rst[pl]--;
mark();
pl=5;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar5;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='z'){
if(small[xx[pl]+1][yy[pl]-1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar6:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]+1][yy[pl]-1]=chx;
rst[pl]--;
mark();
pl=6;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar6;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='x'){
if(small[xx[pl]+1][yy[pl]]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar7:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]+1][yy[pl]]=chx;
rst[pl]--;
mark();
pl=7;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar7;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else if(goo=='c'){
if(small[xx[pl]+1][yy[pl]+1]=='.'){
cout<<"按1确认,按0取消";
char kkk;
ar8:kkk=getch();
if(kkk=='1'){
cout<<"OK,落子成功\n";
small[xx[pl]+1][yy[pl]+1]=chx;
rst[pl]--;
mark();
pl=8;
return;
}
else if(kkk=='0'){
cout<<"OK,已悔棋\n";
goto al;
}
else{
cout<<"无效\n";
goto ar8;
}
}
else{
cout<<"无效,请重新落子。\n";
goto al;
}
}
else{
cout<<"无效!!!\n";
goto al;
}
}
void draw(){
system("cls");
cout<<"无剩余空间!!!接下来将比较双方占领宫数!!!\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(huge[i][j]=='X') xam++;
if(huge[i][j]=='Y') yam++;
}
}
if(xam>yam) cout<<"X胜!!!\n";
else if(xam<yam) cout<<"O胜!!!\n";
else cout<<"平局!!!\n";
}
int main(){
pre();
rtry:init();
pl=4;
while(1){
bool f=true;
tem++;
for(int i=0;i<9;i++){
if(rst[i]){
f=false;
break;
}
}
if(win()) goto fn;
if(f){
draw();
goto fn;
}
print_board();
hint();
go();
system("cls");
}
fn:cout<<"按Q重来,否则关闭\n";
char rt;
rt=getch();
if(rt=='q'){
system("cls");
goto rtry;
}
return 0;
}//460行,行行出bug