Commit 67835c58 authored by Jason Riggs's avatar Jason Riggs

change

parent 11d86453
......@@ -3,11 +3,13 @@
#include <utility>
#include "AIplayer.h"
#include "Board.h"
#include "Cell.h"
using namespace std;
pair<int, int> AIplayer::move1(displayBoard AImove) {
// declare variables
Cell check;
int x = 0, y = 0;
pair<int, int> AIpair;
......@@ -15,10 +17,7 @@ pair<int, int> AIplayer::move1(displayBoard AImove) {
// loop until valid move is found
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
bool flag1 = AImove.isValidAdjacent(row, col, 'X');
bool flag2 = AImove.isValidFlip(row, col, 'O');
cout << row << " " << col << " " << flag1 << " " << flag2<<endl;
if (AImove.isValidAdjacent(row, col, 'X') && AImove.isValidFlip(row, col, 'O')) {
if (check.isLegalPlacementfor(row, col, AImove.board, 'O', 'X')) {
x = row;
y = col;
}
......@@ -30,11 +29,9 @@ pair<int, int> AIplayer::move1(displayBoard AImove) {
}
pair<int, int> AIplayer::move2() {
// declare class variable for board
displayBoard AImove;
pair<int, int> AIplayer::move2(displayBoard AImove) {
// declare variables
Cell check;
pair<int, int> AIpair;
int x = 0;
int y = 0;
......@@ -45,10 +42,9 @@ pair<int, int> AIplayer::move2() {
// check each position for valid move
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
//if (AImove.isValidAdjacent(row, col, 'X') && AImove.isValidFlip(row, col, 'O')) {
if (check.isLegalPlacementfor(row, col, AImove.board, 'O', 'X')) {
AImove.upCheck(row, col, 'O', false);
//temp = AImove.getDifference();
cout << AImove.getDifference()<< " " << x++<<endl;
temp = AImove.getDifference();
AImove.downCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.leftCheck(row, col, 'O', false);
......@@ -68,7 +64,7 @@ pair<int, int> AIplayer::move2() {
y = row;
x = col;
}
//}
}
}
}
AIpair.first = x;
......
#include <cstdlib>
#include <utility>
#include "Board.h"
#include "Cell.h"
using namespace std;
class displayBoard;
class Cell;
class AIplayer {
public:
AIplayer() { srand(0); } // basic constructor to seed random number generator
AIplayer(int seed) { srand(seed); } // constructor to seed random number generator with desired seed
pair<int, int> move1(displayBoard board); // easy move
pair<int, int> move2(); // hard move mutator
pair<int, int> move1(displayBoard AImove); // easy move
pair<int, int> move2(displayBoard AImove); // hard move mutator
private: // not used
......
......@@ -6,6 +6,11 @@ using namespace std;
// Place starting pieces
void displayBoard::start() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = ' ';
}
}
// The rest of the board has random data in it possible including X and O
board[4-1][4-1] = {'X'};
board[5-1][4-1] = {'O'};
......@@ -36,13 +41,14 @@ void displayBoard::printBoard() {
if (isalpha(board[i][j])) {
cout << " " << board[i][j] << " |";
} else {
cout << " " << board[i][j] << " |";
cout << board[i][j] << " |";
}
}
cout << endl << " +---+---+---+---+---+---+---+---+ "<< endl;
}
}
void displayBoard::downCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going down
x -= 1;
y -= 1;
......@@ -396,66 +402,3 @@ void displayBoard::uprightCheck(int x,int y,char chip, bool change) { // functi
// Checks to see if there is an opponents piece around the placed piece
bool displayBoard::isValidAdjacent(int x, int y, char oppTurn) {
if(y == 2){
cout << "displayBoard::isValidAdjacent(" << x << "," << y << "," << (char)oppTurn<< ")";
cout << " where (4,2) is \'"<< (char)board[4][2] << "\' (" << (int)board[4][2] << ")" << endl;
}
if (board[x + 1][y] == oppTurn) {
return true;
}
else if (board[x - 1][y] == oppTurn) {
return true;
}
else if (board[x][y + 1] == oppTurn) {
return true;
}
else if (board[x][y - 1] == oppTurn) {
return true;
}
else if (board[x - 1][y - 1] == oppTurn) {
return true;
}
else if (board[x - 1][y + 1] == oppTurn) {
return true;
}
else if (board[x + 1][y - 1] == oppTurn) {
return true;
}
else if (board[x + 1][y + 1] == oppTurn) {
return true;
}
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) {
return true;
}
else if (board[row][col - i] == playerTurn) {
return true;
}
else if (board[row + i][col] == playerTurn) {
return true;
}
else if (board[row - i][col] == playerTurn) {
return true;
}
else if (board[row + i][col + i] == playerTurn) {
return true;
}
else if (board[row + i][col - i] == playerTurn) {
return true;
}
else if (board[row - i][col + i] == playerTurn) {
return true;
}
else if (board[row - i][col - i] == playerTurn) {
return true;
}
}
return false;
}
\ No newline at end of file
......@@ -24,17 +24,13 @@ class displayBoard {
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);
char board[8][8] = {};
private:
char board[8][8] = {};
int difference = 0; // count for the total chips being turned in a move
int x;
int y;
int difference = 0; // count for the total chips being turned in a move
int x;
int y;
};
......
#include <iostream>
#include <stdexcept>
#include <vector>
#include <cassert>
#include "Cell.h"
using namespace std;
bool Cell::isLegalPlacementfor(int x, int y, char board[][8], char playDisc, char oppDisc) {
cout << "Looking at " << x << " " << y << " to place " << (char)playDisc << endl;
vector<Directions> directions;
Cell current(x - 1, y - 1);
if (current.isOccupied(board, playDisc) || current.isOccupied(board, oppDisc)) {
return false;
}
Cell north(x - 1, y - 2);
if (north.isOccupied(board, oppDisc)) {
directions.push_back(north.NORTH);
cout << "N " << north.x << " " << north.y << endl;
}
Cell northEast(x, y - 2);
if (northEast.isOccupied(board, oppDisc)) {
directions.push_back(northEast.NORTHEAST);
cout << "NE " << northEast.x << " " << northEast.y << endl;
}
Cell east(x, y - 1);
if (east.isOccupied(board, oppDisc)) {
directions.push_back(EAST);
cout << "E " << east.x << " " << east.y << endl;
}
Cell southEast(x, y);
if (southEast.isOccupied(board, oppDisc)) {
directions.push_back(SOUTHEAST);
cout << "SE " << southEast.x << " " << southEast.y << endl;
}
Cell south(x - 1, y);
if (south.isOccupied(board, oppDisc)) {
directions.push_back(SOUTH);
cout << "S " << south.x << " " << south.y << endl;
}
Cell southWest(x - 2, y);
if (southWest.isOccupied(board, oppDisc)) {
directions.push_back(SOUTHWEST);
cout << "SW " << southWest.x << " " << southWest.y << endl;
}
Cell west(x - 2, y - 1);
if (west.isOccupied(board, oppDisc)) {
directions.push_back(WEST);
cout << "W " << west.x << " " << west.y << endl;
}
Cell northWest(x - 2, y - 2);
if (northWest.isOccupied(board, oppDisc)) {
directions.push_back(NORTHWEST);
cout << "NW " << northWest.x << " " << northWest.y << endl;
}
for (int i = 0; i < directions.size(); i++) {
cout << directions.at(i) << endl;
}
for (auto direction : directions) {
try{
int value = current.sequenceLength(direction, board, oppDisc, "");
cout << "For Direction " << direction << " there was a value of " << value << endl;
if (value > 0) {
return true;
}
}
catch(runtime_error &excpt) {
cout << excpt.what() << endl;
}
}
return false;
}
int Cell::sequenceLength(Directions direction, char board[][8], char playDisc, string indent) {
int x1, y1;
cout << indent << "Entering sequenceLength with " << x + 1 << " " << y + 1 << " Looking for " << (char)playDisc <<endl;
switch (direction) {
case NORTH:
x1 = x;
y1 = y - 1;
break;
case NORTHEAST:
x1 = x + 1;
y1 = y - 1;
break;
case EAST:
x1 = x + 1;
y1 = y;
break;
case SOUTHEAST:
y1 = y + 1;
x1 = x + 1;
break;
case SOUTH:
x1 = x;
y1 = y + 1;
break;
case SOUTHWEST:
y1 = y + 1;
x1 = x - 1;
case WEST:
x1 = x - 1;
y1 = y;
break;
case NORTHWEST:
y1 = y - 1;
x1 = x - 1;
break;
default:
exit(1);
}
cout << indent << "New sequenceLength with " << x1 + 1 << " " << y1 + 1 << endl;
if (x1 < 0 || x1 >= 8 || y1 < 0 || y1 >= 8) {
cout << "Off the Board!" << endl;
throw runtime_error("Invalid Sequence");
}
Cell sequence(x1, y1);
if (board[x1][y1] == ' ') {
cout << indent << "Found a space. Treat as invalid" << endl;
throw runtime_error("Invalid Input");
}
if (board[x1][y1] != playDisc) {
cout << indent << "Found \'" << (char)board[x1][y1] << "\' Returning 0" << endl;
return 0;
}
int result = (1 + sequence.sequenceLength(direction, board, playDisc, " " + indent));
cout << indent << "Found \'" << (char)board[x1][y1] << "\' Returning " << result << endl;
return result;
}
bool Cell::isOccupied(char board[][8], char disc) {
return (board[x][y] == disc);
}
#ifndef CELL_H
#define CELL_H
#include <vector>
using namespace std;
class Cell {
public:
Cell(int x, int y) {this->x = x; this->y = y;}
Cell() {this->x = 0; this->y = 0;}
enum Directions{NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST};
bool isLegalPlacementfor(int x, int y, char board[][8], char playDisc, char oppDisc);
int sequenceLength(Directions direction, char board[][8], char playDisc, string indent);
bool isOccupied(char board[][8], char disc);
int x, y;
private:
};
#endif
......@@ -2,6 +2,7 @@
#include <utility>
#include "Board.h"
#include "AIplayer.h"
#include "Cell.h"
using namespace std;
......@@ -53,8 +54,10 @@ int main() {
// start the game
displayBoard board;// Brand new board
board.start();
board.printBoard();
Cell check;
cout << "X goes first."<<endl;
......@@ -69,7 +72,7 @@ int main() {
cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
cin >> x >> y;
// Check fo valid move
if(board.isValidAdjacent(x-1,y-1,'O') && board.isValidFlip(x-1, y-1, 'X')){
if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
// Player moves
board.playerMove1(x,y);
// Check for flips in every direction
......@@ -98,28 +101,28 @@ int main() {
aiX = AImove.first;
aiY = AImove.second;
//Check fo valid move
// if(board.isValidAdjacent(aiX-1, aiY-1, 'X') && board.isValidFlip(aiX-1, aiY-1, 'O')){
// // Player moves
// board.playerMove2(aiX,aiY);
// // Check for flips in every direction
// board.upCheck(aiX,aiY, 'O', true);
// board.downCheck(aiX,aiY, 'O', true);
// board.leftCheck(aiX,aiY, 'O', true);
// board.rightCheck(aiX,aiY, 'O', true);
if(check.isLegalPlacementfor(aiX, aiY, board.board, 'O', 'X')){
// Player moves
board.playerMove2(aiX,aiY);
// Check for flips in every direction
board.upCheck(aiX,aiY, 'O', true);
board.downCheck(aiX,aiY, 'O', true);
board.leftCheck(aiX,aiY, 'O', true);
board.rightCheck(aiX,aiY, 'O', true);
// board.uprightCheck(aiX,aiY,'O', true);
// board.upleftCheck(aiX,aiY,'O', true);
// board.downrightCheck(aiX,aiY,'O', true);
// board.downleftCheck(aiX,aiY,'O', true);
board.uprightCheck(aiX,aiY,'O', true);
board.upleftCheck(aiX,aiY,'O', true);
board.downrightCheck(aiX,aiY,'O', true);
board.downleftCheck(aiX,aiY,'O', true);
//Change to player 1
turn--;
// }else{
// // Prints invalid and then asks for another move
// cout << "Invalid Move!"<<endl;
}else{
// Prints invalid and then asks for another move
cout << "Invalid Move!"<<endl;
// }
}
}
board.printBoard();
......@@ -137,7 +140,7 @@ int main() {
cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
cin >> x >> y;
// Check fo valid move
if(board.isValidAdjacent(x-1,y-1,'O') && board.isValidFlip(x-1, y-1, 'X')){
if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
// Player moves
board.playerMove1(x,y);
// Check for flips in every direction
......@@ -161,12 +164,12 @@ int main() {
// Player two's Move
} else if (turn == 2) {
AImove = AIturn.move2();
AImove = AIturn.move2(board);
cout << AImove.first << " " << AImove.second << endl;
aiX = AImove.first;
aiY = AImove.second;
// Check fo valid move
if(board.isValidAdjacent(aiX-1, aiY-1, 'X') && board.isValidFlip(aiX-1, aiY-1, 'O')){
if(check.isLegalPlacementfor(aiX, aiY, board.board, 'O', 'X')){
// Player moves
board.playerMove2(aiX,aiY);
// Check for flips in every direction
......@@ -206,7 +209,7 @@ int main() {
// Player one's move
if (turn == 1) {
// Check fo valid move
if(board.isValidAdjacent(x-1,y-1,'O') && board.isValidFlip(x-1, y-1, 'X')){
if(check.isLegalPlacementfor(x, y, board.board, 'X', 'O')){
// Player moves
board.playerMove1(x,y);
// Check for flips in every direction
......@@ -231,7 +234,7 @@ int main() {
// Player two's Move
} else if (turn == 2) {
// Check fo valid move
if(board.isValidAdjacent(x-1, y-1, 'X') && board.isValidFlip(x-1, y-1, 'O')){
if(check.isLegalPlacementfor(x, y, board.board, 'O', 'X')){
// Player moves
board.playerMove2(x,y);
// Check for flips in every direction
......
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