用户:
萌新大佬查看:3 回复:3 评论:3 创建时间:2020-10-06T14:37:31
我又做出了一个C++游戏————————————————————————————————————————————五子棋!!!!想要代码就点个赞,点5个给详细注释!![]()
萌新大佬#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
int qp[22][22]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
void printc(const char* s, int color){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
bool h_(int who,int x,int y){
bool is_h;
for(int i=x-4;i<=x;i++){
is_h=true;
for(int j=0;j<5;j++){
if(qp[y][i+j]!=who)is_h=false;
}
if(is_h)return is_h;
}
return is_h;
}
bool s_(int who,int x,int y){
bool is_s;
for(int i=y-4;i<=y;i++){
is_s=true;
for(int j=0;j<5;j++){
if(qp[i+j][x]!=who)is_s=false;
}
if(is_s)return is_s;
}
return is_s;
}
bool x_1(int who,int x,int y){
bool is_x1;
for(int i=0;i<5;i++){
is_x1=true;
for(int j=0;j<5;j++){
if(qp[y-i+j][x-i+j]!=who)is_x1=false;
}
if(is_x1)return is_x1;
}
return is_x1;
}
bool x_2(int who,int x,int y){
bool is_x2;
for(int i=0;i<5;i++){
is_x2=true;
for(int j=0;j<5;j++){
if(qp[y-i+j][x+i-j]!=who)is_x2=false;
}
if(is_x2)return is_x2;
}
return is_x2;
}
int main(){
int x=10,y=10,o=0,h=0;
string z[3]={"黑","白"};
while(1){
system("cls");
for(int i=0;i<22;i++){
for(int j=0;j<22;j++){
if(qp[i][j]==1)cout<<"○";
else if(qp[i][j]==2)cout<<"●";
else if(qp[i][j]==4)printc("●",4);
else if(qp[i][j]==3)printc("○",4);
else cout<<" ";
}
cout<<endl;
}
char a;
a=_getch();
if(a=='w'){
qp[y][x]=o;
y--;
o=qp[y][x];
qp[y][x]=h%2+3;
}
else if(a=='s'){
qp[y][x]=o;
y++;
o=qp[y][x];
qp[y][x]=h%2+3;
}
else if(a=='a'){
qp[y][x]=o;
x--;
o=qp[y][x];
qp[y][x]=h%2+3;
}
else if(a=='d'){
qp[y][x]=o;
x++;
o=qp[y][x];
qp[y][x]=h%2+3;
}
else if(a==' '){
if(o==0){
qp[y][x]=o=h%2+1;
if(h_(h%2+1,x,y)||s_(h%2+1,x,y)||x_1(h%2+1,x,y)||x_2(h%2+1,x,y)){
system("cls");
printf("%s子玩家赢了!!\n",z[h%2].c_str());
system("pause");
exit(1);
}
h++;
}
else {
cout<<"wrong step";
system("pause");
}
}
}
}点赞0
评论