Commit a666f285 authored by Jason Riggs's avatar Jason Riggs

change

parent fd7a23cb
#include <iostream>
#include <cstdlib>
#include <utility>
#include "AIplayer.h"
#include "Board.h"
using namespace std;
// Creates a random x and y for the easy computers move
void AIplayer::move1(int &x, int &y) {
pair<int, int> AIplayer::move1() {
// declare variables
displayBoard AImove;
int tempRow, tempCol;
while (!(AImove.isValidAdjacent(tempCol, tempRow, 'X') && AImove.isValidFlip(tempCol, tempRow, 'O'))) {
tempRow = rand() % 8;
tempCol = rand() % 8;
if (AImove.isValidAdjacent(tempCol, tempRow, 'X') && AImove.isValidFlip(tempCol, tempRow, 'O')) {
x = tempCol;
y = tempRow;
int tempRow, tempCol, x = 0, y = 0;
pair<int, int> AIpair;
// loop until valid move is found
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
tempRow = row;
tempCol = col;
if (AImove.isValidAdjacent(tempRow, tempCol, 'X') && AImove.isValidFlip(tempRow, tempCol, 'O')) {
x = tempRow;
y = tempCol;
}
}
}
AIpair.first = x;
AIpair.second = y;
return AIpair;
}
// Picks the best possible move for the hard computer
void AIplayer::move2(int &x, int &y) {
pair<int, int> AIplayer::move2() {
// declare class variable for board
displayBoard AImove;
// declare variables
pair<int, int> AIpair;
int x = 0;
int y = 0;
// chip gain
int chips = 0;
int temp = 0;
// check each position for valid move
for (int row = 0; row < 9; ++row) {
for (int col = 0; col < 9; ++col) {
if (AImove.isValidAdjacent(row, col, 'X') && AImove.isValidFlip(row, col, 'O')) {
AImove.upCheck(x, y, 'O', false);
temp = AImove.getDifference();
AImove.downCheck(x, y, 'O', false);
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')) {
AImove.upCheck(row, col, 'O', false);
//temp = AImove.getDifference();
cout << AImove.getDifference()<< " " << x++<<endl;
AImove.downCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.leftCheck(x, y, 'O', false);
AImove.leftCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.rightCheck(x, y, 'O', false);
AImove.rightCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.uprightCheck(x,y,'O', false);
AImove.uprightCheck(row, col,'O', false);
temp += AImove.getDifference();
AImove.upleftCheck(x,y,'O', false);
AImove.upleftCheck(row, col,'O', false);
temp += AImove.getDifference();
AImove.downrightCheck(x,y,'O', false);
AImove.downrightCheck(row, col,'O', false);
temp += AImove.getDifference();
AImove.downleftCheck(x,y,'O', false);
AImove.downleftCheck(row, col,'O', false);
temp += AImove.getDifference();
if (temp > chips) {
chips = temp;
y = row;
x = col;
}
}
//}
}
}
AIpair.first = x;
AIpair.second = y;
return AIpair;
}
#include <cstdlib>
#include <utility>
class Board;
using namespace std;
class Error;
class AIplayer {
public:
// basic constructor to seed random number generator
AIplayer() { srand(0); }
// constructor to seed random number generator with desired seed
AIplayer(int seed) { srand(seed); }
// easy move
void move1(int &x, int &y);
// hard move mutator
void move2(int &x, int &y);
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(); // easy move
pair<int, int> move2(); // hard move mutator
private: // not used
};
......@@ -42,15 +42,15 @@ void displayBoard::printBoard() {
}
}
// Checks vertical down for pieces to switch
void displayBoard::downCheck(int x, int y, char chip, bool change) {
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;
int firstY = y;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = firstY + 1; i < 8; i++) {
for (int i = firstY + 1; i < 8; i++) { // loop that goes down from the origin and finds the first chip of the same character
if (board[x][i] == chip) {
secondY = i;
if (0 <= secondY < 8) {
......@@ -61,10 +61,18 @@ void displayBoard::downCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (secondY - firstY) - 1;
this->difference = (secondY - firstY) - 1; // the difference will be the amount of chips that are being flipped
for (int i = firstY; i <= secondY; i++) {
if (board[x][i] == 'X' || board[x][i] == 'O') { // loop that checks if there are any spaces between the two points that
// doesnt have a chip in it
} else {
change = false;
}
}
if (change == true) {
for (int i = firstY; i <= secondY; i++) {
for (int i = firstY; i <= secondY; i++) { // loop that goes through the line, changing the chips
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
......@@ -73,15 +81,15 @@ void displayBoard::downCheck(int x, int y, char chip, bool change) {
}
}
// Checks vertical up for pieces to switch
void displayBoard::upCheck(int x, int y, char chip, bool change) {
void displayBoard::upCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going up
x -= 1;
y -= 1;
int firstY;
int secondY = y;
bool addChip = false;
this->difference = 0;
for (int i = secondY - 1; i >= 0; i--) {
for (int i = secondY - 1; i >= 0; i--) { // loop that goes down from the origin and finds the first chip of the same character
if (board[x][i] == chip) {
firstY = i;
if (0 <= firstY < 8) {
......@@ -92,10 +100,19 @@ void displayBoard::upCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (secondY - firstY) - 1;
this->difference = (secondY - firstY) - 1; // the difference will be the amount of chips that are being flipped
for (int i = firstY; i <= secondY; i++) {
if (board[x][i] == 'X' || board[x][i] == 'O') { // loop that checks if there are any spaces between the two points that
// doesnt have a chip in it
} else {
change = false;
}
}
if (change == true) {
for (int i = firstY; i <= secondY; i++) {
for (int i = firstY; i <= secondY; i++) { // loop that goes through the line, changing the chips
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
......@@ -104,16 +121,16 @@ void displayBoard::upCheck(int x, int y, char chip, bool change) {
}
}
// Checks horizontal left for pieces to switch
void displayBoard::leftCheck(int x, int y, char chip, bool change) {
void displayBoard::leftCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going left
x -= 1;
y -= 1;
int firstX;
int secondX = x;
bool addChip = false;
this->difference = 0;
for (int i = secondX - 1; i >= 0; i--) {
for (int i = secondX - 1; i >= 0; i--) { // loop that goes down from the origin and finds the first chip of the same character
if (board[i][y] == chip) {
firstX = i;
if (0 <= firstX < 8) {
......@@ -124,10 +141,19 @@ void displayBoard::leftCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (secondX - firstX) - 1;
this->difference = (secondX - firstX) - 1; // the difference will be the amount of chips that are being flipped
for (int i = firstX; i <= secondX; i++) {
if (board[i][y] == 'X' || board[i][y] == 'O') { // loop that checks if there are any spaces between the two points that
// doesnt have a chip in it
} else {
change = false;
}
}
if (change == true) {
for (int i = firstX; i <= secondX; i++) {
for (int i = firstX; i <= secondX; i++) { // loop that goes through the line, changing the chips
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
......@@ -136,16 +162,17 @@ void displayBoard::leftCheck(int x, int y, char chip, bool change) {
}
}
// Checks horizontal right for pieces to switch
void displayBoard::rightCheck(int x, int y, char chip, bool change) {
void displayBoard::rightCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going right
x -= 1;
y -= 1;
int firstX = x;
int secondX;
bool addChip = false;
this->difference = 0;
for (int i = firstX + 1; i < 8; i++) {
for (int i = firstX + 1; i < 8; i++) { // loop that goes down from the origin and finds the first chip of the same character
if (board[i][y] == chip) {
secondX = i;
if (0 <= secondX < 8) {
......@@ -156,10 +183,19 @@ void displayBoard::rightCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (secondX - firstX) - 1;
this->difference = (secondX - firstX) - 1; // the difference will be the amount of chips that are being flipped
for (int i = firstX; i <= secondX; i++) {
if (board[i][y] == 'X' || board[i][y] == 'O') { // loop that checks if there are any spaces between the two points that
// doesnt have a chip in it
} else {
change = false;
}
}
if (change == true) {
for (int i = firstX; i <= secondX; i++) {
for (int i = firstX; i <= secondX; i++) { // loop that goes through the line, changing the chips
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
......@@ -168,8 +204,7 @@ void displayBoard::rightCheck(int x, int y, char chip, bool change) {
}
}
// Checks diagonal left down for pieces to switch
void displayBoard::downleftCheck(int x,int y,char chip, bool change) {
void displayBoard::downleftCheck(int x,int y,char chip, bool change) { // function for checking and changing chips in a line going diagonally down and to the left
x -= 1;
y -= 1;
int firstX = x;
......@@ -177,13 +212,14 @@ void displayBoard::downleftCheck(int x,int y,char chip, bool change) {
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
if (board[firstX - i][firstY + i] == chip) {
if (board[firstX - i][firstY + i] == chip) { // loop that goes down from the origin and finds the first chip of the same character
secondX = firstX - i;
secondY = firstY + i;
if (0 <= secondX < 8 && 0 <= secondY < 8) {
if (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
......@@ -191,24 +227,31 @@ void displayBoard::downleftCheck(int x,int y,char chip, bool change) {
}
if (addChip == true) {
difference = (firstX - secondX) - 1;
this->difference = (firstX - secondX) - 1; // the difference will be the amount of chips that are being flipped
int temp = firstX - secondX;
for(int i = 1; i < temp; i++) { // loop that checks if there are any spaces between the two points that doesnt have a chip in it
if (board[firstX - i][firstY + i] == 'X' || board[firstX - i][firstY + i] == 'O') {
} else {
change = false;
}
}
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
for(int i = 1; i < temp; i++) { // loop that goes through the line, changing the chips
if (board[firstX - i][firstY + i] == 'X' || board[firstX - i][firstY + i] == 'O') {
board[firstX - i][firstY + i] = {chip};
}
temp --;
i++;
}
}
}
}
// Checks diagonal left up for pieces to switch
void displayBoard::upleftCheck(int x, int y, char chip, bool change) {
void displayBoard::upleftCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going up and to the left
x -= 1;
y -= 1;
int firstX = x;
......@@ -216,13 +259,14 @@ void displayBoard::upleftCheck(int x, int y, char chip, bool change) {
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
if (board[firstX - i][firstY - i] == chip) {
if (board[firstX - i][firstY - i] == chip) { // loop that goes down from the origin and finds the first chip of the same character
secondX = firstX - i;
secondY = firstY - i;
if (0 <= secondX < 8 && 0 <= secondY < 8) {
if (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
......@@ -230,24 +274,31 @@ void displayBoard::upleftCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (firstX - secondX) - 1;
this->difference = (firstX - secondX) - 1; // the difference will be the amount of chips that are being flipped
int temp = firstX - secondX;
for(int i = 1; i < temp; i++) { // loop that checks if there are any spaces between the two points that doesnt have a chip in it
if (board[firstX - i][firstY - i] == 'X' || board[firstX - i][firstY - i] == 'O') {
} else {
change = false;
}
}
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
for(int i = 1; i < temp; i++) { // loop that goes through the line, changing the chips
if (board[firstX - i][firstY - i] == 'X' || board[firstX - i][firstY - i] == 'O') {
board[firstX - i][firstY - i] = {chip};
}
temp --;
i++;
}
}
}
}
// Checks diagonal right down for pieces to switch
void displayBoard::downrightCheck(int x, int y, char chip, bool change) {
void displayBoard::downrightCheck(int x, int y, char chip, bool change) { // function for checking and changing chips in a line going down and to the right
x -= 1;
y -= 1;
int firstX = x;
......@@ -255,13 +306,14 @@ void displayBoard::downrightCheck(int x, int y, char chip, bool change) {
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
if (board[firstX + i][firstY + i] == chip) {
if (board[firstX + i][firstY + i] == chip) { // loop that goes down from the origin and finds the first chip of the same character
secondX = firstX + i;
secondY = firstY + i;
if (0 <= secondX < 8 && 0 <= secondY < 8) {
if (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
......@@ -269,24 +321,31 @@ void displayBoard::downrightCheck(int x, int y, char chip, bool change) {
}
if (addChip == true) {
difference = (secondX - firstX) - 1;
this->difference = (secondX - firstX) - 1; // the difference will be the amount of chips that are being flipped
int temp = secondX - firstX;
for(int i = 1; i < temp; i++) { // loop that checks if there are any spaces between the two points that doesnt have a chip in it
if (board[firstX + i][firstY + i] == 'X' || board[firstX + i][firstY + i] == 'O') {
} else {
change = false;
}
}
if (change == true){
int temp = secondX - firstX;
int i = 1;
while (temp > 0) {
for(int i = 1; i < temp; i++) { // loop that goes through the line, changing the chips
if (board[firstX + i][firstY + i] == 'X' || board[firstX + i][firstY + i] == 'O') {
board[firstX + i][firstY + i] = {chip};
}
temp --;
i++;
}
}
}
}
// Checks diagonal right up for pieces to switch
void displayBoard::uprightCheck(int x,int y,char chip, bool change) {
void displayBoard::uprightCheck(int x,int y,char chip, bool change) { // function for checking and changing chips in a line going up and to the right
x -= 1;
y -= 1;
int firstX = x;
......@@ -294,13 +353,14 @@ void displayBoard::uprightCheck(int x,int y,char chip, bool change) {
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
if (board[firstX + i][firstY - i] == chip) {
if (board[firstX + i][firstY - i] == chip) { // loop that goes down from the origin and finds the first chip of the same character
secondX = firstX + i;
secondY = firstY - i;
if (0 <= secondX < 8 && 0 <= secondY < 8) {
if (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
......@@ -308,22 +368,33 @@ void displayBoard::uprightCheck(int x,int y,char chip, bool change) {
}
if (addChip == true) {
difference = (firstX - secondX) - 1;
this->difference = (secondX - firstX) - 1; // the difference will be the amount of chips that are being flipped
int temp = secondX - firstX;
for(int i = 1; i < temp; i++) { // loop that checks if there are any spaces between the two points that doesnt have a chip in it
if (board[firstX + i][firstY - i] == 'X' || board[firstX + i][firstY - i] == 'O') {
} else {
change = false;
}
}
if (change == true){
int temp = firstX - secondX;
int i = 1;
while (temp > 0) {
int temp = secondX - firstX;
for(int i = 1; i < temp; i++) { // loop that goes through the line, changing the chips
if (board[firstX + i][firstY - i] == 'X' || board[firstX + i][firstY - i] == 'O') {
board[firstX + i][firstY - i] = {chip};
}
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) {
......
#include <iostream> // For cin and cout
#include <utility>
#include "Board.h"
#include "AIplayer.h"
using namespace std;
......@@ -10,6 +11,7 @@ int main() {
int difficulty = 0;
int x,y;
int aiX, aiY;
pair<int, int> AImove;
bool winner = false;
// Seeding random number genrator
......@@ -58,13 +60,73 @@ int main() {
//
switch (difficulty) {
// Easy difficulty
case 1: {
do {
// Player one's move
if (turn == 1) {
// Ask for players move
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')){
// Player moves
board.playerMove1(x,y);
// Check for flips in every direction
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++;
}else{
cout << "Invalid Move!"<<endl;
}
// Player two's Move
} else if (turn == 2) {
AImove = AIturn.move1();
cout << AImove.first << " " << AImove.second << endl;
// Check fo valid move
// if(board.isValidAdjacent(aiX, aiY, 'X') && board.isValidFlip(aiX, aiY, '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);
// 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;
// }
}
board.printBoard();
} while (winner == false);
}
break;