五子棋的c++代码

设计步骤

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

游戏运行截图

在这里插入图片描述这里是最开始的欢迎界面
在这里插入图片描述这里是棋子在棋盘中每个点及其显示
后面的测试部分就上图了,大家可以自行测试,没毛病,哈哈哈哈!

游戏代码

#include “stdafx.h”
#include< iostream>
#include< cstdlib>
#include<Windows.h> //包含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<row;i++)
{
for (int j = 0;j<col;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<row;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;
}

  • 17
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的 Python 五子棋游戏代码示例: ```python import numpy as np # 初始化棋盘 def create_board(): board = np.zeros((15,15)) return board # 判断当前位置是否可以下棋 def is_valid_move(board, row, col): return board[row][col] == 0 # 下棋 def place_piece(board, row, col, piece): board[row][col] = piece # 判断胜负 def is_win(board, piece): # 判断行 for r in range(15): for c in range(11): if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece and board[r][c+4] == piece: return True # 判断列 for r in range(11): for c in range(15): if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece and board[r+4][c] == piece: return True # 判断斜线 for r in range(11): for c in range(11): if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece and board[r+4][c+4] == piece: return True if board[r][c+4] == piece and board[r+1][c+3] == piece and board[r+2][c+2] == piece and board[r+3][c+1] == piece and board[r+4][c] == piece: return True return False # 打印棋盘 def print_board(board): print(board) # 主函数 def play_game(): board = create_board() print_board(board) game_over = False turn = 1 while not game_over: # 玩家1下棋 if turn == 1: row = int(input("Player 1, enter row(0-14):")) col = int(input("Player 1, enter column(0-14):")) if is_valid_move(board, row, col): place_piece(board, row, col, 1) if is_win(board, 1): print("Player 1 wins!") game_over = True turn = 2 else: print("Invalid move! Try again.") # 玩家2下棋 else: row = int(input("Player 2, enter row(0-14):")) col = int(input("Player 2, enter column(0-14):")) if is_valid_move(board, row, col): place_piece(board, row, col, 2) if is_win(board, 2): print("Player 2 wins!") game_over = True turn = 1 else: print("Invalid move! Try again.") print_board(board) play_game() ``` 这个示例中使用了 numpy 库来创建二维数组表示棋盘,使用了简单的循环判断胜负和玩家下棋。你可以根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值