C++ implementation of gobang subroutine

  • 2020-06-03 08:01:19
  • OfStack

This is a small program that USES C++ to write 5 pieces of chess. A does not handle well if it occupies the position already played. Change hight, width, q[][] to enlarge the board.


#include<iostream>
#include<vector>
using namespace std;

class qipan
{
public:
 qipan() {}
 ~qipan() {};


 // Up, down, left, right, oblique 
 char left(int x, int y)
 {// Check for suitability 
 if (x >= 1 && x <= hight&& y - 1 >= 1 && y - 1 <= width)
 {
  return q[x][y - 1];
 }
 else {
  return 'F';
 }

 }
 char right(int x, int y)
 {
 if (x >= 1 && x <= hight&&y + 1 >= 1 && y + 1 <= width)
 {
  return q[x][y + 1];
 }
 else {
  return 'F';
 }

 }
 char up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y >= 1 && y <= width)
 {
  return q[x - 1][y];
 }
 else {
  return 'F';
 }

 }
 char down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y >= 1 && y <= width)
 {
  return q[x + 1][y];
 }
 else {
  return 'F';
 }

 }
 char left_up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y - 1 >= 1 && y - 1 <= width)
 {
  return q[x - 1][y - 1];
 }
 else {
  return 'F';
 }

 }
 char left_down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y - 1 >= 1 && y - 1 <= width)
 {
  return q[x + 1][y - 1];
 }
 else {
  return 'F';
 }

 }
 char right_up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y + 1 >= 1 && y + 1 <= width)
 {
  return q[x - 1][y + 1];

 }
 else {
  return 'F';
 }


 }
 char right_down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y + 1 >= 1 && y + 1 <= width)
 {
  return q[x + 1][y + 1];
 }
 else {
  return 'F';
 }
 }

 void init_cur() {
 h_cur = hang;
 l_cur = lie;

 }
 bool win()
 {
 bool WIN = false;
 char temp = q[hang][lie];

 // For example, look above each time 5 A, if 1 The sample, iter++ ; Otherwise,  break ; Plus the direction down. Same thing iter++ ; The last iter+1==5,WIN=true; And out of 
 // Repeat in all directions, up, down, left, right, sideways. 
 // Win and walk out. 
 // Show who won 


 // Or so 
 int count_lr = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left(h_cur, l_cur) == temp)
  count_lr++;
  else
  break;
  l_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right(h_cur, l_cur) == temp)
  count_lr++;
  else
  break;
  l_cur++;

 }
 if (count_lr == 5)
  WIN = true;
 // Up and down 
 int count_ud = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (up(h_cur, l_cur) == temp)
  count_ud++;
  else
  break;
  h_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (down(h_cur, l_cur) == temp)
  count_ud++;
  else
  break;
  h_cur++;

 }
 if (count_ud == 5)
  WIN = true;


 // Left oblique 
 int count_lt = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left_up(h_cur, l_cur) == temp)
  count_lt++;
  else
  break;
  h_cur--;
  l_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left_down(h_cur, l_cur) == temp)
  count_lt++;
  else
  break;
  h_cur++;
  l_cur--;

 }
 if (count_lt == 5)
  WIN = true;

 // On the right side of the diagonal 
 int count_rt = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right_up(h_cur, l_cur) == temp)
  count_rt++;
  else
  break;
  h_cur--;
  l_cur++;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right_down(h_cur, l_cur) == temp)
  count_rt++;
  else
  break;
  h_cur++;
  l_cur++;

 }
 if (count_rt == 5)
  WIN = true;

 return WIN;
 }


 void qipan_array()
 {
 for (int i = 0; i < hight; i++) {
  for (int j = 0; j < width; j++)
  q[i][j] = '+';
 }
 }


 void prin_qipan()
 {
 // print 2 Dimensional array; every 1 The line prints the uplink number, as well as the column number 
 for (int i = 0; i <hight; i++)
 {

  for (int j = 0; j < width; j++)
  {
  cout << q[i][j] << " ";

  }
  cout << i;
  cout << endl;
 }

 for (int j = 0; j <width; j++)
 {
  cout << j << " ";
 }
 cout << endl << "________________________________" << endl;
 }
 int xiaqi(int player)
 {
 if (player == 1) {
  q[hang][lie] = '*';
 }
 else if (player == 2)
 {
  q[hang][lie] = '@';
 }
 else
  cout << "input player is wrong" << endl;
 return 0;
 }

 // Initialize column and column 
 int gethang(int h)
 {

 hang = h;
 return 0;
 }
 int getlie(int l)
 {
 lie = l;
 return 0;
 }


 int h_cur;
 int l_cur;
 const int hight = 9;
 const int width = 9;
 int hang; int lie;
 char q[9][9];
};

int main()
{
 int hang, lie;
 qipan wzq;
 wzq.qipan_array();
 cout << "A  with B  play 5 connect " << endl;
 cout << "A use * and B use @" << endl;
 cout << "________________________________" << endl;
 for (int i = 0; i < 15; i++) {
 cout << "A  Input line:  ";
 cin >> hang;
 cout << "A  The input columns:  ";
 cin >> lie;
 if (wzq.q[hang][lie] != '+')
  cout << " The number of rows and columns entered is already occupied " << endl;
 else {
  wzq.gethang(hang);
  wzq.getlie(lie);

  wzq.xiaqi(1);
  wzq.prin_qipan();
  if (wzq.win())
  {
  cout << "A is winner" << endl;
  exit(0);
  }
 }
 //b  began 
 cout << "B  Input line:  ";
 cin >> hang;
 cout << "B  The input columns:  ";
 cin >> lie;
 if (wzq.q[hang][lie] != '+')
  cout << " The number of rows and columns entered is already occupied " << endl;
 else {
  wzq.gethang(hang);
  wzq.getlie(lie);

  wzq.xiaqi(2);

  wzq.prin_qipan();
  if (wzq.win())
  {
  cout << "B is winner" << endl;
  exit(0);
  }
 }
 }
 return 0;


}

Related articles: