C-language mini-games _ Three games

Source: Internet
Author: User

C-language mini-games _ Three games
Game Rules: players take one step and computers take one step. Of course, you wrote the computer code.

#include <stdio.h>
#include <stdlib.h>
/ ************************************************* ****************************** /
// Function function: Initialize the chessboard
/ ************************************************* ****************************** /
void init_chess (char (* chess_arr) [3])
{
    int i, j;
    printf ("Game started: \ n");

    for (i = 0; i <3; i ++)
    {
        for (j = 0; j <3; j ++)
        {
            * (* (chess_arr + i) + j) = '';
        }
    }
    for (i = 0; i <3; i ++)
    {
        printf ("% c |% c |% c", chess_arr [i] [0], chess_arr [i] [1], chess_arr [i] [2]);
        if (i! = 2)
        {
            printf ("\ n --- | --- | --- \ n");
        }
    }
    printf ("\ n");
}
/ ************************************************* ****************************** /
// Function function: The player enters the coordinates to play chess
/ ************************************************* ****************************** /
void input_player_xy (char (* chess_arr) [3], int x, int y)
{
    int i = 0;
    if (chess_arr [x] [y] == '')
    {
        chess_arr [x] [y] = 'Y';
    }
    else
    {
        printf ("This step to pass \ n");
    }
    for (i = 0; i <3; i ++)
    {
        printf ("% c |% c |% c", chess_arr [i] [0], chess_arr [i] [1], chess_arr [i] [2]);
        if (i! = 2)
        {
            printf ("\ n --- | --- | --- \ n");
        }
    }
    printf ("\ n");
}
/ ************************************************* ****************************** /
// Function function: print the coordinates of chess on the computer
/ ************************************************* ****************************** /
void input_computer_xy (char (* chess_arr) [3])
{
    int i = 0;
    for (i = 0; i <3; i ++)
    {
        printf ("% c |% c |% c", chess_arr [i] [0], chess_arr [i] [1], chess_arr [i] [2]);
        if (i! = 2)
        {
            printf ("\ n --- | --- | --- \ n");
        }
    }
    printf ("\ n");
}
/ ************************************************* ****************************** /
// Function function: determine the coordinates of the player playing chess
/ ************************************************* ****************************** /
void input_player (char (* chess_arr) [3])
{
    int x = 0, y = 0;
    printf ("Please play your chess:");
    scanf ("% d% d", & x, & y);
    input_player_xy (chess_arr, x, y);

}
/ ************************************************* ****************************** /
// Function function: determine the coordinates of the computer to play chess
/ ************************************************* ****************************** /
void input_computer (char (* chess_arr) [3])
{
    int i = 0, j = 0;
    for (i = 0; i <3; i ++)
    {
        for (j = 0; j <3; j ++)
        {
            if (chess_arr [i] [j] == '')
            {
              chess_arr [i] [j] = 'O';
              goto flag;
            }
        }
    }
flag:
    input_computer_xy (chess_arr);
}
/ ************************************************* ****************************** /
// Function function: The main function determines whether the computer or the player wins
/ ************************************************* ****************************** /
int main ()
{
    char chess_arr [3] [3];
    int flag = 1;
     
    init_chess (chess_arr);
    while (flag)
    {
        int i = 0, j = 0;
        int diag_flag_player = 0, diag_flag_computer = 0, count = 0;
        input_player (chess_arr);
        input_computer (chess_arr);
        for (i = 0; i <3; i ++)
        {
            if (((chess_arr [i] [0] == 'Y') && (chess_arr [i] [1] == 'Y') && (chess_arr [i] [2] == 'Y')) ||
                 ((chess_arr [0] [i] == 'Y') && (chess_arr [1] [i] == 'Y') && (chess_arr [2] [i] == 'Y'))
              )
            {
                printf ("player win! \ n");
                flag = 0;
                break;
            }
            if (((chess_arr [i] [0] == 'O') && (chess_arr [i] [1] == 'O') && (chess_arr [i] [2] == 'O')) ||
                      ((chess_arr [0] [i] == 'O') && (chess_arr [1] [i] == 'O') && (chess_arr [2] [i] == 'O'))
                   )
            {
                printf ("computer win! \ n");
                flag = 0;
                break;
            }
            for (j = 0; j <3; j ++)
            {
                if (chess_arr [i] [j]! = '')
                {
                    count ++;
                }
            }
            if (chess_arr [i] [i] == 'Y')
            {
                diag_flag_player ++;
            }
            if (chess_arr [i] [2-i] == 'O')
            {
                diag_flag_computer ++;
            }
        }
        if (diag_flag_player == 3)
        {
            flag = 0;
            printf ("player win! \ n");
        }
        if (diag_flag_computer == 3)
        {
            flag = 0;
            printf ("computer win! \ n");
        }
        if (count == 9)
        {
            flag = 0;
            printf ("player equal computer! \ n");
        }
    }
    system ("pause");
    return 0;
} 

 

As you can see clearly, it is not stored in multiple files.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.