#include<stdio.h> #include<windows.h> #define ROW 14 #define COL 14 void
gotoxy(int x,int y)// Formal parameter { HANDLE hOut; COORD pos= {x,y}; // Starting position of cursor ( Section 1 column , Section 3 that 's ok )
0 Yes, No 1 column 2 Yes, No 3 that 's ok hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos); //printf(" Positioning cursor position search (%d,%d)\n",pos.X,pos.Y);
} /* Function name :paintGameUI() Function function : Draw chessboard Function parameters : nothing Function return value : nothing */ void paintGameUI() // Print game board
use ROW COL The macro definition can be changed above // Chessboard size { int i=0; int j=0; printf("┌ "); // Top
for(i=0;i<COL;i++) { printf("┬ "); } printf("┐\n"); // middle for(j=0;j<ROW;j++) {
printf("├ "); for(i=0;i<COL;i++) { printf("┼ "); } printf("┤\n"); } // bottom
printf("└ "); for(i=0;i<COL;i++) { printf("┴ "); } printf("┘\n"); } void
playgame() { int arr[ROW][COL]={0}; // initialization 0- No chess pieces 1 User playing chess black 2 Computer chess white int x,y;
// User entered lines column int row,col; // The position of the drop int i,j; // rule Judge whether to win or lose Traversing a two-dimensional array paintGameUI(); //
Draw chessboard while(1)// Ensure multiple Ensure that the user's computer is effectively placed for many times { while(1) // Ensure that users can effectively settle down once Users play chess {
gotoxy(0,16);// Print text 0 from left 16 Distance from top printf(" "); // Overwrite the input before the user ,// Allow users to enter again
gotoxy(0,16); // Print text 0 from left 16 Distance from top printf(" Please enter the position of playing chess , for example :(2 3):"); scanf("%d
%d",&x,&y); row = x-1; // that 's ok Length from top Array is 0 Started col =y-1; // column Length from left Position of playing chess
if(arr[row][col]==0) // Is the chess position a piece { gotoxy(col*2,row); printf("●");// Users play chess
// Modify the value of a two-dimensional array arr[row][col]=1; // Users play chess for(i=0;i<ROW;i++) { for(j=0;j<COL;j++) {
// All five are horizontal 1 Columnar variation if( arr[i][j]==arr[i][j+1]&&arr[i][j]==arr[i][j+2]&&
arr[i][j]==arr[i][j+3]&&arr[i][j]==arr[i][j+4]&&j+4<=COL&&arr [i][j]==1) {
gotoxy(0,18); printf(" User wins \n"); return ; }// All five are upright 1 Line change else
if(arr[i][j]==arr[i+1][j]&&arr[i][j]==arr[i+2][j]&&
arr[i][j]==arr[i+3][j]&&arr[i][j]==arr[i+4][j]&&i+4<=ROW&&arr [i][j]==1) {
gotoxy(0,18); printf(" User wins \n"); return ; } } } break; // It's their turn to play chess } else
if(arr[row][col]==1||arr[row][col]==2) // Users play chess 1 Computer chess 2 { gotoxy(0,17);
printf(" Please re-enter the coordinates "); } } while(1) // Ensure that the computer is effectively placed once Computer chess { // Computer chess Computer random drop row=
rand()%14+0; // Two dimensional array index 0-13 col= rand()%14+0; // Computer random drop // Row distance top row Column distance left col
if(arr[row][col]==0) // Is the chess position a piece empty Computers can play chess { gotoxy(col*2,row); printf("○");// play chess
// Modify the value of a two-dimensional array arr[row][col]=2; // Computer chess for(i=0;i<ROW;i++) // Computer wins Better use algorithm {
for(j=0;j<COL;j++) { // All five are horizontal 1 Columnar variation if(
arr[i][j]==arr[i][j+1]&&arr[i][j]==arr[i][j+2]&&
arr[i][j]==arr[i][j+3]&&arr[i][j]==arr[i][j+4]&&j+4<=COL&&arr [i][j]==2) {
gotoxy(0,18); printf(" The computer won \n"); return; }// All five are upright 1 else
if(arr[i][j]==arr[i+1][j]&&arr[i][j]==arr[i+2][j]&&
arr[i][j]==arr[i+3][j]&&arr[i][j]==arr[i+4][j]&&i+4<=ROW&&arr [i][j]==2) {
gotoxy(0,18); printf(" The computer won \n"); return; } } } break; } } } } int main() {
playgame(); return 0; }

Technology
Daily Recommendation