C + + Implementation Gobang Small program __ Database

Source: Internet
Author: User

This is a Gobang program written in C + +, and it's not good to deal with a if it occupies a position that is already down. Change hight, with width, and q[][] can enlarge the chessboard.

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


    Class Qipan {Public:qipan () {} ~qipan () {}; Up or down, oblique direction 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 &AMP;&A mp
        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 &am P;&amP
        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 &
        gt;= 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, each time first look at the top 5, if the same, iter++, otherwise break, plus the next direction, the same iter++, the last iter+1==5,win=true, and exit//each direction repeat, up and down, oblique.
        Won the direct exit.
        Show who won/left or right 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;      Upper and lower 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;
        Right oblique 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 two-dimensional array; print uplink number for each line, and column number for (int i = 0; I 

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.