C语言 字符版 五子棋

以前写的,因为准备刷机,所以把代码粘上来了……

一共两版……虽然……哪个都不好……

 

#include <stdio.h>

void start(void);                        //初始化棋盘
void player(char * name, int * coordi);    //提示选手下棋并获取坐标
int valid (int * coordi);                //判断所下棋子的位置是否有效
void qipan(void);                        //输出棋盘的函数
int over (int x, int y);                //判断游戏是否结束,输赢平

static int chessboard[19][19];            //用二维数组来表示棋盘,全部初始化为0

int main (void)
{
    //选手名字
    char name1[50];
    char name2[50];
    //记录两个选手下棋的坐标
    int player1[2];
    int player2[2];
    char conti = 'y';
    system ("title 简易五子棋");
    system ("mode con cols=78 lines=26");
    system ("color F0");
    while (conti == 'y' || conti == 'Y')
    {
        start ();
        qipan ();
        //录入信息(选手名字)
        printf ("请输入第一个选手的名字:");
        gets (name1);
        printf ("请输入第二个选手的名字:");
        gets (name2);
        while (1)
        {
            //录入信息(所下棋子坐标)
            player (name1, player1);
            //一号选手所下位置标记为1
            chessboard[player1[0] - 1][player1[1] - 1] = 1;
            qipan();
            if(1 == over (player1[0] - 1, player1[1] - 1))
                break;
            player(name2, player2);
            //二号标记为2
            chessboard[player2[0] - 1][player2[1] - 1] = 2;
            qipan();
            if(1 == over (player2[0] - 1, player2[1] - 1))
                break;
        }
        printf ("是否继续下一局游戏呢?(y继续,其余键退出。):");
        scanf ("%c", &conti);
        while (getchar() != '\n')
            continue;
    }
    printf ("欢迎下次继续,拜拜~\n");
    return 0;
}
//输出棋盘的函数
void qipan(void)
{
    int i,j;
    system("CLS");
    printf ("当前棋盘为下图示:\n");
    printf ("y\\x1 2 3 4 5 6 7 8 9 10111213141516        |\n");
    for (i = 0; i < 16; i++)
    {
        if (i < 9)
            printf ("%d. ", i + 1);
        else
            printf ("%d ", i + 1);
        for (j = 0; j < 16; j++)
        {
            if (chessboard[i][j] == 1)
            {
                if (i == 3 && j == 15)
                printf ("●        |欢迎使用简易五子棋");
                else if (i == 5 && j == 15)
                printf ("●        |目前只支持人人对战");
                else if (i == 7 && j == 15)
                printf ("●        |且对战时双方轮流输入坐标");
                else if (i == 9 && j == 15)
                printf ("●        |由于作者水平有限,制作粗糙");
                else if (i == 11 && j == 15)
                printf ("●        | 敬请见谅");
                else if (j == 15)
                printf ("●        |");
                else 
                    printf ("");
            }
            else if (chessboard[i][j] == 2)
                {
                if (i == 3 && j == 15)
                printf ("○        | 欢迎使用简易五子棋");
                else if (i == 5 && j == 15)
                printf ("○        | 目前只支持人人对战");
                else if (i == 7 && j == 15)
                printf ("○        | 且对战时双方轮流输入坐标");
                else if (i == 9 && j == 15)
                printf ("○        | 由于作者水平有限,制作粗糙");
                else if (i == 11 && j == 15)
                printf ("○        | 敬请见谅");
                else if (j == 15)
                printf ("○        |");
                else 
                    printf ("");
            }
            else if (i == 0 && j == 0)
                printf ("");
            else if (i == 15 && j == 0)
                printf ("");
            else if (i == 0 && j == 15)
                printf ("┓        |");
            else if (i == 15 && j == 15)
                printf ("┛        |");
            else if (i == 0)
                printf ("");
            else if (i == 15)
                printf ("");
            else if (j == 0)
                printf ("");
            else if (j == 15)
            {
                if (i == 3)
                printf ("┨        | 欢迎使用简易五子棋");
                else if (i == 5)
                printf ("┨        | 目前只支持人人对战");
                else if (i == 7)
                printf ("┨        | 且对战时双方轮流输入坐标");
                else if (i == 9)
                printf ("┨        | 由于作者水平有限,制作粗糙");
                else if (i ==11)
                printf ("┨        | 敬请见谅");
                else
                    printf ("┨        |");
            }
            else
                printf ("");
        }
        printf ("\n");
    }
}
//提示选手下棋并获取坐标
void player(char * name, int * coordi)
{
    //coordi记录坐标的数组
    char ch;
    //val记录所下棋子是否有效
    int val = 1;
    printf ("请选手%s下棋:\n", name);
    while (val)
    {
        printf ("请输入x(1-16):");
        while (scanf ("%d", &coordi[1]) != 1)
        {
            while ((ch = getchar ()) != '\n')
                putchar(ch);
            printf ("不是数字。请按规则输入数字,谢谢。\n");
            printf ("请输入x(1-16):");
        }
        while ((ch = getchar ()) != '\n')
                continue;
        printf ("请输入y(1-16):");
        while (scanf ("%d", &coordi[0]) != 1)
        {
            while ((ch = getchar ()) != '\n')
                putchar(ch);
            printf ("不是数字。请按规则输入数字,谢谢。\n");
            printf ("请输入y(1-16):");
        }
        while ((ch = getchar ()) != '\n')
                continue;
        val = valid (coordi);
    }
}
//判断所下棋子的位置是否有效
int valid (int * coordi)
{
    int val = 0;
    if ( !(coordi[0] <= 16 && coordi[0] >= 1))
    {
        printf ("输入坐标越界,请正确输入.\n");
        return 1;
    }
    if ( !(coordi[1] <= 16 && coordi[1] >= 1))
    {
        printf ("输入坐标越界,请正确输入.\n");
        return 1;
    }
    if (chessboard[coordi[0] - 1][coordi[1] - 1] != 0)
    {
        printf ("该位置已经有棋子了,请换个位置吧~\n");
        return 1;
    }
    return 0;
}
//判断游戏是否结束,输赢平
int over (int x, int y)
{
    int now = chessboard[x][y];
    int flow = 1;
    int i = 1;    //用来挪坐标
    int j;
    //横向判断
    while ((x + i) < 16 && chessboard[x + i][y] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((x - i) >= 0 && chessboard[x - i][y] == now)
    {
        flow++;
        i++;
    }
    if (flow >= 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断
    i = 1;
    flow = 1;
    while ((y + i) < 16 && chessboard[x][y + i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y - i) >= 0 && chessboard[x][y - i] == now)
    {
        flow++;
        i++;
    }
    if (flow >= 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断,主对角线
    i = 1;
    flow = 1;
    while ((y + i) < 16 && (x + i) < 16 && chessboard[x + i][y + i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y - i) >= 0 && (x - i) >= 0 && chessboard[x - i][y - i] == now)
    {
        flow++;
        i++;
    }
    if (flow == 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断,副对角线
    i = 1;
    flow = 1;
    while ((y - i) >= 0 && (x + i) < 16 && chessboard[x + i][y - i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y + i) < 16 && (x - i) >= 0 && chessboard[x - i][y + i] == now)
    {
        flow++;
        i++;
    }
    if (flow == 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //是否排满
    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15; j++)
        {
            if (!chessboard[i][j])
                break;
            if (i == 15 && j == 15)
            {    printf ("平局了,看来势均力敌哦~\n");
                return 1;
            }
        }
    }
    return 0;
}
void start(void)
{
    int i,j;

    for (i = 0; i < 16; i++)
        for (j = 0; j < 16; j++)
            chessboard[i][j] = 0;
}

 

#include <stdio.h>
#include <stdlib.h>
#define DOWN 's'//40
#define UP 'w'//38
#define LEFT 'a'//37
#define RIGHT 'd'//39
#define ESC 27
#define SPACE 32
#define BLACK 1
#define WHITE 2
#define NONE 0
int cursorx = 6,cursory = 6;    //光标横坐标,纵坐标
int now = BLACK;                //用来表示正在下棋的玩家
int chessboard[15][15];            //棋盘
void printfcb(void);            //输出棋盘
void rungame(void);                //运行游戏
int over (int x, int y);        //游戏是否结束
void initialise(void);
int main(void)
{
    char ch = 'y';

    system("title 豪华五子棋");
    system("color F1");
    system("mode con cols=61 lines=31");
    printf("\n\n    欢迎使用小夏出品豪(dou)华(bi)五子棋\n\n");
    printf("          小夏出品,必属精品\n\n");
    printf("    发现bug欢迎告诉我,反正我也改不了。o(╯□╰)o\n\n");
    printf("    1号为黑子,2号为白子\n\n");
    printf("    ASDW控制光标移动,SPACE下子,esc退出\n\n");
    printf("    按任意键继续……");
    getch();
    system("color F0");
    while (ch == 'y')
    {
        rungame();
        initialise();
        printf("是否再战一局?y继续,其它键退出。\n");
        ch = getch();
    }
    printf ("Bye~");
    return 0;
}
void rungame(void)
{    
    char ch;
    int win = 0;

    printfcb();
    while (!win)
    {
        ch = getch();
        switch (ch) 
        {
            case RIGHT: 
                if(cursory == 14)
                    cursory = 0;
                else
                    cursory++;
                printfcb();
                break;
            case LEFT:
                if(cursory == 0)
                    cursory = 14;
                else
                    cursory--;
                printfcb();
                break;
            case UP:
                if(cursorx == 0)
                    cursorx = 14;
                else
                    cursorx--;
                printfcb();
                break;
            case DOWN:
                if(cursorx == 14)
                    cursorx = 0;
                else
                    cursorx++;
                printfcb();
                break;
            case SPACE:
                if (chessboard[cursorx][cursory] == NONE)
                {
                    chessboard[cursorx][cursory] = now;
                    printfcb();
                    win = over(cursorx, cursory);
                    if (now == BLACK)
                        now = WHITE;
                    else
                        now = BLACK;
                }
                break;
            case ESC:
                exit(0);
                break;
            default:
                break;
        }
    }
}
void printfcb(void)
{
    int i,j;

    system("cls");
    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15; j++)
        {
            if (chessboard[i][j] == BLACK)
                printf ("");
            else if (chessboard[i][j] == WHITE)
                printf ("");
            else if (i == 0 && j == 0)
                printf ("");
            else if (i == 0 && j == 14)
                printf ("");
            else if (i == 14 && j == 0)
                printf ("");
            else if (i == 14 && j == 14)
                printf ("");
            else if (i ==0)
                printf ("");
            else if (i == 14)
                printf ("");
            else if (j == 0)
                printf ("");
            else if (j == 14)
                printf ("");
            else 
                printf ("");
            if ((i==0 || i == 14) && j < 14)
            printf ("");
            else if(j < 14)
                printf ("");
        }
        printf ("\n");
        if (i < 14)
        for (j = 0; j < 15; j++)
        {
            if (j == 0 || j == 14)
                printf ("");
            else
                printf ("");
            if (i + 1 == cursorx && j + 1 == cursory)
                printf ("");
            else if (i + 1 ==cursorx && j == cursory)
                printf ("");
            else if (i == cursorx && j + 1 == cursory)
                printf ("");
            else if (i == cursorx && j == cursory)
                printf ("");
            else if (j < 14)
                printf ("  ");
        }
        printf ("\n");
    }
}
//判断游戏是否结束,输赢平
int over (int x, int y)
{
    int now = chessboard[x][y];
    int flow = 1;
    int i = 1;    //用来挪坐标
    int j;
    //横向判断
    while ((x + i) < 16 && chessboard[x + i][y] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((x - i) >= 0 && chessboard[x - i][y] == now)
    {
        flow++;
        i++;
    }
    if (flow >= 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断
    i = 1;
    flow = 1;
    while ((y + i) < 16 && chessboard[x][y + i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y - i) >= 0 && chessboard[x][y - i] == now)
    {
        flow++;
        i++;
    }
    if (flow >= 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断,主对角线
    i = 1;
    flow = 1;
    while ((y + i) < 16 && (x + i) < 16 && chessboard[x + i][y + i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y - i) >= 0 && (x - i) >= 0 && chessboard[x - i][y - i] == now)
    {
        flow++;
        i++;
    }
    if (flow == 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //纵向判断,副对角线
    i = 1;
    flow = 1;
    while ((y - i) >= 0 && (x + i) < 16 && chessboard[x + i][y - i] == now)
    {
        flow++;
        i++;
    }
    i = 1;
    while ((y + i) < 16 && (x - i) >= 0 && chessboard[x - i][y + i] == now)
    {
        flow++;
        i++;
    }
    if (flow == 5)
    {
        printf ("恭喜%d号选手连成五子,游戏结束~\n", now);
        return 1;
    }
    //是否排满
    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15; j++)
        {
            if (!chessboard[i][j])
                break;
            if (i == 15 && j == 15)
            {    printf ("平局了,看来势均力敌哦~\n");
                return 1;
            }
        }
    }
    return 0;
}
void initialise(void)
{
    int i,j;
    for (i = 0; i < 15; i++)
        for (j = 0; j < 15; j++)
        {
            chessboard[i][j] = 0;
            cursorx = 6,cursory = 6;    
        }
}

 

posted @ 2016-11-13 22:50  我不吃饼干呀  阅读(627)  评论(0编辑  收藏  举报