There are two function prototypes that can be arbitrarily implemented as a
1. Pass in an n checkerboard that produces n*n squares in the heap space
int * * Createboard (int n);
int createBoard1 (int ***p,int n);
2.N pieces randomly fall on the board < need to prevent falling in the same position >
int initboard (int **p,int n);
3. Print the Board
int printboard (int **p,int n);
If two pieces fall on the same line or the same column output good chess, otherwise the output is not good chess.
To print a piece of chess in the game with an O flag in X
4. Destroying the board
int destroyboard (int **p,int n);
int destroyBoard1 (int ***p,int n);
1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 int**createboard (intn);//generate a chessboard5 voidPrintboard (int**board,intn);//Print Board6 intInitboard (int**board,intn);//Lazi7 intIsgood (int**board,intn);//Judging whether it's good chess8 intDestroyboard (int**board,intn);//destroying the Board9 intMainvoid)Ten { One int**board=0; A intn=6; -board=Createboard (n); - Printboard (board, N); theprintf"--------------------\ n"); - Initboard (board, N); - Printboard (board, N); - + if(Isgood (board, N)) -printf"very good\n"); + Else Aprintf"So low\n"); atprintf"--------------------\ n"); - Destroyboard (board, N); -printf"The board was destroyed\n"); - return 0; - } - //Create a chessboard in int**createboard (intN) - { to int**board= (int**)malloc(sizeof(int*)*n); + intI=0, j=0; - for(i=0; i<n;i++) theBoard[i]= (int*)malloc(sizeof(int)*n); * for(i=0; i<n;i++) $ for(j=0; j<n;j++)Panax Notoginsengboard[i][j]=0; - returnBoard; the } + //Print Board A voidPrintboard (int**board,intN) the { + intI=0, j=0; - for(i=0; i<n;i++) $ { $ for(j=0; j<n;j++) -printf"%4d", Board[i][j]); -printf"\ n"); the } - }Wuyi //Random Lazi the intInitboard (int**board,intN) - { Wu introw=0, line=0; - Srand (Time (NULL)); About $ intI=0; - while(i<N) - { -Row=rand ()%N; ALine=rand ()%N; + the if(board[row][line]==0)//If there are no children here, then here Lazi - { $board[row][line]=1; thei++; the } the } the returni; - } in //determine if a pawn is in the same row or column the intIsgood (int**board,intN) the { About /* the The coordinates of the sub-axis of the coordinate and the vertical axis are deposited into two arrays, and the last two arrays of the same elements are good chess the */ the int*row= (int*)calloc(N,sizeof(int)); + int*line= (int*)calloc(N,sizeof(int)); - intI=0, j=0; the intk=0;Bayi for(i=0; i<n;i++) the for(j=0; j<n;j++) the { - if(board[i][j]==1){ -row[k]=i; theline[k]=J; thek++; the } the } - intflag=0; the for(i=0; i<k-1; i++) the for(j=i+1; j<k;j++) the {94 if(row[i]==row[j]| | line[i]==Line[j]) { theflag=1; the returnFlag; the }98 } About returnFlag; - }101 //destroying the Board102 intDestroyboard (int**board,intN)103 {104 intI=0; the for(i=0; i<n;i++)106 {107 Free(Board[i]);108 }109 Free(board); the return 0;111}
Exercise: a natural chess game