c language small project three piece chess
Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
preface
Tip: C language learning, record your learning process, thank you for your criticism and correction
1, Overall thinking
First of all, my goal is to design a three piece chess game, which includes three basic functions: chessboard printing, man-machine combat and result judgment. The data is carried by character array as a whole.
Then split the target to achieve:
Chessboard printing, using cyclic output Jiugong grid to output Jiugong grid
In man-machine battle, the player inputs coordinates, the program judges whether it is legal and occupied, and then assigns values.
The computer uses srand((unsigned int)time(NULL)); Generate random coordinates, where modulus is used to limit the range of random numbers x = rand()% row;, Assign value after reasonable judgment.
Result judgment: the computer and players make a judgment every time. The essence is to judge the contents of the array, horizontal, vertical and diagonal. There are four judgment results: player win, computer win, draw and continue. Return specific symbols according to different situations for output.
2, Concrete implementation
Project creation, using multiple files. When the program code is long, the idea will be clearer when writing with multiple files, which is convenient for later maintenance.
Usually I divide it into: test file test c. Function declaration game h. The function implements game c.
1. Overall code and analysis
The first is the writing of the test file, which is the overall architecture of the function. It is the code directly on the screen and in direct contact with the user.
test.c
Import header file
#include"game.h"
Menu navigation void menu
void menu() { printf("**************************\n"); printf("****1. play 0. exit****\n"); printf("**************************\n"); }
Game implementation void game()
void game() { char ret = 0; //Array - stores the chessboard information of players and computers char board [row][col] = { 0 }; //Initialize chessboard InitBoard(board, row, col); //Print chessboard DisplayBoard(board, row, col); //play chess while (1) { //Players play chess PlayerMove(board, row, col); DisplayBoard(board, row, col); ret=IsWin(board, row, col); if (ret != 'C') { break; } //Computer chess ComputerMove(board, row, col); DisplayBoard(board, row, col); ret=IsWin(board, row, col); if (ret != 'C') { break; } } if (ret == '*') { printf("Player wins\n"); } if (ret == '@') { printf("Computer win\n"); } if(ret=="Q") { printf("it ends in a draw\n"); } }
Overall test void test()
Execute the functions in the menu according to the user's selection
void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select:>"); scanf("%d", &input); switch (input) { case 1: printf("Gobang game\n"); game(); break; case 0: printf("Exit the game\n"); break; default: printf("Selection error, please re select\n"); break; } } while (input); }
Overall test (main function) int main()
int main() { test(); return 0; }
Next, write the function declaration:
game.h
Import header file
#include<stdio.h> #include<stdlib.h> #include<time.h>
Function declaration
void InitBoard(char board[row][col], int r, int c);//Chessboard creation void DisplayBoard(char board[row][col], int r, int c);//Chessboard printing void PlayerMove(char board[row][col], int r,int c);//Player turn void ComputerMove(char board[row][col], int r, int c);//Computer round
//Tell four clock game status //Player wins - "*" computer wins - "@" //Draw "Q" continue "C" char IsWin(char board[row][col], int r, int c);//Victory and defeat judgment
Function implementation
game.c
Header file import
#include"game.h"
Chessboard creation function
void InitBoard(char board[row][col], int r, int c) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } }
Chessboard printing function
void DisplayBoard(char board[row][col], int r, int c) { //It's too hard to write five lines of printf code directly int i = 0; for (i = 0; i < row; i++) { //1. Print one line of data int j = 0; for (j = 0; j < col; j++) { printf(" %c ",board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); //2. Print split lines if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } }
Player turn
Get two coordinates and enter the while loop. If the array under the coordinates is empty, play chess and jump out of the loop. If not, continue.
void PlayerMove(char board[row][col], int r, int c) { int x = 0; int y = 0; printf("Player turn:>\n"); while (1) { printf("Please enter the following coordinates:>\n"); scanf("%d%d", &x, &y); //Judge the legitimacy of coordinates if (x >= 1 && x <= row && y >= 1 && y <= col) //Think from the perspective of users and follow the principle of user friendliness { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("This coordinate is occupied\n"); } } else { printf("Illegal coordinates, please re select!\n"); } } }
Computer round
Declare the srnad() function, use time (NULL) as the parameter, and strongly convert it to unsigned int.
Randomly generate two numbers and use remainder to make the random number fall into the limited range.
While loop to judge whether the generated coordinates have been occupied. If not, play chess and jump out of the loop.
void ComputerMove(char board[row][col], int r, int c) { int x = 0; int y = 0; printf("Computer round:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '@'; break; } } }
Judge whether the chessboard is full 0 - not full 1 - full
int IsFull(char board[row][col], int r, int c) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] ==' ') { return 0;//Not full } } } return 1; }
Victory and defeat judgment
Define the IsWin() function and return four statuses. 1. Player wins. 2. Computer wins. 3. Draw. 4. Continue
If not, the other three results will be given.
Essentially determine the contents of the array. Horizontal and vertical oblique
char IsWin(char board[row][col], int r, int c) { int i = 0; //Three horizontal lines of judgment for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ') { return board[i][1]; } } //Judge vertical three columns for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i]; } } //Judge diagonal if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') return board[1][1]; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') return board[1][1]; //Judge whether there is a draw if (1 == IsFull(board, row, col)) { return 'Q'; } return 'C'; }
2. Knowledge Tips
The code is as follows (example):
1. The srand ((unsigned long) time (null)) function is usually used together with rand().
In C language, it is located in < stdlib h> Header file.
Prototype: void srand(unsigned int seed)
Prototype: void rand(void)
Seed is equivalent to a seed. The srand function sets a random starting point according to the seed, and the rand function returns a random number [seed, rand_max] according to the starting point.
The default srand (1) initializes the rand () value. In order to make each program call produce different values, system time is often used for initialization, that is, time (NULL).
2.#define defines the size of the array to facilitate code reuse.
3.#define _CRT_SECURE_NO_WARNINGS 1 solves the unsafe problem.
Temporary scheme: add directly
Permanent solution: find NEWC + + file through the installation path Cpp file, open with notpad + +, add.