C++ object oriented implementation of gobang game
- 2020-04-02 02:59:50
- OfStack
Try to incorporate object-oriented ideas into your programs
ChessBoard. H
//ChessBoard.h
#pragma once
#define ROW 15
#define COL 15
#include<iostream>
using namespace std;
class ChessBoard//A board
{
public:
char m_cSquare[ROW][COL];
public:
ChessBoard();
void show();
};
ChessBoard. CPP
//ChessBoard.cpp
#include"ChessBoard.h"
ChessBoard::ChessBoard()
{
for(int i=1;i<ROW-1;i++)
for(int j=1;j<COL-1;j++)
m_cSquare[i][j]=' ';
for(int j=0;j<COL;j++)
m_cSquare[0][j]=m_cSquare[ROW-1][j]='-';
for(int i=1;i<ROW;i++)
m_cSquare[i][0]=m_cSquare[i][COL-1]='|';
}
void ChessBoard::show()
{
system("cls");
for(int i=0;i<ROW;i++)
{ for(int j=0;j<COL;j++)
cout<<m_cSquare[i][j]<<' ';//Here "<<"" is important so that the ROW*COL output on the screen is square
cout<<endl;
}
}
Player. H
//Player.h
#pragma once
//Macros define four directions in which to detect whether the five particles are connected or not
#define HORIZON 0
#define VERTICAL 1
#define LEFTBOTTOMTORIGHTTOP 2
#define LEFTTOPTORIGHTBOTTOM 3
#include"ChessBoard.h"
#include<iostream>
using namespace std;
#include<string>
class Player
{
private:
string m_name;
char m_chessType;
int m_x;
int m_y;
ChessBoard* m_ptBoard;
public:
Player(string name,char chessType):m_name(name),m_chessType(chessType),m_ptBoard(NULL){}
void attachToBoard(ChessBoard* ptBoard){m_ptBoard=ptBoard;}
bool isInChessBoard(int x,int y);
bool isLine(int x,int y);
bool isWin();
void setChess();
};
Player. CPP
//Player.cpp
#include"Player.h"
bool Player::isInChessBoard(int x,int y)
{
if(x<ROW-1 && x>0 && y<COL-1 && y>0)
return true;
else
return false;
}
bool Player::isLine(int x,int y)
{
for(int direc=HORIZON;direc<=LEFTTOPTORIGHTBOTTOM;direc++)//Four directions, quick fix
{
int tempX,tempY,cnt=1;//CNT: the number of pieces of the same type in a row, up to five, that side wins
for(int i=-4;i<=4;i++)
{
if(i==0)continue;//So if I go through the loop, I'm not doing anything, right
switch(direc)
{
case HORIZON:
tempX=x; tempY=y+i; break;
case VERTICAL:
tempX=x+i; tempY=y; break;
case LEFTBOTTOMTORIGHTTOP:
tempX=x-i; tempY=y+i; break;
case LEFTTOPTORIGHTBOTTOM:
tempX=x+i; tempY=y+i; break;
}
if(isInChessBoard(tempX,tempY) && m_ptBoard->m_cSquare[tempX][tempY]==m_chessType)
cnt++;
else
cnt=0;
if(cnt==5)//Abital into line
return true;
}
}return false;
}
void Player::setChess()
{
cout<<" Please enter player "<<m_name<<" the x Coordinates and y Coordinates: "<<endl;
cin>>m_x>>m_y;
while(cin.fail() || m_ptBoard->m_cSquare[m_x][m_y]!=' ')//The input is not an int or an existing chess piece in this position
{
cout<<" Incorrect input. Please enter player again "<<m_name<<" the x Coordinates and y Coordinates: "<<endl;
cin.clear(); //Clear fail state
cin.sync(); //Clear buffer
cin>>m_x>>m_y;
}
if(isInChessBoard(m_x,m_y))
m_ptBoard->m_cSquare[m_x][m_y]=m_chessType;
}
bool Player::isWin()
{
return isLine(m_x,m_y)?true:false;
}
The main. CPP
//main.cpp
#include"ChessBoard.h"
#include"Player.h"
int main()
{
ChessBoard board;
Player playA("aaa",'*');//Player aaa's pieces are '*'
playA.attachToBoard(&board);
Player playB("bbb",'#');//The chess piece shape of player BBB is '#'
playB.attachToBoard(&board);
board.show();
while(1)
{
playA.setChess();//Player A puts down A piece
if(playA.isWin())
{ cout<<"Winer!"; break;}
board.show();
playB.setChess();//Player B puts down a piece
if(playB.isWin())
{ cout<<"Winer!"; break;}
board.show();
}
return 1;
}
The above is all the content of this article, I hope to be able to help you master C++.