当前位置:网站首页>C language -- Sanzi chess
C language -- Sanzi chess
2022-06-25 05:18:00 【Summer orange TV】
Sanzi
Catalog
menu
Initial chessboard
Game implementation
- Players play chess
- The computer plays chess
- Judgement of winning or losing
Game over
Module processing
1 game.h
#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 player_move(char board[ROW][COL], int row, int col);
// The computer plays chess
void computer_move(char board[ROW][COL], int row, int col);
// Judgement of winning or losing
char is_win(char board[ROW][COL], int row, int col);2.game.c3
#define _CRT_SECURE_NO_WARNINGS
#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 player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Players play chess \n");
while (1)
{
printf(" Please enter the coordinates :>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{ // Playing chess
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 input , Please re-enter \n");
}
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" The computer plays chess :>\n");
while (1)
{
x = rand()%row;//0-2
y = rand()%col;//0-2
if (board[x][y] == ' ');
{
board[x][y] = '#';
break;
}
}
}
static int if_full(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++)
{
if (board[i][j] == ' ')
{
return 0;// Not full
}
}
}
return 1;// Full of
}
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][2]!= ' ')
{
return board[i][1];
}
}
// Judgment column
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i]!= ' ')
{
return board[1][i];
}
}
// Judging 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 (if_full(board, row, col) == 1)
{
return 'Q';
}
// continue
return 'C';
}3.test.c
#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()
{
char ret = 0;
// Store chess data
char board[ROW][COL] = { 0 };
// Initialize the chessboard to full space
InitBoard(board, ROW, COL);
// Print chessboard
DisplayBoard(board, ROW, COL);
while (1)
{
// Players play chess
player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
// Judgement of winning or losing
ret = is_win(board,ROW,COL);
if (ret != 'C')
{
break;
}
// The computer plays chess
computer_move(board, ROW, COL);// Play chess immediately
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" The player won \n");
}
else if (ret == '#')
{
printf(" The computer won \n");
}
else
{
printf(" It ends in a draw ");
}
}
// How to end the game
// The player *
// Computers win #
// It ends in a draw Q
// continue C
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();// game
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice \n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}Realization

Conclusion
It's not easy to create , I hope you can give the blogger some praise .

边栏推荐
- Rce code execution & command execution (V)
- How PHP gets the user's City
- Flex flexible layout for mobile terminal page production
- Everything is an object
- parallel recovery slave next change & parallel recovery push change
- Create dynamic array
- Summary of SQL injection (I)
- Jason learning
- Read the general components of antd source code
- 2021-03-23
猜你喜欢

XSS (cross site script attack) summary (II)

Svg code snippet of loading animation

Various pits encountered in the configuration of yolov3 on win10

Ctfhub eggs

Compatible with Internet Explorer

The article is on the list. Welcome to learn

EL & JSTL (XIII)
![[Huawei machine test] hj16 shopping list](/img/54/d28f5aea9350af7797ca7c069e564d.jpg)
[Huawei machine test] hj16 shopping list

Introduction to the hardest core PWN in the whole network_ Graphic analysis

Penetration test - right raising topic
随机推荐
Small sample learning data set
[pan Wai 1] Huawei computer test
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Dynamic programming full backpack
Go Concurrency
Google Earth engine (GEE) - Global jrc/gsw1_ 1 / batch download of yearlyhistory dataset (China region)
SSRF-lab
Mobile number regular expression input box loses focus verification
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
Ctfhub eggs
Virtual honeypot Honeyd installation and deployment
Five simple data types of JS
Fun CMD command line~
Detailed summary of float
Introduction to the hardest core PWN in the whole network_ Graphic analysis
Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
Uva1103 ancient pictograph recognition
Difference between asemi high power FET and triode
Penetration test - right raising topic
Svg code snippet of loading animation