标题:写了一个五子棋游戏,不过没有完善,只有横向判断胜利,其余的还没有加,求 ...
只看楼主
王黄丽
Rank: 1
等 级:新手上路
帖 子:53
专家分:4
注 册:2018-7-27
结帖率:100%
 问题点数:0 回复次数:2 
写了一个五子棋游戏,不过没有完善,只有横向判断胜利,其余的还没有加,求指导!!!
//编写一个控制台下的五子棋游戏
#include <stdio.h>

//定义一个二维数组表示棋盘
int piece[16][16];


//实现绘制棋盘的函数
void paint(void);
//实现下棋功能函数
void play(void);
//判断是否获胜
int judge(int player);

int main(void) {
    //绘制棋盘
    paint();
    //开始启动游戏
    play();
    return 0;
}

//判断是否获胜
int judge(int player) {
    //1.横向判断
    int i = 0, j = 0;
    for(i = 0; i < 16; i++){
        for(j = 0; j < 12; j++)
        {
            if((piece[i][j]&piece[i][j+i]&piece[i][j+2]&piece[i][j+3]&piece[i][j+4]) == player) {
                return 1;//表示获胜
            }
        }
    }
    //2.纵向判断
    //3.左上到右下的判断
    //4.右上到左下的判断
    return 0;//表示没有获胜
}

//实现下棋功能函数
void play(void) {
    int player = 0;//表示黑方
    while(1) {
        int x = 0, y = 0;
        if(0 == player) {
        printf("请黑方输入落子坐标:\n");
        scanf("%d%d", &x, &y);
        piece[x][y] = 1;
        //从新绘制棋盘
        paint();
        //判断是否获胜
        if(judge(player+1)) {
            printf("黑方胜利,游戏结束\n");
            break;
        }
        //切换下子方到白方
        player = 1;
        }
        if(1 == player){
            printf("请白方输入落子坐标:\n");
            scanf("%d%d",&x, &y);
            piece[x][y] = 2;
            //从新绘制棋盘
            paint();
            //判断是否获胜
            if(judge(player+1)) {
            printf("白方胜利,游戏结束\n");
            break;
        }
            //切换下子方
            player = 0;

        }
    }
}

//实现绘制棋盘的函数
void paint(void) {
    //1.绘制棋盘中的第一行坐标
    int i = 0;
    int j = 0;
    for (i = 0; i < 17; i++) {
        if(0 == i) {
            printf(" ");
        }
        else
        {
            printf("%x ", i - 1);
        }
    }
    printf("\n");
    //2.绘制棋盘中剩余的部分内容
   
    for(i = 0; i < 16; i++)
    {
        printf("%x ",i);
        for(j = 0; j < 16; j++)
        {
            if(0 == piece[i][j]) {
            printf("* ");
            }
            else if(1 == piece[1][j]) {
                printf("@ ");
            }
            else{
                printf("& ");
            }
        }
        printf("\n");
    }
}
搜索更多相关主题的帖子: 判断 int void player printf 
2018-08-03 11:49
尘安
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:55
注 册:2018-8-9
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define N 16

int chessboard[N][N]={0};   //二维数组表示棋盘;0表示空白;1表示黑方执子;2表示白方执子
int pieces[2]={0,0};

void drow_board();  //打印棋盘
void play();        //下棋
int judge();       //判断获胜与否

int main()
{
    int t;
    for(;;)
    {
        drow_board();
        play();
        t=judge();
        if(t)
            break;
    }
    if(t==1)
        printf("   \***  黑方获胜!  ***/\n");
    else
        printf("   \***  白方获胜!  ***/\n");
    return 0;
}

void drow_board()       //绘制棋盘
{
    system("cls");
    int i,j;  //循环变量
    printf("   ");
    for(i=0;i<N;i++)        //在第一行打印坐标
        printf("%3d",i);
    printf("\n");
    for(i=0;i<N;i++)        //打印余下棋盘
    {
        printf("%3d",i);
        for(j=0;j<N;j++)
        {
            switch(chessboard[i][j])
            {
            case 0:     //空白
                printf("  +");
                break;
            case 1:     //黑方执子
                printf("  @");
                break;
            case 2:     //白方执子
                printf("  &");
                break;
            }
        }
        printf("\n");
    }
    printf("\t黑棋:%d 目\t白棋:%d 目\n\n",pieces[0],pieces[1]);
}

void play()
{
    int i,j;    //棋子坐标
    static int player=1;    //1表示黑方,2表示白方
    while(1)
    {
        if(player==1)
            printf("请黑方落子(输入坐标x y):");
        else if(player==2)
            printf("请白方落子(输入坐标x y):");
        fflush(stdin);
        scanf("%d%d",&i,&j);
        fflush(stdin);
        if(i<0||i>=N||j<0||j>=N)
            printf(" |***  error:the input coordinates are invalid!  ***|\n");
        else if(chessboard[i][j]!=0)
            printf(" |***  error:The coordinates already have pieces!  ***|\n");
        else
            break;
    }
    chessboard[j][i]=player;
    if(player==1)
    {
        pieces[0]++;    //黑棋落子数目
        player++;
    }
    else if(player==2)
    {
        pieces[1]++;    //白棋落子数目
        player--;
    }
}

int judge()    //判断获胜与否
{
    int i,j;    //循环变量
    int result=0; //存放结果
    for(i=0;i<N;i++)        //横向判断
        for(j=0;j<N-5;j++)
            if(chessboard[i][j]&&chessboard[i][j+1]&&chessboard[i][j+2]&&chessboard[i][j+3]&&chessboard[i][j+4])        //判断连续五个数是否不为0
                if(chessboard[i][j]==chessboard[i][j+1]==chessboard[i][j+2]==chessboard[i][j+3]==chessboard[i][j+4])    //判断连续不为0的五个数是否相等
                    result=chessboard[i][j];
    for(j=0;j<N;j++)        //纵向判断
        for(i=0;i<N-5;i++)
            if(chessboard[i][j]&&chessboard[i+1][j]&&chessboard[i+2][j]&&chessboard[i+3][j]&&chessboard[i+4][j])
                if(chessboard[i][j]==chessboard[i+1][j]==chessboard[i+2][j]==chessboard[i+3][j]==chessboard[i+4][j])
                    result=chessboard[i][j];
    for(i=0;i<N-5;i++)      //斜向右判断
        for(j=0;j<N-5;j++)
            if(chessboard[i][j]&&chessboard[i+1][j+1]&&chessboard[i+2][j+2]&&chessboard[i+3][j+3]&&chessboard[i+4][j+4])
                if(chessboard[i][j]==chessboard[i+1][j+1]==chessboard[i+2][j+2]==chessboard[i+3][j+3]==chessboard[i+4][j+4])
                    result=chessboard[i][j];
    for(j=N-1;j>4;j--)      //斜向左判断
        for(i=0;i<N-5;i++)
            if(chessboard[i][j]&&chessboard[i+1][j-1]&&chessboard[i+2][j-2]&&chessboard[i+3][j-3]&&chessboard[i+4][j-4])
                if(chessboard[i][j]==chessboard[i+1][j-1]==chessboard[i+2][j-2]==chessboard[i+3][j-3]==chessboard[i+4][j-4])
                    result=chessboard[i][j];
    return result;
}

2018-08-13 14:05
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
得分:0 
可参考我用js html5写的代码,7楼“function testwin”是判断输赢的函数。
https://bbs.bccn.net/thread-446060-1-1.html

能编个毛线衣吗?
2018-08-13 14:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489002-1-1.html




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

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