| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1049 人关注过本帖
标题:【原创】Chess 1.0,C语言五子棋,渣渣新手猿....欢迎大家指点批评指点。
只看楼主 加入收藏
CatCrossbow
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-7-4
收藏
 问题点数:0 回复次数:0 
【原创】Chess 1.0,C语言五子棋,渣渣新手猿....欢迎大家指点批评指点。
刚进入这个论坛,我是一名高一学生新手......请大家多多指教

写这个程序忘记过吃饭、忘记过睡觉,
连续不断调试修改测试过三四天终于写出来了。

Chess 1.0

这是一个五子棋程序,通过输入二维坐标来在棋盘输入棋子。
目前已经有防止棋子重复并报错机制,判定输赢机制,
即基本机制已经完成。
未来版本将会加入棋盘大小选择,悔棋机制,上一步显示还有输入机制修改完善。

测试图如下:
[local]1[/local]
[local]2[/local]
[local]3[/local]
[local]4[/local]

源代码如下:    (附带注释)
#include <stdio.h>
#include <conio.h>

void ScreenClean()    //清屏函数
{
    short Time = 0;
   
    while(1)
    {
        printf("n");
        ++Time;
        if(Time == 46)
            break;
    }
}

short TableSet(short Table[26][51],short Set_y,short Set_x)    //棋盘定义
{
    short Table_y,Table_x,Word;
   
    const Space = 32;
   
    Table_y = 0,Table_x = 0,Word = 65;
    while(1)
    {
        Table[Table_y][Table_x] = Table_y + Word;
        ++Table_y;
        if(Table_y == 25)
            break;
    }
   
    Table_y = 25,Table_x = 1;
    while(1)
    {
        Table[Table_y][Table_x] = Word;
        ++Table_x;
        Table[Table_y][Table_x] = Space;
        ++Table_x;
        ++Word;
        if(Table_x == 51)
            break;
    }
   
    Table_y = 0,Table_x = 1;
    while(1)
    {
        Table[Table_y][Table_x] = Space;
        ++Table_x;
        if(Table_x == 51)
        {
            Table_x = 1;
            ++Table_y;
        }
        if(Table_y == 25)
            break;
    }
   
    Table[25][0] = Space;
   
    Table_y = 0,Table_x = 0;
   
    return Table[Set_y][Set_x];
}

void TablePrint(short Table[26][51])    //打印棋盘
{
    short Print_y = 0,Print_x = 0;
   
    while(1)
    {
        printf("%c",Table[Print_y][Print_x]);
        ++Print_x;
        if(Print_x == 51)
        {
            Print_x = 0;
            ++Print_y;
            printf("n");
        }
        if(Print_y == 26)
            break;
    }
}

short AreaJudge(short Table_y,short Table_x)    //棋子所在区域判定
{
    short Area[3][3] = {{0,1,2},{3,4,5},{6,7,8}},Area_y,Area_x;
   
    if(Table_y <= 3)
        Area_y = 0;
    else if(Table_y > 3 && Table_y <= 22)
        Area_y = 1;
    else
        Area_y = 2;
   
    if(Table_x <= 7)
        Area_x = 0;
    else if (Table_x > 7 && Table_x <=42)
        Area_x = 1;
    else
        Area_x = 2;
   
    return Area[Area_y][Area_x];
}

short JudgeWay(short Table[26][51],short Table_y,short Table_x,short Chess,short Way)    //棋子的八种判定方式
{
    short Judge_y = 0,Judge_x = 0,
    Judgement = 0;
   
    const Space_x = 2;
   
    if(Way == 0)
        --Judge_y;
        
    else if(Way == 1)
        ++Judge_y;
        
    else if(Way == 2)
        Judge_x -= Space_x;
        
    else if(Way == 3)
        Judge_x += Space_x;
        
    else if(Way == 4)
    {
        --Judge_y;
        Judge_x -= Space_x;
    }
   
    else if(Way == 5)
    {
        --Judge_y;
        Judge_x += Space_x;
    }
   
    else if(Way == 6)
    {
        ++Judge_y;
        Judge_x -= Space_x;
    }
   
    else
    {
        ++Judge_y;
        Judge_x += Space_x;
    }
   
    if(Table[Table_y][Table_x] == Chess && Table[Table_y + Judge_y][Table_x + Judge_x] == Chess && Table[Table_y + Judge_y * 2][Table_x + Judge_x * 2] == Chess && Table[Table_y + Judge_y * 3][Table_x + Judge_x * 3] == Chess && Table[Table_y + Judge_y * 4][Table_x + Judge_x * 4] == Chess)
        ++Judgement;
   
    return Judgement;
}

