How to use C language to realize the game of three chess? By accepting this source code, Xiaobai can learn it!

I wonder if you have ever played a three-middle chess game? Today, I will share how the C language implements the three-in-one chess game for your reference. The specific content is as follows.

 

First, we start with the main function to analyze the overall logic of the game

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("Exit the game\n");

  break;

default:

  printf("The input is wrong, please try again!\n");

  break;

}

}while(input);

return 0;

}

To realize the overall logic through a do...while loop, the game menu interface must first be displayed, that is, the menu menu() function must be written:

void menu()

{

printf("************************************\n");

printf("******** 1.play  0.exit *********\n");

printf("************************************\n");

}

After the display, the value entered by the user is used to determine whether the user wants to play the game or exit the game. Use switch to achieve this: when the user enters 1, it means playing the game, entering 0 means exiting the game, and entering other numbers will prompt an input error. re-enter!

The next step is to write the game() function:

void game()

char board[ROW][COL] = {0};

char result='N';

initBoard(board,ROW,COL);

display(board,ROW,COL);

while(1)

{

playerMove(board,ROW,COL);

display(board,ROW,COL);

result=judge(board,ROW,COL);

if (result != 'N')

{

  break;

}

computerMove(board,ROW,COL);

display(board,ROW,COL);

result=judge(board,ROW,COL);

if (result != 'N')

{

  break;

}

}

switch(result)

{

case '*':

printf("Congratulations, you won!!!\n");

break;

case '#':

printf("Unfortunately, the computer won!!!\n");

break;

case 'E':

printf("You and the computer are in a tie!!!\n");

break;

default:

break;

}

}

First enter the game, to initialize the board interface and show it to the user, that is, write the initBoard() function and display() function:

void initBoard(char board[ROW][COL],int row,int col)

{

int i = 0;

for(; i<row; i++)

{

int j=0;

for(; j <col; j++)

{

  board[i][j]=' ';

}

}

}

void display(char board[ROW][COL],int row,int col)

{

int i = 0;

for(; i<row; i++)

{

int j=0;

//Print data

for(; j<col; j++)

{

  printf(" %c ",board[i][j]);

  if(j<col-1)

  printf("|");

}

printf("\n");

//Print split line

if(i<row-1)

{

  for(j=0; j<col;j++)

  {

  printf("---");

  if(j<row-1)

  {

  printf("|");

  }

  }

  printf("\n");

}

}

}

The next step is to enter the loop to realize the logic of the user and the computer playing chess, using the playerMove() function and computerMove() function to achieve:

void playerMove(char board[ROW][COL],int row,int col)

{ int x=0;

int y=0;

printf("The player goes:>");

while(1)

{

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("The coordinates are occupied, please re-enter!\n");

}

}

else

{

printf("Illegal coordinates, please re-enter");

}

}

}

void computerMove(char board[ROW][COL],int row,int col)

{

printf("Computer walk:>\n");

while(1)

{

int x = rand() % row;

int y = rand() % col;

if(board[x][y]==' ')

{

  board[x][y] = '#';

  break;

}

}

}

At this time, it should be noted that every time the user or the computer finishes a step, the chessboard should be displayed and the winner or loser should be judged. Therefore, a judge() function should be written:

char judge(char board[ROW][COL],int row,int col)

{

int i=0;

int j=0;

for(;i<row;i++)

{

if(board[i][0]!=' ' && board[i][0]==board[i][1] && board[i][1]==board[i][2])

{

  return board[i][0];

}

}

for(;j<col;j++)

{

if(board[0][j]!=' ' && board[0][j]==board[1][j] && board[1][j]==board[2][j])

{

  return board[0][j];

}

}

if(board[0][0]!=' ' && board[0][0]==board[1][1] && board[1][1]==board[2][2])

{

return board[0][0];

}

if(board[0][2]!=' ' && board[0][2]==board[1][1] && board[1][1]==board[2][1])

{

return board[0][2];

}

for (i = 0; i < row; i++)

{

int j = 0;

for (; j < col; j++)

{

  if (board[i][j] == ' ')

  {

  return 'N';

  }

}

}

return 'E';

}

The condition for judging win or loss is that the three chess pieces are connected in a straight line to be a win. If at the end the user and the computer play the board full and there is no victory or defeat, it is judged as a tie.

Finally, in the game function, the return value of the judge() function is judged by switch, whether the player wins, the computer wins, or the tie.

Note: When passing the array as a parameter, you must remember to pass the size of the array. If you don’t pass the size of the array, use sizeof(arr)/sizeof when calculating the size of the array in the new function. (arr[0]) will have a problem, because the array will be reduced to a pointer when passed as a parameter. The size of the pointer in a 32-bit system is 4 bytes. Assuming that the passed is an int type array, sizeof( arr)/sizeof(arr[0])=1, we can't implement the desired function according to normal logic.

The above is a detailed introduction to the realization of the three chess game in C language, and I hope it will be helpful to everyone's learning.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112822813