自制双人五子棋
2025-01-19 22:30:06
发布于:广东
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const int N = 15;
int nx,ny;
string name1,name2;
char oc = '.';
bool color = 1;
char table[N][N];
bool ff;
char f(int x,int y){
char co = table[x][y];
if(co=='.'){
ff = 0;
return '.';
}int cnt=0;
for(int i=y;i<=y+4&&i<14;i++)if(table[x][i] == co)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=x;i<=x+4&&i<14;i++)if(table[i][y] == co)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=0;i<=4;i++)if(table[x+i][y+i]==co and x+i<14 and y+i<14)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=0;i<=4;i++)if(table[x-i][y+i]==co and x-i>0 and y+i<14)cnt++;
if(cnt==5){
ff = 1;
return co;
}ff=0;
return co;
}bool dw(){
for(int i=1;i<=13;i++){
for(int j=1;j<=13;j++){
char dd= f(i,j);
if(ff){
if(dd=='O')cout << name1 <<"赢了\n";
else cout << name2 << "赢了\n";
getch();
system("cls");
return false;
}
}
}return true;
}
void gotoxy(short x,short y){
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}void init(){
color = 1;
nx = ny = 6;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i==nx and j==ny)table[i][j] = '@';
else if(!i or !j or i==N-1 or j==N-1)table[i][j] = '#';
else table[i][j] = '.';
}
}
}void print(){
gotoxy(0,0);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cout << table[i][j] << " ";
}cout << endl;
}
}void move(int px,int py){
table[nx][ny] = oc;
oc = '.';
nx += px,ny+=py;
if(nx<1 or ny<1 or nx>13 or ny>13)nx -=px,ny-=py;
if(table[nx][ny]!='.'){
oc = table[nx][ny];
table[nx][ny] = '!';
}
else table[nx][ny] = '@';
}void down(){
if(table[nx][ny]!='@')return;
if(color){
oc = 'O';
table[nx][ny] = 'O';
}else{
table[nx][ny] = 'X';
oc = 'X';
}color = !color;
}
int main(){
bool end = 1;
cout << "请输入玩家1的名字,他的棋子形状是O"<<endl;
cin >> name1;
cout << "请输入玩家2的名字,他的棋子形状是X"<<endl;
cin >> name2;
system("cls");
while(end){
init();
print();
oc = '.';
while(dw()){
char f;
f = getch();
if(f=='a')move(0,-1);
if(f=='s')move(1,0);
if(f=='d')move(0,1);
if(f=='w')move(-1,0);
if(f==' ')down();
print();
}while(1){
cout << "按下c键进行下一轮e键结束\n";
char input = getch();
if(input=='c')break;
else if(input=='e'){
end = 0;
break;
}else cout << "输入错误,请重新输入\n";
}
}
}
在游戏中@代表落子位置
按wasd控制落子位置
按空格键开始
O先下 X后下
会自动判定输赢
参与者名单
「欢愉」花火
最佳MVP
腾讯会议
全部评论 3
大佬%%%
2025-01-19 来自 湖南
1大佬秒回了!!感谢您的支持!!
2025-01-19 来自 广东
2还能wasd
2025-01-19 来自 湖南
1欸嘿
2025-01-19 来自 广东
1
耶!新项目!
2025-01-19 来自 广东
1要是有自动判断五子连珠就好了
2025-01-19 来自 浙江
0有啊
2025-01-19 来自 广东
0
有帮助,赞一个