Using C language to realize "retarded" board game

Source: Internet
Author: User

#define  _CRT_SECURE_NO_WARNINGS#include <stdio.h> #include  <stdlib.h>/* function declaration */void  init_arr (Int arr[ ][3]);   //array initialization void print_checkerboard (int arr[ ][3 ]);  //print Checkerboard function int player (int arr[ ][3], int line, int list);  // Player Chess Void robot (Int *arr),  //computer chess Int judge (int arr[ ][3]);   //judgment win or lose int  main () {int arr[3][3];int x=0, y=0;int result = 0;int result =  0;printf ("welcome to play the checkboard game!\n"); Init_Arr (ARR); Print_checkerboard (ARR);while  (1) {printf ("Enter x,y coordinates for your move:") ; scanf ("%d",  &x); scanf ("%d",  &y); Result= player (arr, x, y);if  (Result) {Robot (&arr[0][0]); Print_checkerboard (arr); Result = judge (arr);if  (result == 1) {printf ("The player  win!\n\n "); break;} if  (result == 2) {printf ("the robot win!\n\n"); if  (result == 3) {printf ("peace!\n\n"); break;}} else printf ("can not next chess here!\n");} System ("pause"); return 0;} Void print_checkerboard (int arr[ ][3])  //print checkerboard function {int size = 3;for  (int  i = 0; i < size; i++) {printf ("\t\t %c | %c | % C \n ", arr[i][0],arr[i][1],arr[i][2]); if (i<size-1) printf (" \t\t---|---|---\ n ");} Void init_arr (int arr[ ][3])   //array initialization {int row = 3;int list =  3;for  (int i = 0; i < row; i++) {for  (int j =  0; j < list; j++) {arr[i][j] =  '   ';}} Int player (int arr[ ][3],int line,int list)  //player chess {if  (line <  4 && list < 4) {if  (arr[line - 1][list - 1] !=  '   ') {return 0; // The position of the chess is not correct}else{arr[line - 1][list - 1] =  ' X '; return 1; //chess position is correct}}else{ return 0; //chess position is incorrect}}void robot (int *arr)  //computer Chess {int size = 9;for   (int i = 0; i < size; i++)  {if  (arr[i] == '   ') { arr[i] =  ' 0 '; break;}} Int judge (int arr[ ][3])   //judgment win or lose {int size = 3;int flag =  0;for  (int i = 0; i < 3; i++) {if  ((arr[i][0] == &NBSP;ARR[I][1])  &&  (arr[i][1] == arr[i][2])  &&  (arr[i][0]  ==  ' X '))  | | ((Arr[0][i] == arr[1][i])  &&  (Arr[1][i] == arr[2][i])  &&   (arr[0][i] ==  ' X ')) return 1;  //player wins}if  ((arr[0][0]&NBSP;==&NBSP;ARR[1][1])  &&  (arr[0][0] == arr[2][2])  &&  (arr[0 ][0] ==  ' X '))  | | ((Arr[0][2] == arr[1][1])  &&  (arr[1][1] == arr[2][0])  &&   (arr[0][2] ==  ' X ')) {return 1;  //player wins}for  (int i = 0;  i < 3; i++) {if  ((arr[i][0] == arr[i][1)  &&  (arr[i][ 1]&NBSP;==&NBSP;ARR[I][2])  &&  (arr[i][0] ==  ' 0 '))  | | ((Arr[0][i] == arr[1][i])  &&  (Arr[1][i] == arr[2][i])  &&   (arr[0][i] ==  ' 0 ')) return 2;  //computer Victory}if  ((arr[0][0] == arr[1][ 1])  &&  (arr[0][0] == arr[2][2])  &&  (arr[0][0] ==  ' 0 '))  | | ((Arr[0][2] == arr[1][1])  &&  (arr[1][1] == arr[2][0])  &&   (arr[0] [2] ==  ' 0 ')) {return 2;  //computer victory}for  (int i = 0; i <  size; i++)//Determine if the board is full {for  (int j = 0; j < size; j++) {if   (arr[i][j] !=  '   ') flag++;}} if  (flag == 9) {return 3;//Draw}return 0;//return to continue judgment}


Using C language to realize "retarded" board game

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.