一个简单的小游戏,用C++实现五子棋(双人游戏,不会做AI)
玩法:输入行、列落子
直接上代码(绝不是因为我太懒了)
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
string A = "○ ",B = "● ", N = "+ ",M = "* ";bool f;
void Slow(string a){
int l=a.size();for(int i=0;i<l;i++){cout<<a[i];Sleep(20);} Sleep(100);
}
void color(short a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}
void Slow3(string a) {int l=a.size();for(int i=0;i<l;i++){color(i*2%15==0?i*2%15+1:i*2%15);cout<<a[i];Sleep(20);}cout << endl; Sleep(100);}
struct point{
int s,x,y;
}a[19][19];
void START(){
for(int i = 1; i <= 19; i++){
for(int j = 1; j <= 19; j++){
a[i][j].s = 3;
a[i][j].x = i;
a[i][j].y = j;
}
}
}
void PRINT(){
system("cls");
cout << " ";
for(int i = 1; i <= 19; i++){
if(i<10){
cout << i << ' ';
}
else{
cout << i;
}
}
cout << endl;
for(int i = 1; i <= 19; i++){
if(i<10){
cout << i << ' ';
}
else{
cout << i;
}
for(int j = 1; j <= 19; j++){
if(a[i][j].s == 1){
cout << A;
}
else if(a[i][j].s == 2){
cout << B;
}
else if(a[i][j].s == 4){
color(4);
cout << A;
color(15);
}
else if(a[i][j].s == 5){
color(4);
cout << B;
color(15);
}
else if(i==10&&j==10){
cout << M;
}
else{
cout << N;
}
}
cout << endl;
}
}
void DOWN(bool fl){
if(fl){
Slow("白棋玩家请落棋\n");
Sleep(500);
Slow("请依次输入纵坐标->横坐标(通俗来讲,先打左边一列数,再看上面一行数)\n");
int x = 1,y = 1;
cin >> x >> y;
if(a[x][y].s!=3 || x<1 || x>19 || y<1 || y>19){
Slow("错误! ");
PRINT();
DOWN(fl);
}
a[x][y].s = 1;
}
else{
Slow("黑棋玩家请落棋\n");
Sleep(500);
Slow("请依次输入纵坐标->横坐标(通俗来讲,先打左边一列数,再看上面一行数)\n");
int x = 1,y = 1;
cin >> x >> y;
if(a[x][y].s!=3 || x<1 || x>19 || y<1 || y>19){
Slow("错误! ");
PRINT();
DOWN(fl);
}
a[x][y].s = 2;
}
}
int t;
bool JZ(int t,int x,int y){
for(int i = y-4; i <= y;i++){
if(a[x][i].s == a[x][i+1].s && a[x][i].s == a[x][i+2].s && a[x][i].s == a[x][i+3].s && a[x][i].s == a[x][i+4].s && a[x][i].s == t){
a[x][i].s = a[x][i+1].s = a[x][i+2].s = a[x][i+3].s = a[x][i+4].s = t+3;
return 1;
}
}
return 0;
}
bool JH(int t,int x,int y){
for(int i = x-4; i <= x;i++){
if(a[i][y].s == a[i+1][y].s && a[i][y].s == a[i+2][y].s && a[i][y].s == a[i+3][y].s && a[i][y].s == a[i+4][y].s && a[i][y].s == t){
a[i][y].s = a[i+1][y].s = a[i+2][y].s = a[i+3][y].s = a[i+4][y].s = t+3;
return 1;
}
}
return 0;
}
bool JX(int t,int x,int y){
for(int i = x-4,j = y-4; i <= x&&j<=y;i++,j++){
if(a[i][j].s == a[i+1][j+1].s && a[i][j].s == a[i+2][j+2].s && a[i][j].s == a[i+3][j+3].s && a[i][j].s == a[i+4][j+4].s && a[i][j].s == t){
a[i][j].s = a[i+1][j+1].s = a[i+2][j+2].s = a[i+3][j+3].s = a[i+4][j+4].s = t+3;
return 1;
}
}
return 0;
}
bool JX2(int t,int x,int y){
if(x<=4)return 0;
if(a[x][y].s == a[x-1][y+1].s && a[x][y].s == a[x-2][y+2].s && a[x][y].s == a[x-3][y+3].s && a[x][y].s == a[x-4][y+4].s && a[x][y].s == t){
a[x][y].s = a[x-1][y+1].s = a[x-2][y+2].s = a[x-3][y+3].s = a[x-4][y+4].s = t+3;
return 1;
}
return 0;
}
int JUDGE(){
for(int i = 5; i <= 15; i++){
for(int j = 5; j <= 15; j++){
if(a[i][j].s!=3){
t = a[i][j].s;
}
else{
continue;//此格作废
}
bool aa = JZ(t,i,j);
bool bb = JH(t,i,j);
bool cc = JX(t,i,j);
bool dd = JX2(t,i,j);
if(aa||bb||cc||dd){
return t;
}
}
}
return 0;
}
int main(){
Slow3("欢迎进入超级无敌螺旋闪光神奇五子棋进阶版PLUS2!!!\n");
Sleep(1000);
int temp = 0;
START();
while(1){
color(7);
PRINT();
DOWN(f);
temp = JUDGE();
if(temp){
break;
}
f = !f;
}
PRINT();
if(temp == 1){
Slow("白棋胜");
}
else{
Slow("黑棋胜");
}
return 0;
}
码完收工!