不能在我的函数中打印任何东西

时间:2013-11-11 06:15:08

标签: c

我正在为大学的计算机编程课程制作一个tic tac toe游戏。我有几行需要在程序中打印3次。我为此做了一个函数,我调用它并没有得到任何错误,但问题是函数没有运行。它只有几行可以打印,但它没有这样做。请通过阅读源文件来查看代码: C来源:https://dl.dropboxusercontent.com/u/62524851/Untitled1.c 该程序的exe文件:https://dl.dropboxusercontent.com/u/62524851/Untitled1.exe

#include <stdio.h>
#include <stdlib.h>

void text(char layout[9], int guide[9])
{
    system("cls");
    printf("\n\n\t\t      ----------Tic tac toe----------");
    printf("\n\nEnter number of the column you want to make your move in.\nNumbers of columns are shown below:\nPress 10 to quit.\n\n");
    printf("\t\t\t%d\t%d\t%d", guide[0], guide[1], guide[2]);
    printf("\n\n\t\t\t%d\t%d\t%d", guide[3], guide[4], guide[5]);
    printf("\n\n\t\t\t%d\t%d\t%d\n\n", guide[6], guide[7], guide[8]);
    printf("\t\t\t%c\t%c\t%c", layout[0], layout[1], layout[2]);
    printf("\n\n\t\t\t%c\t%c\t%c", layout[3], layout[4], layout[5]);
    printf("\n\n\t\t\t%c\t%c\t%c\n\n", layout[6], layout[7], layout[8]);
}

void main(void)
{
    int guide[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, user, user2, x, i;
    char layout[9] = {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, c, name2[8], a[8];
    do
    {
        system("cls");
        printf("\n\n\t\t      ----------Tic tac toe----------");
        printf("\n\t\t      By Muhammad Saad Masroor. 23507");
        printf("\n\n\n\n\n\n\n\n\n\n\n\nPress c to continue");
        c = getch();
    } while (c != 'c');
    system("cls");
    printf("\n\n\t\t      ----------Tic tac toe----------\n\n");
    printf("With whome do you want to play?\n\nA. Computer\nB. Friend\n");
    c = getch();
    puts("Enter your name: ");
    gets(a);
    if (c == 'a')
    {
        system("cls");
        printf("\n\n\n\n\n\n\n\n\t\t\t          Lets play!");
        printf("\n\n\t\t          Between %s and Computer", a);
        getch();
    }
    else if (c == 'b')
    {
        puts("Enter your friend's name: ");
        gets(name2);
        system("cls");
        printf("\n\n\n\n\n\n\n\n\t\t\t          Lets play!");
        printf("\n\n\t\t            Between %s and %s", a, name2);
        getch();
    }
    while (x != 10)
    {
        void text(char layout[9], int guide[9]);

        printf("%s's turn", a);
        scanf("%d", &user);
        if (user == user2)
        {
            printf("Invalid input: %d, enter another number", user);
            scanf("%d", &user);
        }
        switch (user)
        {
        case (1):
            layout[0] = 'X';
            break;

        case (2):
            layout[1] = 'X';
            break;

        case (3):
            layout[2] = 'X';
            break;

        case (4):
            layout[3] = 'X';
            break;

        case (5):
            layout[4] = 'X';
            break;

        case (6):
            layout[5] = 'X';
            break;

        case (7):
            layout[6] = 'X';
            break;

        case (8):
            layout[7] = 'X';
            break;

        case (9):
            layout[8] = 'X';
            break;

        case (10):
            x = 10;
            printf("You quit");
            break;

        default:
            printf("Invalid input");
            getch();
            break;
        }

        void text(char layout[9], int guide[9]);

        // User wins
        if (layout[0] == 'X' && layout[1] == 'X' && layout[2] == 'X' ||
            layout[3] == 'X' && layout[4] == 'X' && layout[5] == 'X' ||
            layout[6] == 'X' && layout[7] == 'X' && layout[8] == 'X' ||
            layout[0] == 'X' && layout[3] == 'X' && layout[6] == 'X' ||
            layout[1] == 'X' && layout[4] == 'X' && layout[7] == 'X' ||
            layout[2] == 'X' && layout[5] == 'X' && layout[8] == 'X' ||
            layout[0] == 'X' && layout[4] == 'X' && layout[8] == 'X' ||
            layout[2] == 'X' && layout[4] == 'X' && layout[6] == 'X')

        {
            printf("%s wins!\n", a);
            x = 10;
            break;
        }
        else if (user == 10)
        {
            break;
        }
        else if (c == 'b')
        {
            printf("%s's turn", name2);
            scanf("%d", &user2);
        }
        if (user2 == user)
        {
            printf("Invalid input: %d, enter another number", user);
            scanf("%d", &user2);
        }
        if (user2 == 10)
        {
            printf("You quit");
            getch();
            x = 10;
        }
        else if (c == 'a')
        {
            user2 = rand()%(9);
            if (user2 == user)
            {
                user2 = rand()%(9);
            }
            printf("Computer's turn: %d", user2);
        }
        switch (user2)
        {
        case (1):
            layout[0] = 'O';
            getch();
            break;

        case (2):
            layout[1] = 'O';
            break;

        case (3):
            layout[2] = 'O';
            break;

        case (4):
            layout[3] = 'O';
            break;

        case (5):
            layout[4] = 'O';
            break;

        case (6):
            layout[5] = 'O';
            break;

        case (7):
            layout[6] = 'O';
            break;

        case (8):
            layout[7] = 'O';
            break;

        case (9):
            layout[8] = 'O';
            break;

        default:
            printf("Invalid input");
            break;
        }

        void text(char layout[9], int guide[9]);

        // Computer/2nd user wins
        if (layout[0] == 'O' && layout[1] == 'O' && layout[2] == 'O' ||
            layout[3] == 'O' && layout[4] == 'O' && layout[5] == 'O' ||
            layout[6] == 'O' && layout[7] == 'O' && layout[8] == 'O' ||
            layout[0] == 'O' && layout[3] == 'O' && layout[6] == 'O' ||
            layout[1] == 'O' && layout[4] == 'O' && layout[7] == 'O' ||
            layout[2] == 'O' && layout[5] == 'O' && layout[8] == 'O' ||
            layout[0] == 'O' && layout[4] == 'O' && layout[8] == 'O' ||
            layout[2] == 'O' && layout[4] == 'O' && layout[6] == 'O')

        {
            if (c == 'a')
            {
                printf("Computer wins!\n");
                getch();
                x = 10;
                break;
            }
            else if (c == 'b')
            {
                printf("%s wins!", name2);
                getch();
                x = 10;
                break;
            }
        }
    }
}

与原始代码相比,代码重新缩进并稍微重新格式化。

1 个答案:

答案 0 :(得分:1)

在继续之前,请输入以下代码:

switch (user)
{
case (1):
    layout[0]='X';
    break;
...

应替换为:

layout[user-1] = 'X';

在进行分配之前,您需要验证移动是否有效:

if (user == 10)
    ...report quit and break loop...
else if (user <= 0 || user > 10)
    ...report error and collect new number...

并且使用40多行来重复该模因,其中5个就足够了。代码中还有其他明显的重复,最好避免使用。


作为您真正的问题,麻烦的是您没有在任何地方调用函数text()。你刚刚多次宣布它。

因此,例如,在main()中,您有:

while (x != 10)
{
    void text(char layout[9], int guide[9]);
    printf("%s's turn", a);
    scanf("%d", &user);

这再次声明text(),但不会调用它。你需要这样的东西:

while (x != 10)
{
    text(layout, guide);
    printf("%s's turn", a);
    scanf("%d", &user);