function encapsulation (use of arrays, through board games)

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>int main () { void  empty (char arr[][3]) ;  void   print (char arr[][3]);  void   player (char[][3]);  Void   conplay (Char[][3]);  char  check (char arr[][3]);   char   arr[3][3]; char  key; printf ("game start");  printf ("\ n");  empty (arr);  print (arr);  do {  player (arr);   key=check (arr);   if (key!= '   ')    break;  conplay (arr);   key=check (arr);   print (arr);  } while (key== '   '),  if (key== ' X ')  {  print (arr);   printf ("player wins \ n");  }  else  {  print (arr);   printf ("Computer wins \ n");    } return  0;  }void  empty (Char arr[][3]) { int i,j; for (i=0;i<3;i++)   for (j=0;j<3;j++)    arr[i][j]= '   ';} Void   print (Char  arr[][3]) { int i; for (i=0;i<3;i++)  {   printf (" %c | %c | %c ", arr[i][0],arr[i][1],arr[i][2]);   printf ( "\ n");   if (i!=2)    printf ("---|---|---");   printf ("\ n"); }}void   player (Char  arr[][3]) { int i,j; printf ("Please enter the position you want to place:"),  scanf ("%d%d", &i , &j);  if (i>3| | j>3)  {  printf ("Input error, re-enter \ n");   player (arr); }      if (arr[i-1][j-1]!= '   ')     {     printf ("This position has been populated, please re-enter");      player (arr);    }    else        arr[i-1][j-1]= ' X ';} Void   conplay (char  arr[][3]) {    int i,j;     for (i=0;i<3;i++)     {     for (j=0;j<3;j++)      {       if (arr[i][j]== '   ')        arr[i][j] = ' O ';      else         continue;       return 0;     }    }} Char  check (Char arr[][3]) {    int i;    for (i=0;i <3;i++)     {     if (arr[i][0]==arr[i][1]&&arr[i][0]== ARR[I][2])       return arr[i][1];     else   if (Arr[1][i]==arr[2][i]&&arr[1][i]==arr[0][i])       return  Arr[1][i];     else  if (Arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2])       return  (Arr[0][0]);      else  if (Arr[2][0]==arr[1][1] &AMP;&AMP;ARR[1][1]==ARR[0][2])       return  (arr[2][0]);     }    return  '   ';}

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1679293

function encapsulation (use of arrays, through board games)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.