From 65cc387022dec66aee9bc86277e11a5c0bbefab0 Mon Sep 17 00:00:00 2001 From: jason riggs Date: Thu, 14 Mar 2019 17:47:14 +0000 Subject: [PATCH] change --- Board.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++++- Board.h | 38 ++++++---- mainOthello.cpp | 16 ++++- 3 files changed, 220 insertions(+), 17 deletions(-) diff --git a/Board.cpp b/Board.cpp index 31a62d0..5070f4e 100644 --- a/Board.cpp +++ b/Board.cpp @@ -44,6 +44,183 @@ void displayBoard::printBoard() { } } -char displayBoard::chipCheck(int x, int y) { - return board[x][y]; -} \ No newline at end of file +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; + x -= 1; + + for (int i = firstX; i < 8; i++) { + if (board[x][i] == chip) { + firstX = i; + break; + } + } + + for (int i = 7; i > firstX; i--) { + if (board[x][i] == chip) { + secondX = i; + break; + } + } + + for (int i = firstX; i <= secondX; i++) { + board[x][i] = {chip}; + } +} + +void displayBoard::horizontalCheck(int y, char chip) { + int firstY = 0; + int secondY = 0; + y -= 1; + + for (int i = firstY; i < 8; i++) { + if (board[i][y] == chip) { + firstY = i; + break; + } + } + + for (int i = 7; i > firstY; i--) { + if (board[i][y] == chip) { + secondY = i; + break; + } + } + + for (int i = firstY; i <= secondY; i++) { + board[i][y] = {chip}; + } +} + +void displayBoard::downleftCheck(int x,int y,char chip) { + x -= 1; + y -= 1; + int firstX = x; + int firstY = y; + int secondX; + int secondY; + bool addChip = false; + + + for (int i = 1; i < 8; i++) { + if (board[firstX - i][firstY + i] == chip) { + secondX = firstX - i; + secondY = firstY + i; + if (secondX < 8 && secondY < 8) { + addChip = true; + } + break; + } + } + + if (addChip == true) { + int temp = firstX - secondX; + int i = 1; + while (temp > 0) { + board[firstX - i][firstY + i] = {chip}; + temp --; + i++; + } + } +} + +void displayBoard::upleftCheck(int x, int y, char chip) { + x -= 1; + y -= 1; + int firstX = x; + int firstY = y; + int secondX; + int secondY; + bool addChip = false; + + + for (int i = 1; i < 8; i++) { + if (board[firstX - i][firstY - i] == chip) { + secondX = firstX - i; + secondY = firstY - i; + if (secondX < 8 && secondY < 8) { + addChip = true; + } + break; + } + } + + if (addChip == true) { + int temp = firstX - secondX; + int i = 1; + while (temp > 0) { + board[firstX - i][firstY - i] = {chip}; + temp --; + i++; + } + } +} + +void displayBoard::downrightCheck(int x, int y, char chip) { + x -= 1; + y -= 1; + int firstX = x; + int firstY = y; + int secondX; + int secondY; + bool addChip = false; + + + for (int i = 1; i < 8; i++) { + if (board[firstX + i][firstY + i] == chip) { + secondX = firstX + i; + secondY = firstY + i; + if (secondX < 8 && secondY < 8) { + addChip = true; + } + break; + } + } + + if (addChip == true) { + int temp = secondX - firstX; + int i = 1; + while (temp > 0) { + board[firstX + i][firstY + i] = {chip}; + temp --; + i++; + } + } +} + +void displayBoard::uprightCheck(int x,int y,char chip) { + x -= 1; + y -= 1; + int firstX = x; + int firstY = y; + int secondX; + int secondY; + bool addChip = false; + + + for (int i = 1; i < 8; i++) { + if (board[firstX + i][firstY - i] == chip) { + secondX = firstX + i; + secondY = firstY - i; + if (secondX < 8 && secondY < 8) { + addChip = true; + } + break; + } + } + + if (addChip == true) { + int temp = secondX - firstX; + int i = 1; + while (temp > 0) { + board[firstX + i][firstY - i] = {chip}; + temp --; + i++; + } + } +} + diff --git a/Board.h b/Board.h index 8d41181..eaeb704 100644 --- a/Board.h +++ b/Board.h @@ -1,15 +1,27 @@ +#include +#include + + +using namespace std; + class displayBoard { - public: - void start(); - // mutators and other - void playerMove1(int x, int y); - void playerMove2(int x, int y); - void printBoard(); - char chipCheck(int x, int y); - - private: - char board[9][9] = {}; - int x; - int y; -}; \ No newline at end of file + public: + void start(); + void playerMove1(int x, int y); + void playerMove2(int x, int y); + void printBoard(); + char getChip(int x, int y); + 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); + private: + char board[8][8] = {}; + int x; + int y; + + +}; diff --git a/mainOthello.cpp b/mainOthello.cpp index d42fdee..f0cf38d 100644 --- a/mainOthello.cpp +++ b/mainOthello.cpp @@ -34,7 +34,7 @@ int main() { displayBoard board; board.start(); - // loop until finish + do { board.printBoard(); cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ; @@ -44,12 +44,26 @@ int main() { if (turn == 1) { board.playerMove1(x,y); + 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'); turn++; } else if (turn == 2) { board.playerMove2(x,y); + 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'); turn--; } + } while (winner == false); + return 0; -- GitLab