To realize the code of Sanzi chess, we must first understand the game rules of Sanzi chess, that is, both sides play chess in turn. If one party's three pieces form a line, the party wins.
1, Process
The logic of the game is:
1. Establishment of game menu;
2. Create and initialize the chessboard;
3. Print chessboard;
4. The player plays chess and prints the chessboard ('*');
5. Judge the outcome;
6. Computer playing chess and printing chessboard ('#');
7. Judge the outcome;
8. If there is a draw, return to step 2 to continue.
2, Steps
1. Menu interface
1. Start the game
0. Exit the game
void menu() { printf("*************************\n"); printf("***** 1.play *****\n"); printf("***** 0.exit *****\n"); printf("*************************\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("Selection error"); break; } } while (input); } int main() { test(); return 0; }
2. Create a chessboard
Using a two-dimensional array of three rows and three columns, the return type is char.
#define ROW 3 #define COL 3
3. Chessboard initialization
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] = ' '; } } }
4. Print chessboard
void DisplayBoard(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++) { 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"); } }
5. Players play chess
void player_move(char board[ROW][COL], int row, int col) { int x, y; x = y = 0; printf("Players play chess:>"); 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("This coordinate is occupied, please re-enter!\n"); } } } }
6. Computer chess
Computer generated random number here
void computer_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Computer chess:>\n"); while (1) { x = rand() % ROW; y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
7. Judge the outcome
Players win and return "*";
Computer wins and returns "#";
The draw returns "Q";
Continue the game and return to "C".
1. Win
(1) Determine all lines (if all characters in one line are the same, it will win);
(2) Determine all columns (if all characters in a column are the same, it will win);
(3) Determine two diagonal lines (if the characters of one diagonal line are all the same, it will win).
Note: it is necessary to exclude the case of empty chessboard (i.e. all spaces without sub items).
2. Draw
(1) Call IsFull function;
(2) Return 1 to indicate that the chessboard is full; return 0 to indicate that the chessboard is not full;
(3) If the chessboard is full and there is no winner, there will be a draw;
(4) If the board is not full, the game continues.
char is_win(char board[ROW][COL], int row, int col) { int i = 0; //Three lines 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]; } } //Three columns for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][1] && board[1][i] != ' ') { return board[1][i]; } } //Diagonal judgment 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 the draw if (1 == is_full(board, row, col)) { return 'Q'; } //continue return 'C'; }
III Multi file form
When we write code, if the program is large, we'd better use multi file form, which is intuitive and easy to maintain, so we can write game in blocks when we write this code h,game.c,test.c.
1.game.h (Declaration of game implementation function)
#pragma once #define ROW 3 #define COL 3 #include <stdio.h> #include <time.h> #include <stdlib.h> // void InitBoard(char board[ROW][COL], int row, int col); void DisplayBoard(char board[ROW][COL], int row, int col); void player_move(char board[ROW][COL], int row, int col); void computer_move(char board[ROW][COL], int row, int col); char is_win(char board[ROW][COL], int row, int col);
2.game.c (game implementation logic)
#define _CRT_SECURE_NO_WARNINGS 1 #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; int j = 0; 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 (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } } void player_move(char board[ROW][COL], int row, int col) { int x, y; x = y = 0; printf("Players play chess:>"); 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("This coordinate is occupied, please re-enter!\n"); } } } } void computer_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("Computer chess:>\n"); while (1) { x = rand() % ROW; y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } char is_full(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; } } } return 1; } char is_win(char board[ROW][COL], int row, int col) { int i = 0; //Three lines 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]; } } //Three columns for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][1] && board[1][i] != ' ') { return board[1][i]; } } //Diagonal judgment 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 the draw if (1 == is_full(board, row, col)) { return 'Q'; } //continue return 'C'; }
3.test.c (test logic of the game)
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("*************************\n"); printf("***** 1.play *****\n"); printf("***** 0.exit *****\n"); printf("*************************\n"); } void game() { //data storage char board[ROW][COL]={0}; InitBoard(board,ROW,COL);//Initialize chessboard DisplayBoard(board,ROW,COL); char ret = 0; while (1) { player_move(board,ROW,COL); DisplayBoard(board, ROW, COL); ret = is_win(board, ROW, COL); if (ret != 'C') { break; } computer_move(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = is_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 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("Selection error"); break; } } while (input); } int main() { test(); return 0; }