Advertisement
Guest User

cpp

a guest
Nov 14th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.06 KB | None | 0 0
  1. #include "gomoku.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <string.h>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. Gomoku::Gomoku(int boardSize) {
  11.     menu();
  12. }
  13.  
  14. void Gomoku::menu() {
  15.  
  16.     cout << "==================================================================================" << endl;
  17.     cout << "===============|| XXXXXX  XXXXXX  XX    XX  XXXXXX  X  X  X    X ||===============" << endl;
  18.     cout << "===============|| X       X    X  X X  X X  X    X  X X   X    X ||===============" << endl;
  19.     cout << "===============|| X   XX  X    X  X  XX  X  X    X  XX    X    X ||===============" << endl;
  20.     cout << "===============|| X    X  X    X  X      X  X    X  X X   X    X ||===============" << endl;
  21.     cout << "===============|| XXXXXX  XXXXXX  X      X  XXXXXX  X  X  XXXXXX ||===============" << endl;
  22.     cout << "================================================== by Loic Caron =================" << endl;
  23.     cout << "                     ++++++++++++++++++++++++++++++++++++" << endl;
  24.     cout << "                     +   1 - PLAY AGAINST A FRIEND      +" << endl;
  25.     cout << "                     +   2 - PLAY AGAINST THE MACHINE   +" << endl;
  26.     cout << "                     +   3 - /////////////////          +" << endl;
  27.     cout << "                     ++++++++++++++++++++++++++++++++++++ \n" << endl;
  28.  
  29.     choice = 0;
  30.     cout << "Please enter your choice: ";
  31.     cin >> choice;
  32.  
  33.     if (choice > 2 || choice < 1) { //While the user enter a number which is not on the board, he is pleased to enter a valid one
  34.  
  35.         cerr << "/!\\ Please enter a valid number /!\\" << endl;
  36.         cout << "Please enter your choice: ";
  37.         cin >> choice;
  38.     }
  39.  
  40.     if (choice == 1) {
  41.  
  42.         system("CLS");
  43.  
  44.         cout << "==================================================================================" << endl;
  45.         cout << "===============|| XXXXXX  XXXXXX  XX    XX  XXXXXX  X  X  X    X ||===============" << endl;
  46.         cout << "===============|| X       X    X  X X  X X  X    X  X X   X    X ||===============" << endl;
  47.         cout << "===============|| X   XX  X    X  X  XX  X  X    X  XX    X    X ||===============" << endl;
  48.         cout << "===============|| X    X  X    X  X      X  X    X  X X   X    X ||===============" << endl;
  49.         cout << "===============|| XXXXXX  XXXXXX  X      X  XXXXXX  X  X  XXXXXX ||===============" << endl;
  50.         cout << "================================================== by Loic Caron ================= \n " << endl;
  51.         cout << "   ++++++++++++++++++++++++++++++++  RULES  ++++++++++++++++++++++++++++++++++++" << endl;
  52.         cout << "   +  Cross plays first, and players alternate in placing a stone of their     +" << endl;
  53.         cout << "   +  color on an empty intersection. The winner is the first player to get    +" << endl;
  54.         cout << "   +  an unbroken row of five stones horizontally, vertically, or diagonally.  +" << endl;
  55.         cout << "   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n" << endl;
  56.  
  57.         getBoardsize();
  58.         playVsPlayer();
  59.     }
  60.     if (choice == 2) {
  61.  
  62.         system("CLS");
  63.  
  64.         cout << "==================================================================================" << endl;
  65.         cout << "===============|| XXXXXX  XXXXXX  XX    XX  XXXXXX  X  X  X    X ||===============" << endl;
  66.         cout << "===============|| X       X    X  X X  X X  X    X  X X   X    X ||===============" << endl;
  67.         cout << "===============|| X   XX  X    X  X  XX  X  X    X  XX    X    X ||===============" << endl;
  68.         cout << "===============|| X    X  X    X  X      X  X    X  X X   X    X ||===============" << endl;
  69.         cout << "===============|| XXXXXX  XXXXXX  X      X  XXXXXX  X  X  XXXXXX ||===============" << endl;
  70.         cout << "================================================== by Loic Caron ================= \n " << endl;
  71.         cout << "   ++++++++++++++++++++++++++++++++  RULES  ++++++++++++++++++++++++++++++++++++" << endl;
  72.         cout << "   +  Black plays first, and players alternate in placing a stone of their     +" << endl;
  73.         cout << "   +  color on an empty intersection. The winner is the first player to get    +" << endl;
  74.         cout << "   +  an unbroken row of five stones horizontally, vertically, or diagonally.  +" << endl;
  75.         cout << "   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n" << endl;
  76.  
  77.         getBoardsize();
  78.         playVsBot();
  79.     }
  80. }
  81.  
  82. int Gomoku::getBoardsize() {
  83.  
  84.     cout << "Please choose the size of your board (to 5 from 26) :"; //The user can choose the size of his playboard
  85.     cin >> boardSize;
  86.  
  87.     if (boardSize > 5 && boardSize < 27) { //If the size is valid, we can continue
  88.  
  89.         cout << "Size of your board: " << boardSize << " x " << boardSize << endl; //Inform the user on the size of the board
  90.         setBoard();
  91.         return boardSize;
  92.     }
  93.     if (!(cin >> boardSize)) {
  94.  
  95.         cerr << "Please enter a valid number." << endl;
  96.         getBoardsize();
  97.     }
  98.     return 0;
  99. }
  100.  
  101. void Gomoku::setBoard() { //Create a board
  102.  
  103.     for (int i = 0; i < boardSize + 1; i++) { //Initialize the board with 0
  104.  
  105.         for (int j = 0; j < boardSize + 1; j++) {
  106.  
  107.             board.push_back(vector<int>(boardSize, 0));
  108.         }
  109.     }
  110.     printBoard();
  111. }
  112.  
  113. void Gomoku::printBoard() {
  114.  
  115.     cout << " ";
  116.     cout << "  ";
  117.     for (char k = 65; k < 65 + boardSize; k++) { //Adapt the size of the abscissa axis according to the size of the board
  118.         cout << k << " ";
  119.     }
  120.     cout << "\n";
  121.  
  122.     int cpt;
  123.  
  124.     for (int i = 0; i < boardSize; i++) {
  125.  
  126.         cpt = i + 1;
  127.  
  128.         for (int j = 0; j < boardSize; j++) {
  129.  
  130.             if (board[i][j] == 0) { //If the value is 0 we return a point to indicate that the box is empty
  131.  
  132.                 if (j == 0) {
  133.  
  134.                     if (i < 9) {
  135.  
  136.                         cout << " " << cpt << " . ";
  137.  
  138.                     }
  139.                     else {
  140.  
  141.                         cout << cpt << " . ";
  142.                     }
  143.  
  144.                 }
  145.                 else {
  146.  
  147.                     cout << ". ";
  148.                 }
  149.             }
  150.             if (board[i][j] == 1) { //If the value is 1, we return a cross because player 1 chose this box
  151.  
  152.                 if (j == 0 && i < 9) {
  153.  
  154.                     cout << " " << cpt << " X ";
  155.                 }
  156.                 if (j == 0 && i > 8) {
  157.  
  158.                     cout << cpt << " X ";
  159.                 }
  160.                 if (j > 0) {
  161.  
  162.                     cout << "X ";
  163.                 }
  164.             }
  165.             if (board[i][j] == 2) { //If the value is 2, we return a circle because player 2 chose this box
  166.  
  167.                 if (j == 0 && i < 9) {
  168.  
  169.                     cout << " " << cpt << " O ";
  170.  
  171.                 }
  172.                 if (j == 0 && i > 8) {
  173.  
  174.                     cout << cpt << " O ";
  175.                 }
  176.                 if (j > 0) {
  177.  
  178.                     cout << "0 ";
  179.                 }
  180.             }
  181.             if (j == boardSize - 1) {
  182.  
  183.                 cout << cpt;
  184.             }
  185.         }
  186.         cout << "\n";
  187.     }
  188.     cout << "   ";
  189.     for (char k = 65; k < 65 + boardSize; k++) {
  190.         cout << k << " ";
  191.     }
  192.     cout << "\n";
  193. }
  194.  
  195. int Gomoku::player1() {
  196.  
  197.     row = NULL;
  198.     letter = NULL;
  199.     column = NULL;
  200.     cout << "Enter a letter: ";
  201.     cin >> letter;
  202.     column = letter;
  203.  
  204.     if (column < 65 || (column > 90 && column < 97) || column > 122) { //If column1 is not a letter the user is pleased to enter a letter
  205.  
  206.         cout << "Please enter a letter. \n";
  207.         player1();
  208.     }
  209.     else {
  210.  
  211.         if (column > 96 && column < 123) { //if column1 is a capital letter (column1 - 32) to have a lowercase letter and (column1 - 64) to have an integer so we have (column1-32-64)
  212.  
  213.             column -= 96;
  214.  
  215.         }
  216.         if (column > 64 && column < 91) { //if column1 is already a lowercase letter, column1-64 to have an integer between 1 and 26
  217.  
  218.             column -= 64;
  219.         }
  220.     }
  221.     if (column > boardSize || column < 1) { //The user can't enter a number which is not on the playboard
  222.  
  223.         cout << "This letter is not on your playboard. Please enter a valid one. \n";
  224.         player1();
  225.     }
  226.     cout << "Enter a number between 1 and " << boardSize << ": ";
  227.     cin >> row;
  228.  
  229.     if (row > boardSize || row < 1) { //If the number is not on the board or if the user enter a letter
  230.  
  231.         while (row > boardSize || row < 1) { //While the user enter a number which is not on the board, he is pleased to enter a valid one
  232.  
  233.             cerr << "Please enter a valid number." << endl;
  234.             cout << "Enter a number between 1 and " << boardSize << ":";
  235.             cin >> row;
  236.         }
  237.     }
  238.     row -= 1;
  239.     column -= 1;
  240.  
  241.     if (board[row][column] == 1 || board[row][column] == 2) {
  242.  
  243.         cout << "This place is already taken, please choose an other one. \n";
  244.         player1();
  245.     }
  246.     if (board[row][column] == 0) {
  247.         //cout << row << endl;
  248.         //cout << column << endl;
  249.         //system("pause");
  250.         //system("CLS");
  251.         board[row][column] = 1;
  252.         printBoard();
  253.     }
  254.     return row;
  255.     return column;
  256. }
  257.  
  258.  
  259. int Gomoku::player2() {
  260.  
  261.     row = 0;
  262.     letter = NULL;
  263.     column = 0;
  264.     cout << "Enter a letter: ";
  265.     cin >> letter;
  266.     column = letter;
  267.  
  268.     if (column < 65 || (column > 90 && column < 97) || column > 122) { //If column2 is not a letter the user is pleased to enter a lettter
  269.  
  270.         cout << "Please enter a letter. \n";
  271.         player2();
  272.     }
  273.     else {
  274.  
  275.         if (column >= 97 && column <= 122) { //if column2 is a capital letter (column2 - 32) to have a lowercase letter and (column2 - 64) to have an integer so we have (column2-32-64)
  276.  
  277.             column -= 96;
  278.  
  279.         }
  280.         if (column >= 65 && column <= 90) { //if column2 is already a lowercase letter, column2-64 to have an integer between 1 and 26
  281.  
  282.             column -= 64;
  283.         }
  284.     }
  285.     if (column > boardSize) { //The user can't enter a letter which is not on the playboard
  286.  
  287.         cout << "This letter is not on your playboard. Please enter a valid one. \n";
  288.         player2();
  289.     }
  290.     cout << "Enter a number between 1 and " << boardSize << ":";
  291.     cin >> row;
  292.  
  293.     if (row > boardSize || row < 1) { //If the number is not on the board or if the user enter a letter
  294.  
  295.         while (row > boardSize || row < 1) { //While the user enter a letter which is not on the board, he is pleased to enter a valid one
  296.  
  297.             cerr << "Please enter a valid number." << endl;
  298.             cout << "Enter a number between 1 and " << boardSize << ":";
  299.             cin >> row;
  300.         }
  301.     }
  302.     row -= 1;
  303.     column -= 1;
  304.     if (board[row][column] == 1 || board[row][column] == 2) {
  305.  
  306.         cout << "This place is already taken, please choose an other one. \n";
  307.         player1();
  308.     }
  309.     if (board[row][column] == 0) {
  310.  
  311.         //system("CLS");
  312.         board[row][column] = 2;
  313.         printBoard();
  314.     }
  315.     return row;
  316.     return column;
  317. }
  318.  
  319. void Gomoku::bot() {
  320.    
  321.     random = NULL;
  322.     rowBot = NULL;
  323.     columnBot = NULL;
  324.    
  325.     random = rand()%8;
  326.     cout << random;
  327.    
  328.     //Try to block the player
  329.     if(random == 1 && board[row+1][column] == 0) { //Bottom box
  330.  
  331.         rowBot = row + 1;
  332.         columnBot = column;
  333.     }
  334.     else if (board[row+1][column] == 1 || board[row+1][column] == 2){//The bot try an other box
  335.    
  336.         random += 1;
  337.     }
  338.     if (random == 2 && board[row - 1][column] == 0) { //Top box
  339.  
  340.         rowBot = row - 1;
  341.         columnBot = column;
  342.     }
  343.     else if (board[row-1][column] == 1 || board[row-1][column] == 2){
  344.    
  345.         random += 1;
  346.     }
  347.     if (random == 3 && board[row][column+1] == 0) { //Right box
  348.  
  349.         rowBot = row;
  350.         columnBot = column + 1;
  351.     }
  352.     else if (board[row][column+1] == 1 || board[row][column+1] == 2){
  353.    
  354.         random += 1;
  355.     }
  356.     if (random == 4 && board[row][column-1] == 0) { //Left box
  357.  
  358.         rowBot = row;
  359.         columnBot = column - 1;
  360.     }
  361.     else if (board[row][column-1] == 1 || board[row][column-1] == 2){
  362.    
  363.         random += 1;
  364.     }
  365.     if(random == 5 && board[row+1][column+1] == 0) { //Bottom right box
  366.    
  367.            rowBot = row + 1;
  368.            columnBot = column + 1;
  369.        }
  370.        else if (board[row+1][column+1] == 1 || board[row+1][column+1] == 2){
  371.        
  372.            random += 1;
  373.        }
  374.     if(random == 6 && board[row-1][column-1] == 0) { //Top left box
  375.    
  376.            rowBot = row - 1;
  377.            columnBot = column - 1;
  378.        }
  379.        else if (board[row-1][column-1] == 1 || board[row-1][column-1] == 2){
  380.        
  381.            random += 1;
  382.        }
  383.     if(random == 7 && board[row+1][column-1] == 0) { //Bottom left box
  384.    
  385.            rowBot = row + 1;
  386.            columnBot = column - 1;
  387.        }
  388.        else if (board[row+1][column-1] == 1 || board[row+1][column-1] == 2){
  389.        
  390.            random += 1;
  391.        }
  392.     if(random == 8 && board[row-1][column+1] == 0) { //Top right box
  393.    
  394.            rowBot = row - 1;
  395.            columnBot = column + 1;
  396.        }
  397.        else if (board[row-1][column+1] == 1 || board[row-1][column+1] == 2){
  398.        
  399.            if(board[row+1][column] == 0){
  400.                
  401.                rowBot = row + 1;
  402.                columnBot = column;
  403.            }
  404.        }
  405.    
  406.        /*for (int i = 0; i < boardSize; i++) { //If the bot can align more than 2 symbols
  407.    
  408.            c = 0;
  409.    
  410.            for (int j = 0; j < boardSize; j++) {
  411.    
  412.                if (board[i][j] == symb) {
  413.    
  414.                    c++;
  415.                    if (c >= 2) {
  416.                        
  417.                        rowBot = row;
  418.                        columnBot = column - 2;
  419.                        
  420.                        if(board[i][j-2] == 1 || board[i][j-2] == 2){
  421.                            
  422.                            
  423.                        }
  424.                    }
  425.                }
  426.                else {
  427.    
  428.                    c = 0;
  429.                }
  430.            }
  431.        }
  432.    
  433.        for (int i = 0; i < boardSize; i++) { //Check every column
  434.    
  435.            c = 0;
  436.    
  437.            for (int j = 0; j < boardSize; j++) {
  438.    
  439.                if (board[j][i] == symb) {
  440.    
  441.                    c++;
  442.                    if (c == 5) {
  443.    
  444.                        return true;
  445.                    }
  446.                }
  447.                else {
  448.    
  449.                    c = 0;
  450.                }
  451.            }
  452.        }
  453.    
  454.        for (int i = 0;i < boardSize;i++) { //Check every diagonal from top left to bottom right
  455.    
  456.            for (int j = 0;j < boardSize;j++) {
  457.    
  458.                if (board[i][j] == symb) {
  459.    
  460.                    c = 0;
  461.    
  462.                    for (int m = 0;(i + m < boardSize) && (j + m < boardSize);m++) {
  463.    
  464.                        if (board[i + m][j + m] == symb) {
  465.    
  466.                            c++;
  467.    
  468.                            if (c == 5) {
  469.    
  470.                                return true;
  471.                            }
  472.                        }
  473.                        else {
  474.    
  475.                            c = 0;
  476.                        }
  477.                    }
  478.                }
  479.            }
  480.        }
  481.    
  482.        for (int i = 0;i < boardSize;i++) { //Check every diagonal from top right to bottom left
  483.    
  484.            for (int j = 0;j < boardSize;j++) {
  485.    
  486.                if (board[i][j] == symb) {
  487.    
  488.                    int c = 0;
  489.    
  490.                    for (int n = 0;(i + n < boardSize) && (j - n >= 0);n++) {
  491.    
  492.                        if (board[i + n][j - n] == symb) {
  493.    
  494.                            c++;
  495.    
  496.                            if (c == 5) {
  497.    
  498.                                return true;
  499.                            }
  500.                        }
  501.                        else {
  502.    
  503.                            c = 0;
  504.                        }
  505.                    }
  506.                }
  507.            }
  508.        }*/
  509.    
  510.     system("CLS");
  511.     board[rowBot][columnBot] = 2;
  512.     printBoard();
  513. }
  514.  
  515. bool Gomoku::determineWinner() {
  516.  
  517.     if (turn % 2 == 0) { //To indicate which symbols must be checked
  518.  
  519.         symb = 2;
  520.     }
  521.     else {
  522.  
  523.         symb = 1;
  524.     }
  525.  
  526.     for (int i = 0; i < boardSize; i++) { //Check the rows
  527.  
  528.         c = 0;
  529.  
  530.         for (int j = 0; j < boardSize; j++) {
  531.  
  532.             if (board[i][j] == symb) {
  533.  
  534.                 c++;
  535.                 if (c == 5) {
  536.  
  537.                     return true;
  538.                 }
  539.             }
  540.             else {
  541.  
  542.                 c = 0;
  543.             }
  544.         }
  545.     }
  546.  
  547.     for (int i = 0; i < boardSize; i++) { //Check every column
  548.  
  549.         c = 0;
  550.  
  551.         for (int j = 0; j < boardSize; j++) {
  552.  
  553.             if (board[j][i] == symb) {
  554.  
  555.                 c++;
  556.                 if (c == 5) {
  557.  
  558.                     return true;
  559.                 }
  560.             }
  561.             else {
  562.  
  563.                 c = 0;
  564.             }
  565.         }
  566.     }
  567.  
  568.     for (int i = 0;i < boardSize;i++) { //Check every diagonal from top left to bottom right
  569.  
  570.         for (int j = 0;j < boardSize;j++) {
  571.  
  572.             if (board[i][j] == symb) {
  573.  
  574.                 c = 0;
  575.  
  576.                 for (int m = 0;(i + m < boardSize) && (j + m < boardSize);m++) {
  577.  
  578.                     if (board[i + m][j + m] == symb) {
  579.  
  580.                         c++;
  581.  
  582.                         if (c == 5) {
  583.  
  584.                             return true;
  585.                         }
  586.                     }
  587.                     else {
  588.  
  589.                         c = 0;
  590.                     }
  591.                 }
  592.             }
  593.         }
  594.     }
  595.  
  596.     for (int i = 0;i < boardSize;i++) { //Check every diagonal from top right to bottom left
  597.  
  598.         for (int j = 0;j < boardSize;j++) {
  599.  
  600.             if (board[i][j] == symb) {
  601.  
  602.                 int c = 0;
  603.  
  604.                 for (int n = 0;(i + n < boardSize) && (j - n >= 0);n++) {
  605.  
  606.                     if (board[i + n][j - n] == symb) {
  607.  
  608.                         c++;
  609.  
  610.                         if (c == 5) {
  611.  
  612.                             return true;
  613.                         }
  614.                     }
  615.                     else {
  616.  
  617.                         c = 0;
  618.                     }
  619.                 }
  620.             }
  621.         }
  622.     }
  623.  
  624.     return false;
  625. }
  626.  
  627. void Gomoku::playVsPlayer() {
  628.  
  629.     bool verif = false;
  630.     turn = 0;
  631.  
  632.     do {
  633.         player1(); //Player 1's turn
  634.  
  635.         turn += 1;
  636.  
  637.         if (determineWinner()) { //Then check if the player 1 win
  638.  
  639.             cout << "Player 1 win! \n";
  640.             verif = true;   //Return true if there are 5 consecutive symbols
  641.         }
  642.         else { //if the player 1 don't win, the player 2 can play
  643.  
  644.             player2();
  645.  
  646.             turn += 1;
  647.  
  648.             if (determineWinner()) { //Vheck if the player 2 win
  649.  
  650.                 cout << "Player 2 win! \n";
  651.                 verif = true;
  652.             }
  653.         }
  654.     } while (verif == false);
  655.    
  656.     /*end = NULL;
  657.     cout << "Play again? (1)"<< endl;
  658.     cout << "Go back to menu? (2)" << endl;
  659.     cout << "Quit? (3)" << endl;
  660.     cout << "\n Please enter your choice: ";
  661.     cin >> end;
  662.    
  663.     if (end == 1){
  664.        
  665.         playVsPlayer();
  666.     }
  667.     if (end == 2){
  668.        
  669.         menu();
  670.     }*/
  671. }
  672.  
  673. void Gomoku::playVsBot() {
  674.  
  675.     bool verif = false;
  676.     turn = 0;
  677.  
  678.     do {
  679.         player1(); //Player 1's turn
  680.  
  681.         turn += 1;
  682.  
  683.         if (determineWinner()) { //Then check if the player 1 win
  684.  
  685.             cout << "Player 1 win! \n";
  686.             verif = true;   //Return true if there are 5 consecutive symbols
  687.         }
  688.         else { //if the player 1 don't win, the player 2 can play
  689.  
  690.             bot();
  691.  
  692.             turn += 1;
  693.  
  694.             if (determineWinner()) { //Vheck if the player 2 win
  695.  
  696.                 cout << "Bot win! \n";
  697.                 verif = true;
  698.             }
  699.         }
  700.     } while (verif == false);
  701. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement