c++井字棋

 帖主用c++做了一个井字棋!刚学c++不久,只用了一些基础的代码,有bug欢迎留言!

废话不多数,让我们开始吧!

教程

首先我们把框架写出来:

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
    
}

然后我们再新建一些变量存储每个格子的状态:

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;//用来记录每个格子的状态
	int p1_y,p1_x,p2_y,p2_x;//玩家1和玩家2下棋子输入的坐标
}

然后我们再画出一个“井”字的地图

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		if (a == 0){
            cout<<" ";
        else if (a == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
		cout<<"|"; 
        if (b == 0){
            cout<<" ";
        else if (b == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
        if (c == 0){
            cout<<" ";
        else if (c == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
        cout<<endl<<"-+-+-"<<endl;
        if (d == 0){
            cout<<" ";
        else if (d == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
		cout<<"|"; 
        if (e == 0){
            cout<<" ";
        else if (e == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
        if (f == 0){
            cout<<" ";
        else if (f == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
        cout<<endl<<"-+-+-"<<endl;
        if (g == 0){
            cout<<" ";
        else if (g == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
		cout<<"|"; 
        if (h == 0){
            cout<<" ";
        else if (h == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
        if (i == 0){
            cout<<" ";
        else if (i == 1){
            cout<<"O";
        }
        else{
            cout<<"X");
        }
    }
}

但是以上代码实在是太长了,所以我就用到了三目运算进行简化

/*
三目运算:条件?代码1:代码2;

只要条件满足,就会执行代码1,否则就会执行代码2
*/
#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
    }
}

地图就画好了,那么我们就再实现玩家1的下棋功能吧

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		cout<<endl<<"请玩家1输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p1_x>>p1_y;
		if (p1_x < 1 || p1_x > 3 || p1_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p1_x == 1 && p1_y == 1) a = 1;
		if (p1_x == 2 && p1_y == 1) b = 1;
		if (p1_x == 3 && p1_y == 1) c = 1;
		if (p1_x == 1 && p1_y == 2) d = 1;
		if (p1_x == 2 && p1_y == 2) e = 1;
		if (p1_x == 3 && p1_y == 2) f = 1;
		if (p1_x == 1 && p1_y == 3) g = 1;
		if (p1_x == 2 && p1_y == 3) h = 1;
		if (p1_x == 3 && p1_y == 3) i = 1;
		system("cls");
    }
}

然后我们再复制一份给玩家2

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		cout<<endl<<"请玩家1输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p1_x>>p1_y;
		if (p1_x < 1 || p1_x > 3 || p1_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		if (p1_x == 1 && p1_y == 1) a = 1;
		if (p1_x == 2 && p1_y == 1) b = 1;
		if (p1_x == 3 && p1_y == 1) c = 1;
		if (p1_x == 1 && p1_y == 2) d = 1;
		if (p1_x == 2 && p1_y == 2) e = 1;
		if (p1_x == 3 && p1_y == 2) f = 1;
		if (p1_x == 1 && p1_y == 3) g = 1;
		if (p1_x == 2 && p1_y == 3) h = 1;
		if (p1_x == 3 && p1_y == 3) i = 1;
		system("cls");
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		cout<<endl<<"请玩家2输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p2_x>>p2_y;
		if (p2_x < 1 || p2_x > 3 || p2_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p2_x == 1 && p2_y == 1) a = 2;
		if (p2_x == 2 && p2_y == 1) b = 2;
		if (p2_x == 3 && p2_y == 1) c = 2;
		if (p2_x == 1 && p2_y == 2) d = 2;
		if (p2_x == 2 && p2_y == 2) e = 2;
		if (p2_x == 3 && p2_y == 2) f = 2;
		if (p2_x == 1 && p2_y == 3) g = 2;
		if (p2_x == 2 && p2_y == 3) h = 2;
		if (p2_x == 3 && p2_y == 3) i = 1;
		system("cls");
    }
} 

最后我们再添加上一个玩家试图在一个已经有的格子里再下一个棋子的限制

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		if (a == 2 && b == 2 && c == 2 || d == 2 && e == 2 && f == 2 || g == 2 && h == 2 && i == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		if (a == 2 && d == 2 && g == 2 || b == 2 && e == 2 && h == 2 || c == 2 && f == 2 && i == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		if (a == 2 && e == 2 && i == 2 || c == 2 && e == 2 && g == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		cout<<endl<<"请玩家1输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p1_x>>p1_y;
		if (p1_x < 1 || p1_x > 3 || p1_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p1_x == 1 && p1_y == 1 && a == 1 || p1_x == 1 && p1_y == 1 && a == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 1 && b == 1 || p1_x == 2 && p1_y == 1 && b == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 1 && c == 1 || p1_x == 3 && p1_y == 1 && c == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 2 && d == 1 || p1_x == 1 && p1_y == 2 && d == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 2 && e == 1 || p1_x == 2 && p1_y == 2 && e == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 2 && f == 1 || p1_x == 3 && p1_y == 2 && f == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 3 && g == 1 || p1_x == 1 && p1_y == 3 && g == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 3 && h == 1 || p1_x == 2 && p1_y == 3 && h == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 3 && i == 1 || p1_x == 2 && p1_y == 3 && i == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 1) a = 1;
		if (p1_x == 2 && p1_y == 1) b = 1;
		if (p1_x == 3 && p1_y == 1) c = 1;
		if (p1_x == 1 && p1_y == 2) d = 1;
		if (p1_x == 2 && p1_y == 2) e = 1;
		if (p1_x == 3 && p1_y == 2) f = 1;
		if (p1_x == 1 && p1_y == 3) g = 1;
		if (p1_x == 2 && p1_y == 3) h = 1;
		if (p1_x == 3 && p1_y == 3) i = 1;
		system("cls");
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		if (a == 1 && b == 1 && c == 1 || d == 1 && e == 1 && f == 1 || g == 1 && h == 1 && i == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		if (a == 1 && d == 1 && g == 1 || b == 1 && e == 1 && h == 1 || c == 1 && f == 1 && i == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		if (a == 1 && e == 1 && i == 1 || c == 1 && e == 1 && g == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		cout<<endl<<"请玩家2输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p2_x>>p2_y;
		if (p2_x < 1 || p2_x > 3 || p2_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p2_x == 1 && p2_y == 1 && a == 1 || p2_x == 1 && p2_y == 1 && a == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 1 && b == 1 || p2_x == 2 && p2_y == 1 && b == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 1 && c == 1 || p2_x == 3 && p2_y == 1 && c == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 2 && d == 1 || p2_x == 1 && p2_y == 2 && d == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 2 && e == 1 || p2_x == 2 && p2_y == 2 && e == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 2 && f == 1 || p2_x == 3 && p2_y == 2 && f == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 3 && g == 1 || p2_x == 1 && p2_y == 3 && g == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 3 && h == 1 || p2_x == 2 && p2_y == 3 && h == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 3 && i == 1 || p2_x == 2 && p2_y == 3 && i == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 1) a = 2;
		if (p2_x == 2 && p2_y == 1) b = 2;
		if (p2_x == 3 && p2_y == 1) c = 2;
		if (p2_x == 1 && p2_y == 2) d = 2;
		if (p2_x == 2 && p2_y == 2) e = 2;
		if (p2_x == 3 && p2_y == 2) f = 2;
		if (p2_x == 1 && p2_y == 3) g = 2;
		if (p2_x == 2 && p2_y == 3) h = 2;
		if (p2_x == 3 && p2_y == 3) i = 1;
		system("cls");
    }
} 

那么我们的c++井字棋就做完了!

全代码

#include<iostream>
#include<windows.h>
using namespace std;
int main(){
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
	int p1_y,p1_x,p2_y,p2_x;
	while (true){
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		if (a == 2 && b == 2 && c == 2 || d == 2 && e == 2 && f == 2 || g == 2 && h == 2 && i == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		if (a == 2 && d == 2 && g == 2 || b == 2 && e == 2 && h == 2 || c == 2 && f == 2 && i == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		if (a == 2 && e == 2 && i == 2 || c == 2 && e == 2 && g == 2){
			cout<<"玩家2胜利!!";
			return 0;
		}
		cout<<endl<<"请玩家1输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p1_x>>p1_y;
		if (p1_x < 1 || p1_x > 3 || p1_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p1_x == 1 && p1_y == 1 && a == 1 || p1_x == 1 && p1_y == 1 && a == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 1 && b == 1 || p1_x == 2 && p1_y == 1 && b == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 1 && c == 1 || p1_x == 3 && p1_y == 1 && c == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 2 && d == 1 || p1_x == 1 && p1_y == 2 && d == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 2 && e == 1 || p1_x == 2 && p1_y == 2 && e == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 2 && f == 1 || p1_x == 3 && p1_y == 2 && f == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 3 && g == 1 || p1_x == 1 && p1_y == 3 && g == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 2 && p1_y == 3 && h == 1 || p1_x == 2 && p1_y == 3 && h == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 3 && p1_y == 3 && i == 1 || p1_x == 2 && p1_y == 3 && i == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p1_x == 1 && p1_y == 1) a = 1;
		if (p1_x == 2 && p1_y == 1) b = 1;
		if (p1_x == 3 && p1_y == 1) c = 1;
		if (p1_x == 1 && p1_y == 2) d = 1;
		if (p1_x == 2 && p1_y == 2) e = 1;
		if (p1_x == 3 && p1_y == 2) f = 1;
		if (p1_x == 1 && p1_y == 3) g = 1;
		if (p1_x == 2 && p1_y == 3) h = 1;
		if (p1_x == 3 && p1_y == 3) i = 1;
		system("cls");
		a == 0?cout<<" ":(a == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		b == 0?cout<<" ":(b == 1?cout<<"O":cout<<"X");
		cout<<"|";
		c == 0?cout<<" ":(c == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		d == 0?cout<<" ":(d == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		e == 0?cout<<" ":(e == 1?cout<<"O":cout<<"X");
		cout<<"|";
		f == 0?cout<<" ":(f == 1?cout<<"O":cout<<"X");
		cout<<endl<<"-+-+-"<<endl;
		g == 0?cout<<" ":(g == 1?cout<<"O":cout<<"X");
		cout<<"|"; 
		h == 0?cout<<" ":(h == 1?cout<<"O":cout<<"X");
		cout<<"|";
		i == 0?cout<<" ":(i == 1?cout<<"O":cout<<"X");
		cout<<endl;
		if (a == 1 && b == 1 && c == 1 || d == 1 && e == 1 && f == 1 || g == 1 && h == 1 && i == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		if (a == 1 && d == 1 && g == 1 || b == 1 && e == 1 && h == 1 || c == 1 && f == 1 && i == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		if (a == 1 && e == 1 && i == 1 || c == 1 && e == 1 && g == 1){
			cout<<"玩家1胜利!!";
			return 0;
		}
		cout<<endl<<"请玩家2输入你要下的棋子"<<endl<<"的位置坐标(两个数之间用"<<endl<<"空格隔开):";
		cin>>p2_x>>p2_y;
		if (p2_x < 1 || p2_x > 3 || p2_y < 1 || p1_y > 3){
			cout<<endl<<"学会认真输入再玩吧,再见";
			return 0;
		}
		if (p2_x == 1 && p2_y == 1 && a == 1 || p2_x == 1 && p2_y == 1 && a == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 1 && b == 1 || p2_x == 2 && p2_y == 1 && b == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 1 && c == 1 || p2_x == 3 && p2_y == 1 && c == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 2 && d == 1 || p2_x == 1 && p2_y == 2 && d == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 2 && e == 1 || p2_x == 2 && p2_y == 2 && e == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 2 && f == 1 || p2_x == 3 && p2_y == 2 && f == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 3 && g == 1 || p2_x == 1 && p2_y == 3 && g == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 2 && p2_y == 3 && h == 1 || p2_x == 2 && p2_y == 3 && h == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 3 && p2_y == 3 && i == 1 || p2_x == 2 && p2_y == 3 && i == 2){
			cout<<"你会不会玩啊,这个点已经有棋子了";
			return 0;
		}
		if (p2_x == 1 && p2_y == 1) a = 2;
		if (p2_x == 2 && p2_y == 1) b = 2;
		if (p2_x == 3 && p2_y == 1) c = 2;
		if (p2_x == 1 && p2_y == 2) d = 2;
		if (p2_x == 2 && p2_y == 2) e = 2;
		if (p2_x == 3 && p2_y == 2) f = 2;
		if (p2_x == 1 && p2_y == 3) g = 2;
		if (p2_x == 2 && p2_y == 3) h = 2;
		if (p2_x == 3 && p2_y == 3) i = 1;
		system("cls");
    }
} 

这个作品我想了好久才做出来,点个赞吧QAQ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值