C语言实现五子棋对战系统

本文实例为大家分享了C语言实现五子棋对战的具体代码,供大家参考,具体内容如下

一直以来,有不少热爱并希望学习五子棋的人,或者仅为了娱乐来下五子棋的人,他们一般通过下棋对战来增加自己的对战经验,而在现实生活由于五子棋布板麻烦,经常缺少能下棋的环境,并且下棋时效率较低,记录步数也较为麻烦。利用计算机来模拟下五子棋环境,只要有计算机,就可以很方便的随时随地进行下棋,并且对战过程中对步数和下子过程进行记录,方便了喜欢下五子棋的人,让他们的五子棋学习更加高效或者娱乐起来更加方便。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
 
char draw[32][60] = {{" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
    {" "},
    {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},
};
 
typedef struct//坐标结构体
{
    int x;
    int y;
} node;
 
typedef struct//栈结构体
{
    node data[120];
    int top;
} stack;
 
int win[3], player, step1, step2;
stack player1,player2;
 
char ch;
node pos;
 
int convY(int y);
int convX(int x);
void refreshDraw();
void BLUE();
void RED();
void GREEN();
void WHITE();
void printMap();
void printMenu();
void refreshMap();
void playerOperation(char cheese);
void jundge(int p, char cheese);
void in(char cheese);
void playerVsPlayer();
void playerVscomputer();
void gotoxy(int x, int y);
void printStep(int player, int step);
void askExit();
void printPos(int player, int x, int y);
void printRoad();
 
int main()
{
    char num[99];
    printMenu();
    while(1)
    {
        WHITE();
        refreshDraw();
        pos.x = 0;
        pos.y = 0;
        win[1] = 0;
        win[2] = 0;
        gets(num);
        if(strcmp(num, "1") == 0)
        {
            playerVsPlayer();
            system("cls");
            printMenu();
        }
        else if(strcmp(num, "2") == 0)
        {
            askExit();
            continue;
        }
        else
        {
            printf("请输入正确数字!\n");
        }
    }
 
    return 0;
}
 
void BLUE()//字体变蓝
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE|FOREGROUND_INTENSITY);
}
 
void RED()//字体变红
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTENSITY);
}
 
void GREEN()//字体变绿
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_INTENSITY);
}
 
void WHITE()//字体变白
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);
}
 
void printMap()//打印棋盘和棋子
{
    for(int i = 0; i <= 29; i++)
    {
        for(int j = 0; j <= 59; j++)
        {
            if(i==convX(pos.x) && (j==convY(pos.y)-1 || j==convY(pos.y)+1) )
            {
                if(player == 1)
                {
                    GREEN();
                }
                else if(player == 2)
                {
                    RED();
                }
 
            }
            else if(draw[i][j] == '*')
            {
                RED();
            }
            else if(draw[i][j] == 'O')
            {
                GREEN();
            }
            printf("%c", draw[i][j]);
            WHITE();
        }
        printf("\n");
    }
}
 
void refreshDraw()//清空棋盘
{
    int i;
    for(i = 0; i <= 30; i=i+2)
    {
        strcpy(draw[i]," ");
        strcpy(draw[i+1],"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]");
    }
    strcpy(draw[31]," ");
}
 
void printMenu()//打印菜单
{
    WHITE();
    printf("\n\n     ****************************\n");
    printf("     *                          *\n");
    printf("     *         欢迎使用         *\n");
    printf("     *    五子棋模拟对战系统    *\n");
    printf("     *                          *\n");
    printf("     ****************************\n\n\n");
    printf("\n【设计制作:by 软件18-8邵蔚】\n\n");
    printf("游戏规则为:\n二人参与游玩,双方分别使用两种棋子,\n");
    printf("下在棋盘的 [ ] 上,\n");
    printf("先形成5子连线(斜线、竖线或横线)的人获胜。\n\n");
    printf("操作说明:\n玩家用键盘“W”“S”“A”“D”来选择下棋位置,选择好后,\n");
    printf("按“J”键来下棋,然后本回合结束,下一名玩家操作。\n\n");
    printf("输入 1 并按回车开始游戏\n");
    printf("输入 2 并按回车退出游戏\n");
}
 
int convY(int y)//将棋盘坐标转换为字符串数组下标
{
    return 4*(y+8)-3;
}
 
int convX(int x)//将棋盘坐标转换为字符串数组下标
{
    return 2*(8-x)-1;
}
 
void refreshMap()//刷新棋盘画面
{
    gotoxy(0,0);
    printMap();
    gotoxy(0,0);
}
 
