当前位置:网站首页>C language implementation of three chess
C language implementation of three chess
2022-07-18 01:44:00 【Lulu ovo】
Simple Gobang , The computer immediately , Use a two-dimensional array to define the chessboard and store the pieces .
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROWSIZE 3// Number of macro definition lines
#define COLSIZE 3// Macro defines the number of columns
char jugde(char chess[ROWSIZE][COLSIZE]) {// Judge the outcome
// Judgment line
for (int i = 0; i < ROWSIZE; i++) {
if (chess[i][0] != ' ' && chess[i][0] == chess[i][1] && chess[i][0] == chess[i][2]) {
return chess[i][0];
}
}
// Judgment column
for (int i = 0; i < COLSIZE; i++) {
if (chess[0][i] != ' ' && chess[0][i] == chess[1][i] && chess[0][i] == chess[2][i]) {
return chess[0][i];
}
}
// Judging diagonals
if (chess[0][0] != ' ' && chess[0][0] == chess[1][1] && chess[0][0] == chess[2][2]) {
return chess[0][0];
}
// Judge negative diagonal
if (chess[0][2] != ' ' && chess[0][2] == chess[1][1] && chess[0][2] == chess[2][0]) {
return chess[0][2];
}
return ' ';
}
int boardFall(char chess[RAND_MAX][COLSIZE]) {// Judge whether the chessboard is full , If full, exit the loop
int temp_fall = 0;
for (int i = 0; i < ROWSIZE; i++) {
for (int j = 0; j < COLSIZE; j++) {
if (chess[i][j] != ' ') {
temp_fall++;
}
}
}
if (temp_fall == 9) {
return 1;
}
return 0;
}
int computerPut(char chess[ROWSIZE][COLSIZE]) {// Computer input ( Time stamp is used to assign random factors to make the computer fall randomly )
int rand_row = 0;
int rand_col = 0;
while (1) {
rand_row = rand() % 3;
rand_col = rand() % 3;
if (chess[rand_row][rand_col] == ' ') {
chess[rand_row][rand_col] = 'O';
break;
}
if (boardFall(chess) == 1) {
break;
}
}
}
int playerPut(char chess[ROWSIZE][COLSIZE],int row,int col) {// Players enter
if ((row < ROWSIZE && row >= 0) && (col >= 0 && col < COLSIZE)) {
if (chess[row][col] == ' ') {
chess[row][col] = 'X';
return 1;
}
else {
printf(" There are already chess pieces in this position. Please relocate \n");
return 0;
}
}
else {
printf(" The position of the chess piece is beyond the chessboard. Please settle it again \n");
return 0;
}
}
int inputBoard(char chess[ROWSIZE][COLSIZE]) {// Print chessboard
for (int i = 0; i < ROWSIZE; i++) {
printf("+---+---+---+\n");
for (int j = 0; j < COLSIZE; j++) {
printf("|");
printf(" %c ", chess[i][j]);
if (j == 2) {
printf("|");
printf("\n");
}
}
}
printf("+---+---+---+");
}
int initBoard(char chess[ROWSIZE][COLSIZE]) {// Initialize chessboard
for (int i = 0; i < ROWSIZE; i++) {
for (int j = 0; j < COLSIZE; j++) {
chess[i][j] =' ';
}
}
}
int main() {
char chess[ROWSIZE][COLSIZE] = { 0 };
initBoard(chess);// Initialize chessboard
int row_ = 0;
int col_ = 0;
char temp_3 = 0;
int temp_ = 0;
int temp_1 = 0;
int temp_2 = 0;
srand((unsigned int)time(0));
while (1) {
system("cls");
//1. Print chessboard
inputBoard(chess);
printf("\n");
//2. User input
while (1) {
printf(" Players enter the chess coordinates ( Space separates the middle ):");
scanf("%d %d", &row_, &col_);
temp_ = playerPut(chess, row_ - 1, col_ - 1);
if (temp_ == 1) {
break;
}
}
// The victory judgment ends the cycle
temp_3 = jugde(chess);
temp_2 = boardFall(chess);
if (temp_3 == 'X') {
break;
}
if (temp_3 == 'O') {
break;
}
if (temp_2 == 1) {
break;
}
//3. Computer input
computerPut(chess);
// Judge the outcome and end the cycle
temp_3 = jugde(chess);
temp_2 = boardFall(chess);
if (temp_3 == 'X') {
break;
}
if (temp_3 == 'O') {
break;
}
if (temp_2 == 1) {
break;
}
}
system("cls");
inputBoard(chess);
temp_3 = jugde(chess);
printf("\n");
if (temp_3 == 'X') {
printf(" Players win !");
}
if (temp_3 == 'O') {
printf(" Computers are better than !");
}
if (temp_3 == ' ') {
printf(" A draw !");
}
}
边栏推荐
- 树莓派 安装 opencv 环境,解决NO module named ‘cv2’
- [C exercise] find the K power of n (recursive implementation)
- 【Verilog】32位单精度浮点数比较大小
- 保持电气化时代的交通安全“零伤亡”,沃尔沃底气何来?
- Markdown extended syntax
- What designs are needed in the architecture to build a general monitoring and alarm platform
- 【C 练习】输入年月,求出月份天数
- Realizing Halcon scale with MATLAB low-level source code_ image_ Max operator effect
- 反射
- 2021-09-18
猜你喜欢

redis配置文件

redis事务和消息订阅发布

Axure如何做到屏幕自适应

Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time

在线综艺冷场:“爱优腾芒”放不开,“抖快B”拿不下

Realizing Halcon scale with MATLAB low-level source code_ image_ Range (can be used for background segmentation)

STM32应用开发实践教程:环境温湿度监测的应用开发

【C】 Const decorated pointer

After the WebView page of the applet is forwarded, open the page blank

容器介绍及总结
随机推荐
C# 飞行棋小游戏
职员信息管理系统--程序设计
【HCIA】数通网络基础
树莓派远程 桌面显示不全
【HCIA】OSI 模型
[C exercise] print all "Narcissus numbers" from 0 to 100000
C. K-beautiful Strings
蜘蛛抓取频率提高,可以快速增加网站收录吗?
搜索结果页竞价广告展现升级,对SEO有什么影响?
Cloud native: docker practical experience (III) deploy MySQL 8 master-slave replication on docker
【C】数组的地址
【C】实用调试技巧
【C数据的存储】
【C 练习】序列中删除指定的数字
Thinking of reading
After the WebView page of the applet is forwarded, open the page blank
针对采集丢权益,企业站,还值得深耕内容吗?
阿同的小心愿
[practice C] xiaolele walks up the steps
Vulnerability scanning tools Nessus, rapid7 insightvm, OpenVAS installation & simple use