当前位置:网站首页>Sanzi chess (player + computer)
Sanzi chess (player + computer)
2022-07-29 03:14:00 【Wind rises and falls】
List of articles
Preface
Detailed explanation of all codes In the code
1. test.c— Content framework
#include"game.h"
void menu()
{
printf("********************\n");
printf("***1.play 0.exit****\n");
printf("********************\n");
}
void game()
{
char ret = 0;
char board[row][col] = {
0 };
initboard(board, row, col);// Initialize chessboard
displayboard(board, row, col);// Print chessboard
while (1)// Use while Only in this way can players play chess with computers for many times
{
pepplayer( board, row, col);// The player
ret = iswin(board, row, col);//iswin Used to judge who wins
if (ret != 'c')//c Representatives continue to If ret The value of is not continue Just jump out of the loop
{
break;
}
displayboard(board, row, col);
complayer( board, row, col);// The computer
ret = iswin(board, row, col);
if (ret != 'c')
{
break;
}
displayboard(board, row, col);
}
if (ret == '*')
{
printf(" Game player wins \n");
}
else if (ret == '#')
{
printf(" Computers win \n");
}
else
{
printf(" It ends in a draw \n");
}
}
int main()
{
int input = 0;
do
{
menu();
printf(" Input number :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Input error \n");
break;
default:
break;
}
} while (input);
return 0;
}
2. game .h— Function declaration and header file
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define col 3// On behalf of the line
#define row 3// Representative column
void initboard(char board[row][col], int rows, int cols);// The parameters here are supposed to be row and col But constants cannot be used as parameters
void displayboard(char board[row][col], int rows, int cols);
void pepplayer(char board[row][col], int rows, int cols);
void complayer(char board[row][col], int rows, int cols);
char iswin(char board[row][col], int rows, int cols);// Judge who wins
3. game .c– Definition of function
#include"game.h"
void initboard(char board[row][col], int rows, int cols)// initialization
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ' ';// Assign each to Space If the assignment is 0 Will report a mistake
}
}
}
void displayboard(char board[row][col], int rows, int cols)// Print
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%c ", board[i][j]);// Print out the blank space first
if (j < cols - 1)// The last column does not |
{
printf(" |");// And then print it | You need to add a space here Otherwise, you will find that it is different from the following | Can't connect
}
}
printf("\n");// Line break
if (i < rows - 1)// Because the last line doesn't need ---
{
for (j = 0; j < rows; j++)
{
printf("---");
if (j < row - 1)// The last column does not |
{
printf("|");
}
}
}
printf("\n");// Space is also required here
}
}
void pepplayer(char board[row][col], int rows, int cols)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter the coordinates :>");// Because it needs to be called many times, it is placed inside
scanf("%d%d", &x, &y);
if (x >= 1 && x <= rows && y >= 1 && y <= cols)// Range
{
if (board[x - 1][y - 1] == ' ')// The input is from 1 The value of the beginning And the array subscript is the value -1
{
board[x - 1][y - 1] = '*';//* Representing game player
break;
}
else
{
printf(" The coordinates are occupied \n");
}
}
else
{
printf(" Illegal coordinates \n");
}
}
}
void complayer(char board[row][col], int rows, int cols)
{
while (1)
{
int x = rand() % row;// Use rand()%3 return 0 To 2
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';// Here itself from 0 At first, so you don't need -1
break;
}
}
}
int isfull(char board[row][col], int rows, int cols)// Judge whether the chessboard is full If full, return 1
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char iswin(char board[row][col], int rows, int cols)
{
int i = 0;
for (i = 0; i < col; i++)// The situation of each column
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')// If each column is equal
{
return board[0][i]; // Returns a value I knew it was '*' / '#'
}
}
for (i = 0; i< row; i++)// The situation of each line
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')// If each line is equal
{
return board[i][0];// Returns a value I knew it was '*' / '#'
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];// Diagonals
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (isfull(board, row, col) == 1)// The chessboard is full
{
return 'Q';
}
else
return 'c';// None of the above happened That is, the chessboard continues to execute
}
边栏推荐
- MySQL忘记密码怎么办
- C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
- Hangao database best practice configuration tool Hg_ BP log collection content
- 基于单片机烟雾温湿度甲醛监测设计
- Alibaba Sentinel - workflow and principle analysis
- 军品技术文件划分及说明
- 百度副总裁李硕:数字技术加持下中国劳动力成本上升是好事
- Multi table (Association) query of SQL query data
- STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
- 《QA离业务代码能有多近?》通过codediff直接暴露缺陷
猜你喜欢

Verilog的时间系统任务----$time、$stime、$realtime

Flask creation process day05-06 creation project

cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.

Multi table (Association) query of SQL query data

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

Shell programming specifications and variables

Shell编程规范与变量

Linux下安装MySQL8.0的详细步骤

Flask的创建的流程day05-06之创建项目

爆肝整理JVM十大模块知识点总结,不信你还不懂
随机推荐
Data truncation and estimation
【C】 Array
一种简单通用的获取函数栈空间大小的方法
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
C traps and defects Chapter 3 semantic "traps" 3.2 pointers to non arrays
三子棋(玩家+电脑)
What is SOA (Service Oriented Architecture)?
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
01-SDRAM:初始化模块的代码
Summary of SAP localized content in China
Why did I choose the test when the development salary was high?
增量实时灾备笔记
多行文本省略
IDEA安装后无法启动
军品研制过程-转阶段
[freeswitch development practice] unimrcp compilation and installation
Introduction and advanced MySQL (XIV)