这个小游戏是在2020年5月份无聊写的,代码量不大,权当娱乐哈
基本思路:
1.创建一个15*15棋盘类,并设计相关函数(输出棋盘,下黑棋,下白棋等)
2.编写judge()函数,判断胜负条件
3.主函数思路:黑方先下,每次不断更新棋盘的元素:如果该处已经有棋子了,则重新下一次,以此不断循环直到游戏结束。
代码如下:
1.头文件
//wuziqi.h
#ifndef _WUZIQI_H_
#define _WUZIQI_H_
#include <iostream>
using namespace std;
class chessboard;
class chessboard
{
public:
int judge();
void build();//建立棋盘
void changeblack(int goalx, int goaly, int& goal);//改变棋盘数组元素即更新棋子
void changewhite(int goalx, int goaly, int& goal);
void out();//输出棋盘
private:
string arr[15][15];
};
#endif // !_WUZIQI_H_
2.cpp文件
//wuziqi.cpp
#include <iostream>
#include "wuziqi.h"
using namespace std;
int chessboard::judge()
{
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
if (//横一样
arr[i][j] == "●" &&
arr[i][j + 1] == "●" &&
arr[i][j + 2] == "●" &&
arr[i][j + 3] == "●" &&
arr[i][j + 4] == "●"
) return 0;//不能使用连等
}
for (int j = 0; j < 11; j++)
{
if (//竖一样
arr[j][i] == "●" &&
arr[j + 1][i] == "●" &&
arr[j + 2][i] == "●" &&
arr[j + 3][i] == "●" &&
arr[j + 4][i] == "●"
) return 0;
}
for (int j = 0; j < 11; j++)
{
if (//顺斜一样
arr[i][j] == "●" &&
arr[i + 1][j + 1] == "●" &&
arr[i + 2][j + 2] == "●" &&
arr[i + 3][j + 3] == "●" &&
arr[i + 4][j + 4] == "●"
) return 0;
}
for (int j = 14; j > 3; j--)
{
if (//逆斜一样
arr[i][j] == "●" &&
arr[i + 1][j - 1] == "●" &&
arr[i + 2][j - 2] == "●" &&
arr[i + 3][j - 3] == "●" &&
arr[i + 4][j - 4] == "●"
) return 0;
}
for (int j = 0; j < 11; j++)
{
if (//横一样
arr[i][j] == "○" &&
arr[i][j + 1] == "○" &&
arr[i][j + 2] == "○" &&
arr[i][j + 3] == "○" &&
arr[i][j + 4] == "○"
) return 0;//不能使用连等
}
for (int j = 0; j < 11; j++)
{
if (//竖一样
arr[j][i] == "○" &&
arr[j + 1][i] == "○" &&
arr[j + 2][i] == "○" &&
arr[j + 3][i] == "○" &&
arr[j + 4][i] == "○"
) return 0;
}
for (int j = 0; j < 11; j++)
{
if (//顺斜一样//列从0开始
arr[i][j] == "○" &&
arr[i + 1][j + 1] == "○" &&
arr[i + 2][j + 2] == "○" &&
arr[i + 3][j + 3] == "○" &&
arr[i + 4][j + 4] == "○"
) return 0;
}
for (int j = 14; j > 3; j--)
{
if (//逆斜一样//列从14开始
arr[i][j] == "○" &&
arr[i + 1][j - 1] == "○" &&
arr[i + 2][j - 2] == "○" &&
arr[i + 3][j - 3] == "○" &&
arr[i + 4][j - 4] == "○"
) return 0;
}
}
return 1;
}
//棋盘--------------------------------------------------------------------------------------
void chessboard::build()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
arr[i][j] = " ";
}
}//初始化棋盘
}
void chessboard::changeblack(int goalx, int goaly, int& goal)//更新点位
{
if (arr[goalx][goaly] != " ")
{
cout << "这一点已经有子了,请重新下一次黑棋" << endl;
goal--;
}
else
arr[goalx][goaly] = "○";
}
void chessboard::changewhite(int goalx, int goaly, int& goal)//更新点位
{
if (arr[goalx][goaly] != " ")
{
cout << "这一点已经有子了,请重新下一次白旗" << endl;
goal--;
}
else
arr[goalx][goaly] = "●";
}
void chessboard::out()
{
cout << " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 " << endl;
for (int i = 0; i < 15; i++)
{
cout << "---------------------------------------------------------------" << endl;
if (i < 10)
cout << i << " | ";
else
cout << i << "| ";
for (int j = 0; j < 15; j++)
{
cout << arr[i][j] << "| ";
}
cout << endl;
}//输出棋盘,每次输入一个点就输出一次棋盘
cout << "---------------------------------------------------------------" << endl;
}
//main.cpp
#include <iostream>
#include "wuziqi.h"
using namespace std;
int main(void)
{
chessboard goalboard;//目标棋盘
goalboard.build();
goalboard.out();
for (int i = 1; i < 10000; i++)
{
if (i % 2 != 0)//奇数黑方走,偶数白方走
{
int x = 0, y = 0;
cout << "请输入黑棋x坐标和y坐标" << endl;
cin >> y >> x;
goalboard.changeblack(x, y, i);//持续更新黑棋盘
goalboard.out();
if (goalboard.judge() == 0)
{
cout << "游戏结束 黑棋胜利!" << endl;
cout << "输入任意坐标重新开始" << endl;
goalboard.build();
}
}
else
{
int x = 0, y = 0;
cout << "请输入白棋x坐标和y坐标" << endl;
cin >> y >> x;
goalboard.changewhite(x, y, i);//持续更新白棋盘
goalboard.out();
if (goalboard.judge() == 0)
{
cout << "游戏结束 白旗胜利!" << endl;
cout << "输入任意坐标重新开始" << endl;
goalboard.build();
}
}
}
return 0;
}
运行效果图:
PS:
这个小游戏还有不少不足之处,如输入棋子坐标超过棋盘大小怎么办?胜负的判定是否还有更简单的方法?都值得再去思考完善。
菜鸡一枚难免有很多不足之处,欢迎指教!感激不尽!