Advertisement
Guest User

cpp

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