黑白棋游戏

/*黑白棋游戏*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#define max 8
#define bai 1
#define hei 2
#define robot 3
int h, m, s;//时、分、秒
time_t start_time;
int count_black;  //黑棋数目
int count_white; //白棋数目
int endflag; //下期结束标志
int result; //游戏结果
int player; //当前玩家
int chessflag; //当前坐标能否下棋的标志
int playerflag; // 当前玩家能否下棋的标志
int cx, cy; // 当前光标所在位置
int px, py; //光标上一步所在位置
int a[max][max]; //棋子的数值储存

struct chess
{
    char s[5];
} box[max * 2 + 1][max * 2 + 1]; //棋子的图形存储
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int f(int i); //棋子的数值储存与图形储存变换
void people_robot();
void people_people();
void iniData(); //初始化数据
void iniChess(); //初始化棋子
void iniBox(); //初始化棋盘
void cursor(int x, int y); //改变新位置的棋框为光标
void ucursor(int x, int y); //还原光标之前位置的棋框
void showBox(); //显示棋盘
void resultInformation(); //结果信息
void runPeopleGame(); //运行人人模式
void runRobotGame(); //运行人机模式
int playerJudge(int player); //判断player玩家是否能落子
int chessJudge(int cx, int cy, int player); //判断player玩家在(x, Y)处能否落子
void chessCount(); //棋子数目统计
void resultJudge(); //输赢判断
int eatChess(int cx, int cy); //吃子
void downChess(); //落子
void refresh(); //更新棋盘
void playerBlack(); //黑方下棋
void playerWhite(); //白方下棋
void playerRobot(); //机器下棋
void startGame(); //开始游戏
void countTime(); //计时器
int main()
{
    startGame();
}
int f(int i)
{
    return 2 * i + 1; //棋子坐标i*2+1
}
void iniData()
{
    system("color E0");//背景颜色
    cx = 0;
    cy = 0;
    px = 0;
    py = 0;
    h = 0;
    m = 0;
    s = 0;
    start_time = 0;
    count_black = 2;
    count_white = 2;
    chessflag = 0;
    endflag = 0;
    result = 4; //输赢未判定
    player = bai;
    playerflag = 1;
}
void people_robot()//人机模式
{

    iniData();
    iniBox();
    iniChess();start_time = time(NULL);
    showBox();
    runRobotGame();

}
void people_people()
{

    iniData();
    iniBox();
    iniChess();start_time = time(NULL);
    showBox();
    runPeopleGame();
}
void startGame()
{
    int a;
    printf("-----------------黑白棋游戏-----------------\n\n");
    printf("-------模式选择---------\n");
    printf("1. 人人对战\n2. 人机对战\n");
    printf("请输入你的选择(1 或 2):");
    scanf("%d", &a);
    if(a == 1)
    {
        system("cls");
        people_people();
    }
    else
    {
        system("cls");    //人机对战;
        people_robot();
    }
}
void iniChess() //初始化棋子
{
    int i, j;
    for(i = 0; i < max; i++)
    {
        for(j = 0; j < max; j++)
        {
            a[i][j] = 0;
        }
    }
    a[3][3] = 1;//1为白子
    a[4][4] = 1;//2为黑子
    a[3][4] = 2;
    a[4][3] = 2;
}
void cursor(int x, int y) //光标位置
{
    strcpy(box[f(x)][f(y) - 1].s, "╠");
    strcpy(box[f(x)][f(y) + 1].s, "╣");
}
void ucursor(int x, int y)
{
    if((py == 7 && cy == 0) || (py == 0 && cy == 7))
    {
        strcpy(box[f(x)][f(y) - 1].s, "│");
        strcpy(box[f(x)][f(y) + 1].s, "│");
    }
    if(py < cy)
    {
        strcpy(box[f(x)][f(y) - 1].s, "│");
    }
    else if(py > cy)
    {
        strcpy(box[f(x)][f(y) + 1].s, "│");
    }
    else
    {
        strcpy(box[f(x)][f(y) - 1].s, "│");
        strcpy(box[f(x)][f(y) + 1].s, "│");
    }
}
void iniBox() //初始化棋盘
{
    int i, j;
    strcpy(box[0][0].s, "┌");
    strcpy(box[max * 2][0].s, "└");
    strcpy(box[0][max * 2].s, "┐");
    strcpy(box[max * 2][max * 2].s, "┘");
    for(i = 1; i < max *  2; i++)//第一列
    {
        if(i % 2 == 0)strcpy(box[i][0].s, "├");
        else strcpy(box[i][0].s, "│");
    }
    for(i = 1; i < max *  2; i++)//最后一列
    {
        if(i % 2 == 0)strcpy(box[i][max * 2].s, "┤");
        else strcpy(box[i][max * 2].s, "│");
    }
    for(i = 1; i < max * 2; i++)//第一行
    {
        if(i % 2 == 0)
        {
            strcpy(box[0][i].s, "┬");
        }
        else
        {
            strcpy(box[0][i].s, "─");
        }
    }
    for(i = 1; i < max * 2; i++)//最后一行
    {
        if(i % 2 == 0)
        {
            strcpy(box[max * 2][i].s, "┴");
        }
        else
        {
            strcpy(box[max * 2][i].s, "─");
        }
    }
    for(i = 1; i < 2 * max; i++)//棋盘列数
    {
        if(i % 2 == 0)
        {
            for(j = 1; j < max * 2; j++)//棋盘行数
            {
                if(j % 2 == 0)
                {
                    strcpy(box[j][i].s, "┼");
                }
                else
                {
                    strcpy(box[j][i].s, "│");
                }
            }
        }
        else
        {
            for(j = 1; j < max * 2; j++)
            {
                if(j % 2 == 0)
                {
                    strcpy(box[j][i].s, "─");
                }
                else
                {
                    strcpy(box[j][i].s, "  ");
                }
            }
        }
    }
    strcpy(box[3 * 2 + 1][3 * 2 + 1].s, "○") ;
    strcpy(box[4 * 2 + 1][4 * 2 + 1].s, "○");
    strcpy(box[3 * 2 + 1][4 * 2 + 1].s, "●");
    strcpy(box[4 * 2 + 1][3 * 2 + 1].s, "●");
    cursor(0,0);//初始化光标位置
}
void resultInformation()
{
    printf("------------------------------------\n");
    char str[10];
    if(result == 0)printf("平局\n");
    else if(result == hei)
    {
        strcpy(str, "黑棋赢");
        printf("黑棋赢\n");

    }
    else if(result == bai)
    {
        printf("白棋赢\n");
        strcpy(str, "白棋赢");
    }
    else if(result == robot)
    {
        printf("机器赢\n");
        strcpy(str, "机器赢");
    }
    MessageBox(NULL, str, "结果", 0);
    printf("------------------------------------\n");
    printf("按任意键返回主菜单");
    getch();
    startGame();
}
void showBox() //显示棋盘
{
    int i, j;
    //printf("上下左右键控制方向,空格键放棋子\n");
    for(i = 0; i < max * 2 + 1; i++)
    {
        for(j = 0; j < max * 2 + 1; j++)
        {
            printf("%s", box[i][j].s);
        }
        printf("\n");
    }
    printf("         ○:%d     ●:%d\n", count_white, count_black);
    countTime();
    printf("time: %d:%d:%d\n", h, m, s);
    printf("此处是否可放棋子:%s\n", chessflag == 1 ? "是" : "否");
    printf("当前棋手是否可以放子:%s\n", playerflag == 1 ? "是" : "否");
    printf("当前棋手:%s\n", player == hei ? "●" : "○");
    if(endflag)
    {
        resultInformation();
    }
}
int chessJudge(int cx, int cy, int player)
{
    if(a[cx][cy] != 0)//判断此处是否已有棋子
        return 0;
    int i, j, ii, jj;
    int book = 0;
    if(a[cx][cy + 1] == 3 - player && cy <= 5)                  //向右试探
    {
        for(i = cy + 2; i < max; i++)
        {
            if(a[cx][i] == 0)
                break;
            else if(a[cx][i] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx][cy - 1] == 3 - player && cy >= 2)                  //向左试探
    {
        for(i = cy - 2; i >= 0; i--)
        {
            if(a[cx][i] == 0)
                break;
            else if(a[cx][i] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx - 1][cy] == 3 - player && cx >= 2)                  //向上试探
    {
        for(i = cx - 2; i >= 0; i--)
        {
            if(a[i][cy] == 0)
                break;
            else if(a[i][cy] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx + 1][cy] == 3 - player && cx <= 5)                  //向下试探
    {
        for(i = cx + 2; i < max; i++)
        {
            if(a[i][cy] == 0)
                break;
            else if(a[i][cy] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx + 1][cy - 1] == 3 - player && (cx <= 5 && cy >= 2))                  //向左下试探
    {
        for(i = cx + 2, j = cy - 2; i < max, j >= 0; i++, j--)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx - 1][cy - 1] == 3 - player && (cx >= 2 && cy >= 2))                  //向左上试探
    {
        for(i = cx - 2, j = cy - 2; i >= 0, j >= 0; i--, j--)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx + 1][cy + 1] == 3 - player && (cx <= 5 && cy <= 5))                  //向右下试探
    {
        for(i = cx + 2, j = cy + 2; i < max, j < max; i++, j++)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {
                book++;
                break;
            }
        }
    }
    if(a[cx - 1][cy + 1] == 3 - player && (cx >= 2 && cy <= 5))                  //向右上试探
    {
        for(i = cx - 2, j = cy + 2; i >= 0, j < max; i--, j++)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {
                book++;
                break;
            }
        }
    }
    if(book)return 1;
    else return 0;
}
int playerJudge(int player)
{
    int i, j;
    int book = 0;
    for(i = 0; i < max; i++)
    {
        for(j = 0; j < max; j++)
        {
            if(chessJudge(i, j, player))
            {
                book++;
            }
        }
    }
    if(book)return 1;
    else return 0;
}
void chessCount()
{
    int i, j;
    int shei = 0;
    int sbai = 0;
    for(i = 0; i < max; i++)
    {
        for(j = 0; j < max; j++)
        {
            if(a[i][j] == hei)shei++;
            else if(a[i][j] == bai)sbai++;
        }
    }
    count_white = sbai;
    count_black = shei;
}
void resultJudge()
{
    int book = 0;
    if(count_white + count_black == max * max)//情况一:期盘下满
    {
        book++;
        if(count_white > count_black)
        {
            result = bai;
        }
        else if(count_white < count_black)
        {
            result = hei;
        }
        else if(count_white == count_black)
        {
            result = 0;
        }
    }
    else if(count_white == 0 && count_black == 0)//其中一方被吃完
    {
        book++;
        if(count_white == 0)result = hei;
        if(count_black == 0)result = bai;
    }
    if(book) endflag = 1;
    else endflag == 0;
}
int eatChess(int cx, int cy)
{
    int i, j, ii, jj;
    int book = 0;
    if(a[cx][cy + 1] == 3 - player && cy <= 5)                  //向右试探
    {
        for(i = cy + 2; i < max; i++)
        {
            if(a[cx][i] == 0)
                break;
            else if(a[cx][i] == player)
            {

                book++;
                for(j = i - 1; j > cy; j--)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(cx)][f(j)].s, "○");
                        a[cx][j] = bai;
                    }

                    else
                    {

                        strcpy(box[f(cx)][f(j)].s, "●");
                        a[cx][j] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx][cy - 1] == 3 - player && cy >= 2)                  //向左试探
    {
        for(i = cy - 2; i >= 0; i--)
        {
            if(a[cx][i] == 0)
                break;
            else if(a[cx][i] == player)
            {

                book++;
                for(j = i + 1; j < cy; j++)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(cx)][f(j)].s, "○");
                        a[cx][j] = bai;
                    }

                    else
                    {

                        strcpy(box[f(cx)][f(j)].s, "●");
                        a[cx][j] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx - 1][cy] == 3 - player && cx >= 2)                  //向上试探
    {
        for(i = cx - 2; i >= 0; i--)
        {
            if(a[i][cy] == 0)
                break;
            else if(a[i][cy] == player)
            {

                book++;
                for(j = i + 1; j < cx; j++)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(j)][f(cy)].s, "○");
                        a[j][cy] = bai;
                    }

                    else
                    {

                        strcpy(box[f(j)][f(cy)].s, "●");
                        a[j][cy] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx + 1][cy] == 3 - player && cx <= 5)                  //向下试探
    {
        for(i = cx + 2; i < max; i++)
        {
            if(a[i][cy] == 0)
                break;
            else if(a[i][cy] == player)
            {

                book++;
                for(j = i - 1; j > cx; j--)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(j)][f(cy)].s, "○");
                        a[j][cy] = bai;
                    }

                    else
                    {

                        strcpy(box[f(j)][f(cy)].s, "●");
                        a[j][cy] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx + 1][cy - 1] == 3 - player && (cx <= 5 && cy >= 2))                  //向左下试探
    {
        for(i = cx + 2, j = cy - 2; i < max, j >= 0; i++, j--)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {

                book++;
                for(ii = i - 1, jj = j + 1; ii > cx, jj < cy; ii--, jj++)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(ii)][f(jj)].s, "○");
                        a[ii][jj] = bai;
                    }

                    else
                    {

                        strcpy(box[f(ii)][f(jj)].s, "●");
                        a[ii][jj] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx - 1][cy - 1] == 3 - player && (cx >= 2 && cy >= 2))                  //向左上试探
    {
        for(i = cx - 2, j = cy - 2; i >= 0, j >= 0; i--, j--)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {

                book++;
                for(ii = i + 1, jj = j + 1; ii < cx, jj < cy; ii++, jj++)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(ii)][f(jj)].s, "○");
                        a[ii][jj] = bai;
                    }

                    else
                    {

                        strcpy(box[f(ii)][f(jj)].s, "●");
                        a[ii][jj] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx + 1][cy + 1] == 3 - player && (cx <= 5 && cy <= 5))                  //向右下试探
    {
        for(i = cx + 2, j = cy + 2; i < max, j < max; i++, j++)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {

                book++;
                for(ii = i - 1, jj = j - 1; ii > cx, jj > cy; ii--, jj--)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(ii)][f(jj)].s, "○");
                        a[ii][jj] = bai;
                    }

                    else
                    {

                        strcpy(box[f(ii)][f(jj)].s, "●");
                        a[ii][jj] = hei;
                    }
                }

                break;
            }
        }
    }
    if(a[cx - 1][cy + 1] == 3 - player && (cx >= 2 && cy <= 5))                  //向右上试探
    {
        for(i = cx - 2, j = cy + 2; i >= 0, j < max; i--, j++)
        {
            if(a[i][j] == 0)
                break;
            else if(a[i][j] == player)
            {

                book++;
                for(ii = i + 1, jj = j - 1; ii < cx, jj > cy; ii++, jj--)
                {
                    if(player == bai)
                    {

                        strcpy(box[f(ii)][f(jj)].s, "○");
                        a[ii][jj] = bai;
                    }

                    else
                    {
                        strcpy(box[f(ii)][f(jj)].s, "●");
                        a[ii][jj] = hei;
                    }
                }

                break;
            }
        }
    }
    if(book)return 1;
    else return 0;
}
void runPeopleGame() //运行人人模式
{
    while(endflag != 1)
    {
        playerWhite();// 白棋先下
        playerBlack();
    }
}
void runRobotGame()
{
    while(endflag != 1)
    {
        playerWhite();
        Sleep(500 );
        playerRobot();
        //Sleep(5000);
    }
}
void downChess() //落子
{
    eatChess(cx, cy);
    if(chessJudge(cx, cy, player))
        chessflag = 1;
    else chessflag = 0;
    px = cx;
    py = cy;
    a[cx][cy] = player;//下子
    if(player == bai)//下子图形表示
    {
        strcpy(box[f(cx)][f(cy)].s, "○");
    }
    else
    {
        strcpy(box[f(cx)][f(cy)].s, "●");
    }
    player = 3 - player;//切换棋手
    chessCount();
    resultJudge();//判断输赢//下子函数
}
void refresh()//更新棋盘
{
    if(px == cx && py == cy)//当光标回到上次的位置
    {
        chessCount();
        cursor(cx, cy);
        system("cls");
        showBox();
    }
    else
    {
        if(playerJudge(player))//当前玩家是否能放子
            playerflag = 1;
        else
        {
            playerflag = 0;
        }
        if(chessJudge(cx, cy, player))
            chessflag = 1;
        else chessflag= 0;
        //界面刷新
        cursor(cx, cy);
        system("cls");
        showBox();
        ucursor(px, py);
        system("cls");
        showBox();
    }
}
void playerBlack()
{
    int key;
    int flag = 1;
    while(flag)
    {
        if(playerJudge(player) == 0)//如果当前玩家无法落子,换对手下
        {
            player = 3 - player;
            if(playerJudge(player) == 0)//如果对手也无法落子,游戏结束判断输赢
            {
                resultJudge();
            }
            return;
        }
        key = getch();
        if(key == 'r')
        {
            if(a[cx][cy] == 0 && chessJudge(cx, cy, player))
            {
                flag = 0;
                downChess();
            }
        }
        else
        {
            px = cx;
            py = cy;
            switch(key)
            {
            case 'a':
                cy--;
                break;
            case 'd':
                cy++;
                break;
            case 'w':
                cx--;
                break;
            case 's':
                cx++;
                break;
            }
        }
        //防止越界
        if(cx > max - 1)cx = 0;
        if(cx < 0)cx = max - 1;
        if(cy > max - 1)cy = 0;
        if(cy < 0)cy = max - 1;
        refresh();
    }

}
void playerWhite()
{
    int flag = 1;
    while(flag)
    {
        int key;
        if(playerJudge(player) == 0)//如果当前玩家无法落子,换对手下
        {
            player = 3 - player;
            if(playerJudge(player) == 0)//如果对手也无法落子,游戏结束判断输赢
            {
                resultJudge();
            }
            return;
        }
        key = getch();
        if(key == 0x20 )
        {
            if(a[cx][cy] == 0 && chessJudge(cx, cy, player))
            {
                flag = 0;
                downChess();
            }
        }
        else if(key == 0xE0)
        {
            px = cx;
            py = cy;
            key = getch();
            switch(key)
            {
            case 0x4B://key_left
                cy--;
                break;
            case 0x48://key_up
                cx--;
                break;
            case 0x4D://key_right
                cy++;
                break;
            case 0x50://key_down
                cx++;
                break;
            }
        }
        //防止越界
        if(cx > max - 1)cx = 0;
        if(cx < 0)cx = max - 1;
        if(cy > max - 1)cy = 0;
        if(cy < 0)cy = max - 1;
        refresh();
    }

}

void playerRobot()
{
    if(playerJudge(player) == 0)//如果当前玩家无法落子,换对手下
    {
        player = 3 - player;
        if(playerJudge(player) == 0)//如果对手也无法落子,游戏结束判断输赢
        {
            resultJudge();
            endflag = 1;
            showBox();
        }
        return;
    }
    int i, j;
    int flag = 0;
    for(i = 0; i < max; i++)
    {
        for(j = 0; j < max; j++)
        {
            if(a[i][j] == 0 && chessJudge(i, j, player))
            {
                a[i][j] = player;//下子
                eatChess(i, j);
                if(player == bai)//下子图形表示
                {
                    strcpy(box[f(i)][f(j)].s, "○");
                }
                else
                {
                    strcpy(box[f(i)][f(j)].s, "●");
                }
                player = 3 - player;//切换棋手
                chessCount();
                resultJudge();//判断输赢//下子函数
                flag++;
                break;
            }
        }
        if(flag)break;
    }
    refresh();
}

void countTime()
{
        int total;
        time_t temp;
        temp = time(NULL);
        total = temp - start_time;
        s = total % 60;
        m = total / 60 % 60;
        h = total / 60 / 60;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值