Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "gomoku.h"
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <string.h>
- #include <ctime>
- using namespace std;
- Gomoku::Gomoku(int boardSize) {
- menu();
- }
- void Gomoku::menu() {
- cout << "==================================================================================" << endl;
- cout << "===============|| XXXXXX XXXXXX XX XX XXXXXX X X X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| X XX X X X XX X X X XX X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| XXXXXX XXXXXX X X XXXXXX X X XXXXXX ||===============" << endl;
- cout << "================================================== by Loic Caron =================" << endl;
- cout << " ++++++++++++++++++++++++++++++++++++" << endl;
- cout << " + 1 - PLAY AGAINST A FRIEND +" << endl;
- cout << " + 2 - PLAY AGAINST THE MACHINE +" << endl;
- cout << " + 3 - ///////////////// +" << endl;
- cout << " ++++++++++++++++++++++++++++++++++++ \n" << endl;
- choice = 0;
- cout << "Please enter your choice: ";
- cin >> choice;
- 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
- cerr << "/!\\ Please enter a valid number /!\\" << endl;
- cout << "Please enter your choice: ";
- cin >> choice;
- }
- if (choice == 1) {
- system("CLS");
- cout << "==================================================================================" << endl;
- cout << "===============|| XXXXXX XXXXXX XX XX XXXXXX X X X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| X XX X X X XX X X X XX X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| XXXXXX XXXXXX X X XXXXXX X X XXXXXX ||===============" << endl;
- cout << "================================================== by Loic Caron ================= \n " << endl;
- cout << " ++++++++++++++++++++++++++++++++ RULES ++++++++++++++++++++++++++++++++++++" << endl;
- cout << " + Cross plays first, and players alternate in placing a stone of their +" << endl;
- cout << " + color on an empty intersection. The winner is the first player to get +" << endl;
- cout << " + an unbroken row of five stones horizontally, vertically, or diagonally. +" << endl;
- cout << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n" << endl;
- getBoardsize();
- playVsPlayer();
- }
- if (choice == 2) {
- system("CLS");
- cout << "==================================================================================" << endl;
- cout << "===============|| XXXXXX XXXXXX XX XX XXXXXX X X X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| X XX X X X XX X X X XX X X ||===============" << endl;
- cout << "===============|| X X X X X X X X X X X X ||===============" << endl;
- cout << "===============|| XXXXXX XXXXXX X X XXXXXX X X XXXXXX ||===============" << endl;
- cout << "================================================== by Loic Caron ================= \n " << endl;
- cout << " ++++++++++++++++++++++++++++++++ RULES ++++++++++++++++++++++++++++++++++++" << endl;
- cout << " + Black plays first, and players alternate in placing a stone of their +" << endl;
- cout << " + color on an empty intersection. The winner is the first player to get +" << endl;
- cout << " + an unbroken row of five stones horizontally, vertically, or diagonally. +" << endl;
- cout << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n" << endl;
- getBoardsize();
- playVsBot();
- }
- }
- int Gomoku::getBoardsize() {
- cout << "Please choose the size of your board (to 5 from 26) :"; //The user can choose the size of his playboard
- cin >> boardSize;
- if (boardSize > 5 && boardSize < 27) { //If the size is valid, we can continue
- cout << "Size of your board: " << boardSize << " x " << boardSize << endl; //Inform the user on the size of the board
- setBoard();
- return boardSize;
- }
- if (!(cin >> boardSize)) {
- cerr << "Please enter a valid number." << endl;
- getBoardsize();
- }
- return 0;
- }
- void Gomoku::setBoard() { //Create a board
- for (int i = 0; i < boardSize + 1; i++) { //Initialize the board with 0
- for (int j = 0; j < boardSize + 1; j++) {
- board.push_back(vector<int>(boardSize, 0));
- }
- }
- printBoard();
- }
- void Gomoku::printBoard() {
- cout << " ";
- cout << " ";
- for (char k = 65; k < 65 + boardSize; k++) { //Adapt the size of the abscissa axis according to the size of the board
- cout << k << " ";
- }
- cout << "\n";
- int cpt;
- for (int i = 0; i < boardSize; i++) {
- cpt = i + 1;
- for (int j = 0; j < boardSize; j++) {
- if (board[i][j] == 0) { //If the value is 0 we return a point to indicate that the box is empty
- if (j == 0) {
- if (i < 9) {
- cout << " " << cpt << " . ";
- }
- else {
- cout << cpt << " . ";
- }
- }
- else {
- cout << ". ";
- }
- }
- if (board[i][j] == 1) { //If the value is 1, we return a cross because player 1 chose this box
- if (j == 0 && i < 9) {
- cout << " " << cpt << " X ";
- }
- if (j == 0 && i > 8) {
- cout << cpt << " X ";
- }
- if (j > 0) {
- cout << "X ";
- }
- }
- if (board[i][j] == 2) { //If the value is 2, we return a circle because player 2 chose this box
- if (j == 0 && i < 9) {
- cout << " " << cpt << " O ";
- }
- if (j == 0 && i > 8) {
- cout << cpt << " O ";
- }
- if (j > 0) {
- cout << "0 ";
- }
- }
- if (j == boardSize - 1) {
- cout << cpt;
- }
- }
- cout << "\n";
- }
- cout << " ";
- for (char k = 65; k < 65 + boardSize; k++) {
- cout << k << " ";
- }
- cout << "\n";
- }
- int Gomoku::player1() {
- row = NULL;
- letter = NULL;
- column = NULL;
- cout << "Enter a letter: ";
- cin >> letter;
- column = letter;
- if (column < 65 || (column > 90 && column < 97) || column > 122) { //If column1 is not a letter the user is pleased to enter a letter
- cout << "Please enter a letter. \n";
- player1();
- }
- else {
- 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)
- column -= 96;
- }
- if (column > 64 && column < 91) { //if column1 is already a lowercase letter, column1-64 to have an integer between 1 and 26
- column -= 64;
- }
- }
- if (column > boardSize || column < 1) { //The user can't enter a number which is not on the playboard
- cout << "This letter is not on your playboard. Please enter a valid one. \n";
- player1();
- }
- cout << "Enter a number between 1 and " << boardSize << ": ";
- cin >> row;
- if (row > boardSize || row < 1) { //If the number is not on the board or if the user enter a letter
- 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
- cerr << "Please enter a valid number." << endl;
- cout << "Enter a number between 1 and " << boardSize << ":";
- cin >> row;
- }
- }
- row -= 1;
- column -= 1;
- if (board[row][column] == 1 || board[row][column] == 2) {
- cout << "This place is already taken, please choose an other one. \n";
- player1();
- }
- if (board[row][column] == 0) {
- //cout << row << endl;
- //cout << column << endl;
- //system("pause");
- //system("CLS");
- board[row][column] = 1;
- printBoard();
- }
- return row;
- return column;
- }
- int Gomoku::player2() {
- row = 0;
- letter = NULL;
- column = 0;
- cout << "Enter a letter: ";
- cin >> letter;
- column = letter;
- if (column < 65 || (column > 90 && column < 97) || column > 122) { //If column2 is not a letter the user is pleased to enter a lettter
- cout << "Please enter a letter. \n";
- player2();
- }
- else {
- 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)
- column -= 96;
- }
- if (column >= 65 && column <= 90) { //if column2 is already a lowercase letter, column2-64 to have an integer between 1 and 26
- column -= 64;
- }
- }
- if (column > boardSize) { //The user can't enter a letter which is not on the playboard
- cout << "This letter is not on your playboard. Please enter a valid one. \n";
- player2();
- }
- cout << "Enter a number between 1 and " << boardSize << ":";
- cin >> row;
- if (row > boardSize || row < 1) { //If the number is not on the board or if the user enter a letter
- 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
- cerr << "Please enter a valid number." << endl;
- cout << "Enter a number between 1 and " << boardSize << ":";
- cin >> row;
- }
- }
- row -= 1;
- column -= 1;
- if (board[row][column] == 1 || board[row][column] == 2) {
- cout << "This place is already taken, please choose an other one. \n";
- player1();
- }
- if (board[row][column] == 0) {
- //system("CLS");
- board[row][column] = 2;
- printBoard();
- }
- return row;
- return column;
- }
- void Gomoku::bot() {
- random = NULL;
- rowBot = NULL;
- columnBot = NULL;
- random = rand()%8;
- cout << random;
- //Try to block the player
- if(random == 1 && board[row+1][column] == 0) { //Bottom box
- rowBot = row + 1;
- columnBot = column;
- }
- else if (board[row+1][column] == 1 || board[row+1][column] == 2){//The bot try an other box
- random += 1;
- }
- if (random == 2 && board[row - 1][column] == 0) { //Top box
- rowBot = row - 1;
- columnBot = column;
- }
- else if (board[row-1][column] == 1 || board[row-1][column] == 2){
- random += 1;
- }
- if (random == 3 && board[row][column+1] == 0) { //Right box
- rowBot = row;
- columnBot = column + 1;
- }
- else if (board[row][column+1] == 1 || board[row][column+1] == 2){
- random += 1;
- }
- if (random == 4 && board[row][column-1] == 0) { //Left box
- rowBot = row;
- columnBot = column - 1;
- }
- else if (board[row][column-1] == 1 || board[row][column-1] == 2){
- random += 1;
- }
- if(random == 5 && board[row+1][column+1] == 0) { //Bottom right box
- rowBot = row + 1;
- columnBot = column + 1;
- }
- else if (board[row+1][column+1] == 1 || board[row+1][column+1] == 2){
- random += 1;
- }
- if(random == 6 && board[row-1][column-1] == 0) { //Top left box
- rowBot = row - 1;
- columnBot = column - 1;
- }
- else if (board[row-1][column-1] == 1 || board[row-1][column-1] == 2){
- random += 1;
- }
- if(random == 7 && board[row+1][column-1] == 0) { //Bottom left box
- rowBot = row + 1;
- columnBot = column - 1;
- }
- else if (board[row+1][column-1] == 1 || board[row+1][column-1] == 2){
- random += 1;
- }
- if(random == 8 && board[row-1][column+1] == 0) { //Top right box
- rowBot = row - 1;
- columnBot = column + 1;
- }
- else if (board[row-1][column+1] == 1 || board[row-1][column+1] == 2){
- if(board[row+1][column] == 0){
- rowBot = row + 1;
- columnBot = column;
- }
- }
- /*for (int i = 0; i < boardSize; i++) { //If the bot can align more than 2 symbols
- c = 0;
- for (int j = 0; j < boardSize; j++) {
- if (board[i][j] == symb) {
- c++;
- if (c >= 2) {
- rowBot = row;
- columnBot = column - 2;
- if(board[i][j-2] == 1 || board[i][j-2] == 2){
- }
- }
- }
- else {
- c = 0;
- }
- }
- }
- for (int i = 0; i < boardSize; i++) { //Check every column
- c = 0;
- for (int j = 0; j < boardSize; j++) {
- if (board[j][i] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- for (int i = 0;i < boardSize;i++) { //Check every diagonal from top left to bottom right
- for (int j = 0;j < boardSize;j++) {
- if (board[i][j] == symb) {
- c = 0;
- for (int m = 0;(i + m < boardSize) && (j + m < boardSize);m++) {
- if (board[i + m][j + m] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- }
- }
- for (int i = 0;i < boardSize;i++) { //Check every diagonal from top right to bottom left
- for (int j = 0;j < boardSize;j++) {
- if (board[i][j] == symb) {
- int c = 0;
- for (int n = 0;(i + n < boardSize) && (j - n >= 0);n++) {
- if (board[i + n][j - n] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- }
- }*/
- system("CLS");
- board[rowBot][columnBot] = 2;
- printBoard();
- }
- bool Gomoku::determineWinner() {
- if (turn % 2 == 0) { //To indicate which symbols must be checked
- symb = 2;
- }
- else {
- symb = 1;
- }
- for (int i = 0; i < boardSize; i++) { //Check the rows
- c = 0;
- for (int j = 0; j < boardSize; j++) {
- if (board[i][j] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- for (int i = 0; i < boardSize; i++) { //Check every column
- c = 0;
- for (int j = 0; j < boardSize; j++) {
- if (board[j][i] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- for (int i = 0;i < boardSize;i++) { //Check every diagonal from top left to bottom right
- for (int j = 0;j < boardSize;j++) {
- if (board[i][j] == symb) {
- c = 0;
- for (int m = 0;(i + m < boardSize) && (j + m < boardSize);m++) {
- if (board[i + m][j + m] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- }
- }
- for (int i = 0;i < boardSize;i++) { //Check every diagonal from top right to bottom left
- for (int j = 0;j < boardSize;j++) {
- if (board[i][j] == symb) {
- int c = 0;
- for (int n = 0;(i + n < boardSize) && (j - n >= 0);n++) {
- if (board[i + n][j - n] == symb) {
- c++;
- if (c == 5) {
- return true;
- }
- }
- else {
- c = 0;
- }
- }
- }
- }
- }
- return false;
- }
- void Gomoku::playVsPlayer() {
- bool verif = false;
- turn = 0;
- do {
- player1(); //Player 1's turn
- turn += 1;
- if (determineWinner()) { //Then check if the player 1 win
- cout << "Player 1 win! \n";
- verif = true; //Return true if there are 5 consecutive symbols
- }
- else { //if the player 1 don't win, the player 2 can play
- player2();
- turn += 1;
- if (determineWinner()) { //Vheck if the player 2 win
- cout << "Player 2 win! \n";
- verif = true;
- }
- }
- } while (verif == false);
- /*end = NULL;
- cout << "Play again? (1)"<< endl;
- cout << "Go back to menu? (2)" << endl;
- cout << "Quit? (3)" << endl;
- cout << "\n Please enter your choice: ";
- cin >> end;
- if (end == 1){
- playVsPlayer();
- }
- if (end == 2){
- menu();
- }*/
- }
- void Gomoku::playVsBot() {
- bool verif = false;
- turn = 0;
- do {
- player1(); //Player 1's turn
- turn += 1;
- if (determineWinner()) { //Then check if the player 1 win
- cout << "Player 1 win! \n";
- verif = true; //Return true if there are 5 consecutive symbols
- }
- else { //if the player 1 don't win, the player 2 can play
- bot();
- turn += 1;
- if (determineWinner()) { //Vheck if the player 2 win
- cout << "Bot win! \n";
- verif = true;
- }
- }
- } while (verif == false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement