0

I am making a TicTacToe program and I'm trying to use OOP techniques. Within my 'Board' class I am wanting the program to store each way a set of moves can be won.

I hope this can be demonstrated here:

Board.h

#pragma once

class Board
{
private: 
    int winningRows[8][3]; //Variable in question

public:
    static const char X = 'X'; //Game piece 'X'
    static const char O = 'O'; //Game piece 'O'
    static const char EMPTY = ' '; //Empty game piece
    static const char TIE = 'T'; //Game is tie
    static const char NOONE = 'N'; //Nobody has won game yet
    static const int numbOfSquares = 9; //Number of squares on the board

    int InitializeWinningCombinations();
    void FindWinner();
};

Board.cpp

 #include "stdafx.h"
 #include "Board.h"

int Board::InitializeWinningCombinations()
{
    /*
    The playing board
    0, 1, 2
    3, 4, 5
    6, 7, 8
    */
    //All possible ways player can win game
    winningRows[8][3] = {
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    };

    //return winnigRows[8][3];
}


void Board::FindWinner()
{
    //I am wanting to get the variable here so I can play around with it later. 
    int winningRows = InitializeWinningCombinations();
}

I could just have the 'winningRows' variable inside the 'FindWinnner' function but from my understanding it is best to abstract as much as possible and have it as a member of the 'Board' class

Thank you for your time.

Ryan Swann
  • 115
  • 1
  • 5

2 Answers2

1
winningRows[8][3] = {
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    };

Is an attempted array assignment not an initialization and it cannot be done. You can initialize the array in a constructor like

Board() : winningRows{
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    } {}

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

You'd have to change the signature to

int** InitializeWinningCombinations();

Then you could call it as

int** winningRows = InitializeWinningCombinations();
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Sorry for this question, that's using pointers? I am currently going through a textbook and trying to work ahead than what is presented but I am causing myself some issues. – Ryan Swann Dec 28 '15 at 17:58
  • You may also wish to comment on the only uncommented line in `InitializeWinningCombinations` as it is rather broken at the moment. – R_Kapp Dec 28 '15 at 17:59
  • Yes the way C++ uses pointers to represent the array is that the pointer is to the **first element of the array**, then the type of the array (`int` in this case) is used to know how "big" of steps to take to get to each element. [This post](https://stackoverflow.com/questions/16001803/pointer-to-pointer-dynamic-two-dimensional-array) gives a good description of this. – Cory Kramer Dec 28 '15 at 18:00