当前位置:网站首页>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家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
- The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
- Three talking about exception -- error handling
- D language, possible 'string plug-ins'
- 混沌工程平台 ChaosBlade-Box 新版重磅发布
- Selenium installing selenium in pycharm
- [USACO05JAN]Watchcow S(欧拉回路)
- 给Android程序员的一些面试建议「建议收藏」
- 线性dp求解 最长子序列 —— 小题三则
- 你知道Oracle的数据文件大小有上限么?
猜你喜欢

Whole house Wi Fi: a pain point that no one can solve?

Will your sleep service dream of the extra bookinfo on the service network

瀏覽器驅動的下載

Origin plots thermogravimetric TG and differential thermogravimetric DTG curves

代码实现MNLM
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]

Solve "sub number integer", "jump happily", "turn on the light"

Skillfully use SSH to get through the Internet restrictions

Runhe hi3516 development board openharmony small system and standard system burning

【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
随机推荐
Selenium installing selenium in pycharm
Why is the default of switch followed by break?
693. 行程排序(map + 拓扑)
Getting started with QT - making a simple calculator
Data consistency between redis and database
混沌工程平台 ChaosBlade-Box 新版重磅发布
2022 Heilongjiang provincial examination on the writing skills of Application Essays
Explanation: here is your UFO, Goldbach conjecture
Achievements in science and Technology (27)
Design of non main lamp: how to make intelligent lighting more "intelligent"?
每天坚持20分钟go的基础二
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
量子三体问题: Landau Fall
故事点 vs. 人天
路由(二)
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Selenium element positioning method
无主灯设计:如何让智能照明更加「智能」?
Quantum three body problem: Landau fall
Just 1000 fans, record it