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

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++井字棋游戏代码可以使用EasyX图形库进行实现,下面是一个简单的示例代码: ``` #include <graphics.h> #include <conio.h> const int kBoardSize = 3; // 棋盘大小 // 绘制棋盘 void DrawBoard() { cleardevice(); // 清空屏幕 int width = getwidth() / kBoardSize; int height = getheight() / kBoardSize; // 绘制横线 for (int i = 1; i < kBoardSize; i++) { line(0, i * height, getwidth(), i * height); } // 绘制竖线 for (int i = 1; i < kBoardSize; i++) { line(i * width, 0, i * width, getheight()); } } // 下棋 void PlayChess(int row, int col, bool is_black) { int x = col * getwidth() / kBoardSize + getwidth() / kBoardSize / 2; int y = row * getheight() / kBoardSize + getheight() / kBoardSize / 2; if (is_black) { setcolor(BLACK); circle(x, y, getwidth() / kBoardSize / 2 - 10); } else { setcolor(WHITE); line(x - getwidth() / kBoardSize / 2 + 10, y - getheight() / kBoardSize / 2 + 10, x + getwidth() / kBoardSize / 2 - 10, y + getheight() / kBoardSize / 2 - 10); line(x - getwidth() / kBoardSize / 2 + 10, y + getheight() / kBoardSize / 2 - 10, x + getwidth() / kBoardSize / 2 - 10, y - getheight() / kBoardSize / 2 + 10); } } int main() { initgraph(600, 600); DrawBoard(); bool is_black = true; // 黑棋先手 while (true) { if (_kbhit()) { // 如果有按键按下 char ch = _getch(); if (ch == 27) break; // 如果是Esc键,退出游戏 int row = -1; int col = -1; bool can_put = false; switch (ch) { case 'q': row = 0; col = 0; can_put = true; break; case 'w': row = 0; col = 1; can_put = true; break; case 'e': row = 0; col = 2; can_put = true; break; case 'a': row = 1; col = 0; can_put = true; break; case 's': row = 1; col = 1; can_put = true; break; case 'd': row = 1; col = 2; can_put = true; break; case 'z': row = 2; col = 0; can_put = true; break; case 'x': row = 2; col = 1; can_put = true; break; case 'c': row = 2; col = 2; can_put = true; break; } if (can_put) { PlayChess(row, col, is_black); is_black = !is_black; } } } closegraph(); return 0; } ``` 此示例中,通过`initgraph`函数初始化图形界面,然后在`DrawBoard`函数中绘制棋盘,`PlayChess`函数用于在棋盘上下棋。在`main`函数中,通过`_kbhit`函数检测键盘是否有按键按下,然后根据按键的不同来确定落子的位置,通过`PlayChess`函数在棋盘上下棋,并通过`is_black`变量来控制黑白棋的交替。当按下Esc键时,游戏退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值