当前位置:网站首页>[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
边栏推荐
- Simpleitk learning notes
- [together Shangshui Shuo series] day 7 content +day8
- Today, many CTOs were killed because they didn't achieve business
- Ensemble, série shuishu] jour 9
- Final review (Day7)
- Brief introduction of realsense d435i imaging principle
- mysql启动报错:The server quit without updating PID file几种解决办法
- 【无标题】
- 2022.7.2day594
- 获取并监控远程服务器日志
猜你喜欢

Notepad++ wrap by specified character

【一起上水硕系列】Day 7 内容+Day8

Disassembly and installation of Lenovo r7000 graphics card

Intégration profonde et alignement des séquences de protéines Google
![[together Shangshui Shuo series] day 7 content +day8](/img/fc/74b12addde3a4d3480e98f8578a969.png)
[together Shangshui Shuo series] day 7 content +day8

"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
![[Shangshui Shuo series together] day 10](/img/a3/e8b9df588bef67ead925813a75c8c0.png)
[Shangshui Shuo series together] day 10

Apache+php+mysql environment construction is super detailed!!!

XML Configuration File

DEX net 2.0 for crawl detection
随机推荐
Win10 install pytullet and test
chromedriver对应版本下载
Mapbox tasting value cloud animation
期末复习(DAY6)
6.23 warehouse operation on Thursday
AtCoder Beginner Contest 258(A-D)
Configure DTD of XML file
期末复习(Day2)
【一起上水硕系列】Day 7 内容+Day8
ROS Compilation Principle
Redis breakdown penetration avalanche
Today, many CTOs were killed because they didn't achieve business
Map的扩容机制
2022.7.2day594
Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
Altaro set grandfather parent child (GFS) archiving
大二困局(复盘)
How do I migrate my altaro VM backup configuration to another machine?
Deploy crawl detection network using tensorrt (I)
Can altaro back up Microsoft teams?