Commit a6949de3 authored by Christian Rippe's avatar Christian Rippe

Merge branch 'jason' into 'master'

Jason

See merge request !1
parents 33e086af 3c6591d4
#include <iostream>
#include <cstdlib>
#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;
// loop until valid move is found
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (check.isLegalPlacementfor(row, col, AImove.board, 'O', 'X')) {
x = row;
y = col;
}
}
}
AIpair.first = x;
AIpair.second = y;
return AIpair;
}
pair<int, int> AIplayer::move2(displayBoard AImove) {
// declare variables
Cell check;
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 < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (check.isLegalPlacementfor(row, col, AImove.board, 'O', 'X')) {
AImove.upCheck(row, col, 'O', false);
temp = AImove.getDifference();
AImove.downCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.leftCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.rightCheck(row, col, 'O', false);
temp += AImove.getDifference();
AImove.uprightCheck(row, col,'O', false);
temp += AImove.getDifference();
AImove.upleftCheck(row, col,'O', false);
temp += AImove.getDifference();
AImove.downrightCheck(row, col,'O', false);
temp += AImove.getDifference();
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>
#include "Board.h"
#include "Cell.h"
using namespace std;
class displayBoard;
class Cell;
class AIplayer {
public:
pair<int, int> move1(displayBoard AImove); // easy move
pair<int, int> move2(displayBoard AImove); // hard move mutator
private: // not used
};
#include <iostream>
#include <vector>
#include "Board.h"
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'};
board[5-1][5-1] = {'X'};
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;
for (int j=0; j<8; j++) {
cout << j+1 << " |";
for (int i=0; i<8; i++) {
if (isalpha(board[i][j])) {
cout << " " << board[i][j] << " |";
} else {
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;
int firstY = y;
int secondY;
bool addChip = false;
this->difference = 0;
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) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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++) { // loop that goes through the line, changing the chips
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
}
}
}
}
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--) { // 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) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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++) { // loop that goes through the line, changing the chips
if (board[x][i] == 'X' || board[x][i] == 'O') {
board[x][i] = {chip};
}
}
}
}
}
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--) { // 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) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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++) { // loop that goes through the line, changing the chips
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
}
}
}
}
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++) { // 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) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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++) { // loop that goes through the line, changing the chips
if (board[i][y] == 'X' || board[i][y] == 'O') {
board[i][y] = {chip};
}
}
}
}
}
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;
int firstY = y;
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
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 (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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;
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};
}
}
}
}
}
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;
int firstY = y;
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
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 (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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;
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};
}
}
}
}
}
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;
int firstY = y;
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
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 (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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;
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};
}
}
}
}
}
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;
int firstY = y;
int secondX;
int secondY;
bool addChip = false;
this->difference = 0;
for (int i = 1; i < 8; i++) {
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 (secondX >= 0 && secondX < 8 && secondY >= 0 && secondY < 8) {
addChip = true;
}
break;
}
}
if (addChip == true) {
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;
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};
}
}
}
}
}
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
#include <vector>
using namespace std;
class displayBoard {
public:
void start();
void playerMove1(int x, int y);
void playerMove2(int x, int y);
void printBoard();
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);
char board[8][8] = {};
private:
int difference = 0; // count for the total chips being turned in a move
int x;
int y;
};
#endif
\ No newline at end of file
#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) {
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);
}
Cell northEast(x, y - 2);
if (northEast.isOccupied(board, oppDisc)) {
directions.push_back(northEast.NORTHEAST);
}
Cell east(x, y - 1);
if (east.isOccupied(board, oppDisc)) {
directions.push_back(EAST);
}
Cell southEast(x, y);
if (southEast.isOccupied(board, oppDisc)) {
directions.push_back(SOUTHEAST);
}
Cell south(x - 1, y);
if (south.isOccupied(board, oppDisc)) {
directions.push_back(SOUTH);
}
Cell southWest(x - 2, y);
if (southWest.isOccupied(board, oppDisc)) {
directions.push_back(SOUTHWEST);
}
Cell west(x - 2, y - 1);
if (west.isOccupied(board, oppDisc)) {
directions.push_back(WEST);
}
Cell northWest(x - 2, y - 2);
if (northWest.isOccupied(board, oppDisc)) {
directions.push_back(NORTHWEST);
}
// 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, "");
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;
switch (direction) {
case NORTH:
x1 = x;
y1 = y - 1;
break;
case NORTHEAST:
x1 = x + 1;
y1 = y - 1;
break;
case EAST:
x1 = x