当前位置:网站首页>[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
边栏推荐
- 2022.7.2 模拟赛
- [Shangshui Shuo series together] day 10
- 【一起上水硕系列】Day 10
- 32GB Jetson Orin SOM 不能刷机问题排查
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- How to set up altaro offsite server for replication
- (perfect solution) how to set the position of Matplotlib legend freely
- Learn libcef together -- set cookies for your browser
- Go practice -- design patterns in golang's singleton
- Go practice -- gorilla / websocket used by gorilla web Toolkit
猜你喜欢
随机推荐
求质数的方法
PHP笔记超详细!!!
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
ROS Compilation Principle
Ensemble, série shuishu] jour 9
Introduction to rust Foundation (basic type)
2022.7.2day594
6.23星期四库作业
期末复习(Day2)
Win10 install pytullet and test
获取并监控远程服务器日志
2022.6.30DAY591
Differences among bio, NiO and AIO
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
期末复习(day3)
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Final review (Day2)
Niuke JS separator
[untitled]
Communication - how to be a good listener?