简单的五子棋判定游戏

注意边界问题即可,5个以上的连续不算赢

#include <iostream>
#include 
<string>
using namespace std;

int stone[20][20];

bool match(int type,int j,int i)
{
    
if (type==1)  //横列
    {
       
for (int k=1;k<5;k++)
       
{
           
if (stone[i][j]!=stone[i][j+k])
              
return false;
       }

       
if (j<16&&(stone[i][j]==stone[i][j+5]||stone[i][j]==stone[i][j-1]))
          
return false;
    }

    
else if (type==2)  //竖列
    {
        
for (int k=1;k<5;k++)
        
{
            
if (stone[i][j]!=stone[i+k][j])
               
return false;
        }

        
if (i<16&&(stone[i][j]==stone[i+5][j]||stone[i][j]==stone[i-1][j]))
            
return false;
    }

    
else if (type==3)   //斜向下列
    {
        
for (int k=1;k<5;k++)
        
{
            
if (stone[i][j]!=stone[i+k][j+k])
              
return false;
        }

        
if(i<16&&j<16&&(stone[i][j]==stone[i+5][j+5]||stone[i][j]==stone[i-1][j-1]))
            
return false;
    }

    
else if (type==4)  //斜向上列
    {
        
for (int k=1;k<5;k++)
        
{
            
if(stone[i][j]!=stone[i-k][j+k])
                
return false;
        }

        
if (i>4&&j<16&&(stone[i][j]==stone[i-5][j+5]||stone[i][j]==stone[i+1][j-1]))
           
return false;
    }

    
return true;
}

int main()
{
   
int n,wini,winj;
   
bool win;
   scanf(
"%d",&n);
   
while (n>0)
   
{
       memset(stone,
0,sizeof(stone));
       
for (int i=1;i<20;i++)
       
{
           
for (int j=1;j<20;j++)
           
{
               scanf(
"%d",&stone[i][j]);
           }

       }

       win
=false;
       
for (int j=1;j<20;j++)
       
{
         
if (win==false)
         
{
             
for (int i=1;i<20;i++)
             
{
                 
if (stone[i][j]!=0)
                 
{
                     
if (j<16&&match(1,j,i))
                     
{
                         wini
=i;winj=j;win=true;break;
                     }

                     
if (i<16&&match(2,j,i))
                     
{
                         wini
=i;winj=j;win=true;break;
                     }

                     
if (i<16&&j<16&&match(3,j,i))
                     
{
                         wini
=i;winj=j;win=true;break;
                     }

                     
if (i>4&&j<16&&match(4,j,i))
                     
{
                         wini
=i;winj=j;win=true;break;
                     }

                 }

             }

         }
   
       }

       
if (win)
           printf(
"%d\n%d %d\n",stone[wini][winj],wini,winj);
       
else
           printf(
"0\n");
       n
--;
   }

}