【C和C++】 井字走三关(人人对战)

#include<stdio.h>
#include<string.h>
void initialize(char a[3][3]);
void print(char a[3][3]);
int judge(char a[3][3]);
void Input1P(char a[3][3]);
void Input2P(char a[3][3]);
int main(void)
{
 char a[3][3];
 initialize(a);
 int count=1;
 print(a);
 while(judge(a)!=1&&judge(a)!=2&&count!=10){
  if(count%2==1){
   printf("It is 1P time\n");
   Input1P(a);
  }
  else{
   printf("It is 2P time\n");
   Input2P(a);
  }
  count++;
  print(a);
 }
 if(count<=10){
  switch(judge(a)){
   case 1:printf("1P is winer\n");return 0;
   case 2:printf("2P is winer\n");return 0;
  }
 } 
 printf("This is draw\n");
 return 0;
}
void initialize(char a[3][3])
{
 int i,j;
 for(i=0;i<=2;i++)
  for(j=0;j<=2;j++)
   a[i][j]=' ';
}
void print(char a[3][3])
{
 int i;
 for(i=0;i<=2;i++)
  printf("%c|%c|%c\n",a[i][0],a[i][1],a[i][2]);
}
int judge(char a[3][3])
{
 if(((a[0][0]==a[0][1]&&a[0][1]==a[0][2])&&a[0][0]=='x')||
 ((a[1][0]==a[1][1]&&a[1][1]==a[1][2])&&a[1][0]=='x')||
 ((a[2][0]==a[2][1]&&a[2][1]==a[2][2])&&a[2][0]=='x')||
 ((a[0][0]==a[1][0]&&a[1][0]==a[2][0])&&a[0][0]=='x')||
 ((a[0][1]==a[1][1]&&a[1][1]==a[2][1])&&a[0][1]=='x')||
 ((a[0][2]==a[1][2]&&a[1][2]==a[2][2])&&a[0][2]=='x')||
 ((a[0][0]==a[1][1]&&a[1][1]==a[2][2])&&a[0][0]=='x')||
 ((a[0][2]==a[1][1]&&a[1][1]==a[2][0])&&a[0][2]=='x'))
  return 1;
 else if(((a[0][0]==a[0][1]&&a[0][1]==a[0][2])&&a[0][0]=='o')||
 ((a[1][0]==a[1][1]&&a[1][1]==a[1][2])&&a[1][0]=='o')||
 ((a[2][0]==a[2][1]&&a[2][1]==a[2][2])&&a[2][0]=='o')||
 ((a[0][0]==a[1][0]&&a[1][0]==a[2][0])&&a[0][0]=='o')||
 ((a[0][1]==a[1][1]&&a[1][1]==a[2][1])&&a[0][1]=='o')||
 ((a[0][2]==a[1][2]&&a[1][2]==a[2][2])&&a[0][2]=='o')||
 ((a[0][0]==a[1][1]&&a[1][1]==a[2][2])&&a[0][0]=='o')||
 ((a[0][2]==a[1][1]&&a[1][1]==a[2][0])&&a[0][2]=='o'))
  return 2; 
 else 
  return 0;
}
void Input1P(char a[3][3])
{
 int x;
 scanf("%d",&x);
 switch(x)
 {
 case 7:a[0][0]='x';break;
 case 8:a[0][1]='x';break;
 case 9:a[0][2]='x';break;
 case 4:a[1][0]='x';break;
 case 5:a[1][1]='x';break;
 case 6:a[1][2]='x';break;
 case 1:a[2][0]='x';break;
 case 2:a[2][1]='x';break;
 case 3:a[2][2]='x';
 }
}
void Input2P(char a[3][3])
{
 int x;
 scanf("%d",&x);
 switch(x)
 {
 case 7:a[0][0]='o';break;
 case 8:a[0][1]='o';break;
 case 9:a[0][2]='o';break;
 case 4:a[1][0]='o';break;
 case 5:a[1][1]='o';break;
 case 6:a[1][2]='o';break;
 case 1:a[2][0]='o';break;
 case 2:a[2][1]='o';break;
 case 3:a[2][2]='o';
 }
}

 

posted on 2014-12-30 16:43  雪原那么远  阅读(205)  评论(0编辑  收藏  举报

导航