|
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include "stdio.h"
#include "windows.h"
using namespace std;
char stage[27][27];
int x;
char y;
int J_end=0;
int player=1;//player1:x player2:y
bool qizi[27][27];//初始值为false,表示没有棋子
int judge(int index){
char aaa;
if(index==1) aaa='x' ;
if(index==2) aaa='o';
//return 1,plyer1 获胜;return 2,player2 获胜。
for(int i=1;i<=15;i++){
for(int k=1;k<=15;k++){
if(stage[k]==aaa){
int line=1;
//横向判断
for(int m=k+1;m<=k+4;m++){
if(stage[m]==aaa) line++;
}
if(line==5) return index;
//纵向判断
line=1;
for(int n=i+1;n<=i+4;n++){
if(stage[n][k]==aaa) line++;
}
if(line==5) return index;
//正对角判断
line=1;
for(int shu=1,ii=i,kk=k;shu<=4;shu++){
if(stage[ii+shu][kk+shu]==aaa) line++;
}
if(line==5) return index;
//次对角判断
if((y-96)>=5) {
line=1;
for(int shu=1,ii=i,kk=k;shu<=4;shu++){
if(stage[ii-shu][kk-shu]==aaa) line++;
}
if(line==5) return index;
}
}
}
}
return 0;
}
int draw(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
cout<<" "<<"a"<<" "<<"b"<<" "<<"c"<<" "<<"d"<<" "<<"e"<<" "<<"f"<<" "<<"g"<<" "
<<"h"<<" "<<"i"<<" "<<"j"<<" "<<"k"<<" "<<"l"<<" "<<"m"<<" "<<"n"<<" "<<"o"
//<<" "<<"p"<<" "<<"q"<<" "<<"r"<<" "<<"s"<<" "<<"t"<<" "<<"u"<<" "<<"v"<<" "<<"w"<<" "<<"x"<<" "<<"y"<<" "<<"z"
<<endl<<"1"<<" ";
for(int i=1;i<=15;i++){
if(stage[1]=='x'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
cout<<stage[1]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else if(stage[1]=='o'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
cout<<stage[1]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else cout<<stage[1]<<" ";
}
for(int temp_flag=2;temp_flag<=9;temp_flag++){
cout<<endl<<temp_flag<<" ";
for(int k=1;k<=15;k++){
if(stage[temp_flag][k]=='x'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
cout<<stage[temp_flag][k]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else if(stage[temp_flag][k]=='o'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
cout<<stage[temp_flag][k]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else cout<<stage[temp_flag][k]<<" ";
}
}
for(int temp_flag=10;temp_flag<=15;temp_flag++){
cout<<endl<<temp_flag<<" ";
for(int k=1;k<=15;k++){
if(stage[temp_flag][k]=='x'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
cout<<stage[temp_flag][k]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else if(stage[temp_flag][k]=='o'){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
cout<<stage[temp_flag][k]<<" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
}
else cout<<stage[temp_flag][k]<<" ";
}
}
cout<<endl;
}
int main(){
//初始化数组
for(int i=1;i<=15;i++){
for(int k=1;k<=15;k++)
stage[k]='.';
}
while(J_end!=1&&J_end!=2){
//绘制棋盘
draw();
cout<<"======================================"<<endl;
if(player==1){
p1:cout<<"玩家一的回合:"<<" ";
qizi[x][y-96]=true;
J_end=judge(1);
cin>>x>>y;
if(qizi[x][y-96]){
cout<<endl<<" Error!这里已经有棋子了。"<<endl<<"请重新输入:";
goto p1;
}
if(y>111||y<97||x<1||x>15){
cout<<endl<<" Error!超出棋盘范围。"<<endl<<"请重新输入:";
goto p1;
}
stage[x][y-96]='x';
player=2;
}
else if(player==2){
p2:cout<<"玩家二的回合:"<<" ";
cin>>x>>y;
if(qizi[x][y-96]){
cout<<endl<<" Error!这里已经有棋子了。"<<endl<<"请重新输入:";
goto p2;
}
if(y>111||y<97||x<1||x>15){
cout<<endl<<" Error!超出棋盘范围。"<<endl<<"请重新输入:";
goto p2;
}
stage[x][y-96]='o';
qizi[x][y-96]=true;
J_end=judge(2);
player=1;
}
if(J_end!=1&&J_end!=2)
system("cls");
}
cout<<endl<<"======================================"<<endl;
if(J_end==1) cout<<"恭喜玩家一获胜";
else cout<<"恭喜玩家二获胜";
}
|
|