void playerOperation(char cheese)//扫描玩家输入的 "W""A""S""D""J""K" , 并且在棋盘显示或删除棋子;
{
    while(1)
    {
        refreshMap();
        ch = getch();
        if(ch == 'a' && pos.y > -7)
        {
            pos.y--;
        }
        else if(ch == 'd' && pos.y < 7)
        {
            pos.y++;
        }
        else if(ch == 'w' && pos.x < 7)
        {
            pos.x++;
        }
        else if(ch == 's' && pos.x > -7)
        {
            pos.x--;
        }
        else if(ch == 'j' && draw[convX(pos.x)][convY(pos.y)] == ' ')
        {
            draw[convX(pos.x)][convY(pos.y)] = cheese;
            in(cheese);
            refreshMap();
            break;
        }
        else if(ch == 'k' && player1.top > 0)
        {
            int tx, ty;
            if(cheese == '*')
            {
                tx = player1.data[player1.top].x;
                ty = player1.data[player1.top].y;
                player1.top--;
 
                draw[convX(tx)][convY(ty)] = ' ';
                refreshMap();
                break;
            }
            else if(cheese == 'O')
            {
                tx = player2.data[player2.top].x;
                ty = player2.data[player2.top].y;
                player2.top--;
 
            }
            draw[convX(tx)][convY(ty)] = ' ';
            refreshMap();
            break;
        }
        else if(ch == 'k' && player1.top <= 0)
        {
            system("cls");
            printf("提示:没有棋子了!\n");
            system("pause");
            refreshMap();
            printStep(1, step1);
            printStep(2, step2);
            gotoxy(65,16);
            printf(" 按 K 来悔棋。");
            gotoxy(65, 23);
            GREEN();
            printf("(玩家1 : O)");
            gotoxy(65, 24);
            RED();
            printf("(玩家1 : *)");
        }
    }
}
 
void jundge(int p, char cheese)//判断是否有赢家
{
    int tx, ty, sum = 0;
 
    tx = pos.x;
    ty = pos.y;
    sum = 0;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        tx++;
    }
    tx = pos.x - 1;
    ty = pos.y;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        tx--;
    }
    if(sum >= 5)
    {
        win[p] = 1;
        return;
    }
//--------------------------------------------------------------------
    tx = pos.x;
    ty = pos.y;
    sum = 0;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        ty++;
    }
    tx = pos.x;
    ty = pos.y - 1;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        ty--;
    }
    if(sum >= 5)
    {
        win[p] = 1;
        return;
    }
//----------------------------------------------------------------------
    tx = pos.x;
    ty = pos.y;
    sum = 0;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        ty++;
        tx++;
    }
    tx = pos.x - 1;
    ty = pos.y - 1;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        ty--;
        tx--;
    }
    if(sum >= 5)
    {
        win[p] = 1;
        return;
    }
//----------------------------------------------------------------------
    tx = pos.x;
    ty = pos.y;
    sum = 0;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        tx--;
        ty++;
    }
    tx = pos.x + 1;
    ty = pos.y - 1;
    while(draw[convX(tx)][convY(ty)] == cheese && sum <= 5)
    {
        if(draw[convX(tx)][convY(ty)] == cheese)
            sum++;
        tx++;
        ty--;
    }
    if(sum >= 5)
    {
        win[p] = 1;
        return;
    }
 
    return;
}
 
void in(char cheese)//将棋子坐标入栈
{
    if(cheese == '*')
    {
        player2.top++;
        player2.data[player2.top].x = pos.x;
        player2.data[player2.top].y = pos.y;
    }
    else if(cheese == 'O')
    {
        player1.top++;
        player1.data[player1.top].x = pos.x;
        player1.data[player1.top].y = pos.y;
    }
}
 
