当前位置:网站首页>[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
2022-07-03 05:38:00 【Sobtemesa】
Sanzi is a kind of black and white chess . Sanzi chess is a traditional folk game , Just walk your three pieces into a line ( cross 、 vertical 、 Diagonals ), Even if the other party loses . however , There are many times when there is a draw . Let's use it C Language to achieve a simple version of the three chess !

Catalog
1. Select start or exit from the menu interface .
2. Create a chessboard and initialize .
4. Players play chess ('*' representative player)
5. To decide the outcome ( transport , win , It ends in a draw (‘Q’))
6. Computer chess ('*' representative computer)
7. To decide the outcome ( transport , win , It ends in a draw (‘Q’))
Preface
Usually, the project development needs to be prepared in the form of sub documents , Respectively :
- test.c As an entry point for code testing
- game.c The function function realization involved in the preparation of sanziqi
- game.h This header file is usually used to complete the function declaration 、 References to header files 、 Macro definition variables .
First complete the code :
One 、test.c
#include"game.h"
game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
Initboard(board, ROW, COL);
Display(board, ROW, COL);
while (1)
{
Playermove(board, ROW, COL);
ret=Iswin(board, ROW, COL);
Display(board, ROW, COL);
if (ret != 'C')
{
break;
}
Computermove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
Display(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '#')
{
printf(" The computer won \n");
}
if (ret == '*')
{
printf(" The player won \n");
}
if (ret == 'Q')
{
printf(" It's a draw \n");
}
}
menu()
{
printf("********************************\n");
printf("*******1. play game*************\n");
printf("*******0. exit **************\n");
printf("********************************\n");
printf("********************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Incorrect input , Re input \n");
}
}while (input);
return 0;
}Two 、game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Display(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void Playermove(char board[ROW][COL], int row, int col)
{
printf(" Players go \n");
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter your coordinates ");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (board[x-1][y-1] = ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf(" Occupied , Re input .\n");
}
}
else
{
printf(" Coordinate input error , Please re-enter \n");
}
}
}
Computermove(char board[ROW][COL], int row, int col)
{
printf(" Computer go \n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Isfull(char board [ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')
{
return board[i][0];
}
}
for (j= 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')
{
return board[0][j];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
{
return board[2][0];
}
if (Isfull(board,ROW,COL) == 1)
{
return 'Q';
}
else
{
return 'C';
}
}
3、 ... and 、game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void Initboard(char board[ROW][COL], int row, int col);
void Display(char board[ROW][COL], int row, int col);
void Playermove(char board[ROW][COL], int row, int col);
Computermove(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col); The whole idea :

1. Select start or exit from the menu interface .
And the game program itself should allow users to play repeatedly , So in the main function , We need a loop to complete this function .
do-while When the loop is called, it will first run the loop body , Then make a judgment , So here we choose to use do-while loop .


2. Create a chessboard and initialize .
Officially enter the game game(), First create a chessboard and initialize it .
At the beginning of the program , Let's first create a 3*3 A two-dimensional character array ,
Because of the length and width, we often use , For the convenience of subsequent modification , We are game.h Macro definition method is adopted in :
The next in game() Function to create an array :
And initialize this array , Because this array needs to be output and displayed in front of the user in the future , So we initialize the value to a space “ ”.
Initialization is done with a function :

3. Print chessboard .


4. Players play chess ('*' representative player)
1. Players need to fall within the chessboard .
2. Note that the coordinate naming of the array is different from our usual naming of coordinates .x,y All need to be reduced by one .
3. Players should play chess in the place above the chessboard .

5. To decide the outcome ( transport , win , It ends in a draw (‘Q’))
Determine the victory of computer players :
1 Determine all rows
2 Determine all columns
3 Determine two diagonals

Connected to the above figure :

Because the chessboard may be full , But no one wins ,
So we need to judge the draw ,
Here we write a is_full() function , Used to judge whether the chessboard is full :
1 call isFull function .
2 If any element in the array is ’ ‘, So not full , return 0. If not for ’ ', Full of , return 1.
3 If The chessboard is full There is no winner or loser , draw .

The meaning of the returned result :
1 ‘*’ Indicates that the player wins
2 ‘#’ It means that the computer wins
3 ’C ’ It means there is no point in winning or losing
4 ‘Q’ It means draw

6. Computer chess ('*' representative computer)
1.rand() Is used to generate random numbers , Here we also need to be in the main function main Add a line of code .
Take time as the seed of random number , Make sure that the row and column coordinates are really random .

2. Generating random numbers requires reference time.h This header file .
![]()

7. To decide the outcome ( transport , win , It ends in a draw (‘Q’))
Synchronous step five
8. loop 3-8 step
边栏推荐
- 在PyCharm中配置使用Anaconda环境
- PHP notes are super detailed!!!
- Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
- Troubleshooting of 32GB Jetson Orin SOM failure to brush
- Map的扩容机制
- Source insight License Activation
- 今天很多 CTO 都是被干掉的,因为他没有成就业务
- 獲取並監控遠程服務器日志
- "250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
- [set theory] relational closure (relational closure related theorem)
猜你喜欢

Go practice -- design patterns in golang's singleton

Altaro set grandfather parent child (GFS) archiving

How do I migrate my altaro VM backup configuration to another machine?

@Autowired 导致空指针报错 解决方式

How to install and configure altaro VM backup for VMware vSphere

Latest version of source insight

"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)

PHP notes are super detailed!!!

Disassembly and installation of Lenovo r7000 graphics card

College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
随机推荐
Introduction to redis using Lua script
chromedriver对应版本下载
(perfect solution) how to set the position of Matplotlib legend freely
2022.DAY592
Principles of BTC cryptography
How to use source insight
Ansible firewall firewalld setting
Redis使用Lua脚本简介
Pytorch through load_ state_ Dict load weight
Gan network thought
Source insight operation manual installation trial
ES 2022 正式发布!有哪些新特性?
穀歌 | 蛋白序列的深度嵌入和比對
Why is go language particularly popular in China
How to install and configure altaro VM backup for VMware vSphere
Azure file synchronization of altaro: the end of traditional file servers?
期末复习(DAY7)
聊聊如何利用p6spy进行sql监控
在PyCharm中配置使用Anaconda环境
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN