Commit 21425bc4 authored by Jason Riggs's avatar Jason Riggs

change

parent 02b9c5d1
......@@ -4,6 +4,7 @@
using namespace std;
// Place starting pieces
void displayBoard::start() {
board[4-1][4-1] = {'X'};
board[5-1][4-1] = {'O'};
......@@ -11,16 +12,19 @@ void displayBoard::start() {
board[4-1][5-1] = {'O'};
}
// Place X's pieces
void displayBoard::playerMove1(int x, int y) {
board[x-1][y-1] = {'X'};
}
// Place O's pieces
void displayBoard::playerMove2(int x, int y) {
board[x-1][y-1] = {'O'};
}
// Prints the board
void displayBoard::printBoard() {
cout << " 1 2 3 4 5 6 7 8 " << endl;
cout << " +---+---+---+---+---+---+---+---+ "<< endl;
......@@ -38,63 +42,134 @@ void displayBoard::printBoard() {
}
}
char displayBoard::getChip(int x, int y) {
return board[x-1][y-1];
}
void displayBoard::verticalCheck(int x, char chip) {
int firstX = 0;
int secondX = 0;
// Checks vertical down for pieces to switch
void displayBoard::downCheck(int x, int y, char chip, bool change) {
x -= 1;
for (int i = firstX; i < 8; i++) {
y -= 1;
int firstY = y;
int secondY;
bool addChip = false;
for (int i = firstY + 1; i < 8; i++) {
if (board[x][i] == chip) {
firstX = i;
secondY = i;
if (0 <= secondY < 8) {
addChip = true;
}
break;
}
}
for (int i = 7; i > firstX; i--) {
if (addChip == true) {
difference = (secondY - firstY) - 1;
if (change == true) {
for (int i = firstY; i <= secondY; i++) {
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
}
}
}
}
// Checks vertical up for pieces to switch
void displayBoard::upCheck(int x, int y, char chip, bool change) {
x -= 1;
y -= 1;
int firstY;
int secondY = y;
bool addChip = false;
for (int i = secondY - 1; i >= 0; i--) {
if (board[x][i] == chip) {
secondX = i;
firstY = i;
if (0 <= firstY < 8) {
addChip = true;
}
break;
}
}
for (int i = firstX; i <= secondX; i++) {
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
if (addChip == true) {
difference = (secondY - firstY) - 1;
if (change == true) {
for (int i = firstY; i <= secondY; i++) {
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
}
}
}
}
}
void displayBoard::horizontalCheck(int y, char chip) {
int firstY = 0;
int secondY = 0;
// Checks horizontal left for pieces to switch
void displayBoard::leftCheck(int x, int y, char chip, bool change) {
x -= 1;
y -= 1;
int firstX;
int secondX = x;
bool addChip = false;
for (int i = firstY; i < 8; i++) {
for (int i = secondX - 1; i >= 0; i--) {
if (board[i][y] == chip) {
firstY = i;
firstX = i;
if (0 <= firstX < 8) {
addChip = true;
}
break;
}
}
for (int i = 7; i > firstY; i--) {
if (addChip == true) {
difference = (secondX - firstX) - 1;
if (change == true) {
for (int i = firstX; i <= secondX; i++) {
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
}
}
}
}
// Checks horizontal right for pieces to switch
void displayBoard::rightCheck(int x, int y, char chip, bool change) {
x -= 1;
y -= 1;
int firstX = x;
int secondX;
bool addChip = false;
for (int i = firstX + 1; i < 8; i++) {
if (board[i][y] == chip) {
secondY = i;
secondX = i;
if (0 <= secondX < 8) {
addChip = true;
}
break;
}
}
for (int i = firstY; i <= secondY; i++) {
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
if (addChip == true) {
difference = (secondX - firstX) - 1;
if (change == true) {
for (int i = firstX; i <= secondX; i++) {
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
}
}
}
}
void displayBoard::downleftCheck(int x,int y,char chip) {
// Checks diagonal left down for pieces to switch
void displayBoard::downleftCheck(int x,int y,char chip, bool change) {
x -= 1;
y -= 1;
int firstX = x;
......@@ -108,7 +183,7 @@ void displayBoard::downleftCheck(int x,int y,char chip) {
if (board[firstX - i][firstY + i] == chip) {
secondX = firstX - i;
secondY = firstY + i;
if (secondX < 8 && secondY < 8) {
if (0 <= secondX < 8 && 0 <= secondY < 8) {
addChip = true;
}
break;
......@@ -116,19 +191,24 @@ void displayBoard::downleftCheck(int x,int y,char chip) {
}
if (addChip == true) {
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
if (board[firstX - i][firstY + i] == 'X' || board[firstX - i][firstY + i] == 'O') {
board[firstX - i][firstY + i] = {chip};
difference = (firstX - secondX) - 1;
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
if (board[firstX - i][firstY + i] == 'X' || board[firstX - i][firstY + i] == 'O') {
board[firstX - i][firstY + i] = {chip};
}
temp --;
i++;
}
temp --;
i++;
}
}
}
void displayBoard::upleftCheck(int x, int y, char chip) {
// Checks diagonal left up for pieces to switch
void displayBoard::upleftCheck(int x, int y, char chip, bool change) {
x -= 1;
y -= 1;
int firstX = x;
......@@ -142,7 +222,7 @@ void displayBoard::upleftCheck(int x, int y, char chip) {
if (board[firstX - i][firstY - i] == chip) {
secondX = firstX - i;
secondY = firstY - i;
if (secondX < 8 && secondY < 8) {
if (0 <= secondX < 8 && 0 <= secondY < 8) {
addChip = true;
}
break;
......@@ -150,19 +230,24 @@ void displayBoard::upleftCheck(int x, int y, char chip) {
}
if (addChip == true) {
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
if (board[firstX - i][firstY - i] == 'X' || board[firstX - i][firstY - i] == 'O') {
board[firstX - i][firstY - i] = {chip};
difference = (firstX - secondX) - 1;
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
if (board[firstX - i][firstY - i] == 'X' || board[firstX - i][firstY - i] == 'O') {
board[firstX - i][firstY - i] = {chip};
}
temp --;
i++;
}
temp --;
i++;
}
}
}
void displayBoard::downrightCheck(int x, int y, char chip) {
// Checks diagonal right down for pieces to switch
void displayBoard::downrightCheck(int x, int y, char chip, bool change) {
x -= 1;
y -= 1;
int firstX = x;
......@@ -176,7 +261,7 @@ void displayBoard::downrightCheck(int x, int y, char chip) {
if (board[firstX + i][firstY + i] == chip) {
secondX = firstX + i;
secondY = firstY + i;
if (secondX < 8 && secondY < 8) {
if (0 <= secondX < 8 && 0 <= secondY < 8) {
addChip = true;
}
break;
......@@ -184,19 +269,24 @@ void displayBoard::downrightCheck(int x, int y, char chip) {
}
if (addChip == true) {
int temp = secondX - firstX;
int i = 1;
while (temp > 0) {
if (board[firstX + i][firstY + i] == 'X' || board[firstX + i][firstY + i] == 'O') {
board[firstX + i][firstY + i] = {chip};
difference = (secondX - firstX) - 1;
if (change == true){
int temp = secondX - firstX;
int i = 1;
while (temp > 0) {
if (board[firstX + i][firstY + i] == 'X' || board[firstX + i][firstY + i] == 'O') {
board[firstX + i][firstY + i] = {chip};
}
temp --;
i++;
}
temp --;
i++;
}
}
}
void displayBoard::uprightCheck(int x,int y,char chip) {
// Checks diagonal right up for pieces to switch
void displayBoard::uprightCheck(int x,int y,char chip, bool change) {
x -= 1;
y -= 1;
int firstX = x;
......@@ -210,7 +300,7 @@ void displayBoard::uprightCheck(int x,int y,char chip) {
if (board[firstX + i][firstY - i] == chip) {
secondX = firstX + i;
secondY = firstY - i;
if (secondX < 8 && secondY < 8) {
if (0 <= secondX < 8 && 0 <= secondY < 8) {
addChip = true;
}
break;
......@@ -218,21 +308,23 @@ void displayBoard::uprightCheck(int x,int y,char chip) {
}
if (addChip == true) {
int temp = secondX - firstX;
int i = 1;
while (temp > 0) {
if (board[firstX + i][firstY - i] == 'X' || board[firstX + i][firstY - i] == 'O') {
board[firstX + i][firstY - i] = {chip};
difference = (firstX - secondX) - 1;
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
if (board[firstX + i][firstY - i] == 'X' || board[firstX + i][firstY - i] == 'O') {
board[firstX + i][firstY - i] = {chip};
}
temp --;
i++;
}
temp --;
i++;
}
}
}
// Checks to see if there is an opponents piece around the placed piece
bool displayBoard::isValidAdjacent(int row, int col, char oppTurn) {
if (board[row + 1][col] == oppTurn) {
return true;
......@@ -261,6 +353,7 @@ bool displayBoard::isValidAdjacent(int row, int col, char oppTurn) {
return false;
}
// Checks to see if there is a piece to flip around the placed piece
bool displayBoard::isValidFlip(int row, int col, char playerTurn) {
for (int i = 2; i < 8; i++) {
if (board[row][col + i] == playerTurn) {
......
......@@ -11,20 +11,28 @@ class displayBoard {
void playerMove1(int x, int y);
void playerMove2(int x, int y);
void printBoard();
char getChip(int x, int y);
// Checks to flip peices
void verticalCheck(int x, char chip);
void horizontalCheck(int y, char chip);
void upleftCheck(int x, int y, char chip);
void downleftCheck(int x, int y, char chip);
void uprightCheck(int x, int y, char chip);
void downrightCheck(int x, int y, char chip);
int getDifference() { return difference; }
// Vertical and horizontal checks
void downCheck(int x, int y, char chip, bool change);
void upCheck(int x, int y, char chip, bool change);
void leftCheck(int x, int y, char chip, bool change);
void rightCheck(int x, int y, char chip, bool change);
// diagonal checks
void upleftCheck(int x, int y, char chip, bool change);
void downleftCheck(int x, int y, char chip, bool change);
void uprightCheck(int x, int y, char chip, bool change);
void downrightCheck(int x, int y, char chip, bool change);
// Player movement error checking
bool isValidAdjacent(int row, int col, char oppTurn);
bool isValidFlip(int row, int col, char playerTurn);
private:
char board[8][8] = {};
int difference = 0; // count for the total chips being turned in a move
int x;
int y;
......
......@@ -64,12 +64,16 @@ int main() {
// Player moves
board.playerMove1(x,y);
// Check for flips in every direction
board.verticalCheck(x, 'X');
board.horizontalCheck(y, 'X');
board.uprightCheck(x,y,'X');
board.upleftCheck(x,y,'X');
board.downrightCheck(x,y,'X');
board.downleftCheck(x,y,'X');
board.upCheck(x, y, 'X', true);
board.downCheck(x, y, 'X', true);
board.leftCheck(x, y, 'X', true);
board.rightCheck(x, y, 'X', true);
board.uprightCheck(x, y,'X', true);
board.upleftCheck(x, y,'X', true);
board.downrightCheck(x, y,'X', true);
board.downleftCheck(x, y,'X', true);
// Change to player 2
turn++;
......@@ -85,12 +89,16 @@ int main() {
// Player moves
board.playerMove2(x,y);
// Check for flips in every direction
board.verticalCheck(x, 'O');
board.horizontalCheck(y, 'O');
board.uprightCheck(x,y,'O');
board.upleftCheck(x,y,'O');
board.downrightCheck(x,y,'O');
board.downleftCheck(x,y,'O');
board.upCheck(x, y, 'O', true);
board.downCheck(x, y, 'O', true);
board.leftCheck(x, y, 'O', true);
board.rightCheck(x, y, 'O', true);
board.uprightCheck(x,y,'O', true);
board.upleftCheck(x,y,'O', true);
board.downrightCheck(x,y,'O', true);
board.downleftCheck(x,y,'O', true);
// Change to player 1
turn--;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment