我目前正在用 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