当前位置:网站首页>Start to write a small demo - three piece chess
Start to write a small demo - three piece chess
2022-07-02 14:04:00 【Do you want cookies】
File structure
main.c: Program entry
statement.h : Declaration of stored procedures
game.c: The realization of the game
test.c: Program specific architecture
function
main: The main function
menu: The board
game: Game implementation
main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "statement.h"
// Program entry
void test()
{
srand((unsigned)time(NULL));// Just one random time
int input = 0;
do
{
menu();
printf("\n Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Input error \n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
board.c : Operation of storing chessboard
#define _CRT_SECURE_NO_WARNINGS 1
#include "statement.h"
// Initialize chessboard
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] = ' ';// Assign a null character
}
}
}
// Print chessboard
void printboard(char board[ROW][COL], int row, int col)
{
printf("\n");
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
// Print every data
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
// Every data has “ | “
}
printf("\n");
// Print split lines
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
chess.c : Operation of playing chess
#define _CRT_SECURE_NO_WARNINGS 1
#include "statement.h"
void player(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Enter coordinates to play chess :\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
// If the chessboard is not occupied, it can be placed
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'x';// Players are “x”
break;// After playing chess, jump out
}
else
{
printf(" The location is already occupied \n");
}
}
else
{
printf(" Illegal operation \n");
}
}
}
void ai(char board[ROW][COL], int row, int col)
{
// The computer plays chess
while (1)
{
// Generate 0 - 2 The random number
int x = rand() % row;
int y = rand() % col;
// Generated random number = Scope required +1
/* * Suppose you want to generate 0 -2 The random number * So you need rand()% 3; * Random numbers = Range +1 */
if (board[x][y] == ' ')
{
board[x][y] = 'o';// Computer chess pieces are “o”
break;
}
}
}
win.c : Judgement of winning or losing
#define _CRT_SECURE_NO_WARNINGS 1
#include "statement.h"
// If the player wins , return o
// if AI Win. , return x
// If it's a draw , return d
// The game goes on return c
/* * If you return 0 It means the chessboard is not full * If you return 1 It means the chessboard is full , A draw */
static int is_draw(char board[ROW][COL], int row, int col)
// This function is only used in this file , There is no need to expose
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;// false
}
}
}
// If the loop has not finished, it will return 1, The chessboard is full
return 1;// really
}
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] != ' ')
{
// Three elements in a row are the same , And cannot be empty characters
return board[i][0];// Return the corresponding data directly
}
}
// Judgment column
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
// Three elements in a column are the same , And cannot be empty characters
return board[0][i];
}
}
// Judging diagonals
/* * Two diagonal lines * 1. 00 11 22 * 2. */
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[0][2];
}
// Judge a draw
else if (is_draw(board, row, col) == 1)
{
// If you return 1 It means a draw
return 'd';
}
// The game goes on
return 'c';
}
game.c : The specific implementation of the game
#define _CRT_SECURE_NO_WARNINGS 1
#include "statement.h"
void game()
{
char ret = 0;
// Create a chessboard
char board[ROW][COL] = {
0 };
// Initialize chessboard
initboard(board, ROW, COL);
// Print chessboard
printboard(board, ROW, COL);
while (1)
{
// Players play chess
player(board, ROW, COL);
// Judgement of winning or losing
ret = is_win(board, ROW, COL);
if (ret != 'c')
{
//c Continue for the game , If you don't meet the conditions, jump out and judge
break;
}
// Make an action and print it
printboard(board, ROW, COL);
//AI Playing chess
ai(board, ROW, COL);
printboard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == 'x')
{
printf(" Players win \n");
printboard(board, ROW, COL);
}
else if (ret == 'o')
{
printf("AI - \n");
printboard(board, ROW, COL);
}
else
{
// Players and AI No victory , At the same time, the return value is not c It means there is only one situation , It ends in a draw
printf(" It ends in a draw \n");
printboard(board, ROW, COL);
}
}
statement.h : Declaration file
// Files used to store various declarations
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
// Overall program architecture
void test();
// menu
void menu();
// game
void game();
// Initialize chessboard
void initboard(char board[ROW][COL], int row, int col);
// Print chessboard
void printboard(char board[ROW][COL], int row, int col);
// Players play chess
void player(char board[ROW][COL], int row, int col);
//AI Playing chess
void ai(char board[ROW][COL], int row, int col);
// Judgement of winning or losing
char is_win(char board[ROW][COL], int row, int col);
The code has been put in gitree :https://gitee.com/han-zhaochen/c_-proejct.git
边栏推荐
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- P1908 逆序对
- 无主灯设计:如何让智能照明更加「智能」?
- QT - make a simple calculator - realize four operations
- P3807 [template] Lucas theorem /lucas theorem
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
- 软件测试的方法
- Android kotlin fragment technology point
- Dingtalk send message
- uniapp小程序 subPackages分包配置
猜你喜欢
OpenFOAM:lduMatrix&lduAddressing
Chaos engineering platform chaosblade box new heavy release
Use of UIC in QT
Error: eacces: permission denied, access to "/usr/lib/node_modules"
Subcontracting configuration of uniapp applet subpackages
selenium,元素操作以及浏览器操作方法
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
[deep learning] simple implementation of neural network forward propagation
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
随机推荐
Code implementation MNLM
How to use SAP's metadata framework (MDF) to build custom business rules?
A better database client management tool than Navicat
Common options of tcpdump command: Three
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
P1908 逆序对
Dingtalk 发送消息
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
Find love for speed in F1 delta time Grand Prix
【文档树、设置】字体变小
693. Travel sequencing (map + topology)
Why can't d link DLL
How to set QT manual layout
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
[document tree, setting] font becomes smaller
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Halcon extract orange (Orange)
Daily practice of C language --- monkeys divide peaches
Just 1000 fans, record it