当前位置:网站首页>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';
}边栏推荐
- An East SMS login resurrection installation and deployment tutorial
- 数字三角形模型 AcWing 1018. 最低通行费
- 【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
- "Penalty kick" games
- Linear distance between two points of cesium
- Design your security architecture OKR
- 8086指令码汇总表(表格)
- 知识图谱构建流程步骤详解
- How to handle the timeout of golang
- [DSP] [Part 1] start DSP learning
猜你喜欢

持续测试(CT)实战经验分享

An East SMS login resurrection installation and deployment tutorial

Detailed explanation of knowledge map construction process steps
Tencent T4 architect, Android interview Foundation

Use of OLED screen

Number of schemes from the upper left corner to the lower right corner of the chessboard (2)

Rhcsa Road

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

使用.Net驱动Jetson Nano的OLED显示屏

小孩子学什么编程?
随机推荐
An East SMS login resurrection installation and deployment tutorial
SSH connection denied
I've seen many tutorials, but I still can't write a program well. How can I break it?
Cesium Click to draw a circle (dynamically draw a circle)
Gui Gui programming (XIII) - event handling
Catch ball game 1
JMeter server resource indicator monitoring (CPU, memory, etc.)
Node.js: express + MySQL实现注册登录,身份认证
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
Leetcode question 283 Move zero
How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
报错分析~csdn反弹shell报错
【计网】第三章 数据链路层(3)信道划分介质访问控制
In unity space, an object moves around a fixed point on the sphere at a fixed speed
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
知识图谱构建流程步骤详解
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
Digital triangle model acwing 1018 Minimum toll
2022 portal crane driver registration examination and portal crane driver examination materials