void playerVsPlayer()
{
    step1 = 0;
    step2 = 0;
    player1.top = 0;
    player2.top = 0;
    player1.data[0].x = -1;
    player1.data[0].y = -1;
    player2.data[0].x = -1;
    player2.data[0].y = -1;
    player = 2;
    printStep(1, step1);
    printStep(2, step2);
    gotoxy(65, 16);
    printf(" 按 K 来悔棋。");
    gotoxy(65, 17);
    printf(" 按 J 来下子。");
    gotoxy(65, 18);
    printf(" 按 W A S D 控制方向。");
    gotoxy(65, 23);
    GREEN();
    printf("(玩家1 : O)");
    gotoxy(65, 24);
    RED();
    printf("(玩家2 : *)");
    WHITE();
    while(win[1] == 0 && win[2] == 0)
    {
        int tx, ty;
        if(win[1] == 0 && win[2] == 0)
        {
            player = 1;
            playerOperation('O');
            if(ch == 'k' || ch == 'K')
                step2--;
            else
                step1++;
            tx = player1.data[player1.top].x;
            ty = player1.data[player1.top].y;
            printStep(1, step1);
            printStep(2, step2);
            printPos(1, tx, ty);
        }
        jundge(1,'O');
 
        if(win[1] == 0 && win[2] == 0)
        {
            player = 2;
            playerOperation('*');
            if(ch == 'k' || ch == 'K')
                step1--;
            else
                step2++;
            tx = player2.data[player2.top].x;
            ty = player2.data[player2.top].y;
            printStep(1, step1);
            printStep(2, step2);
            printPos(2, tx, ty);
        }
        jundge(2,'*');
    }
    if(win[1] == 1)
    {
        gotoxy(0,31);
        printf("【");
        GREEN();
        printf("玩家1");
        WHITE();
        printf("】 赢了 !\n");
    }
    else if(win[2] == 1)
    {
        gotoxy(0,31);
        printf("【");
        RED();
        printf("玩家2");
        WHITE();
        printf("】 赢了 !\n");
    }
    printf("按回车查看双方下棋路线!\n");
    getchar();
    system("cls");
    if(win[1] == 1)
    {
        GREEN();
        printf("【玩家1】 赢了 !\n");
    }
    else if(win[2] == 1)
    {
        RED();
        printf("【玩家2】 赢了 !\n");
    }
    printRoad();
    getchar();
    return;
}
 
void askExit()//询问退出
{
    char dis[20];
    system("cls");
    printf("确定退出?\n Y/N?\n");
    gets(dis);
    if(strcmp(dis, "y") == 0 || strcmp(dis, "Y") == 0)
    {
        system("cls");
        printf("感谢你的使用!\n");
        exit(0);
    }
    else
    {
        system("cls");
        printMenu();
    }
    return;
}
 
void gotoxy(int x, int y)//移动光标函数
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
 
void printStep(int player, int step)//打印已走步数
{
 
    if(player == 1)
    {
        gotoxy(65,4);
        GREEN();
        printf("【玩家1】 :%02d", step);
        WHITE();
    }
    else if(player == 2)
    {
        gotoxy(65,6);
        RED();
        printf("【玩家2】 :%02d", step);
        WHITE();
    }
    return;
}
 
void printPos(int player, int x, int y)//打印下子坐标
{
    if(player == 1)
    {
        gotoxy(65, 10);
        GREEN();
        if(x == -1 && y == -1)
        {
            printf("                                  ");
        }
        else
        {
            printf("玩家1下棋位置:第%d行,第%d列",8 + x, 8 + y);
        }
        WHITE();
    }
    else if(player == 2)
    {
        gotoxy(65, 10);
        RED();
        if(x == -1 && y == -1)
        {
            printf("                                   ");
        }
        else
        {
            printf("玩家2下棋位置:第%d行,第%d列",8 + x, 8 + y);
        }
        WHITE();
    }
 
    return;
}
 
