当前位置:网站首页>C language implementation [Sanzi chess game] (step analysis and implementation source code)
C language implementation [Sanzi chess game] (step analysis and implementation source code)
2022-07-01 07:03:00 【Zhou Zhouwang】
The rules of the game
I believe everyone has played Gobang , In fact, Sanzi chess is the same , It's in one 3x3( It could be bigger ) On the chessboard , When one side forms 3 Lianqi ( Horizontal vertical oblique ) When you win .
As shown in the figure :
Implementation code
First we need one game.h The header file , Here are the declarations of our interface functions .
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
// 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 PlayerMove(char board[ROW][COL], int row, int col);
// The computer plays chess
void ComputerMove(char board[ROW][COL], int row, int col);
// Judge the game
// To return 4 Different states
// Game player wins - '*'
// Computers win - '#'
// It ends in a draw - ‘Q’
// continue - 'C'
char IsWin(char board[ROW][COL], int row, int col);
Second, we need to be in game.c Complete the implementation of each function in
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
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] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
// Print data
int j = 0;
for (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 (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf(" Players go :>\n");
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter the coordinates ( Lines The first few columns ):>");
scanf("%d%d", &x, &y);//2 1 -- > 1 0
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf(" The coordinates are occupied , Please re-enter \n");
}
}
else
{
printf(" Illegal coordinates , Out of range \n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Computer go :>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;// discontent
}
}
}
return 1;// Chessboard full
}
char IsWin(char board[ROW][COL], int row, int col)
{
// That's ok
int i = 0;
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];
}
}
// Column
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
// 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 (IsFull(board, row, col))
{
return 'Q';
}
// The game goes on
return 'C';
}
Last in test.c Intermediate testing
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("***********************************\n");
printf("*********** 1. play *********\n");
printf("*********** 0. exit *********\n");
printf("***********************************\n");
}
void game()
{
// The process of Sanzi chess
char board[ROW][COL];// Chessboard array
// Initialize chessboard - board All elements are given spaces
InitBoard(board, ROW, COL);
// Print chessboard
DisplayBoard(board, ROW, COL);
// Playing chess
char ret = 0;
while (1)
{
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" Game player wins \n");
}
else if (ret == '#')
{
printf(" Computers win \n");
}
else
{
printf(" It ends in a draw \n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select >");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
return 0;
}
In this way, the implementation of our three piece chess is completed .
Effect display
summary
In general, the implementation of Sanzi chess is relatively simple and easy to understand .
Of course, the version we implemented is the most garbage version , The IQ of a computer is 0, Because the position under the computer is just randomly placed on the empty position , We don't design algorithms for computers .
such as :
We can design for computers , When the player is about to 3 Even chess , The computer is going to intercept , When computers can 3 Even chess , Computers need to know , And where to go to win more probability, and so on .
Thank you for reading , See you next time
If there is a mistake Welcome to exchange
边栏推荐
- Docker installation and deployment redis
- 3. Disabling copy construction
- Rclone Chinese document: a collection of common commands
- Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
- 1286_FreeRTOS的任务优先级设置实现分析
- 【计网】(一) 集线器、网桥、交换机、路由器等概念
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
- 5G Massive MIMO的概念和优点总结
- ESP32 ESP-IDF GPIO按键中断响应
- 代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
猜你喜欢
Spark入门(一篇就够了)
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
Some pits designed by NOC
[image processing] image histogram equalization system with GUI interface
TDB中多个model情况下使用fuseki查询
清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间
(I) apple has open source, but so what?
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
[lingo] find the shortest path problem of undirected graph
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
随机推荐
buildroot override 机制
SQL learning notes nine connections 2
Spark入门(一篇就够了)
How to choose a product manager course when changing to a product manager?
ctfshow-web351(SSRF)
Docker installation and deployment redis
在券商账户上买基金安全吗
Software engineering review
【LINGO】求解二次规划
Product learning (I) - structure diagram
图像风格迁移 CycleGAN原理
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
Is it safe to buy funds on Alipay? Where can I buy funds
Introduction to spark (one article is enough)
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
[matlab] solve nonlinear programming
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
JSP - paging
问题:OfficeException: failed to start and connect(二)
C语言实现【扫雷游戏】完整版(实现源码)