Three-son is a kind of black and white. Three-son chess is a traditional folk game, also known as nine palace chess, circle fork, one dragon, tic-tac-toe and so on. Connect the diagonal lines of the square and place three pieces on each side in turn. As long as the three pieces of your own line, the other side will lose. However, there are many times when there will be a draw. We have learned c language, now we try to write a small game of chess with C language to play!
Chess board pattern:
Game start options:
Option 1: Start the game
Select 0: Exit the game
When you choose to start the game, you start playing against the computer
When we choose the coordinate 2 comma 2,
The computer chooses the coordinates 1,3.
There are only three outcomes:
Players to win:
Computer win:
Flat board:
You can try it yourself!!
Have a look at you and your computer whose chess skill is higher!!
The difficulty level can be adjusted by yourself:
You can decide how many squares you want
#define ROW 3 #define COL 3 #define COL 3Copy the code
Source:
Kt.c (file name)
#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 the game \n"); break; Default: printf(" Error, please select again! \n"); break; } } while (input); } int main(){ test(); return 0; }Copy the code
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 go: \n"); While (1){printf(" Please enter the 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(" the coordinates are invalid, please re-enter! \n"); } } } void ComputerMove(char board[ROW][COL], int row, int col){ int x = 0; int y = 0; Printf (" computer go: \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){// Return 1; return 0; 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'; }Copy the code
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);Copy the code