void printRoad()//打印所有下子坐标
{
    int i;
    GREEN();
    printf("玩家1的下棋路线为:\n");
    for(i = 1; i <= player1.top; i++)
    {
        printf("[ 第%d步 : (%d, %d) ]   ", i, 8 + player1.data[i].x, 8 + player1.data[i].y);
        if(i % 5 == 0)
            printf("\n");
    }
    printf("\n");
 
    RED();
    printf("玩家2的下棋路线为:\n");
    for(i = 1; i <= player2.top; i++)
    {
        printf("[ 第%d步 : (%d, %d) ]   ", i, 8 + player2.data[i].x, 8 + player2.data[i].y);
        if(i % 5 == 0)
            printf("\n");
    }
    printf("\n");
    WHITE();
    printf("输入 E 结束查看并返回菜单!");
 
    while(1)
    {
        scanf("%c", &ch);
        if(ch == 'E' || ch == 'e')
            break;
    }
    return;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 基于C语言实现五子棋游戏完整实例代码

    本文实例讲述了基于C语言实现五子棋游戏的方法,代码备有比较完整的注释,可以帮助读者更好的加以理解. 五子棋游戏代码如下: /* * 使用键盘的上下左右键移动棋盘,空格键表示下棋,ESC键退出程序 */ #include <stdio.h> #include <stdlib.h> #include <bios.h> #include <graphics.h> #include<malloc.h> /* * 对应键盘键的十六进制数字 */ #defi

  • C语言实现五子棋人人对战

    利用简单的c语言基础实现最简单的功能,界面比较丑陋主要是刚学完c的一个小实践,未使用MFC所以界面没有很好看 ,主要目的加强对c语言的理解与运用,同时增加自己的代码量. 首先要学一些头文件可以看我的博客前面的文章 要用到到的头文件stdio.h stdlib.h windows.h time.h conio.h 思路就是 1.画个棋盘,使用数组来代替初始化出* 2.使用循环使双方轮流下棋,使用数组存放棋子的位置 3.判断是否有一方获胜 首先打印棋盘 #include<stdio.h> #inc

  • C语言实现简单五子棋小游戏

    五子棋简单功能实现,供大家参考,具体内容如下 游戏功能演示 代码如下: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <getch.h> // 棋盘 char board[15][15]; // 棋子坐标 char kx = 7 , ky = 7; // 角色 char role = '@'; // 显示棋盘 void show_board(void) { syste

  • C语言版五子棋游戏的实现代码

    这次看了几位大佬的做法,我们也做了很多修改尝试.算是第一次尝试合作完成项目. 我认为学到的东西: 1.第一次尝试写项目,把函数分到不同的.c文件中后更好看了一些. 2.在研究过程中应该分清主次.代码正确运行才是基础要求,其他什么美化界面,调字体调颜色都并非重点. 3.从代码中学到的,①是采用落子数来判断该轮到黑方下还是白方下(落子数为2的倍数时黑方下,否则白方下),这样也能判断出是哪一方胜利,而且落子数还可以判断棋盘是否下满(==16*16时棋盘落满,平局) ②是胜利条件的判断,以中间子为参考,

  • C语言实现五子棋小游戏

    首先我们先来看一个稍微简单些的实现方式: #include <stdio.h> #include <stdlib.h> #define N 15 int chessboard[N + 1][N + 1] = { 0 }; int whoseTurn = 0; void initGame(void); void printChessboard(void); void playChess(void); int judge(int, int); int main(void) { init

  • C语言实现简易五子棋小游戏

    本文实例为大家分享了C语言实现简单五子棋小游戏的具体代码,供大家参考,具体内容如下 效果图如下: 设计思路: 棋盘设计为15×15格,初始状态光标在棋盘的中央,白棋先走,轮流落子,当一方连成五子或下满棋盘时,游戏结束(连成五子的一方获胜,下满棋盘为和棋).当游戏一方胜利后显示胜利信息,提示信息利用汉字点阵输出.程序游戏是一个二维平面图,可用二维数组来实现,数组两个下标可以表示棋盘上的位置,数组元素的值代表棋格上的状态,共有三种情况,分别是0代表空格,1代表白棋,2代表黑棋.程序的主要工作是接收棋

  • C语言实现五子棋游戏

    本文实例为大家分享了C语言实现五子棋的具体代码,供大家参考,具体内容如下 #include <stdio.h> #include <bios.h> #include <ctype.h> #include <conio.h> #include <dos.h> #define CROSSRU 0xbf /*右上角点*/ #define CROSSLU 0xda /*左上角点*/ #define CROSSLD 0xc0 /*左下角点*/ #defin

  • C语言实现简易五子棋

    本文实例为大家分享了C语言实现简易五子棋的具体代码,供大家参考,具体内容如下 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 5 #define COL 5 char g_broad[ROW][COL]; void menu(); void menu() { printf("--------------------

  • 纯C语言实现五子棋

    正在考虑增加一个MFC界面.不是人机对战的. 五子棋.c //date 2014年7月7日09:53:24 //willows //五子棋 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <assert.h> //棋盘初始化函数 //Chessboard棋盘数组,ln=棋盘大小,成功返回Chessboard,不成功NULL void init_Chessboa

  • C语言编写五子棋游戏

    本文实例为大家分享了C语言编写五子棋游戏的具体代码,供大家参考,具体内容如下 一.构建棋盘 首先可以用一个二维数组来构建棋盘,下标表示位置,内容表示黑子白子或者空位.当数组内容为1时,该位置为白字,当数组为0时,该位置为白子,空位输出+ int w[11][11], flag = 0; int a, b; for (int k = 0; k < 11; k++) printf("第%d列\t", k); printf("\n"); for (int i = 0

随机推荐