C++制作五子棋小游戏

文章目录

前言

代码部分

不靠谱的结束语


前言

熟悉的味道,游戏,它又回来了······

主要是看游戏这个专栏很,所以今天再来写一篇关于游戏的博客吧。

来看一下运行截图(瞎下的):

 代码也是基于easyx来做的,所以需要安装easyx后才能够运行。

代码部分

直接上干货:

#include <iostream>
#include <graphics.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

class seat
{
  public:
    int i = 0;
    int j = 0;
    int number = 0;
};

class box
{
  public:
    void draw();

  public:
    int x = 0;
    int y = 0;
    int value = -1;
    int modle = 0;
    bool isnew = false;
    COLORREF color = WHITE;
};

void draw();
void init();
seat findbestseat(int color, int c);
void isWIN();
void game();

int main()
{
    initgraph(700, 700);
    setbkcolor(WHITE);
    cleardevice();
    setbkmode(TRANSPARENT);
    while (true)
    {
        init();
        game();
        cleardevice();
    }
}

box BOX[19][19];
int win = -1;
int whoplay = 0;
int playercolor = 0;
int dx[4]{1, 0, 1, 1};
int dy[4]{0, 1, 1, -1};
int Score[3][5] = {
    {0, 80, 250, 500, 500}, {0, 0, 80, 250, 500}, {0, 0, 0, 80, 500}};
int MAXxs[361];
int MAXys[361];
int mylength = 0;

void box::draw()
{
    COLORREF thefillcolor = getfillcolor();
    setlinestyle(PS_SOLID, 2);
    setfillcolor(color);
    solidrectangle(x, y, x + 30, y + 30);
    if (isnew)
    {
        setlinecolor(LIGHTGRAY);
        line(x + 1, y + 2, x + 8, y + 2);
        line(x + 2, y + 1, x + 2, y + 8);
        line(x + 29, y + 2, x + 22, y + 2);
        line(x + 29, y + 1, x + 29, y + 8);
        line(x + 2, y + 29, x + 8, y + 29);
        line(x + 2, y + 22, x + 2, y + 29);
        line(x + 29, y + 29, x + 22, y + 29);
        line(x + 29, y + 22, x + 29, y + 29);
    }
    setlinecolor(BLACK);
    switch (modle)
    {
    case 0:
        line(x + 15, y, x + 15, y + 30);
        line(x - 1, y + 15, x + 30, y + 15);
        break;
    case 1:
        line(x + 14, y + 15, x + 30, y + 15);
        setlinestyle(PS_SOLID, 3);
        line(x + 15, y, x + 15, y + 30);
        setlinestyle(PS_SOLID, 2);
        break;
    case 2:
        line(x - 1, y + 15, x + 15, y + 15);
        setlinestyle(PS_SOLID, 3);
        line(x + 15, y, x + 15, y + 30);
        setlinestyle(PS_SOLID, 2);
        break;
    case 3:
        line(x + 15, y + 15, x + 15, y + 30);
        setlinestyle(PS_SOLID, 3);
        line(x - 1, y + 15, x + 30, y + 15);
        setlinestyle(PS_SOLID, 2);
        break;
    case 4:
        line(x + 15, y, x + 15, y + 15);
        setlinestyle(PS_SOLID, 3);
        line(x - 1, y + 15, x + 30, y + 15);
        setlinestyle(PS_SOLID, 2);
        break;
    case 5:
        setlinestyle(PS_SOLID, 3);
        line(x + 15, y, x + 15, y + 15);
        line(x + 15, y + 15, x + 30, y + 15);
        setlinestyle(PS_SOLID, 2);
        break;
    case 6:
        setlinestyle(PS_SOLID, 3);
        line(x + 15, y, x + 15, y + 15);
        line(x - 1, y + 15, x + 15, y + 15);
        setlinestyle(PS_SOLID, 2);
        break;
    case 7:
        setlinestyle(PS_SOLID, 3);
        line(x - 1, y + 15, x + 15, y + 15);
        line(x + 15, y + 15, x + 15, y + 30);
        setlinestyle(PS_SOLID, 2);
        break;
    case 8:
        setlinestyle(PS_SOLID, 3);
        line(x + 15, y + 15, x + 30, y + 15);
        line(x + 15, y + 15, x + 15, y + 30);
        setlinestyle(PS_SOLID, 2);
        break;
    case 9:
        line(x + 15, y, x + 15, y + 30);
        line(x - 1, y + 15, x + 30, y + 15);
        setfillcolor(BLACK);
        setlinestyle(PS_SOLID, 1);
        fillci
  • 12
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的C++五子棋小游戏代码示例: ```c++ #include <iostream> #include <vector> using namespace std; const int BOARD_SIZE = 15; // 棋盘大小 class ChessBoard { public: enum ChessType { EMPTY = 0, BLACK, WHITE }; ChessBoard() { for (int i = 0; i < BOARD_SIZE; ++i) { board_.push_back(vector<ChessType>(BOARD_SIZE, EMPTY)); } } bool PutChess(int x, int y, ChessType type) { if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board_[x][y] != EMPTY) { return false; } board_[x][y] = type; return true; } bool IsWin(int x, int y, ChessType type) { int count = 0; for (int i = x + 1; i < BOARD_SIZE && board_[i][y] == type; ++i) { // 横向 ++count; } for (int i = x - 1; i >= 0 && board_[i][y] == type; --i) { // 横向 ++count; } if (count >= 4) { return true; } count = 0; for (int i = y + 1; i < BOARD_SIZE && board_[x][i] == type; ++i) { // 纵向 ++count; } for (int i = y - 1; i >= 0 && board_[x][i] == type; --i) { // 纵向 ++count; } if (count >= 4) { return true; } count = 0; for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board_[i][j] == type; ++i, ++j) { // 正对角线 ++count; } for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board_[i][j] == type; --i, --j) { // 正对角线 ++count; } if (count >= 4) { return true; } count = 0; for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board_[i][j] == type; ++i, --j) { // 反对角线 ++count; } for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board_[i][j] == type; --i, ++j) { // 反对角线 ++count; } if (count >= 4) { return true; } return false; } void Print() { for (int i = 0; i < BOARD_SIZE; ++i) { for (int j = 0; j < BOARD_SIZE; ++j) { switch (board_[i][j]) { case EMPTY: cout << "+ "; break; case BLACK: cout << "X "; break; case WHITE: cout << "O "; break; } } cout << endl; } } private: vector<vector<ChessType>> board_; }; int main() { ChessBoard board; ChessBoard::ChessType cur = ChessBoard::BLACK; int x, y; while (true) { cout << "Player " << (cur == ChessBoard::BLACK ? "X" : "O") << " please input the position (x y): "; cin >> x >> y; if (board.PutChess(x, y, cur)) { board.Print(); if (board.IsWin(x, y, cur)) { cout << "Player " << (cur == ChessBoard::BLACK ? "X" : "O") << " win!" << endl; break; } cur = (cur == ChessBoard::BLACK ? ChessBoard::WHITE : ChessBoard::BLACK); } else { cout << "Invalid input!" << endl; } } return 0; } ``` 这个示例代码实现了一个简单的控制台五子棋小游戏。你可以根据自己的需要进行修改和优化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值