short MatchJudge(short Area,short Table[26][51],short Table_y,short Table_x,short Chess)    //根据棋子所在区域选择判定方式,并判定输赢
{
    short Judgement = 0,Way = 11;
   
    while(1)
    {
        if(Area == 0)
        {
            if(Way == 11)
                Way = 1;
            else if(Way == 1)
                Way = 3;
            else if(Way == 3)
                Way = 7;
            else
                break;
        }
        
        else if(Area == 1)
        {
            if(Way == 11)
                Way = 1;
            else if(Way == 1)
                Way = 2;
            else if(Way == 2)
                Way = 3;
            else
                break;
        }
        
        else if(Area == 2)
        {
            if(Way == 11)
                Way = 1;
            else if(Way == 1)
                Way = 2;
            else if(Way == 2)
                Way = 6;
            else
                break;
        }
        
        else if(Area == 3)
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 1;
            else if(Way == 1)
                Way = 3;
            else
                break;
        }
        
        else if(Area == 4)
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 1;
            else if(Way == 1)
                Way = 2;
            else if(Way == 2)
                Way = 3;
            else if(Way == 3)
                Way = 4;
            else if(Way == 4)
                Way = 5;
            else if(Way == 5)
                Way = 6;
            else if(Way == 6)
                Way = 7;
            else
                break;
        }
        
        else if(Area == 5)
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 1;
            else if(Way == 1)
                Way = 2;
            else
                break;
        }
        
        else if(Area == 6)
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 3;
            else if(Way == 3)
                Way = 5;
            else
                break;
        }
        
        else if(Area == 7)
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 2;
            else if(Way == 2)
                Way = 3;
            else
                break;
        }
        
        else
        {
            if(Way == 11)
                Way = 0;
            else if(Way == 0)
                Way = 2;
            else if(Way == 2)
                Way = 4;
            else
                break;
        }
        
        Judgement = JudgeWay(Table,Table_y,Table_x,Chess,Way);
        
        if(Judgement == 1)
            break;
    }
   
    return Judgement;
}

short main()    //主函数
{
    short Table[26][51],    //(核心)棋盘
    Set_y = 0,Set_x = 0,    //定义棋盘所用的二维变量
    Input_y,Input_x,    //输入的二维坐标
    Table_y,Table_x,    //处理后的确切二维数组坐标
    Turn = 0,Chess,Error = 0,Area,Match;    //回合,棋子,棋子重复报错,棋子所在区域,输赢
   
    //定义棋盘
    while(1)
    {
        Table[Set_y][Set_x] = TableSet(Table,Set_y,Set_x);
        ++Set_x;
        if(Set_x == 51)
        {
            Set_x = 0;
            ++Set_y;
        }
        if(Set_y == 26)
            break;
    }
   
    while(1)
    {
        //根据回合判定棋子
        if(Turn == 0)
        {
            Chess = 79;
        }
        else
        {
            Chess = 88;
        }
        
        ScreenClean();    //初始清屏
        
        //打印回合及棋子
        printf("Now turn:tPlayer %dtChess '%c'n",Turn + 1,Chess);
        printf("n");
        
        //打印棋盘
        TablePrint(Table);
        
        //打印请求输入指示
        printf("nnn");
        printf("Please Input two word together for a coordinate.(x & y)n(eg:FT or LY)n");
        printf("n");
        
        //棋子重复报错
        if(Error == 1)
        {
            printf("Error input!Player %d please input again.n",Turn + 1);
            printf("n");
            --Error;
        }
        
        //输入二维坐标
        printf("Input:t");
        scanf("%c%c",&Input_x,&Input_y);
        getch();
        
        //处理二维坐标
        Table_y = Input_y - 65;
        Table_x = Input_x * 2 - 129;
        
        //将棋子嵌入二维数组
        if(Table[Table_y][Table_x] == 32)
        {
            Table[Table_y][Table_x] = Chess;
            if(Turn == 0)
                ++Turn;
            else
                --Turn;
        }
        else
            ++Error;
        
        Area = AreaJudge(Table_y,Table_x);    //判定区域
        Match = MatchJudge(Area,Table,Table_y,Table_x,Chess);    //判定输赢
        
        if(Match == 1)
            break;
    }
   
    //某方胜出
    ScreenClean();
   
    printf("Player %d had won the game!n",2 - Turn);
    printf("n");
    printf("Please click any key to exit.");
    getch();
   
    return 0;
}
搜索更多相关主题的帖子: 源代码 五子棋 include C语言 
2016-07-05 00:33
快速回复:【原创】Chess 1.0,C语言五子棋,渣渣新手猿....欢迎大家指点批评指点 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018349 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved