当前位置:网站首页>C language games - three chess
C language games - three chess
2022-07-06 20:32:00 【farewell12345】
The game passes 3 Source file implementation
1. The header file game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3 // The number of rows on the chessboard
#define COL 3 // The number of rows on the chessboard
// 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);
// Judgement of winning or losing
char IsWin(char board[ROW][COL], int row, int col);2. The game logic test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**************************\n");
printf("******** 1. Sanzi *****\n");
printf("******** 0. sign out *****\n");
printf("**************************\n");
}
void game()
{
char ret = 0;// Accept the state of winning or losing the game
char board[ROW][COL];
// Initialize chessboard
Initboard(board, ROW, COL);
// Print chessboard
Displayboard(board, ROW, COL);
while (1)
{
// Players play chess
PlayerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
// Judgement of winning or losing
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
// The computer plays chess
ComputerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
// Judgement of winning or losing
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" Players win \n");
}
else if (ret == '#')
{
printf(" Computer victory \n");
}
else
{
printf(" It ends in a draw \n");
}
Displayboard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
// Run the three piece chess game
game();
break;
case 0:
printf(" Exit procedure \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}3. The subject of the game game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// Initialize chessboard
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
int j = 0;
for (j; j < col; j++)
{
board[i][j] = ' ';
}
}
}
// Print chessboard
void Displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
int j = 0;
for (j; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
j = 0;
for (j; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
// Players play chess
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf(" Players go :\n");
printf(" Please enter the coordinates of playing chess :");
int i = 0;
int j = 0;
// Judge the validity of input coordinates
while(1)
{
scanf("%d %d", &i, &j);
if (board[i - 1][j - 1] != ' ')
{
printf(" Illegal input coordinates , Please re-enter \n");
}
else
{
break;
}
}
// Modify the value of the corresponding position in the array
board[i - 1][j - 1] = '*';
}
// The computer plays chess
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf(" Computer go :\n");
while (1)
{
int i = rand() % row;
int j = rand() % col;
if (board[i][j] == ' ')
{
board[i][j] = '#';
break;
}
}
}
// Judgement of winning or losing
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
// There is... In the line 3 Same
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
int j = 0;
for (j; j < col; j++)
{
// There are... In one column 3 Same
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
}
}
// Diagonal yes 3 Same
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];
}
// The chessboard is full of draws
i = 0;
for (i; i < row; i++)
{
j = 0;
for (j; j < col; j++)
{
if (board[i][j] == ' ')
{
return 'C';
}
}
}
return 'Q';
}边栏推荐
- 解剖生理学复习题·VIII血液系统
- Design your security architecture OKR
- [network planning] Chapter 3 data link layer (3) channel division medium access control
- Wechat applet common collection
- Discussion on beegfs high availability mode
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- Trends of "software" in robotics Engineering
- Event center parameter transfer, peer component value transfer method, brother component value transfer
- Leetcode question 448 Find all missing numbers in the array
- 知识图谱之实体对齐二
猜你喜欢

Le lancement du jupyter ne répond pas après l'installation d'Anaconda

JMeter server resource indicator monitoring (CPU, memory, etc.)

Node.js: express + MySQL实现注册登录,身份认证

How to upgrade high value-added links in the textile and clothing industry? APS to help

【微信小程序】运行机制和更新机制

Logic is a good thing

Application layer of tcp/ip protocol cluster

【DSP】【第二篇】了解C6678和创建工程

Continuous test (CT) practical experience sharing
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control
随机推荐
Rhcsa Road
2022 portal crane driver registration examination and portal crane driver examination materials
Problems encountered in using RT thread component fish
使用ssh连接被拒
Cesium Click to draw a circle (dynamically draw a circle)
Gui Gui programming (XIII) - event handling
[DIY]如何制作一款個性的收音機
Summary of different configurations of PHP Xdebug 3 and xdebug2
OLED屏幕的使用
Maximum likelihood estimation and cross entropy loss
Logic is a good thing
JMeter server resource indicator monitoring (CPU, memory, etc.)
APS taps home appliance industry into new growth points
Deep learning classification network -- zfnet
[diy] how to make a personalized radio
Wechat applet common collection
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Learn to punch in Web
【计网】第三章 数据链路层(3)信道划分介质访问控制
Case ① | host security construction: best practice of 3 levels and 11 capabilities