c++游戏——四子棋

在这里插入图片描述

欢迎各位点赞和评论,若有问题,欢迎提出

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <termio.h>
using namespace std;

const int BLACK = 1;
const int WHITE = -1;
const int INF = 100000000;
int num[10][10]; // 值为0表示没有值,非0表示有数据
int rd = 0; // 回合数
int pos = 1; // 所在列
int color = BLACK;

int getch() // 不回显函数
{
    struct termios nts, ots;
    // 得到当前终端(0表示标准输入)的设置
    if (tcgetattr(0, &ots) < 0) return EOF;
    // 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理
    nts = ots;
    cfmakeraw(&nts); 
    // 设置上更改之后的设置
    if (tcsetattr(0, TCSANOW, &nts) < 0) return EOF;
    // 设置还原成老的模式
    int cr;
    cr = getchar();
    if (tcsetattr(0, TCSANOW, &ots) < 0)  return EOF;
    return cr;
}

void moveDown() // 向下移动
{   
    rd++;
    for (int i = 6; i >= 1; i--)
    {
        if (num[i][pos] == 0)
        {
            num[i][pos] = color;
            color *= -1;
            return;
        }
    }
}

void moveRight() // 向右移动
{   
    if (pos < 7) pos++;
}

void moveLeft() // 向左移动
{   
    if (pos > 1) pos--;
}

int findWinner() // 判断游戏是否结束
{
    for (int i = 1; i <= 6; i++)
    {
        for (int j = 1; j <= 7; j++)
        {
            if (num[i][j] == 0) continue;
            // 竖
            if (i + 3 <= 6 && num[i][j] == num[i+1][j] && num[i][j] == num[i+2][j] && num[i][j] == num[i+3][j])
                return num[i][j];
            // 横
            if (j + 3 <= 7 && num[i][j] == num[i][j+1] && num[i][j] == num[i][j+2] && num[i][j] == num[i][j+3])
                return num[i][j];
            // 斜
            if (i + 3 <= 6 && j + 3 <= 7 && num[i][j] == num[i+1][j+1] && num[i][j] == num[i+2][j+2] && num[i][j] == num[i+3][j+3])
                return num[i][j];
            // 斜
            if (i + 3 <= 6 && j - 3 >= 1 && num[i][j] == num[i+1][j-1] && num[i][j] == num[i+2][j-2] && num[i][j] == num[i+3][j-3])
                return num[i][j];
        }
    }
    return 0;
}

void drawBoard() // 打印当前表格
{
    cout << "   ";
    for (int i = 1; i <= 7; i++) 
    {
        if (i == pos)
        {
            if (color == BLACK) cout << " ●  ";
            else cout << " ○  ";
        }
        else cout << "    ";
    }
    cout << "\n";
    // 输出上边框
    cout << "  ╔";
    for (int i = 1; i <= 6; i++) cout << "═══╤";
    cout << "═══╗\n";
    // 输出中间部分
    for (int i = 1; i <= 6; i++) // 行
    {
        cout << "  ║";
        for (int j = 1; j <= 7; j++) // 列
        {
            if (num[i][j] == BLACK)
            {
                cout << " ● ";
            } 
            else if (num[i][j] == WHITE)
            {
                cout << " ○ ";
            }
            else cout << "   ";
            if (j != 7)
                cout << "│";
            else
                cout << "║";
        }
        cout << " \n";
        // 输出下边框
        if (i != 6)
        {
            cout << "  ╟";
            for (int i = 1; i <= 6; i++) cout << "───┼";
            cout << "───╢\n";
        }
        else
        {
            cout << "  ╚";
            for (int i = 1; i <= 6; i++) cout << "═══╧";
            cout << "═══╝\n";
        }
    }
}

bool tie()
{
    for (int j = 1; j <= 7; j++)
    {
        if (num[1][j] == 0) return false;
    }
    return true;
}

void game() // 开始游戏
{
    cout << "\033c" << flush;
    cout << "**********************************" << endl;
    cout << "—————————— Ready Go~ ————————————" << endl;
    cout << "**********************************" << endl;
    getch();
    cout << "\033c" << flush;
    drawBoard(); // 打印棋盘
    
    char ch = 0;
    while (true) // 当游戏没结束时
    {
        cout << "Key:" << ch << " Round:" << rd << endl;
        cout << "Tips:" << endl;
        cout << "s —— 下棋" << endl;
        cout << "a —— 左移" << endl;
        cout << "d —— 右移" << endl;
        cout << "Esc —— 退出游戏" << endl;
        cout << "n —— 开始新游戏" << endl;
        cout << "选择以上指令!" << endl;
        
        ch = getch(); // 输入操作指令
        if (ch == 's') moveDown(); //下键
        else if (ch == 'a') moveLeft(); //左键
        else if (ch == 'd') moveRight(); //右键
        else if (ch == 27) exit(0);
        else if (ch == 'n') // 开始新游戏
        {
            rd = 0; // 清空操作次数
            memset(num, 0, sizeof(num)); // 清空表格数据
            return; 
        }
        else
        {
            cout << endl << "———————输入有误,请重新输入指令!———————" << endl << endl;
            continue;
        }
        cout << "\033c" << flush;
        drawBoard(); // 打印棋盘
        
        int winner = findWinner();
        if (winner != 0)
        {
            if (winner == BLACK)
            {
                cout << " ● ";
            } 
            else if (winner == WHITE)
            {
                cout << " ○ ";
            }
            cout << "获胜" << endl;
            break;
        }
        if (tie()) // 判断平局
        {
            cout << "平局" << endl;
            break;
        }
    }
    cout << "再来一局~" << endl;
    getch();
}

void intro() // 游戏规则展示
{
	cout << "\033c" << flush;
    cout << "***********************************************************" << endl;
    cout << "欢迎来到 四子棋 游戏~" << endl;
    cout << "在这个游戏里会有一个 6*7 的表格" << endl;
    cout << "可以选择 a←、s↓、d→,选择某一列" << endl;
    cout << "当 上下或斜线上四子连成一条直线 游戏结束!" << endl; 
    cout << "点击【ENTER】开始游戏" << endl;
    cout << "点击【ESC】退出游戏" << endl;
    cout << "***********************************************************" << endl;
}

void init()
{
	srand(time(0));
	memset(num, 999999, sizeof(num));
    for (int i = 1; i <= 6; i++)
        for (int j = 1; j <= 7; j++)
            num[i][j] = 0;
	rd = 0; // 回合数
	pos = 1; // 所在列
	color = BLACK; // 黑先
}

int main()
{
    while (true)
    {
    	init(); // 初始化
        intro(); // 游戏规则展示
        char ch;
        ch = getch(); // 不回显函数,当用户按下某个字符时,函数自动读取,无需按回车
        if (ch == 13) // CR回车的ascii是13
        {
            game(); // 开始游戏
        }
        else if (ch == 27) // ESC的ascii是27
        {
            cout << "退出游戏!" << endl;
            break;
        }
        else  cout << "输入有误,请再次输入!" << endl;
    }
    return 0;
}
小猴编程(7930525716622733
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值