# Three character chess and its key points

When a programmer learning programming can understand conventional code, he needs to achieve shape similarity or even shape similarity through continuous imitation and summary

# preface

In the process of programming, in addition to the common knowledge points in textbooks, there are many strange and novel keywords or knowledge points with very interesting functions. This paper will make a simple arrangement and apply it to the production of simple game three character chess.

Tip: the following is the main content of this article. The following cases can be used for reference

# 1, Circulation

## 1.if else

Selection is a very common structure for programming specific functions

```if(Conditional expression)
{
Conditional execution statement
}
else//No execution is required and can be omitted
{
Execute statement when condition is not satisfied
}
```

In addition to single branch selection, multi branch selection can also be realized through else if

```if(Conditional expression)
{
Conditional execution statement
}
else if(Conditional expression)
{
Conditional execution statement
}
....//Multi branch condition judgment
...
else
{
None of the above conditions are met to execute the statement
}
```

## 2.for

Powerful, if and while can be realized

```for(Assignment expression; Judgment; Statement after one loop)
{
Circulatory body;
}
```

## 3.while

```while(Judgment statement)//If the condition is not true, jump out or terminate the loop
{
Conditions are established and implemented;
}
```

# 2, Functions

## 1. Function classification

### 1. Library function

Commonly used and widely applicable functions, that is, in cooperative programming, it is to reduce the different creation of the same function.
The library functions are diverse and widely used. In addition to stdio.h, there are many other functions, such as stdlib mentioned below.
Here are some library function learning tools: www.cplusplus.com
MSDN(Microsoft Developer Network)
www.cplusplus.com
http://en.cppreference.com (English version)
http://en.cppreference.com (Chinese version)

### 2. User defined function

DIY function, which can be created according to the actual situation
The code is as follows (example):

```//get_ Design of Max function
(Return type)     //Formal parameters
int get_max(int x, int y)
//Function name
{
return (x>y)?(x):(y);//Statement item
}
```

You can also have a return type of empty type to directly execute the requirements in the function

## 2. Function parameters

The formal parameter is the variable in parentheses of the function name when the function is created. It is a temporary copy of the actual parameter. The actual parameter is true, and it is the parameter passed to the function.
Any modification of a formal parameter within a function does not affect the argument

```int get_max(int x, int y)//Formal parameters
{
return (x>y)?(x):(y);
}
int main()
{
int num1 = 10;
int num2 = 20;
int max = get_max(num1, num2);//Actual parameters
printf("max = %d\n", max);
return 0;
}
```

## 3. Function call

### 1. Call by value

Formal parameters and arguments occupy different spaces, and the modification of formal parameters will not affect the arguments.

### 2. Address calling

Transfer the memory address to the function to practice the establishment of functions and variables. You can operate the variables directly through the address

## 4. Nested function calls and chained access

Functions can be called nested, but not defined nested

```#include <stdio.h>
void new_line()
{
printf("hehe\n");
}
void three_line()
{
int i = 0;
for(i=0; i<3; i++)
{
new_line();
}
}
int main()
{
three_line();
return 0;
}
```

### 1. Chain access

The return value of one function as the parameter of another function is the basic application of a function

```#include <stdio.h>
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
//What's the result?
//Note: the return value of the printf function is the number of characters printed on the screen
return 0;
}
```

The return value of printf is the number of characters printed on the screen

## 5. Function declaration and definition

Generally, before using a function, it should be declared before use, and it is generally placed in the header file
tset.h

```#ifndef __TEST_H__
#define __TEST_H__
#include <stdio.h>
//Declaration of function
int Add(int x, int y);
```

Declaring the library function in the header file can say that the library function is included in the header file for subsequent use
test.c

```#include "test.h" / / at present, it can be understood that the built-in library function uses < >, and the self created library function uses ""
//Implementation of Add function
int Add(int x, int y)
{
return x+y;
}
```

## 6. Recursion

By calling your own method to achieve the purpose, you can simplify the problem and shorten the code. It is very helpful after mastering it. However, recursion is unstable, so you need to consider the actual situation and add limiting conditions

