#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] &&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)