当前位置:网站首页>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';
}
边栏推荐
- Boder radius has four values, and boder radius exceeds four values
- (工作记录)2020年3月11日至2021年3月15日
- 01 基础入门-概念名词
- 知识图谱之实体对齐二
- New generation garbage collector ZGC
- Tencent Android development interview, basic knowledge of Android Development
- [weekly pit] output triangle
- JS implementation force deduction 71 question simplified path
- 7、数据权限注解
- Unity making plug-ins
猜你喜欢
Discussion on beegfs high availability mode
[cloud native and 5g] micro services support 5g core network
棋盘左上角到右下角方案数(2)
知识图谱构建流程步骤详解
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
I've seen many tutorials, but I still can't write a program well. How can I break it?
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
(工作记录)2020年3月11日至2021年3月15日
How does kubernetes support stateful applications through statefulset? (07)
【每周一坑】输出三角形
随机推荐
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Discussion on beegfs high availability mode
RT thread I2C tutorial
02 basic introduction - data package expansion
Value of APS application in food industry
Design your security architecture OKR
Web security - payload
[DIY]如何制作一款個性的收音機
In unity space, an object moves around a fixed point on the sphere at a fixed speed
使用.Net驱动Jetson Nano的OLED显示屏
【计网】第三章 数据链路层(3)信道划分介质访问控制
Use of OLED screen
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
5. Wireless in vivo nano network: top ten "feasible?" problem
Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
【每周一坑】输出三角形
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
"Penalty kick" games
OLED屏幕的使用