```int factorial(int n)
{
if(n <= 1)
return 1;
else
return n* factorial(n-1);
}
```

Dolls, smaller

# 3, Array

## 1. One dimensional array

Array creation

```//Code 1
int      arr1[10];//Number of elements
//Array type / / array name
//Code 2
int count = 10;
int arr2[count];//Error creating. Constant expression is required in []. Variable length array is c99 introduced, but it is less applicable
//Code 3
char arr3[10];
float arr4[1];
double arr5[20];
```

initialization

```int arr1[10] = {1,2,3};//Incomplete initialization, the rest are initialized to 0 by default
int arr2[] = {1,2,3,4};//Open up space according to initialization content
int arr3[5] = {1，2，3，4，5}；
char arr4[3] = {'a',98, 'c'};
char arr5[] = {'a','b','c'};
char arr6[] = "abcdef";
```

### use

[] subscript reference operator
Array subscripts start at 0 by default

```#include <stdio.h>
int main()
{
int arr[10] = {0};//Incomplete initialization of array
//Count the number of elements in the array
int sz = sizeof(arr)/sizeof(arr[0]);
//Assign a value to the contents of the array. The array is accessed by subscript, which starts from 0. So:
int i = 0;//Subscript
for(i=0; i<sz; i++)//The subscript starts from 0. You need to think clearly about the judgment conditions
{
arr[i] = i;
}
//Output the contents of the array
for(i=0; i<sz; ++i)
{
printf("%d ", arr[i]);
}
return 0;
```

### storage

The array name is actually the address of an element of the array, and the array address is continuous in memory, which also facilitates our operation

```#include <stdio.h>
int main()
{
int arr[10] = {0};
int i = 0;
for(i=0; i<sizeof(arr)/sizeof(arr[0]); ++i)
{
printf("&arr[%d] = %p\n", i, &arr[i]);
}
return 0;
}
```

And from low address to high address

## 2. Two dimensional array

### Create and initialize

```int arr[3][4] = {1,2,3,4};
int arr[3][4] = {{1,2},{4,5}};
//Initialization in one line
int arr[][4] = {{2,3},{4,5}};//Rows can be omitted, but columns cannot be omitted
```

### use

It starts from 0 through subscript, lines and columns. It's easy to be confused at the beginning of learning

```#include <stdio.h>
int main()
{
int arr[3][4] = {0};
int i = 0;
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
arr[i][j] = i*4+j;
}
}
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
printf("%d ", arr[i][j]);
}
}
return 0;
```

Because there is no \ n, the two dimensions we want will not appear under standard c, so we need to design line feed

### Storage is the same as one-dimensional array

The address is also continuous from low to high

## Cross the border

When defining, the number in [] is the number. The subsequent use is the subscript reference operator. Access the subscript to obtain the element. The subscript starts from 0. If the subscript exceeds the defined number, it will cause out of bounds

```int arr[10]
int ret = arr[10]//Cross the border
```

## As a function parameter

It refers to the address transfer of the first element of the array, not the whole array. Some operations on the array should be completed outside the function

#### Bubble sorting

Talk about two adjacent array elements for one-to-one comparison to realize array sorting

```//Ascending arrangement
void bubble_sort(int arr[], int sz)//Parameter number of array elements received
{
int i = 0;
for(i=0; i<sz-1; i++)//Number of cycles
{
int j = 0;
for(j=0; j<sz-i-1; j++)//Sorting times
{
if(arr[j] > arr[j+1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main()
{
int arr[] = {3,1,7,5,8,9,0,2,4,6};
int sz = sizeof(arr)/sizeof(arr[0]);
bubble_sort(arr, sz);
for(i=0; i<sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
```
```It mainly expresses the use of array functions
```

The designer writes the address in this form in order to be easy for novices to understand. When learning, the pointer is after the array.

# Interesting knowledge points

## Creation of random numbers

You need to create a random generation table first

