当前位置:网站首页>[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
边栏推荐
- Can altaro back up Microsoft teams?
- Mapbox tasting value cloud animation
- 大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
- Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
- 2022.6.30DAY591
- Disassembly and installation of Lenovo r7000 graphics card
- Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
- Troubleshooting of 32GB Jetson Orin SOM failure to brush
- 一起上水碩系列】Day 9
猜你喜欢
Altaro o365 total backup subscription plan
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
@Autowired 导致空指针报错 解决方式
How do I migrate my altaro VM backup configuration to another machine?
Apache+php+mysql environment construction is super detailed!!!
@Solutions to null pointer error caused by Autowired
Training method of grasping angle in grasping detection
How to install and configure altaro VM backup for VMware vSphere
Latest version of source insight
Pessimistic lock and optimistic lock of multithreading
随机推荐
Skip table: principle introduction, advantages and disadvantages of skiplist
Apache+php+mysql environment construction is super detailed!!!
Talk about how to use p6spy for SQL monitoring
Together, Shangshui Shuo series] day 9
Capacity expansion mechanism of map
Redis cannot connect remotely.
Map的扩容机制
期末复习DAY8
Interview question -- output the same characters in two character arrays
NG Textarea-auto-resize
Get and monitor remote server logs
"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
3dslam with 16 line lidar and octomap
redis 遇到 NOAUTH Authentication required
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
ninja: build stopped: subcommand failed.
[Shangshui Shuo series together] day 10
Redis expiration elimination mechanism
"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
How to use source insight