C Language three sub-chess games

Source: Internet
Author: User

C   Tri-chess   games//chessboard for 3x3 size, computer chess pieces  x , player  o ;//with the command window to simulate 3 sub-chess, the player and the computer rotation, first connected to 3 sub-award winning; no way to go to a draw # include<stdio.h> #include <string.h> #include <windows.h>int judge_is_moved (char arr[] [12], int place); Void print_chessboard (char arr[][12])//output checkerboard {int i = 0,  j = 0;for  (i = 0; i < 5; i++) {for  (j = 0;  j < 12; j++) {if  (arr[i][j] ==  ') printf ("\ n"); elseprintf ("%c",  ARR[I][J]);}}} Void chess_move (Char arr[][12],int place)//Player Board Lazi {switch  (place) {case 1:arr[0][2]  =  ' o ';break;case 2:arr[0][6] =  ' o ';break;case 3:arr[0][10] =  ' o '; break;case 4:arr[2][2] =  ' o ';break;case 5:arr[2][6] =  ' o '; break;case 6:arr[ 2][10] =  ' o ';break;case 7:arr[4][2] =  ' o ';break;case 8:arr[4][6] =  ' o ' ; break;case 9:arr[4][10] =  ' O '; break;} Print_chessboard (arr);} Void cp_move (char arr[][12])//Computer calculation, Lazi {//judge_is_moved (arr, i)  ==1int place=0;int  i=0;if (arr[0][2]!= '   ' &&judge_is_moved (arr, 1)  ==1) {place=1;} Else{for (i=1;i<10;i++) {if ((i<4) && (arr[0][2]!= ' o ') && (arr[0][6]!= ' o ')      && (arr[0][10]!= ' O ') && (judge_is_moved (arr, i)  ==1)) {place=i;break;} Else if (i>3 && (i<7) && (arr[2][2]!= ' o ') && (arr[2][6]!= ' o ') && (arr[2][ 10]!= ' O ') && (judge_is_moved (arr, i)  ==1)) {place=i;break;} Else if (i>6) && (arr[4][2]!= ' o ') && (arr[4][6]!= ' o ') && (arr[4][10]!= ' o ') && (Judge_is_moved (Arr, i)  ==1)) {place=i;break;} ElseIf (judge_is_moved (arr, i)  ==1) {Place=i;break;}}} switch  (place) {case 1:arr[0][2] =  ' x ';break;case 2:arr[0][6] =  ' x '; case 3:arr[0][10] =  ' x ';break;case 4:arr[2][2] =  ' x ';break;case 5:arr[2][6] =  ' x '; break;case 6:arr[2][10] =  ' x ';break;case 7:arr[4][2] =  ' x '; Break;case 8:arr [4] [6] =  ' x ';break;case 9:arr[4][10] =  ' x '; break;} Print_chessboard (arr);} Int judge_end (char arr[][12])//judgment Game over: 0: No way to go   1: Players win   2: Computer win {if  (   ( arr[0][2] ==  ' o ' &&arr[0][6] ==  ' o ' &&arr[0][10]== ' o ') | | ( arr[2][2] ==  ' o ' &&arr[2][6] ==  ' o ' &&arr[2][10]== ' o ') | | ( arr[4][2] ==  ' o ' &&arr[4][6] ==  ' o ' &&arr[4][10]== ' o ') | | ( arr[0][2] ==  ' o ' &&arr[2][6] ==  ' o ' &&arr[4][10]== ' o ') | | ( arr[0][10] ==  ' o ' &&arr[2][6] ==  ' o ' &&arr[4][2]== ' o ') | | ( arr[0][2] ==  ' o ' &&arr[2][2] ==  ' o ' &&arr[4][2] == ' O ') | | ( arr[0][6] ==  ' o ' &&arr[2][6] ==  ' o ' &&arr[4][6] == ' o ') | | ( arr[0][10] ==  ' o ' &&arr[2][10] ==  ' o ' &&arr[4][10] == ' o '))     return 1;if  (    (arr[0][2] ==  ' X ' &&arr[0][6 ] ==  ' x ' &&arr[0][10] ==  ' x ') | |   (arr[2][2] ==  ' x ' &&arr[2][6] ==  ' x ' &&arr[2][10] ==  ' x ') | |   (arr[4][2] ==  ' x ' &&arr[4][6] ==  ' x ' &&arr[4][10] ==  ' x ') | |   (arr[0][2] ==  ' x ' &&arr[2][6] ==  ' x ' &&arr[4][10] ==  ' x ')  | | ( arr[0][10] ==  ' x ' &&arr[2][6] ==  ' x ' &&arr[4][2]== ' x ') | | ( arr[0][2] ==  ' x ' &&arr[2][2] ==  ' x ' &&arr[4][2] == ' x ') | | ( arr[0][6] ==  ' x ' &&arr[2][6] ==  ' x ' &&arr[4][6] == ' X ') | | ( arr[0][10] ==  ' x ' &&arr[2][10] ==  ' x ' &&arr[4][10] == ' x '))     return 2;if  (    (arr[0][2] !=  '   ' &&arr [0] [6] !=  '   ' &&arr[0][10] !=  '   ')  && (arr[2][2] !=  '   ' &&arr[2][6] !=    ' &&arr[2][10] !=  '   ')  && ( arr[4][2] !=  '   ' &&arr[4][6] !=  '   ' &&arr[4][10] !=  '   '))     return 0;return -1;} Int judge_is_moved (Char arr[][12],int place)//Determine if Lazi position is idle {switch  (place) {Case 1:if   (arr[0][2] ==  '   ') return 1;else return 0;case 2:if  (Arr[0][6]  ==  '   ') return 1;elsereturn 0;case 3:if  (arr[0][10] ==  '   ') return 1;elsereturn 0;case 4:if  (arr[2][2] ==  '   ') return 1;elsereturn 0;case 5:if  (arr[2][6] ==  ')   ') return 1;elsereturn 0;case 6:if  (arr[2][10] ==  '   ') return 1; elsereturn 0;case 7:if  (arr[4][2] ==  '   ') return 1;elsereturn 0;case  8:if  (arr[4][6] ==  '   ') return 1;elsereturn 0;case 9:if  (arr[4][ 10] ==  '   ') return 1;elsereturn 0;default:return 0;}} Int main () {char arr[5][12]={"   |   |   ", "---|---|---" ,                  "    |   |    ","---|---|---",                   "   |   |     "} ;p rintf (" *****************  welcome to play! \ n "); while (1) {int i=0;    chararr[5][12] = { "     |   |    ", "---|---|---",                     "   |    |    ", "---|---|---",                     "   |   |     "&NBSP;};p rintf (" Do you want to start? (0: No, 1: Yes) "); Flag1:scanf ("%d ", &i); if (i==0) {printf (" Thank you for using! ") \ n "); return 0;} Else if (i!=0&&i!=1) {printf ("wrong input! Please re-select:"); goto flag1;} Else{int j = 0;print_chessboard (arr);p rintf ("The game begins! Choose the initiator or the back (0: First, 1: After) "), flag2:    scanf ("%d ",  &j);if  (j != 0  && j != 1) {printf ("wrong input! Please re-select:"); goto flag2;} While  (1) {int place = 0;if  (j == 0) {printf ("Please lazi (input 1--9):"); FLAG3:SCANF ("%d",  &place);if  (place<1 | |  place>9 | |  judge_is_moved (arr,place) ==0) {printf ("Lazi location is illegal! Re-enter:"); goto flag3;} Else{int end = -1;chess_move (Arr, place); End = judge_end (arr);if  (end  ==0) {printf ("Game Over!") There's no way to go! Draw! "); break;} if  (end == 1) {printf ("Game Over!") You win! "); break;} if  (end == 2) {printf ("Game Over!") You've lost! "); break;} printf ("-----Wait for computer lazi \ n"); Sleep (Cp_move); End=-1;end = judge_end (arr);if  (end ==0) {printf ("Game over! There's no way to go! Draw! "); break;} if  (end == 1) {printf ("Game Over!") You win! "); break;} if  (end == 2) {printf ("Game Over!") You've lost! "); break;}} if  (j == 1) {int end = -1;printf ("-----Waiting for computer lazi \ n"); Sleep (Cp_move); End = judge_end (arr);if  (end == 0) {printf ("Game over! There's no way to go! Draw! "); break;} if  (end == 1) {printf ("Game knotBeam! You win! "); break;} if  (end == 2) {printf ("Game Over!") You've lost! "); break;} printf ("Please lazi (input 1--9):"); FLAG4:SCANF ("%d",  &place);if  (place<1 | |  place>9 | |  judge_is_moved (arr,place)  == 0) {printf ("Lazi location is illegal! Re-enter:"); goto flag4;} Else{chess_move (Arr, place); End = judge_end (arr);if  (end == 0) {printf ("Game over! There's no way to go! Draw! "); break;} if  (end == 1) {printf ("Game Over!") You win! "); break;} if  (end == 2) {printf ("Game Over!") You've lost! "); break;}}}}    }return 0;}


C Language three sub-chess 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.