使用VS制作一个小游戏(五子棋)

使用VS制作一个小游戏(五子棋)

一、前期准备:

下载Visual Studio(以下使用的是VS2017)
下载Easyx库:官方easyx库下载 右上方下载

二、思路理解

(本次是简单的单机双人五子棋)

1、初始化一个15*15的棋盘,将空棋盘每个位置赋值0

2、若玩家1放下一颗棋子,则将棋盘上的值置为1,玩家2同理

三、完整代码

#include <iostream>
#include<graphics.h>
using namespace std;

int a[15][15] = { 0 }; //定义棋盘的全局变量
MOUSEMSG mouse; //定义鼠标的全局变量

//自定义的子函数
void init();		//初始化界面
void put(int penson, int x, int y); //在二位数组将元素置为非空,相当于放下棋子
void drow_Black(int x, int y);		//画黑棋
void drow_White(int x, int y);		//画白棋
int change(short k);				//将棋盘与二位数组映射,方便判断输赢
void show(int person);				//胜利时显示的界面
bool Check_Win(int person, int x, int y);	//调用各种函数,判断是否胜利
bool Check_LeftAndRight(int person, int x, int y);	//检索棋盘左右是否五颗相连
bool Check_TopAndButton(int person, int x, int y);	//检索棋盘上下是否五颗相连
bool Check_LeftAndDiagonal(int person, int x, int y);	//检索棋盘左斜是否五颗相连
bool Check_RightAndDiagonal(int p
  • 21
    点赞
  • 156
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,以下是一个简单的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; } ``` 这个示例代码实现了一个简单的控制台五子棋游戏。你可以根据自己的需要进行修改和优化。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值