"C-language" board game

Source: Internet
Author: User

#include <stdio.h> #define  COL 3  #define  row 3void init_board (int arr[ Col][row]) {int i = 0;int j = 0;for  (i = 0; i <  col; i++) {for  (j = 0; j < row; j++) {arr[i][j] =  '   ';}} Void print_board (Int arr[col][row]) {int i = 0;for  (i = 0; i  < col; i++) {printf (" %c | %c | %c \n",  arr[i][0], arr[i][ 1],&NBSP;ARR[I][2]);if  (i != 2) printf ("---|---|---\ n");}} Void play_move (Int arr[col][row]) {int x, y;flag:printf ("Please enter appropriate coordinates:>"); scanf_s ("%d%d",  &x, &y);x = x - 1;y = y - 1;if  (Arr[x][y]  !=  '   ') {printf ("Incorrect input coordinates \ n");//play_move (arr); goto flag;} else{arr[x][y] =  ' x ';}} Void computer_move (Int arr[col][row]) {Int x = 0;int y = 0;for  (x = 0; x < col; x++) {for   (y = 0; y < row; y++) {if  (arr[x][y] ==  '   ') {arr[x][ y] =  ' 0 '; goto flag;}} flag:if  (x*y == 9) {printf ("Draw \ n");}} Char check_win (Int arr[col][row]) {int i = 0;for  (i = 0; i  < col; i++) {if  ((arr[i][0] == arr[i][1])  &&  (arr[i][1] == &NBSP;ARR[I][2])) {return arr[i][0];}} for  (i = 0; i < row; i++) {if  ((Arr[0][i] == arr[1][i])  &&  (Arr[1][i] == arr[2][i]) {return arr[0][i];}} if  ((arr[0][0] == arr[1][1])  &&  (arr[1][1] == arr[2][2]) {return &NBSP;ARR[0][0];} if  ((arr[0][2] == arr[1][1])  &&  (Arr[1][1] == arr[2][0]) {return  arr[1][1];} return  '   ';} Int main () {Char done;int arr[col][row];init_board (arr);p Rint_board (arr);d o{play_move (arr);d One  = check_win (arr);if  (done !=  '   ') {break;} Computer_move (arr);p Rint_board (arr);d One = check_win (arr);  while  (done ==  '   ');p Rint_board (arr);if  (done ==  ' X ') {printf ("player wins \ n") ;} else if  (done ==  ' 0 ') {printf ("Computer wins \ n");} System ("pause"); return 0;}


This article is from the "vs LV" blog, so be sure to keep this source http://survive.blog.51cto.com/10728490/1705319

"C-language" 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.