```          //Random number type, take timestamp as an example
srand(unsigned int time(NULL))
```

After the above creation, only rand () is needed to generate random numbers.

## System language

System is used to execute system commands
shutdown-s-t 60 (- s shutdown - t execute in s seconds) - a stop

```Use with caution, use with caution, use with caution
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char input[20] = { 0 };
system("shutdown -s -t 60");
again:
printf("Your computer will shut down in 60 seconds. Enter "I am color force" to terminate the program\n");
scanf("%s", input);
if (strcmp(input, "I'm a pornographer") == 0)
{
system("shutdown -a");
}
else
{
goto again;
}
return 0;
}
```

## string comparison

string.h
strcmp (array 1, array 2)
If the return value is less than 0, str1 is less than str2.
If the return value is greater than 0, str1 is greater than str2.
If the return value is equal to 0, it means that str1 is equal to str2.

# tic-tac-toe

It is more a comprehensive summary of the learning content at this stage than a practical game

## outline

ROW COL is for the convenience of changing the game

### The main function is essential

```int main()
{
test();//Start interface
return 0;
}
```

### Start interface printing

```void startgame()
{
printf("*******************\n");
printf("****  1.start *****\n");
printf("****  2.off   *****\n");
printf("*******************\n");
}
```

### Start interface

```void test()
{
int i = 0;
do
{
startgame();//Interface
scanf("%d", &i);
switch (i)
{
case 1:
game();
break;Enter the play
case 2:
printf("game over\n");
return;
default:
printf("Illegal input, please reselect\n");
break;
}
} while (i);//The game chooses first and then judges
}

```

### game

```void game()
{
srand((unsigned int)time(NULL));//The ability to generate random numbers just mentioned is limited. At present, we can only let the computer randomly.
char board[ROW][COL] = { 0 };
//Initialize all spaces
initboard(board, ROW, COL);
//Print chessboard
printboard(board, ROW, COL);

char ret = 0;Judgment mark
while (1)
{
playergame(board, ROW, COL);//Players play chess
printboard(board, ROW, COL);
ret = win(board, ROW, COL);//Judge victory
if (ret != 'C')
{
break;
}
computer_game(board, ROW, COL);//Computer victory
printboard(board, ROW, COL);
ret = win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("Player wins\n");
}
else if (ret == '#')
{
printf("Computer win\n");
}
else
{
printf("it ends in a draw\n");
}
}

```

### Game content

Here are the components of the function, which are placed in the header file to create a pile of functions for easy use

#### The chessboard is initialized to a space

```void initboard(char board[ROW][COL], int row,int col)
{
int i;
int j;
for (i = 0; i < row; i++)//define defines the indicator constant, which will be mentioned later, for the convenience of changing the chessboard
{
for (j = 0; j < row; j++)
{
board[i][j] = ' ';
}
}
}
```

#### Chessboard printing

```void printboard(char board[ROW][COL], int row, int col)
{
int i;
int j;
int x;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)//Subscript access
{
printf("|");
}
}
printf("\n");

if (i < row - 1)//Subscript access
{
for (x = 0; x < col; x++)
{
printf("---");

if (x < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
```

#### Players play chess

```void playergame(char board[ROW][COL], int row, int col)
{
printf("Players play chess");
int x;
int y;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';//For players to play chess*
break;//Play in the empty and no chess place
}
else
{
printf("Wrong input, please re-enter\n");
}
}
else
{
printf("Illegal input, please match the chessboard\n");
}
}
}
```

#### Computer chess

```void computer_game(char board[ROW][COL], int row, int col)
{
printf("Computer chess\n");
int x;
int y;

while (1)
{
x = rand() % row;//Subscript access, chessboard restrictions
y = rand() % col;
if (board[x][y] ==' ')
{
board[x][y] = '#';
break;
}
}
}
```

#### Chessboard full

```int full(char board[ROW][COL], int row, int col)
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] != ' ')
{
return 0;
}
}
}
return 1;//When the chessboard is full, return 1 for victory judgment

}
```

