mercredi 20 novembre 2019

Best way to represent Quarto Board Game Pieces

I'm creating Quarto, the board game. It's essentially advanced connect 4.

Each piece has four distinguishing features (WHITE VS BLACK | TALL VS SHORT | SQUARE VS CIRCLE | SOLID VS HOLLOW). If you get four of any of those features in a row, you win.

At the moment, I am current trying to write a function to check for a win. However, it is on track to be O(n^4), and I think I can improve this depending on how I structure my code.

Currently, I have this:

enum Piece
{
    WTSS, WTSH, WTHS, WTHH,
    WSSS, WSSH, WSHS, WSHH,
    BTSS, BTSH, BTHS, BTHH,
    BSSS, BSSH, BSHS, BSHH,
    EMPTY
};

static const char * PieceStrings[] = {
  "WTSS ", "WTSH ", "WTHS ", "WTHH ",
  "WSSS ", "WSSH ", "WSHS ", "WSHH ",
  "BTSS ", "BTSH ", "BTHS ", "BTHH ",
  "BSSS ", "BSSH ", "BSHS ", "BSHH ",
  "____ ",
};

But I don't think this is very efficient. I have thought about making them their own class, but it then makes it hard to initialize and work with all of these pieces.

This is how I am starting to go about checking for a win:

// go through all N+2 possible connections
// check if any have shared feature
bool Board::isWin() {
  int i, j, k;
  for (i = 0, k = BOARDSIZE-1; i < BOARDSIZE; i++, k--) {
    for (j = 0; j < BOARDSIZE; j++) {
      //Horizontal case
      // board[i][j];
      //Vertical case
      // board[j][i];

    }
    // Diagonal cases
    // board[i][i];
    // board[k][i];


  }

  return false;
}

// Checks if pieces have a similar component
bool Board::checkPieces(list<string> & piecesInARow) {
  int i, j;
  list<string>::iterator it = piecesInARow.begin();
  for (i = 0; i < BOARDSIZE; i++) {
    for (j = 0; j < BOARDSIZE; j++) {
      // check if pieces all have shared char at index
    }
  }
}

How can I improve this and make it easier for myself?

Aucun commentaire:

Enregistrer un commentaire