当前位置:网站首页>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 .
边栏推荐
- Eyeshot Ultimate 2022 Crack By Xacker
- Essais de pénétration - sujets d'autorisation
- The article is on the list. Welcome to learn
- For in JS Of and for in
- What is Ethernet and how to connect the computer
- Activereportsjs V3.0 comes on stage
- Example of dynamic programming 3 leetcode 55
- Visual studio 2022 interface beautification tutorial
- [pan Wai 1] Huawei computer test
- Pointer array function combination in C language
猜你喜欢
Virtual honeypot Honeyd installation and deployment
PHP calls map API
UVA816 Abbott’s Revenge
Go deep into the working principle of browser and JS engine (V8 engine as an example)
Example of dynamic programming 3 leetcode 55
Dynamic programming example 1 leetcode 322 coin change
Create an environment for new projects
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
Uva1103 ancient pictograph recognition
滲透測試-提權專題
随机推荐
UVA816 Abbott’s Revenge
Professional things use professional people
CUDA compilation error
Basic bit operation symbols of C language
Small sample learning data set
2021-03-23
How micro engine uploads remote attachments
ThinkPHP 5 log management
Ranorex Studio 10.1 Crack
A review of small sample learning
API interface management setup -eolinker4.0
Laravel's little knowledge
Specific operations for uploading pictures in PHP
How to install the blue lake plug-in to support Photoshop CC 2017
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
A summary of the experiment of continue and break in C language
Handwritten promise all
buuctf(re)
[keil] GPIO output macro definition of aducm4050 official library
Penetration information collection steps (simplified version)