当前位置:网站首页>C language three chess games

C language three chess games

2022-06-11 07:31:00 ∞ big understand

Gobang games

Beginners C Language , We can play some games , It can improve our understanding of c The understanding of the , It can also enhance our practical ability . Next, let's finish this little game together : Sanzi .

Sanzi chess must have a 3*3 The chessboard of , There must be pieces on this chessboard , Then the player takes a step , The computer takes one step , We still need to judge whether we win or lose at each step .

The above is the basic logic of Sanzi chess , Now let's do it step by step .

  1. Initialize chessboard

    When printing a chessboard , The pieces on the chessboard cannot be random , What we want is a clean chessboard , It's not finished yet . So first initialize the chessboard .

    void chushihua(char borad[ROW][ROL], int row, int rol)
    {
          
    	for (int i = 0; i < row; i++)
    	{
          
    		for (int j = 0; j < rol; j++)
    		{
          
    			borad[i][j] = ' ';
    		}
    	}
    }
    

    In this way, our chess pieces are completely empty .

  2. Print chessboard

    The chess pieces on the chessboard have been initialized , We need a function to print out the chessboard , Subsequent matches , Every step , You also need to print out the chessboard .

    void daying(char borad[ROW][ROL],int row, int rol)
    {
          
    	for (int i = 0; i < row; i++)
    	{
          
    		for (int j = 0; j < rol; j++)
    		{
          
    			printf(" %c ", borad[i][j]);
    			if (j < rol - 1)
    				printf("|");
    			
    		}
    		printf("\n");
    		if (i < row - 1)
    		{
          
    			for (int j = 0; j < rol; j++)
    			{
          
    				printf("---");
    				if (j < rol - 1)
    					printf("|");
    			}
    		}
    		printf("\n");
    	}
    	  
    }
    
  3. Players go

    The chessboard has , It's almost chess , So players take one step , Then print out the chessboard . We stipulate that the chess pieces that players take are ’#’.

    void playermove(char borad[ROW][ROL], int row, int rol)
    {
          
    	printf(" Please enter the coordinates , The coordinates are left blank \n");
    	int a = 0; int b = 0;
    	again:
    	scanf("%d%d", &a, &b);
    	while (1)
    	{
          
    		if (a <= 3 && b <= 3)
    		{
          
    			if (borad[a - 1][b - 1] == ' ')
    			{
          
    				borad[a - 1][b - 1] = '*';
    				break;
    			}
    			else
    			{
          
    				printf(" The coordinates have been occupied , Please re-enter \n");
    				goto again;
    			}
    		}
    		else
    		{
          
    			printf(" The coordinates are wrong , Please re-enter \n");
    			goto again;
    		}
    	}
    
    }
    
  4. Computer go

    The computer is a random number , No more coordinates , So the code is as follows :

    void computermove(char borad[ROW][ROL], int row, int rol)
    {
          
    	int x = 0; int y = 0;
    	printf(" Computer go :\n");
    	
    	while (1)
    	{
          
    		x = rand() % row;
    		y = rand() % rol;
    		if (borad[x][y] == ' ')
    		{
          
    			borad[x][y] = '#';
    			break;
    		}
    
    	}
    }
    
  5. Judgement of winning or losing

    The logic of judging whether to win or lose is , Three rows or three columns or three diagonals will be the winner .

    If the above conditions are not met , And it's full , So it's a draw .

    also , Just keep playing chess .

    The code is as follows :

    char iswin(char borad[ROW][ROL], int row, int rol)
    {
          
    	// Judgment line 
    	for (int i = 0; i < row; i++)
    	{
          
    		if (borad[i][0] != ' '&&borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2])
    			return borad[i][0];
    	}
    	// Judgment column 
    	for (int i = 0; i < row; i++)
    	{
          
    		if (borad[0][i] != ' '&&borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i])
    			return borad[0][i];
    	}
    	// Judging diagonals 
    	if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ')
    		return borad[0][0];
    	if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ')
    		return borad[0][2];
    	// It ends in a draw 
    	if (isfull(borad,ROW,ROL))
    		return 'Q';
    	// continue 
    	return 'C';
    	
    }
    

    iswin Function returns ’C‘ Is that the game continues , The return is ’#‘ Computers win , return ’*‘ Game player wins , return ’Q‘ It's a draw . So you also need to have a variable that receives the return value ret.

    if (ret == '*')
    	{
          
    		printf(" Game player wins \n");
    	}
    	 if (ret == '#')
    	{
          
    		printf(" Computers win \n");
    	}
    	 if (ret == 'Q')
    		printf(" It ends in a draw \n");
    

    Judge whether you win or lose , This game has been basically completed .

    The following is the code of the game :

    The header file :

    #pragma once
    #include<stdio.h>
    #define ROW 3
    #define ROL 3
    #include<stdlib.h>
    #include<time.h>
    void mune();
    void chushihua(char borad[ROW][ROL], int row, int rol);
    void daying(char borad[ROW][ROL], int row, int rol);
    void game();
    void playermove(char borad[ROW][ROL], int row, int rol);
    void computermove(char borad[ROW][ROL], int row, int rol);
    char  iswin(char borad[ROW][ROL], int row, int rol);
    
    

    Sanziqi game code :

    #include"game.h"
    void mune()
    {
          
    	printf("*******************************************\n");
    	printf("****************** Sanzi *******************\n");
    	printf("****************** Please put in a coin :******************\n");
    	printf("****************** ******************\n");
    	printf("****************** ******************\n");
    }
    void chushihua(char borad[ROW][ROL], int row, int rol)
    {
          
    	for (int i = 0; i < row; i++)
    	{
          
    		for (int j = 0; j < rol; j++)
    		{
          
    			borad[i][j] = ' ';
    		}
    	}
    }
    void daying(char borad[ROW][ROL],int row, int rol)
    {
          
    	for (int i = 0; i < row; i++)
    	{
          
    		for (int j = 0; j < rol; j++)
    		{
          
    			printf(" %c ", borad[i][j]);
    			if (j < rol - 1)
    				printf("|");
    			
    		}
    		printf("\n");
    		if (i < row - 1)
    		{
          
    			for (int j = 0; j < rol; j++)
    			{
          
    				printf("---");
    				if (j < rol - 1)
    					printf("|");
    			}
    		}
    		printf("\n");
    	}
    	  
    }
    void playermove(char borad[ROW][ROL], int row, int rol)
    {
          
    	printf(" Please enter the coordinates , The coordinates are left blank \n");
    	int a = 0; int b = 0;
    	again:
    	scanf("%d%d", &a, &b);
    	while (1)
    	{
          
    		if (a <= 3 && b <= 3)
    		{
          
    			if (borad[a - 1][b - 1] == ' ')
    			{
          
    				borad[a - 1][b - 1] = '*';
    				break;
    			}
    			else
    			{
          
    				printf(" The coordinates have been occupied , Please re-enter \n");
    				goto again;
    			}
    		}
    		else
    		{
          
    			printf(" The coordinates are wrong , Please re-enter \n");
    			goto again;
    		}
    	}
    
    }
    void computermove(char borad[ROW][ROL], int row, int rol)
    {
          
    	int x = 0; int y = 0;
    	printf(" Computer go :\n");
    	
    	while (1)
    	{
          
    		x = rand() % row;
    		y = rand() % rol;
    		if (borad[x][y] == ' ')
    		{
          
    			borad[x][y] = '#';
    			break;
    		}
    
    	}
    }
    int isfull(char borad[ROW][ROL], int row, int rol)
    {
          
    	for (int i = 0; i < row; i++)
    	{
          
    		for (int j = 0; j < rol; j++)
    		{
          
    			if (borad[i][j] == ' ')
    				return 0;
    		}
    	}
    	return 1;
    }
    char iswin(char borad[ROW][ROL], int row, int rol)
    {
          
    	// Judgment line 
    	for (int i = 0; i < row; i++)
    	{
          
    		if (borad[i][0] != ' '&&borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2])
    			return borad[i][0];
    	}
    	// Judgment column 
    	for (int i = 0; i < row; i++)
    	{
          
    		if (borad[0][i] != ' '&&borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i])
    			return borad[0][i];
    	}
    	// Judging diagonals 
    	if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ')
    		return borad[0][0];
    	if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ')
    		return borad[0][2];
    	// It ends in a draw 
    	if (isfull(borad,ROW,ROL))
    		return 'Q';
    	// continue 
    	return 'C';
    	
    }
    void game()
    {
          
    	char ret = 0;
    	char borad[ROW][ROL];
    	// Initialize chessboard 
    	chushihua(borad, ROW, ROL);
    	// Print chessboard 
    	daying(borad, ROW, ROL);
    	while (1)
    	{
          
    		// Players play chess 
    		playermove(borad, ROW, ROL);
    		daying(borad, ROW, ROL);
    		// Judgement of winning or losing 
    		ret=iswin(borad,ROW,ROL);
    		if (ret != 'C')
    		{
          
    			break;
    		}
    		// The computer plays chess 
    		computermove(borad, ROW, ROL);
    		daying(borad, ROW, ROL);
    		// Judgement of winning or losing 
    		ret=iswin(borad,ROW,ROL);
    		if (ret != 'C')
    		{
          
    			break;
    		}
    	}
    	if (ret == '*')
    	{
          
    		printf(" Game player wins \n");
    	}
    	 if (ret == '#')
    	{
          
    		printf(" Computers win \n");
    	}
    	 if (ret == 'Q')
    		printf(" It ends in a draw \n");
    }
    

    Total program code :

    #include"game.h"
    int main()
    {
            
    	srand((unsigned int)time(NULL));
    	int n = 0;
    	mune();
    	again:
    	scanf("%d", &n);
    	while (n)
    	{
          
    		game();
    		n--;
    	}
    	printf(" Whether the game continues ? Please put in a coin \n");
    	goto again;
    	return 0;
    }
    

    That's what this issue is about .

原网站

版权声明
本文为[∞ big understand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020520335013.html