五子棋的c++代码

设计步骤

很久没写这样的小游戏了,就是想写一个五子棋小游戏,以后代码有改进的地方我会继续发帖的,希望大家多多指导。游戏包含7个部分:五子棋的欢迎界面、棋盘初始化界面、游戏规则说明部分、棋子和棋盘显示界面、判断下棋点是否越界或已有棋子(我分成黑棋、白棋两个部分)、判断输赢。

游戏运行截图

五子棋的c++代码_第1张图片这里是最开始的欢迎界面
五子棋的c++代码_第2张图片这里是棋子在棋盘中每个点及其显示
后面的测试部分就上图了,大家可以自行测试,没毛病,哈哈哈哈!

游戏代码

#include “stdafx.h”
#include< iostream>
#include< cstdlib>
#include //包含Sleep()函数
#include< algorithm>
using namespace std;
const int row = 15;
const int col = 15;
int ch;
bool flag = true;
bool game_over = false;
class Wuziqi
{
private:
char p[row][col];
int i1, j1;
public:
void welcome(); //欢迎界面
void initil(); //初始化界面
void expore(); //游戏说明
void show_qipan(); //显示棋及棋盘
void judgeOut_hei(); //判断下棋点是否越界或有棋子
void judgeOut_bai();
void judgeWin(); //判断输赢
};
void Wuziqi::welcome() //这个界面之后有个system(“cls”);
{
cout << “欢迎来到五子棋!” << endl;
cout << ’ ’ << “1.开始游戏” << endl;
cout << ’ ’ << “2.游戏说明” << endl;
cout << ’ ’ << “3.退出游戏” << endl;
cout << “请输入你的选择” << endl;
cin >> ch;
}
void Wuziqi::expore()
{
cout << “这个游戏是对战双方分别输入下棋点的x、y坐标来确定你所想落子的位置!” << endl;
cout << “向下为y轴,向右为x轴,i为行,j为列!” << endl;
cout << “对战双方交替下棋,先在横向或者纵向想连续有5个子的一方获胜!” << endl;
cout << “黑子先行落子” << endl;
cout << “玩家1的棋子用1表示!” << endl;
cout << “玩家2的棋子用2表示!” << endl;
}
void Wuziqi::initil()
{
for (int i = 0;i < row;i++)
for (int j = 0;j < col;j++)
p[i][j] = ‘_’;
for (int i = 0;i {
for (int j = 0;j cout << p[i][j];
cout << endl;
}
}
void Wuziqi::judgeOut_hei()
{
int x, y;
if (flag == true)
{
cout << “请输入你想下黑子的坐标:” << endl;
cin >> x;
cin >> y;
while (x < 0 || x >= row || y < 0 || y >= col)
{
cout << “你选择的落子点不再棋盘上,请重新输入一个棋盘上的点表示你要落子的位置:” << endl;
cin >> x;
cin >> y;
}
while (p[x][y] == ‘1’ || p[x][y] == ‘2’)
{
cout << “你落子的位置已有棋子,请重新下子:” << endl;
cin >> x;
cin >> y;
}
p[x][y] = ‘1’;
i1 = x;
j1 = y;
flag = false;
}
}
void Wuziqi::judgeOut_bai()
{
int x, y;
if (flag == false)
{
cout << “请输入你想下白子的坐标:” << endl;
cin >> x;
cin >> y;
while (x < 0 || x >= row || y < 0 || y >= col)
{
cout << “你选择的落子点不再棋盘上,请重新输入一个棋盘上的点表示你要落子的位置:” << endl;
cin >> x;
cin >> y;
}
while(p[x][y] == ‘1’ || p[x][y] == ‘2’)
{
cout << “你落子的位置已有棋子,请重新下子:” << endl;
cin >> x;
cin >> y;
}
p[x][y] = ‘2’;
i1 = x;
j1 = y;
flag = true; //落子之后,flag变为true;
}
}
void Wuziqi::judgeWin()
{
const int sz = 5;
for(int i=0;i for (int j = 0;j < col;j++)
{
if (p[i][j] == ‘1’&&p[i][j + 1] == ‘1’&&p[i][j + 2] == ‘1’&&p[i][j + 3] == ‘1’&&p[i][j + 4] == ‘1’)
{
cout << “黑棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘1’&&p[i + 1][j] == ‘1’&&p[i + 2][j] == ‘1’&&p[i + 3][j] == ‘1’&&p[i + 4][j] == ‘1’)
{
cout << “黑棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘1’&&p[i + 1][j + 1] == ‘1’&&p[i + 2][j + 2] == ‘1’&&p[i + 3][j + 3] == ‘1’&&p[i + 4][j + 4] == ‘1’)
{
cout << “黑棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘1’&&p[i + 1][j - 1] == ‘1’&&p[i + 2][j - 2] == ‘1’&&p[i + 3][j - 3] == ‘1’&&p[i + 4][j - 4] == ‘1’)
{
cout << “黑棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘2’&&p[i][j + 1] == ‘2’&&p[i][j + 2] == ‘2’&&p[i][j + 3] == ‘2’&&p[i][j + 4] == ‘2’)
{
cout << “白棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘2’&&p[i + 1][j] == ‘2’&&p[i + 2][j] == ‘2’&&p[i + 3][j] == ‘2’&&p[i + 4][j] == ‘2’)
{
cout << “白棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘2’&&p[i + 1][j + 1] == ‘2’&&p[i + 2][j + 2] == ‘2’&&p[i + 3][j + 3] == ‘2’&&p[i + 4][j + 4] == ‘2’)
{
cout << “白棋获胜” << endl;
game_over = true;
}
if (p[i][j] == ‘2’&&p[i + 1][j - 1] == ‘2’&&p[i + 2][j - 2] == ‘2’&&p[i + 3][j - 3] == ‘2’&&p[i + 4][j - 4] == ‘2’)
{
cout << “白棋获胜” << endl;
game_over = true;
}
}
if (all_of(&p[0][0], &p[row][col], [](int x) {return(x == ‘1’ || x == ‘2’);}))
{
cout << “游戏结果是平局!” << endl;
game_over = true;
}
}
void Wuziqi::show_qipan()
{
for (int i = 0;i < row;i++)
{
for (int j = 0;j < col;j++)
cout << p[i][j];
cout << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Wuziqi wz;
int x, y;
wz.welcome();
if (ch == 2)
{
wz.expore();
Sleep(1000);
}if (ch == 3)
game_over = true;
if (ch == 1)
{
system(“cls”);
wz.initil();
while (!game_over)
{
Sleep(1000);
system(“cls”);
wz.judgeOut_hei();
wz.show_qipan();
wz.judgeWin();
if (game_over == true)
break;
Sleep(1000);
system(“cls”);
wz.judgeOut_bai();
wz.show_qipan();
wz.judgeWin();
}
}
system(“pause”);
return 0;
}

你可能感兴趣的:(c++,小游戏)