c++ Gomoku board not filling all positions of second algorithm

221 Views Asked by At

I am a relatively new to coding and new to stackoverflow. I am creating a game of gomoku, where need to get five pieces in a row to win the game. I have initialized the board, 2d array, all to zero at beginning. The board size is based from an input file. I have created two algorithms the first one being random, and the second is meant to place a piece, one block away from its previous move. The second is meant to be the "smarter" algorithm. I have also printed the array and couted a lot of "tests" / "moves"to see which path is being taken. For some reason the second algorithm seems to not to be placing pieces the whole time, as therefore leads to zeroes appearing when the maximum number of turns is played to fill the board. I have also used a class. I am also aware that I will need to eventually make my functions in the class private not public in the end.

Here is the code of the input

6

Here is the int main

#include <iostream>
#include <fstream>
#include "game.h"
#include <iomanip>
#include <ctime>
using namespace std;



int main() {
    string inputSize = "input.txt";
    int size =0;
    srand(time(0));

    int arr[15][15];

    game Gomuko;

    size = Gomuko.getSize(inputSize, arr);

    Gomuko.initZero(arr, size);

    Gomuko.switchPlayers(arr, size);

    Gomuko.printArr(arr,size);




    return 0;
}

The .cpp of the class

#include "game.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;

game::game() {
    // TODO Auto-generated constructor stub

}

int game::getSize(string inputFileName, int dataArray[15][15]) {
    ifstream inputData;

    int size = 0;
    int counter = 0;

    inputData.open(inputFileName);

    if (!inputData) {
        cout << "Cannot open the file \"" << inputFileName << "\"" << endl;
    }
    while (inputData >> size) {
        counter++;
    }
    cout << "There are " << counter << " board sizes in the inputFile" << endl;
    cout << "The size of the board is " << size << "x" << size << endl << endl;
    return size;
}

void game::initZero(int arr[][15], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            arr[i][j] = 0;
        }
    }
}

void game::printArr(int arr[][15], int size) {

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    cout << endl << endl;
}

int game::randRow(int size) {

    int randNoRow = 0;
    randNoRow = rand() % size;
    return randNoRow;
}

int game::randCol(int size) {

    int randNoCol = 0;
    randNoCol = rand() % size;
    return randNoCol;
}
void game::movePlayerOneFirstMove(int arr[][15], int size) {
    int randNoRow = 0;
    int randNoCol = 0;
    int counter2 = 0;

    randNoRow = randRow(size);
    cout << "randNoRow = " << randNoRow << endl;

    randNoCol = randCol(size);
    cout << "randNoCol = " << randNoCol << endl;

    cout << endl;

    arr[randNoRow][randNoCol] = 1;
    counter2++;
}

int game::findFirstMoveRow(int arr[][15], int size) {
    int positionRow;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (arr[i][j] == 1) {
                positionRow = i;
            }
        }
    }

    return positionRow;
}

int game::findFirstMoveCol(int arr[][15], int size) {
    int positionCol;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (arr[i][j] == 1) {
                positionCol = j;
            }
        }
    }

    return positionCol;
}

int game::PlayerOneSurroundRow(int arr[][15], int size) {

    int positionRow = game::findFirstMoveRow(arr, size);
    cout << "The old row is " << positionRow << endl;

    int oldRow = positionRow;
    int randChoice = 0;
    int newRow = 0;

    randChoice = (rand() % 3);
    cout << "randChoice Row case:  " << randChoice << endl;

    switch (randChoice) {
    case 0:
        newRow = oldRow - 1;
        break;
    case 1:
        newRow = oldRow;
        break;
    case 2:
        newRow = oldRow + 1;
        break;
    }
    cout << "Test2" << endl << endl;
    if (newRow > size - 1) {
        cout << "Row too big as Row is " << newRow << endl;
        newRow = newRow - 1;
        cout << "Row is now " << newRow << endl;

    }
    if (newRow < 0) {
        cout << "Row too small as row is " << newRow << endl;
        newRow = newRow + 1;
        cout << "Row is now " << newRow << endl;

    }

    cout << "The new row is: " << newRow << endl;

    return newRow;
}

int game::PlayerOneSurroundCol(int arr[][15], int size) {

    int positionCol = findFirstMoveCol(arr, size);
    cout << "The old col is " << positionCol << endl;

    int oldCol = positionCol;
    int randChoice = 0;
    int newCol = 0;

    randChoice = (rand() % 3);
    cout << "randChoice Col case: " << randChoice << endl;

    switch (randChoice) {
    case 0:
        newCol = oldCol - 1;
        break;
    case 1:
        newCol = oldCol;
        break;
    case 2:
        newCol = oldCol + 1;
        break;
    }
    cout << "Test2" << endl << endl;
    if (newCol > size - 1) {
        cout << "Col too big as is " << newCol << endl;
        newCol = newCol - 1;
        cout << "Col is now " << newCol << endl;
    }
    if (newCol < 0) {
        cout << "Col too small as is " << newCol << endl;
        newCol = newCol + 1;
        cout << "Col is now " << newCol << endl;

    }

    cout << "The new col is: " << newCol << endl;

    return newCol;
}

