当前位置:网站首页>Sanzi chess (C language)
Sanzi chess (C language)
2022-07-06 07:50:00 【Dull in autumn 11111】
The rules of the game
Players and computers ( No technology , Random down )
The default player will go first , One child at a time , The two sides alternate , Until either computer or player wins , Or the chessboard is full ( It ends in a draw ), Game over .

Application framework
Three files :
text.c__ Test the logic of the game ( Source file )
game.c__ function ( Source file )
game.h__ The definition of symbols Declaration of functions ( The header file )
The basic idea
1. Print menu
1.play( Play ) 0.exit( Quit the game ) Enter other numbers ( Input error , Re input )
Implementation code
//text.c
#include "game.h"
void menu()
{
printf("************************");
printf("******* 1.play ********");
printf("******* 0.exit ********");
printf("************************");
}
void text()
{
int input = 0;
do
{
menu();
printf(" Please enter :->\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Implement the functions of the three piece chess game
break;
case 0:
printf(" End the game :->\n");
break;
default:
printf(" Input error , Please re-enter :->\n");
break;
}
}while(input)
}
int main()
{
text();
return 0;
}2. Complete the Sanzi game function code
In the file text.c Of game() in
Complete the idea of sanziqi :
1. Initialize the chessboard to full space ( Need to have a dividing line )
2. Print chessboard + data ()
3. Players play chess
4. The computer plays chess ( Random )
5. Judgement of winning or losing
See the following code :
//text.c
void game()
{
// An array of chess players
char boar[ROW][COL]={0};
// Initialize the chessboard to be full of spaces
InitBoard(board,ROW,COL);
// Print chessboard
DisplayBoard(board,ROW,COL);
// Players play chess , Judgement of winning or losing
// The computer plays chess at random , Judgement of winning or losing
}
above 5 Each step requires 5 Functions in game.h In a statement , stay game.c The concrete implementation of
The statement is shown below :
//game.h Statement
#include<stdio.h>
#define ROW 3// Create a chessboard , That's ok 3
#define COL 3// Create a chessboard , Column 3
// Use a macro to define , Define rows and columns , Easy to modify
// Initialize chessboard
void InitBoard(char board[ROW][COL], int row, int col);
// Print chessboard
void DisplayBoard(char board[ROW][COL], int row, int col);
// Players play chess
void player_move(char board[ROW][COL], int row, int col);
// The computer plays chess
void computer_move(char board[ROW][COL], int row, int col);
// Judgement of winning or losing
char is_win(char board[ROW][COL], int row, int col);
1. Initialize the array data to spaces
//game.c Function implementation
#include "game.h"
// Initialization data
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
2. There are spaces in the chessboard array , Need to print chessboard
Can be realized as :
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
// Print data
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// Print split lines
if(i<row-1)
printf("---|---|---\n");
}
}But the above code only applies to 3*3 The board , To improve universality , Can be realized as :
//game.c
// Print chessboard + data
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
// Print data
for (int 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 (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}Chessboard printing effect :

3. Players play chess
The default player plays chess first , And the pieces are :*
Players need to enter coordinates , Players think the coordinates are from 1 beginning , Computer coordinates from 0 beginning , That is, players need to reduce the input coordinates 1
//game.c
// Players play chess
void player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Players play chess \n");
while (1)
{
printf(" Please enter the coordinates :->");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
// You need to enter coordinates within a reasonable range
{
// Playing chess
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf(" The coordinates are occupied , Please re-enter \n");
}
}
else
{
printf(" It's illegal to enter coordinates , Please re-enter \n");
}
}
}4. The computer plays chess ( Random )
Computer chess pieces default to :#
//game.c
// The computer plays chess ( Random )
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" The computer plays chess :>\n");
while (1)
{
x = rand() % row;
y = rand() % col;// Random generation
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}notes : Use here rand() function , It needs to be used in the main function srand((unsigned int)time(NULL))
stay game.h The header file should be marked in , as follows :
//game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>5. Judgement of winning or losing ( Three situations )
The main logic text.c To realize the function part of the three piece chess game game() The final version :
/* Game situation : Game player wins , Computers win , It ends in a draw , Continue to cycle */
// Game player wins _'*'
// Computers win _'#'
// It ends in a draw _'Q'
// continue _'C'
//text.c
void game()
{
char ret = 0;
// An array of chess players
char board[ROW][COL] = { 0 };
// Initialize the chessboard to full space
InitBoard(board, ROW, COL);
// Print chessboard
DisplayBoard(board, ROW, COL);
while (1)
{
// Players play chess
player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
// Judgement of winning or losing
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
// As long as it's not “ continue ”, Just break the cycle , Jump out
// The computer plays chess ( Random )
computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
// Judgement of winning or losing
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
// As long as it's not “ continue ”, Just break the cycle , Jump out
}
if (ret == '*')
{
printf(" The player won \n");
}
else if (ret == '#')
{
printf(" The computer won \n");
}
else
{
printf(" It ends in a draw \n");
}
}victory : Either party only needs to 、 Column 、 Diagonals , First three in a row , That is, the party wins
It ends in a draw : The chessboard is full , The conditions for one side to win have not been met , It ends in a draw
continue : The chessboard is not full The game goes on
//game.c
// Judgement of winning or losing
char is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
// Judgment line
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];
}
}
// Judgment column
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j]== board[1][j] && board[0][j] == board[2][j]&& board[0][j] != ' ')
{
return board[0][j];
}
}
// Diagonals
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 a draw
if (if_full(board, row, col) == 1)
{
return 'Q';
}
// continue
return 'C';
}notes : You also need a function to judge whether the chessboard is full ( It ends in a draw )
//game.c
static int if_full(char board[ROW][COL], int row, int col)
// Use static Modify function , Limit the scope to this file , In the service of is_win() function
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;// Not full
}
return 1;
}
}To sum up, that is, the sharing content of Sanzi chess games
边栏推荐
- 数据治理:元数据管理篇
- Epoll and IO multiplexing of redis
- Risk planning and identification of Oracle project management system
- onie支持pice硬盘
- Google may return to the Chinese market after the Spring Festival.
- TS类型体操 之 字符串的妙用
- 软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
- leecode-C语言实现-15. 三数之和------思路待改进版
- 烧录场景下的源代码防泄密方案分享
- Typescript interface and the use of generics
猜你喜欢

