当前位置:网站首页>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
边栏推荐
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- Don't spend money, spend an hour to build your own blog website
- Drawing Nyquist diagram with MATLAB
- Qt入门-制作一个简易的计算器
- Whole house Wi Fi: a pain point that no one can solve?
- P1908 reverse sequence pair
- SystemServer进程
- Node. JS accessing PostgreSQL database through ODBC
- 【文档树、设置】字体变小
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
猜你喜欢

2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验

Subcontracting configuration of uniapp applet subpackages

2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect

联合搜索:搜索中的所有需求

当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选

不会看器件手册的工程师不是个好厨子

Just 1000 fans, record it

Selenium, element operation and browser operation methods

Solution: Compression Technology (original version and sequel version)

Explanation: here is your UFO, Goldbach conjecture
随机推荐
使用BLoC 构建 Flutter的页面实例
Student course selection information management system based on ssm+jsp framework [source code + database]
抓包工具fiddler学习
Origin绘制热重TG和微分热重DTG曲线
QT - make a simple calculator - realize four operations
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
默认插槽,具名插槽,作用域插槽
693. Travel sequencing (map + topology)
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Solve "sub number integer", "jump happily", "turn on the light"
How to use SAP's metadata framework (MDF) to build custom business rules?
【文档树、设置】字体变小
每天坚持20分钟go的基础二
go操作redis
Halcon extract orange (Orange)
OpenFOAM:lduMatrix&lduAddressing
Qt如何设置固定大小
P3807 [template] Lucas theorem /lucas theorem
Android kotlin broadcast technology point
Solution: Compression Technology (original version and sequel version)