C + + Object-oriented implementation Gobang games _c language

Source: Internet
Author: User

Try to incorporate object-oriented thinking into the program

ChessBoard.h

ChessBoard.h
#pragma once
#define
#define COL
#include <iostream>
using namespace Std;
 
Class chessboard//Checkers
{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's the "<<" is important so that the row*col output on the screen is square
    cout<<endl;
  }
}

Player.h

Player.h
#pragma once
//macro definition four detect if five children are connected to line
#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&LT;COL-1 && y>0) return true;
else return false;
/* Below is the core code: How to determine whether five children are connected to a line. This is centered around the pieces that the player is putting down at the moment, from four kinds of direction one by one to determine whether in this direction is connected to the line here will be four direction of the judgment are put together, to avoid four calls in different directions of the judgment, but put a switch in for a somewhat awkward, the readability seems not good/bool Player::isline (int x,int y) {for (int direc=horizon;direc<=lefttoptorightbottom;direc++)//Four orientations, expediency {int tempx
      TEMPY,CNT=1;//CNT: The number of consecutive pieces of the same kind, up to five to win for (int i=-4;i<=4;i++) {if (i==0) continue;//at this time the loop is equivalent to nothing.  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)//five sons into line return true;
}}return false;
  } void Player::setchess () {cout<< "Enter the x and Y coordinates of the player" <<m_name<< ":" <<endl;
  cin>>m_x>>m_y; while (Cin.fail () | | | m_ptboard->m_csquare[m_x][m_y]!= ')/input is not an int variable or there is already a pawn in this position {cout<< "entered incorrectly, please enter the player again" <&
    lt;m_name<< "x 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;}

Main.cpp

Main.cpp
#include "ChessBoard.h"
#include "Player.h"
 
int main ()
{
  Chessboard board;  
  Player PlayA ("AAA", ' * ');/player AAA's Chess piece shape is ' * '
  playa.attachtoboard (&board);
  Player Playb ("BBB", ' # ');/player BBB's Pawn shape 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 put down a pawn
    if (Playb.iswin ())
    {  cout<< "winer!"; break;  
    Board.show ();
  }
  return 1;
}

The above mentioned is the entire content of this article, I hope to be able to master C + + has helped.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.