Skip to content

Commit

Permalink
Add getHorizontalRun to Board class
Browse files Browse the repository at this point in the history
  • Loading branch information
alxmjo committed Dec 14, 2016
1 parent c27f1fe commit 1303d0a
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 33 deletions.
180 changes: 153 additions & 27 deletions Board.cpp
@@ -1,27 +1,9 @@
#include <iostream>
#include "Board.hpp"
#include "Constants.hpp"
using std::cout;
using std::endl;

const int ROWS = 10;
const int COLUMNS = 10;

// A B C
// a b c
// 1 2 3
// r-1c
// rc-1 rc rc+1
// r+1c
// if A, B C || a 1
// if B, A C || b 2
// if C, A B || c 3
// if a, b c || A 1
// if b, a c || B 2
// if c, a b || C 3
// if 1, 2 3 || A a
// if 2, 1 3 || B b
// if 3, 1 2 || C c

// Constructor
Board::Board() {
for (int row = 0; row < ROWS; row++) {
Expand All @@ -32,6 +14,7 @@ Board::Board() {
}

bool Board::makeMove(int row, int column, char c) {

if (array[row][column] == '.' && isValidMove(row, column, c)) {
array[row][column] = c;
return true;
Expand All @@ -40,42 +23,185 @@ bool Board::makeMove(int row, int column, char c) {
}
}

vector<char> Board::getHorizontalRun(int row, int column) {
int runStart = column;
int runEnd = column;
vector<char> horizontalRun;

// find starting position of run
while (true) {
if (isInBounds(row, runStart - 1)) {
if (array[row][runStart - 1] == '.') {
break;
} else {
runStart--;
}
} else {
break;
}
}

// find ending position of array
while (true) {
if (isInBounds(row, runEnd + 1)) {
if (array[row][runEnd + 1] == '.') {
break;
} else {
runEnd++;
}
} else {
break;
}
}

// Use start and end positions to populate vector
for (int i = runStart; i <= runEnd; i++) {
horizontalRun.push_back(array[row][i]);
}

return horizontalRun;
}

bool Board::isValidMove(int row, int column, char c) {
bool above, below, left, right;

bool above, below, left, right,
above2, below2, left2, right2,
aboveConsec, rightConsec, belowConsec, leftConsec;

rightConsec = true;
belowConsec = true;
leftConsec = true;

// check above
if (isInBounds(row - 1, column)) {
cout << "above is in bounds" << endl;
above = isValidPiece(c, array[row - 1][column]);
} else {
above = true;
}

// check below
if (isInBounds(row + 1, column)) {
cout << "below is in bounds" << endl;
below = isValidPiece(c, array[row + 1][column]);
} else {
below = true;
}

// check left
if (isInBounds(row, column - 1)) {
cout << "left is in bounds" << endl;
left = isValidPiece(c, array[row][column - 1]);
} else {
left = true;
}

// check right
if (isInBounds(row, column + 1)) {
cout << "right is in bounds" << endl;
right = isValidPiece(c, array[row][column + 1]);
} else {
right = true;
}

return above && below && left && right;
// check above2
if (isInBounds(row - 2, column)) {
above2 = isValidPiece(c, array[row - 2][column]);
} else {
above2 = true;
}

// check below2
if (isInBounds(row + 2, column)) {
below2 = isValidPiece(c, array[row + 2][column]);
} else {
below2 = true;
}

// check left2
if (isInBounds(row, column - 2)) {
left2 = isValidPiece(c, array[row][column - 2]);
} else {
left2 = true;
}

// check right2
if (isInBounds(row, column + 2)) {
right2 = isValidPiece(c, array[row][column + 2]);
} else {
right2 = true;
}

// check no more than three pieces in a row above
if (isInBounds(row - 3, column)) {
aboveConsec = array[row - 1][column] == '.' || array[row - 2][column] == '.' || array[row - 3][column] == '.';
} else {
aboveConsec = true;
}

// check no more than three pieces in a row below
if (isInBounds(row + 3, column)) {
aboveConsec = array[row + 3][column] == '.' || array[row + 3][column] == '.' || array[row + 3][column] == '.';
} else {
belowConsec = true;
}

// check no more than three pieces in a row left
if (isInBounds(row, column - 3)) {
aboveConsec = array[row][column - 3] == '.' || array[row][column - 3] == '.' || array[row][column - 3] == '.';
} else {
leftConsec = true;
}

// check no more than three pieces in a row left
if (isInBounds(row, column + 3)) {
aboveConsec = array[row][column + 3] == '.' || array[row][column + 3] == '.' || array[row][column + 3] == '.';
} else {
rightConsec = true;
}

return above && below && left && right &&
above2 && below2 && left2 && right2 &&
aboveConsec && rightConsec && belowConsec && leftConsec;
}

// returns true if the char c and the char d are in the same group,
// either by type (ABC, abc, 123) or by count (Aa1, Bb2, Cc3),
// or if the location is empty
bool Board::isValidPiece(char c, char d) {
bool b = false;

switch (c) {
// ------ by type ----- ----- by count ----- - empty -
case 'A': if (d == 'B' || d == 'C' || d == 'a' || d == '1' || d == '.')
b = true;
break;
case 'B': if (d == 'A' || d == 'C' || d == 'b' || d == '2' || d == '.')
b = true;
break;
case 'C': if (d == 'A' || d == 'B' || d == 'c' || d == '3' || d == '.')
b = true;
break;
case 'a': if (d == 'b' || d == 'c' || d == 'A' || d == '1' || d == '.')
b = true;
break;
case 'b': if (d == 'a' || d == 'c' || d == 'B' || d == '2' || d == '.')
b = true;
break;
case 'c': if (d == 'a' || d == 'b' || d == 'C' || d == '3' || d == '.')
b = true;
break;
case '1': if (d == '2' || d == '3' || d == 'A' || d == 'a' || d == '.')
b = true;
break;
case '2': if (d == '1' || d == '3' || d == 'B' || d == 'b' || d == '.')
b = true;
break;
case '3': if (d == '1' || d == '2' || d == 'C' || d == 'c' || d == '.')
b = true;
break;
}

return b;
}

bool Board::isInBounds(int row, int column) {
if (row >= 0 && row <= 9 && column >= 0 && column <= 9) {
if (row >= 0 && row < ROWS && column >= 0 && column < COLUMNS) {
return true;
} else {
return false;
Expand Down
5 changes: 5 additions & 0 deletions Board.hpp
@@ -1,6 +1,9 @@
#ifndef BOARD_HPP
#define BOARD_HPP

#include <vector>
using std::vector;

class Board {
private:
char array[10][10];
Expand All @@ -9,7 +12,9 @@ class Board {
Board();
bool makeMove(int, int, char);
bool isValidMove(int, int, char);
bool isValidPiece(char, char);
bool isInBounds(int, int);
vector<char> getHorizontalRun(int,int);
void print();
};
#endif
2 changes: 2 additions & 0 deletions Constants.hpp
Expand Up @@ -2,5 +2,7 @@
#define CONSTANTS_HPP

const int HAND_SIZE = 6;
const int ROWS = 10;
const int COLUMNS = 10;

#endif
11 changes: 6 additions & 5 deletions Game.cpp
Expand Up @@ -12,13 +12,14 @@ Game::Game() {
turn = PLAYER_1;
player1.fillHand(pool);
player2.fillHand(pool);
}

void Game::playGame() {

cout << endl;
cout << "QUIRK" << endl;
cout << "Place pieces on the board and score points." << endl << endl;
}

void Game::playGame() {

while (state == UNFINISHED) {
printStatus();
promptMove();
Expand Down Expand Up @@ -71,11 +72,11 @@ void Game::promptMove() {
player.removePiece(piece);
player.fillHand(pool);
} else {
cout << "That position is already occupied. Try Again." << endl;
cout << "That piece can't go there. Try again." << endl << endl;
promptMove();
}
} else {
cout << "Sorry, you don't have that piece to play. Try again." << endl;
cout << "You don't have that piece to play. Try again." << endl;
promptMove();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Pool.cpp
Expand Up @@ -27,7 +27,7 @@ char Pool::pullPiece() {
} else {
int randomIndex = rand() % pieces.size();
char c = pieces[randomIndex]; // Get the piece
pieces.erase(pieces.begin() + randomIndex); // Remove the piece from the vector
pieces.erase(pieces.begin() + randomIndex); // Remove the piece

return c;
}
Expand Down

0 comments on commit 1303d0a

Please sign in to comment.