五子棋游戏
#include <iostream> #include <iomanip> int h=16; int l=16; bool is_black=true; int all_list[16][16]; bool black_list[16][16]; bool while_list[16][16]; int x; int y; using namespace std; bool black_iswin2(int i,int ii){ if(black_list[i][ii]==true&&black_list[i][ii]==black_list[i][ii+1]&&black_list[i][ii+1]==black_list[i][ii+2]&&black_list[i][ii+2]==black_list[i][ii+3]&&black_list[i][ii+3]==black_list[i][ii+4] ||black_list[i][ii]==true&&black_list[i][ii]==black_list[i+1][ii]&&black_list[i+1][ii]==black_list[i+2][ii]&&black_list[i+2][ii]==black_list[i+3][ii]&&black_list[i+3][ii]==black_list[i+4][ii] ||black_list[i][ii]==true&&black_list[i][ii]==black_list[i+1][ii+1]&&black_list[i+1][ii+1]==black_list[i+2][ii+2]&&black_list[i+2][ii+2]==black_list[i+3][ii+3]&&black_list[i+3][ii+3]==black_list[i+4][ii+4] ||black_list[i+4][ii]==true&&black_list[i+4][ii]==black_list[i+3][ii+1]&&black_list[i+3][ii+1]==black_list[i+2][ii+2]&&black_list[i+2][ii+2]==black_list[i+1][ii+3]&&black_list[i+1][ii+3]==black_list[i][ii+4] ){ return true; }else{ return false; } } bool while_iswin2(int i,int ii){ if(while_list[i][ii]==true&&while_list[i][ii]==while_list[i][ii+1]&&while_list[i][ii+1]==while_list[i][ii+2]&&while_list[i][ii+2]==while_list[i][ii+3]&&while_list[i][ii+3]==while_list[i][ii+4] ||while_list[i][ii]==true&&while_list[i][ii]==while_list[i+1][ii]&&while_list[i+1][ii]==while_list[i+2][ii]&&while_list[i+2][ii]==while_list[i+3][ii]&&while_list[i+3][ii]==while_list[i+4][ii] ||while_list[i][ii]==true&&while_list[i][ii]==while_list[i+1][ii+1]&&while_list[i+1][ii+1]==while_list[i+2][ii+2]&&while_list[i+2][ii+2]==while_list[i+3][ii+3]&&while_list[i+3][ii+3]==while_list[i+4][ii+4] ||while_list[i+4][ii]==true&&while_list[i+4][ii]==while_list[i+3][ii+1]&&while_list[i+3][ii+1]==while_list[i+2][ii+2]&&while_list[i+2][ii+2]==while_list[i+1][ii+3]&&while_list[i+1][ii+3]==while_list[i][ii+4] ){ return true; }else{ return false; } } void f5(){ cout<<" "; for(int i=0;i<=h;i++){ cout<<setw(2)<<i; } cout<<endl; for(int i=0;i<h;i++){ cout<<setw(3)<<i+1; for(int ii=0;ii<l;ii++){ if(all_list[i][ii]==0){ cout<<setw(2)<<"."; }else if(all_list[i][ii]==1){ cout<<setw(2)<<"黑"; }else if(all_list[i][ii]==2){ cout<<setw(2)<<"白"; } } cout<<endl; } } bool black_iswin(){ for(int i=0;i<h-4;i++){ for(int ii=0;ii<l-4;ii++){ if(black_iswin2(i,ii)){ return true; } } } return false; } bool while_iswin(){ for(int i=0;i<h-4;i++){ for(int ii=0;ii<l-4;ii++){ if(while_iswin2(i,ii)){ return true; } } } return false; } int main(int argc, char** argv) { while(1){ system("cls"); f5(); if(is_black){ cout<<"黑子落棋"<<endl; cin>>x>>y; if(all_list[x-1][y-1]!=0){ continue; } all_list[x-1][y-1]=1; black_list[x-1][y-1]=true; if(black_iswin()){ cout<<"黑子胜利"; return 0; } is_black=false; }else{ cout<<"白子落棋"<<endl; cin>>x>>y; if(all_list[x-1][y-1]!=0){ continue; } all_list[x-1][y-1]=2; while_list[x-1][y-1]=true; if(while_iswin()){ cout<<"白子胜利"; return 0; } is_black=true; } } return 0; }