C Language "black box" can't bear to look straight at the three sub-chess implementation

Source: Internet
Author: User

Today send an interface ugly to explode, extremely low of the three sub-chess program


Similarly, the Win32 has no graphical interface


Enter coordinates to interact,

X for the player, 0 for the computer (the rand () function randomly generates a legal coordinate lazi), the first to even three sons victory. The board is not tied to a three-child draw.


The program is relatively simple to direct the Origin code. Three blocks, game.h represents the program required header file, macro definition, function declaration, TEST.C represents the program game logic, GAME.C represents the program definition function implementation.


The code is not optimized, please forgive me.


Game.h:

/* This header file contains test.c required header file and related function declaration */#ifndef __game_h__#define __game_h__#include <stdio.h> #include <stdlib.h># include<time.h>//defines the number of rows and columns in a chessboard # # # rows 3#define COLS 3//print menu void menu (void);//Initialize checkerboard void init (char board[][cols ], int rows);//print checkerboard void print (char board[][cols], int rows);//Game link void play (char board[][cols], int rows);// Check whether the board is full to determine if draw int checkfull (char board[][cols], int rows),//check if someone wins int checkwin (char board[][cols], int rows);// Players play void player (char board[][cols], int rows),//Computer play void computer (char board[][cols], int rows);//Check whether someone wins or a draw int check ( Char board[][cols], int rows); #endif



TEST.C:

/* Three sub-chess game logic */#include "game.h" int main (void) {int input;//store user input option char board[rows][cols];//store checkerboard menu ();//Print menu scanf ("%d ", &input);//Accept user input while (input) {init (board, rows);//Initialize Checkerboard print (board, rows);//Print Board play (board, rows);//Play Menu ();//Print menu scanf ("%d", &input);//Accept user Input}//game over printf ("\nbye!\n");//program end return 0;}

GAME.C:


/* This source code contains the TEST.C function implementation */#include "game.h"//print menu void (void) {printf ("**********************\n");p rintf ("* * * * * 1. Play *****\n ");p rintf (" * * * * 0.exit *****\n ");p rintf (" **********************\n ");} Initialize the checkerboard void init (char board[][cols], int rows) {int R;int c;for (r = 0; r < rows; r++) for (c = 0; c < COLS; C + +) boa Rd[r][c] = ';//The game does not start when the position of the pawn is initialized to a space}//print the Checkerboard void print (char board[][cols], int rows) {int R;int c;for (r = 0; r < rows; r + +) {for (c = 0; c < COLS; C + +) {printf ("%c", Board[r][c]), if (C < 2)//Print two times "|" printf ("|");}   printf ("\ n"); if (r = = 2)//print the last line of the chessboard the rules are different printf ("|   | elseprintf ("___|___|___");p rintf ("\ n");}} Check if someone wins or a draw int check (char board[][cols], int rows) {int ret;//accepts whether the checkerboard is full return value int flag;//accept wins return value flag = Checkwin (board, rows); <span style= "White-space:pre" ></span>//check if anyone wins if (flag = = 1)//flag 1 player wins {printf ("You win!\n"); return 1;} else if (flag = = 0) <span style= "White-space:pre" ></span>//flag for 0 pc wins {printf ("hehe!\n"); return 1;} RET = ChecKfull (board, rows); <span style= "White-space:pre" ></span>//if no one wins before, here by checking if the board is full determine if draw if (ret = = 1)// RET is 1 The chessboard full draw {printf ("Draw!\n"); return 1;} Return 0;//returns 1 The end of this bureau, return 0 then this bureau is not finished! }//determines whether the board is full by checking if draw int checkfull (char board[][cols], int rows) {int R;int c;for (r = 0; r < rows; r++) for (c = 0; C &lt ; COLS; C + +) if (' = = Board[r][c]) return 0;//returns 0 for the board is not full return 1;//return 1 means the board is full}//check if someone wins int checkwin (char board[][cols], int rows {int R;int c;for (r = 0; r < rows; r++) <span style= "White-space:pre" ></span>//Judge 3 rows if someone wins {if (Board[r] [0] = = board[r][1]) && (board[r][1] = = board[r][2]) && (board[r][2] = = ' x ') <span style= "White-space:p Re "></span>//x for player Lazi return 1;//return 1 player wins if ((board[r][0] = = board[r][1]) && (board[r][1] = = board[r][2]) &  amp;& (board[r][2] = = ' 0 ')) <span style= "White-space:pre" ></span>//0 for computer Lazi return 0;//back 0 pc wins}for (c = 0; c < COLS; C + +) <span style= "White-space:pre" ></span>//judging 3 columnsIf someone wins {if ((board[0][c] = = Board[1][c]) && (board[1][c] = = Board[2][c]) && (board[2][c] = = ' x ')) <spa n style= "White-space:pre" ></span>//x for player Lazi return 1;//returns 1 player wins if ((board[0][c] = = Board[1][c]) && (board [1] [c] = = Board[2][c]) && (board[2][c] = = ' 0 ')) <span style= "White-space:pre" ></span>//0 for PC Lazi return 0 ;//Return 0 computer wins}if ((board[0][0] = = board[1][1]) <span style= "White-space:pre" ></span>//below to determine if a diagonal case is won & & (board[1][1] = = board[2][2]) && (board[2][2] = = ' x ')) return 1;if ((board[0][0] = = board[1][1]) && (bo ARD[1][1] = = board[2][2]) && (board[2][2] = = ' 0 ')) return 0;if ((board[0][2] = = board[1][1]) && (board[1][1 ] = = Board[2][0]) && (board[2][0] = = ' x ')) return 1;if ((board[0][2] = = board[1][1]) && (board[1][1] = = Boar D[2][0]) && (board[2][0] = = ' 0 ')) return 0; The player plays void player (char board[][cols], int rows) {int x;int y;while (1) {printf ("Enter coordinates:"), scanf ("%d%d", &x,&y); if (board[x-1][y-1] = = ") <span style=" White-space:pre "></span>//player will treat array position 0.0 as 1.1 so here X y points Don't subtract 1{board[x-1][y-1] = ' x '; break;} else{printf ("Position not lawfully!\n");}} Exit While Loop print (board, rows);//Print update after board}//computer to play void computer (char board[][cols], int rows) {int x;int y;srand ((unsigned) Time (NULL)); while (1) {x = rand ()% 3;y = rand ()% 3;if (board[x][y] = = ") {board[x][y] = ' 0 '; break;}} Exit loop printf ("Computer play!\n");p rint (board, rows);//Print updated checkerboard}//game link void play (char board[][cols], int rows) {while (1) {player ( board, rows);//Players play if (check (board, rows))//Check if someone wins or draw Break;computer (board, rows);//Computer Play if (check (board, rows))// Check if someone wins or a draw break;} Exit While Loop}



C Language "black box" can't bear to look straight at the three sub-chess implementation

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.