void game::movePlayerOne(int arr[][15], int size, int counter2) {

    int newRow = 0;
    int newCol = 0;
    int oldRow = 0;
    int oldCol = 0;
    int count3 = 0;

    if (counter2 == 0) {
        game::movePlayerOneFirstMove(arr, size);
        cout << "Test1" << endl << endl << endl;
        counter2++;
    }

    else {

        cout << "Test 3" << endl;

        newRow = game::PlayerOneSurroundRow(arr, size);
        newCol = game::PlayerOneSurroundCol(arr, size);

        if (arr[newRow][newCol] == 0 && (newRow < size) && (newCol < size)
                && (newRow > 0) && (newCol > 0)) {
            arr[newRow][newCol] = 1;
            cout << "Test4" << endl;
            cout << "randNoRow = " << newRow << endl;
            cout << "randNoCol = " << newCol << endl;

            oldRow = newRow;
            oldCol = newCol;
        }

        else if ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)
                || (newRow > size) || (newCol > size) || (newRow < 0)
                || (newCol < 0)) {
            cout
                    << "There has been a match, or even out of bounds, going again. "
                    << endl << endl;

            while ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)) {

                cout << "Test5" << endl;
                newRow = game::PlayerOneSurroundRow(arr, size);
                newCol = game::PlayerOneSurroundCol(arr, size);

                cout << "randNoRow = " << newRow << endl;
                cout << "randNoCol = " << newCol << endl;

                count3++;
                if ((arr[newRow][newCol] != 1 && arr[newRow][newCol] != 2)
                        && (newRow < size) && (newCol < size) && (newRow >= 0)
                        && (newCol >= 0)) {
                    arr[newRow][newCol] = 1;
                    cout << "Test6" << endl;

                    cout << "randNoRow = " << newRow << endl;
                    cout << "randNoCol = " << newCol << endl;

                    oldRow = newRow;
                    oldCol = newCol;
                    break;
                }

                if (count3++ > 3) {
                    cout << "Test 7" << endl;
//                  newRow = randRow(size);
//                  newCol = randCol(size);
                    while (arr[newRow][newCol] == 1 || arr[newRow][newCol] == 2) {
                        newRow = randRow(size);
                        newCol = randCol(size);
                        cout << "Test 8" << endl;
                        cout << "randNoRow = " << newRow << endl;
                        cout << "randNoCol = " << newCol << endl;

                        if (arr[newRow][newCol] == 0) {
                            arr[newRow][newCol] = 1;
                            cout << "Test 9" << endl;
                            cout << "randNoRow = " << newRow << endl;
                            cout << "randNoCol = " << newCol << endl;
                            oldRow = newRow;
                            oldCol = newCol;
                            break;
                        }
                    }

                    break;
                }
            }

        }

//  else {
//      arr[newRow][newCol] = 1;
//  }
    }
}

void game::movePlayerTwo(int arr[][15], int size) {

    int randNoRow = 0;
    int randNoCol = 0;

    randNoRow = randRow(size);
    cout << "randNoRow = " << randNoRow << endl;

    randNoCol = randCol(size);
    cout << "randNoCol = " << randNoCol << endl;

    cout << endl;

    if ((arr[randNoRow][randNoCol] == 1) || (arr[randNoRow][randNoCol] == 2)) {
        //cout << "There has been a match, going again. " << endl << endl;
        while ((arr[randNoRow][randNoCol] == 1)
                || (arr[randNoRow][randNoCol] == 2)) {

            int randNoRow = 0;
            int randNoCol = 0;

            randNoRow = randRow(size);
            //cout << "randNoRow = " << randNoRow << endl;

            randNoCol = randCol(size);
            //cout << "randNoCol = " << randNoCol << endl;

            //cout << endl;

            if (arr[randNoRow][randNoCol] == 0) {
                arr[randNoRow][randNoCol] = 2;
                break;
            }
        }

    } else {
        arr[randNoRow][randNoCol] = 2;
    }
}

void game::switchPlayers(int arr[][15], int size) {
    int counter = 0;
    for (int i = 0; i < 36; i++) {
        cout << "This is turn " << counter + 1 << endl;
        if (counter % 2 == 0) {
            cout << "PlayerOne: " << endl;
            game::movePlayerOne(arr, size, counter);
        }
        if (counter % 2 == 1) {
            cout << "PlayerTwo:" << endl;
            game::movePlayerTwo(arr, size);
        }

        counter++;

    }

}

And the header file of the class

#ifndef GAME_H_
#define GAME_H_

#include <string>
#include <ctime>
using namespace std;

class game {
public:
    game();
    int getSize(string inputFileName, int dataArray[15][15]);
    void initZero(int arr[][15], int size);
    void printArr(int arr[][15], int size);
    void movePlayerOne(int arr[][15], int size, int counter2);
    void movePlayerOneFirstMove(int arr[][15], int size);
    void movePlayerTwo(int arr[][15], int size);
    void switchPlayers(int arr[][15], int size);
    int PlayerOneSurroundRow(int arr[][15],int size);
    int PlayerOneSurroundCol(int arr[][15],int size);
    int findFirstMoveRow(int arr[][15], int size);
    int findFirstMoveCol(int arr[][15], int size);
private:

    int randRow(int size);
    int randCol(int size);
};

#endif /* GAME_H_ */
0

There are 0 best solutions below