Today, let's teach you how to play a simple game of Sanzi chess. You can do it yourself.
First, we need a main function to construct a simple overall game logic. Because we want to keep playing the game, we need a do while loop statement to realize it. When we enter 1, we can play the game, when we enter 0, we exit the game, and when we enter other numbers, we will remind us of input errors. We also need a simple menu.
void menu() { printf("******************************************\n"); printf("************ 1. play *************\n"); printf("************ 0. exit *************\n"); printf("******************************************\n"); }
int main() { int input = 0; do { menu(); printf("Please select>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("Exit the game\n"); break; default: printf("Selection error\n"); break; } } while (input); return 0; }
Then we'll start writing the game program. We know that we should use an array to represent the chessboard. In order to facilitate us to do more than three pieces of chess, we can define the size of the chessboard. First, we need to initialize the chessboard, and then print the chessboard. We need to declare it in the header file first.
#include<stdio.h> #include<time.h> #define ROW 3 #define COL 3 //Initialize chessboard void InitBoard(char board[ROW][COL], int row, int col); //Print chessboard void DisplayBoard(char board[ROW][COL], int row, int col);
Then write the game function.
void game() { //Gobang process char board[ROW][COL];//Chessboard array //Initialize the chessboard - all board elements are given spaces InitBoard(board, ROW, COL); //Print chessboard DisplayBoard(board, ROW, COL); }
Define these two functions in another source file.
void InitBoard(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++) { board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0;i < row;i++) { //print data int j = 0; for (j = 0;j < col;j++) { printf(" %c ", board[i][j]); if(j<col-1) printf("|"); } printf("\n"); //Print split lines if (i < row - 1) { for (j = 0;j < col;j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } }
It should be noted here that we need to print the data first, then print |, and then print on the next line to separate the data, so our cycle must not be wrong. If it is not particularly clear, we can see the difference through the annotation statement. The effect after completion is like this:
Next, we need to write programs for players to play chess and computers to play chess. Let's declare these two functions first.
//Players play chess void PlayerMove(char board[ROW][COL], int row, int col); //Computer chess void ComputerMove(char board[ROW][COL], int row, int col);
Then write it to the source file. We keep playing chess until we win, so we have to use a loop to realize it.
void game() { //Gobang process char board[ROW][COL];//Chessboard array //Initialize the chessboard - all board elements are given spaces InitBoard(board, ROW, COL); //Print chessboard DisplayBoard(board, ROW, COL); char ret = 0; while (1) { PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); ComputerMove(board,ROW,COL); DisplayBoard(board, ROW, COL); }
Then define these two functions. Note that you should first judge whether the grid is empty, and then play chess.
void PlayerMove(char board[ROW][COL], int row, int col) { printf("Player walk:>\n"); int x = 0; int y = 0; while (1) { printf("Please enter 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("The coordinates are occupied, please re-enter\n"); } } else { printf("Illegal coordinates, out of range\n"); } } }
Computer chess we need to use rand() function to practice the randomness of computer chess.
void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Computer walk:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
Finally, we need to write a program to judge who won. If you win, output different characters.
int IsFull(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++) { if (board[i][j] == ' ') { return 0;//dissatisfaction } } } return 1;//The chessboard is full }
The above is the whole content of the program. After completion, it looks like this.Isn't it very interesting? Come and have a try!