当前位置:网站首页>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中国未来独角兽TOP100榜单
- Solution: Compression Technology (original version and sequel version)
- Memory management 01 - link script
- OpenFOAM:lduMatrix&lduAddressing
- mysql ---- Oracle中的rownum转换成MySQL
- 693. Travel sequencing (map + topology)
- 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
- P3807 [template] Lucas theorem /lucas theorem
- Qt原代码基本知识
- In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
猜你喜欢
默认插槽,具名插槽,作用域插槽
Drawing Nyquist diagram with MATLAB
Daily practice of C language --- monkeys divide peaches
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Penetrate the remote connection database through the Intranet
Node. JS accessing PostgreSQL database through ODBC
Use of UIC in QT
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
selenium 元素定位方法
SystemServer进程
随机推荐
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
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
Story points vs. human days
Don't spend money, spend an hour to build your own blog website
Android kotlin broadcast technology point
如何设置Qt手工布局
Integral link, inertia link and proportion link in Simulink
Student course selection information management system based on ssm+jsp framework [source code + database]
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
selenium 在pycharm中安装selenium
The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
Codeforces Round #803 (Div. 2)(A~D)
ensp简单入门
Code implementation MNLM
Mysql5.7 installation super easy tutorial
Codeforces Round #803 (Div. 2)(A~D)
P1042 [noip2003 popularization group] Table Tennis
Memory management 01 - link script
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?