Trinity is a form of black and white. Three sons chess is a traditional folk game, also known as nine palace chess, circle cross, one dragon, tic-tac-toe and so on. Connect the diagonal lines of the square and place three pieces on both sides of the square in turn. As long as your three pieces walk into a line, the other party loses. However, there are a lot of situations where there is a draw. We learned C language, now let’s try to write a little game in C language to play!

Trinity board pattern:

Game start selection:

Option 1: Start the game

Option 0: Quit the game

When you choose to start the game, you play chess with the computer

When we choose the coordinate 2 comma 2.

The computer chose the coordinate 1,3.

There are only three possible outcomes:

Players to win:

Computer win:

Flat board:

You can try it yourself!!

See who is better at chess, you or your computer!!

The difficulty level can be adjusted by itself:

The number of checkerboards can be as many as you want

``#define ROW 3 #define COL 3``

Source:

Kt.c (filename)

``#include "game.h" void menu(){ printf("***********************\n"); printf("****1.play 0.exit*****\n"); printf("***********************\n"); } void game(){ char ret = 0; char board[ROW][COL] = { 0 }; InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret ! = 'C'){ break; } ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret ! = 'C'){ break; }} if (ret == '*'){printf(" Player wins \n"); } else if (ret == '#'){printf(" computer wins \n"); } else{printf(" tie \n"); } } void test(){ int input = 0; srand((unsigned int)time(NULL)); do{ menu(); Printf (" Please select :>"); scanf("%d", &input); switch (input) { case 1: game(); break; Case 0: printf(" exit game \n"); break; Default: printf(" Wrong selection, please reselect! \n"); break; } } while (input); } int main(){ test(); return 0; }``

Game. C (filename)

``#include "game.h" void InitBoard(char board[ROW][COL], int row, int col){ int i = 0; int j = 0; for (i = 0; i < row; i++){ for (j = 0; j < col; j++){ board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col){ int i = 0; for (i = 0; i < row; i++){ int j = 0; for (j = 0; j < col; j++){ printf(" %c ", board[i][j]); if (j < col-1) printf("|"); } printf("\n"); if (i < row - 1){ for (j = 0; j < col; j++){ printf("---"); if (j < col-1) printf("|"); } printf("\n"); } } } void PlayerMove(char board[ROW][COL], int row, int col){ int x = 0; int y = 0; Printf (" Player goes: \n"); While (1){printf(" Please enter the desired coordinates: "); 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(" This coordinate is occupied \n"); }} else{printf(" Coordinate illegal, please retype! \n"); } } } void ComputerMove(char board[ROW][COL], int row, int col){ int x = 0; int y = 0; Printf (" computer goes: \n"); while (1){ x = rand() % row; y = rand() % col; if (board[x][y] == ' '){ board[x][y] = '#'; break; }} int isFull (char board[ROW][COL],int ROW,int COL){// int isFull (char board[ROW][COL],int ROW,int COL){// int isFull (char board[ROW][COL],int ROW,int COL); int j = 0; for (i = 0; i < row; i++){ for (j = 0; j < col; j++){ if (board[i][j] == ' '){ return 0; } } } return 1; } char IsWin(char board[ROW][COL], int row, int col){ int i = 0; for (i = 0; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] ! = ' '){ return board[i][0]; } } 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]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] ! = ' '){ return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] ! = ' '){ return board[1][1]; } if (1 == IsFull(board, ROW, COL)){ return 'Q'; } return 'C'; }``

Game. H (filename)

``#define ROW 3 // #define COL 3 #include<stdio.h> #include<stdlib.h> #include<time.h> void InitBoard(char board[ROW][COL],int row,int col); void DisplayBoard(char board[ROW][COL],int row,int col); void PlayerMove(char board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int row, int col); char IsWin(char board[ROW][COL], int row, int col);``