Description of octomap averagenodecolor function

Epoll and IO multiplexing of redis
![If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]](/img/d6/92ad1c6f84415de6ab0dfd16cd6073.png)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]

数据治理:主数据的3特征、4超越和3二八原则

Simulation of Teman green interferometer based on MATLAB

. Net 6 learning notes: what is NET Core

Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
![08- [istio] istio gateway, virtual service and the relationship between them](/img/fb/09793f5fd12c2906b73cc42722165f.jpg)
08- [istio] istio gateway, virtual service and the relationship between them

JMeter performance test steps practical tutorial

Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
随机推荐
数据治理:数据质量篇
[dictionary tree] [trie] p3879 [tjoi2010] reading comprehension
How to estimate the number of threads
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
edge瀏覽器 路徑獲得
数据治理:微服务架构下的数据治理
Launch APS system to break the problem of decoupling material procurement plan from production practice
Codeforces Global Round 19(A~D)
洛谷P4127 [AHOI2009]同类分布 题解
On why we should program for all
解决方案:智慧工地智能巡檢方案視頻監控系統
Entity class design for calculating age based on birthday
洛谷P1836 数页码 题解
Notes on software development
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
Pangolin Library: control panel, control components, shortcut key settings
P3047 [usaco12feb]nearby cows g (tree DP)
HTTP cache, forced cache, negotiated cache
Scala language learning-08-abstract classes
Typescript interface and the use of generics