c++ - 五子棋获胜条件c ++使用指针和数组
问题描述
我目前正在用 C++ 开发五子棋游戏。但我坚持获胜条件。我是 C++ 新手。我需要为此游戏添加使用指针的获胜条件。请我需要帮助。我不知道如何开始。到目前为止,我只能将玩家移动插入到数组中。我需要指针来确定获胜条件的 8 个方向。
获胜条件是:水平、垂直、对角线连续 5 次(但也必须更改为连续 3 次、连续 4 次等)
头文件
// file goboard.h
class boardBox{
public:
char PlayerColor; //z or W // 7 0 1
boardBox* neighbours[8]; // 6 2
boardBox( ); // 5 4 3
};//boardBox
class goboard {
private:
boardBox* entrance;
int height, width;
static const int gridX = 6;
static const int gridY = 7;
char grid[gridX][gridY];
// TODO
public:
void ask_turn(char symb);
goboard ( );
goboard (int height, int width);
~goboard ( );
void showBoard( );
void generate();
// TODO
};//goboard
此文件与头文件链接
// file goboard.cc
#include <iostream>
#include "goboard.h"
using namespace std;
goboard::goboard ( ) {
// TODO
}//goboard::goboard
goboard::~goboard ( ) {
// TODO
}//goboard::~goboard
void goboard::generate()
{
cout << "Board shows like this." << endl;
int number = 1;
for(int x = 0; x < gridX; x++)
{
for(int y = 0; y < gridY; y++)
{
grid[x][y] = '.';
}
}
}
void goboard::showBoard( ) {
printf("\n................\n");
for(int x = 0; x < gridX; x++)
{
for(int y = 0; y < gridY; y++)
{
printf("%c |", grid[x][y]);
}
printf("\n");
}
cout<<endl;
// TODO
}//goboard::showBoard
void goboard::ask_turn(char symb) //symb is symbol Z or W
{
int input;
int input2;
while( true )
{
cout<<"Where would you like to play?"<<endl;
cin>>input;
cin>>input2;
int index = input;
int index2 = input2;
int row = index;
int col = index2;
char grid_position = grid[row][col];
if(grid_position == 'Z' || grid_position == 'W')
{
puts("That grid position is already take!");
}else{
grid[row][col] = symb;
break;
}
}
}
// TODO
主文件
// file hoofd.cc
#include <iostream>
#include <string>
#include "gobord.h"
#define GRID_SIZE 3
using namespace std;
int main (int argc, char *argv[] ) {
goboard Goboard;
Goboard.generate();
char symb = 'Z';
char symb1 = 'W';
while(true){
Goboard.showBoard( );
Goboard.ask_turn(symb);
Goboard.showBoard();
Goboard.ask_turn(symb1);
}
return 0;
}//main
解决方案
编辑:您需要将数据结构从 a 更改char[][]
为 a boardBox[][]
。在您的构造函数中,您需要按如下方式修复每个boardBox
'数组:neighbours
goboard::goboard() {
for (int r = 0; r < gridX; r++) {
for (int c = 0; c < gridY; c++) {
boardBox& box = grid[r][c];
box.neighbours[0] = get_box(r-1, c);
box.neighbours[1] = get_box(r-1, c+1);
// and so on
}
}
}
boardBox* goboard::get_grid(int row, int col) {
return in_board(row, col) ? &grid[row][col] : nullptr;
}
如果该单元格超出范围,则get_grid
返回指向 的指针或空指针。boardBox
获胜条件在移动之后立即发生,因此从逻辑上讲,刚刚放置的棋子必须是任一方向的五连棋的一部分。
让我们首先实现一个函数,该函数遵循某个方向并计算该方向上给定符号的数量:
int count_direction(boardBox *box, int direction, char symb) {
int count = 0;
while (box) {
if (box->playerColor != symb)
break;
box = box->neighbours[direction];
}
return count;
}
它跟随neighbours
数组中的指针,直到我们到达棋盘的末端,用空指针表示。
我们可以使用它来计算沿对角线的数量:
boardBox *box = get_grid(row, col);
int count_diagonal1 = count_direction(box, 3, symb) // down and to the right
+ count_direction(box, 7, symb) // up and to the left
- 1; // count_direction visits (row,col) twice!
实施其他方向并根据计数确定获胜条件由您决定:)
推荐阅读
- php - 为什么我的请求在使用 POST 时在 PHP/Laravel 中为空
- math - 有向图的最大数量
- wpf - WPF 数据网格 所选行的背景颜色
- c# - GetComponentInParent () 不改变父组件
- vhdl - VHDL 无运算符“-”的函数声明
- c++ - Windows Embedded Compact 7 上的意外 IConnectionPointImpl::Unadvise 调用
- python-3.x - Python 顺序优先级(在 vs “==” 中)
- javascript - 有没有一种方法可以提取具有键值对的对象的值,其中键具有“。”
- raspberry-pi - 使用华为加密狗发送短信
- php - PHP 标题更改,包括标题