#### Victory judgment

```char win(char board[ROW][COL], int row, int col)
{
int i;
for (i = 0; i < row; i++)//The writing method here can only be used for three character chess. It can change the size of the chessboard. The author's ability is limited. I will supplement the blog in the future
{
if (board[i][0] == board[i][1] &&board[i][0]==board[i][2]&&board[i][0]!=' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}

if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (1 == full(board, ROW, COL))
{
return 'Q';//The characters should be enclosed in single quotation marks. It's been a long time since I edited the cards here
}
return 'C';
}
```

## Game integration

### game.h

```#pragma once / / given by the compiler
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3

void initboard(char board[ROW][COL], int row, int col);
void printboard(char board[ROW][COL], int row, int col);
void playergame(char board[ROW][COL], int row, int col);
void computer_game(char board[ROW][COL], int row, int col);
int full(char board[ROW][COL], int row, int col);
char win(char board[ROW][COL], int row, int col);//To add;;;;;;;;;
```

### tset.c

```#include "game.h"

void startgame()
{
printf("*******************\n");
printf("****  1.start *****\n");
printf("****  2.off   *****\n");
printf("*******************\n");
}

void game()
{
srand((unsigned int)time(NULL));
char board[ROW][COL] = { 0 };
//initialization
initboard(board, ROW, COL);
//Print chessboard
printboard(board, ROW, COL);
play chess
Player first
//playergame(board, ROW,COL);
Computer chess
//computer_game(board, ROW, COL);
char ret = 0;
while (1)
{
playergame(board, ROW, COL);
printboard(board, ROW, COL);
ret = win(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_game(board, ROW, COL);
printboard(board, ROW, COL);
ret = win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("Player wins\n");
}
else if (ret == '#')
{
printf("Computer win\n");
}
else
{
printf("it ends in a draw\n");
}
}

void test()
{
int i = 0;
do
{
startgame();
scanf("%d", &i);
switch (i)
{
case 1:
game();
break;
case 2:
printf("game over\n");
return;
default:
printf("Illegal input, please reselect\n");
break;
}
} while (i);
}

int main()
{
test();
return 0;
}
```

### game.c

```#include "game.h"

void initboard(char board[ROW][COL], int row,int col)
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
board[i][j] = ' ';
}
}
}

void printboard(char board[ROW][COL], int row, int col)
{
int i;
int j;
int x;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");

if (i < row - 1)
{
for (x = 0; x < col; x++)
{
printf("---");

if (x < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}

void playergame(char board[ROW][COL], int row, int col)
{
printf("Players play chess");
int x;
int y;
while (1)
{
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf("Wrong input, please re-enter\n");
}
}
else
{
printf("Illegal input, please match the chessboard\n");
}
}
}

void computer_game(char board[ROW][COL], int row, int col)
{
printf("Computer chess\n");
int x;
int y;

while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] ==' ')
{
board[x][y] = '#';
break;
}
}
}

int full(char board[ROW][COL], int row, int col)
{
int i;
int j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] != ' ')
{
return 0;
}
}
}
return 1;

}
char win(char board[ROW][COL], int row, int col)
{
char Q;
char C;
int i;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] &&board[i][0]==board[i][2]&&board[i][0]!=' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}

if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (1 == full(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
```

In this way, we can play happily with our artificial ABA

# summary

Comprehensive application, simple to look at and understand. Once you start, you will find many strange knowledge points, which makes us fully realize the importance of starting practice. The summary of knowledge points, the skills of tapping code and the way of understanding are all about seeing and practicing more. There is no shortcut. In the highly difficult understanding, there are interesting knowledge points and functions. Maintain interest, develop thinking and practice code hard. Every challenge is a great sense of achievement. Even if it is confused, it is knocked up one by one. Quantitative change will cause qualitative change.
Whoa, whoa, whoa, whoa, whoa??? Give me a three company~~

Keywords: C Back-end

Added by CONFUSIONUK on Mon, 08 Nov 2021 13:56:36 +0200