当前位置:网站首页>[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
边栏推荐
猜你喜欢

大二困局(复盘)

Brief introduction of realsense d435i imaging principle

Deep embedding and alignment of Google | protein sequences

穀歌 | 蛋白序列的深度嵌入和比對

How to set up altaro offsite server for replication

Notepad++ wrap by specified character

Kubernetes resource object introduction and common commands (V) - (configmap)

2022.DAY592

How to install and configure altaro VM backup for VMware vSphere

中职网络子网划分例题解析
随机推荐
Classification and discussion of plane grab detection methods based on learning
Niuke JS separator
期末复习(DAY7)
今天很多 CTO 都是被幹掉的,因為他沒有成就業務
Notepad++ wrap by specified character
Go practice -- design patterns in golang's singleton
mysql启动报错:The server quit without updating PID file几种解决办法
Go practice -- use redis in golang (redis and go redis / redis)
Training method of grasping angle in grasping detection
Can altaro back up Microsoft teams?
Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
Intégration profonde et alignement des séquences de protéines Google
Together, Shangshui Shuo series] day 9
Redis expiration elimination mechanism
Altaro o365 total backup subscription plan
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
[untitled]
The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
ROS Compilation Principle
Why should we rewrite hashcode when we rewrite the equals method?