Advertisement
Guest User

cpp

a guest
Nov 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.84 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.     cout << row;
  322.     cout << column;
  323.     system("pause");
  324.  
  325.     if(board[row+1][column] == 0) {
  326.  
  327.         system("CLS");
  328.         board[row + 1][column] == 2;
  329.         printBoard();
  330.     }
  331.     if (board[row - 1][column] == 0) {
  332.  
  333.         system("CLS");
  334.         board[row - 1][column] == 2;
  335.         printBoard();
  336.     }
  337.     if (board[row][column+1] == 0) {
  338.  
  339.         system("CLS");
  340.         board[row][column+1] == 2;
  341.         printBoard();
  342.     }
  343.     if (board[row][column-1] == 0) {
  344.  
  345.         system("CLS");
  346.         board[row][column-1] == 2;
  347.         printBoard();
  348.     }
  349. }
  350.  
  351. bool Gomoku::determineWinner() {
  352.  
  353.     if (turn % 2 == 0) { //To indicate which symbols must be checked
  354.  
  355.         symb = 2;
  356.     }
  357.     else {
  358.  
  359.         symb = 1;
  360.     }
  361.  
  362.     for (int i = 0; i < boardSize; i++) { //Check the rows
  363.  
  364.         c = 0;
  365.  
  366.         for (int j = 0; j < boardSize; j++) {
  367.  
  368.             if (board[i][j] == symb) {
  369.  
  370.                 c++;
  371.                 if (c == 5) {
  372.  
  373.                     return true;
  374.                 }
  375.             }
  376.             else {
  377.  
  378.                 c = 0;
  379.             }
  380.         }
  381.     }
  382.  
  383.     for (int i = 0; i < boardSize; i++) { //Check every column
  384.  
  385.         c = 0;
  386.  
  387.         for (int j = 0; j < boardSize; j++) {
  388.  
  389.             if (board[j][i] == symb) {
  390.  
  391.                 c++;
  392.                 if (c == 5) {
  393.  
  394.                     return true;
  395.                 }
  396.             }
  397.             else {
  398.  
  399.                 c = 0;
  400.             }
  401.         }
  402.     }
  403.  
  404.     for (int i = 0;i < boardSize;i++) { //Check every diagonal from top left to bottom right
  405.  
  406.         for (int j = 0;j < boardSize;j++) {
  407.  
  408.             if (board[i][j] == symb) {
  409.  
  410.                 c = 0;
  411.  
  412.                 for (int m = 0;(i + m < boardSize) && (j + m < boardSize);m++) {
  413.  
  414.                     if (board[i + m][j + m] == symb) {
  415.  
  416.                         c++;
  417.  
  418.                         if (c == 5) {
  419.  
  420.                             return true;
  421.                         }
  422.                     }
  423.                     else {
  424.  
  425.                         c = 0;
  426.                     }
  427.                 }
  428.             }
  429.         }
  430.     }
  431.  
  432.     for (int i = 0;i < boardSize;i++) { //Check every diagonal from top right to bottom left
  433.  
  434.         for (int j = 0;j < boardSize;j++) {
  435.  
  436.             if (board[i][j] == symb) {
  437.  
  438.                 int c = 0;
  439.  
  440.                 for (int n = 0;(i + n < boardSize) && (j - n >= 0);n++) {
  441.  
  442.                     if (board[i + n][j - n] == symb) {
  443.  
  444.                         c++;
  445.  
  446.                         if (c == 5) {
  447.  
  448.                             return true;
  449.                         }
  450.                     }
  451.                     else {
  452.  
  453.                         c = 0;
  454.                     }
  455.                 }
  456.             }
  457.         }
  458.     }
  459.  
  460.     return false;
  461. }
  462.  
  463. void Gomoku::playVsPlayer() {
  464.  
  465.     bool verif = false;
  466.     turn = 0;
  467.  
  468.     do {
  469.         player1(); //Player 1's turn
  470.  
  471.         turn += 1;
  472.  
  473.         if (determineWinner()) { //Then check if the player 1 win
  474.  
  475.             cout << "Player 1 win! \n";
  476.             verif = true;   //Return true if there are 5 consecutive symbols
  477.         }
  478.         else { //if the player 1 don't win, the player 2 can play
  479.  
  480.             player2();
  481.  
  482.             turn += 1;
  483.  
  484.             if (determineWinner()) { //Vheck if the player 2 win
  485.  
  486.                 cout << "Player 2 win! \n";
  487.                 verif = true;
  488.             }
  489.         }
  490.     } while (verif == false);
  491. }
  492.  
  493. void Gomoku::playVsBot() {
  494.  
  495.     bool verif = false;
  496.     turn = 0;
  497.  
  498.     do {
  499.         player1(); //Player 1's turn
  500.  
  501.         turn += 1;
  502.  
  503.         if (determineWinner()) { //Then check if the player 1 win
  504.  
  505.             cout << "Player 1 win! \n";
  506.             verif = true;   //Return true if there are 5 consecutive symbols
  507.         }
  508.         else { //if the player 1 don't win, the player 2 can play
  509.  
  510.             bot();
  511.  
  512.             turn += 1;
  513.  
  514.             if (determineWinner()) { //Vheck if the player 2 win
  515.  
  516.                 cout << "Bot win! \n";
  517.                 verif = true;
  518.             }
  519.         }
  520.     } while (verif == false);
  521. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement