Today’s record is gobang small game and C programming homework

C language final programming homework has already been arranged, but because of the final things morelazyIn fact, I have written two small games before, but I want to write something new with LQL Ju ju, so we have been thinking about what to write.

Then in the space of a week, we have experienced

Lazy is the original sin

We ended up with a very simple backgammon game, and not just becausefood, but also because the recent study task is more heavy, including the arrival of the final exam, busy with, also includingAn algorithmLearning tasks, such as I am now onDPI still look confused, andCheck and set,binary,search… … So forgive yourself.

The following effect map effect map and source code source code:







The code implementation, in fact, istwoMain parts:

1. Interface design

2. Call the winner

Source code:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
char mp[15] [15];
const int Len = 15;
const int win_count = 5;
bool Judge(const char map[15] [15].char a)// Determine if the game is over
{
    // printf("## %c\n", a);
    for (int i = 0; i < Len; i++)
    {
        for (int j = 0; j < Len; j++)
        {
            if (map[i][j] == a)
            {
                int count_a = 1;
                / / 1. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                // Vertical direction
                for (int x = i - 1, times = 0; x >= 0; x--, times++)
                {
                    if (map[x][j] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int y = i + 1, times = 0; y < Len; y++, times++)
                {
                    if (map[y][j] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                / / 2. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                / / transverse direction
                count_a = 1;
                for (int x = j - 1, times = 0; x >= 0; x--, times++)
                {
                    if (map[i][x] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int y = j + 1, times = 0; y < Len; y++, times++)
                {
                    if (map[i][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                / / 3. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                // go up and down
                count_a = 1;
                for (int x = i - 1, y = j - 1, times = 0; i >= 0 && j >= 0; x--, y--, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int x = i + 1, y = j + 1, times = 0; i < Len && j < Len; x++, y++, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                / / 4. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
                // go up
                count_a = 1;
                for (int x = i + 1, y = j - 1, times = 0; x < Len && y >= 0; x++, y--, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int x = i - 1, y = j + 1, times = 0; x >= 0 && y < Len; x--, y++, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                //-------------------------------------------------------------------}}}return false;
}
void f(a)// Print the game rules
{
     printf(" --------------------------------------------------------\n");
    printf(" | _________________________________ |\n");
    printf(" | | The Game will begin!!! | |\n");
    printf(" | |_______________________________| |\n");
    printf("The rules of the game * | | * \ n");
    printf("| please both sides players take turns to put pieces of input coordinates (separated by Spaces) | \ n");
    printf("| black (@) is: @ | \ n");
    printf("| | white (O) is: O \ n");
    printf("Put | | black first \ n");
    printf(" ---------------------------------------------------------\n");
}
int main(a)
{
    f(a);for (int i = 10; i >= 1; i--)Sleep(1000), printf("%2d seconds to start game \n", i);
        system("cls");
        for (int i = 0; i <= 15; i++)
            for (int j = 0; j <= 15; j++)
                mp[i][j] = ' ';
    // Print the result (debug)
    printf("");
    for (int i = 1; i <= 15; i++)
        printf("%-4d", i);
    printf("\n");
    printf(" ____________________________________________________________\n");
    for (int i = 0; i < 15; i++)
    {
        if (i >= 0)
            printf("%-3d", i + 1);
        else
            printf("");
        printf("|");

        for (int j = 0; j < 15; j++)
            printf(" %-2c|", mp[i][j]);
        printf("\n ____________________________________________________________\n");
    }
    printf("\n\n");

    int heng, zong, flag = 0;
    printf("Please enter black (@) coordinates :\n");
    mp[15] [15] = ' ';
    while (scanf("%d%d", &zong, &heng))
    {
        / / input
        // printf("%"
        // system("cls");)
        while (heng < 1 || zong < 1 || heng > Len || zong > Len || mp[zong- 1][heng- 1] != ' ')
        {
// printf("h=%d z=%d l=%d %c\n", heng, zong, Len, mp[zong][heng]);
            printf("Input error,input again!!! \n");
            if (flag % 2= =0)
                printf("Please enter black (@) coordinates :\n");
            else
                printf("Please enter white (O) coordinates :\n");
            scanf("%d%d", &zong, &heng);
        }
        cout << "Input success!!! \n";
        system("cls");
        if (flag % 2= =0)
            mp[zong - 1][heng - 1] = The '@';
        else
            mp[zong - 1][heng - 1] = 'O';
        // Print a new map
        printf("");
        for (int i = 1; i <= 15; i++)
            printf("%-4d", i);
        printf("\n");
        printf(" ____________________________________________________________\n");
        for (int i = 0; i < 15; i++)
        {
            if (i >= 0)
                printf("%-3d", i + 1);
            else
                printf("");
            printf("|");

            for (int j = 0; j < 15; j++)
                printf(" %-2c|", mp[i][j]);

            printf("\n ____________________________________________________________\n");
        }
        printf("\n\n");
        // Decide which side wins
        if (flag % 2= =0)
        {
            if (Judge(mp, The '@'))
            {
                printf("Black wins!! \n");
                break; }}else
        {
            if (Judge(mp, 'O'))
            {
                printf("White wins!! \n");
                break;
            }
        }
        flag++;
        if (flag % 2= =0)
            printf("Please enter black (@) coordinates :\n");
        else
            printf("Please enter white (O) coordinates :\n");
    }
    printf("Game over!! \n");
    char p[100];
    gets(p);
    return 0;
}
Copy the code

Look at the high number ~ slip slip slip