Commit fd7a23cb authored by Jason Riggs's avatar Jason Riggs

added AI

parent 21425bc4
#include <iostream>
#include <cstdlib>
#include "AIplayer.h"
using namespace std;
\ No newline at end of file
#include "Board.h"
using namespace std;
// Creates a random x and y for the easy computers move
void AIplayer::move1(int &x, int &y) {
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;
}
}
}
// Picks the best possible move for the hard computer
void AIplayer::move2(int &x, int &y) {
// declare class variable for board
displayBoard AImove;
// 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);
temp += AImove.getDifference();
AImove.leftCheck(x, y, 'O', false);
temp += AImove.getDifference();
AImove.rightCheck(x, y, 'O', false);
temp += AImove.getDifference();
AImove.uprightCheck(x,y,'O', false);
temp += AImove.getDifference();
AImove.upleftCheck(x,y,'O', false);
temp += AImove.getDifference();
AImove.downrightCheck(x,y,'O', false);
temp += AImove.getDifference();
AImove.downleftCheck(x,y,'O', false);
temp += AImove.getDifference();
if (temp > chips) {
y = row;
x = col;
}
}
}
}
}
#ifndef AIPLAYER_H
#define AIPLAYER_H
#include <vector>
#include <string>
using namespace std;
#include <cstdlib>
class Board;
class AIplayer {
public:
AIplayer();
AIplayer(int difficulty) {this->difficulty = difficulty; }
void move();
private:
int difficulty;
// 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);
};
#endif
\ No newline at end of file
#include <iostream> // For cin and cout
#include "Board.h"
#include "AIplayer.h"
using namespace std;
......@@ -8,6 +9,7 @@ int main() {
int turn = 1;
int difficulty = 0;
int x,y;
int aiX, aiY;
bool winner = false;
// Seeding random number genrator
......@@ -16,6 +18,9 @@ int main() {
srand(seed);
cout << endl;
// Creates AI
AIplayer AIturn(seed);
// Header
cout << "Welcome to Othello!" << endl;
cout << "the game of Reversi" << endl;
......@@ -24,9 +29,8 @@ int main() {
// ask for difficulty level
cout << "What difficulty would you like to play against:" << endl;
cout << "1) Easy" << endl;
cout << "2) Medium" << endl;
cout << "3) Hard" << endl;
cout << "4) 2 Player" << endl;
cout << "2) Hard" << endl;
cout << "3) 2 Player" << endl;
cout << "Enter your choice: ";
cin >> difficulty;
......@@ -38,79 +42,160 @@ int main() {
cout << "Invalid selection."<<endl;
cout << "What difficulty would you like to play against:" << endl;
cout << "1) Easy" << endl;
cout << "2) Medium" << endl;
cout << "3) Hard" << endl;
cout << "4) 2 Player" << endl;
cout << "2) Hard" << endl;
cout << "3) 2 Player" << endl;
cout << "Enter your choice: ";
cin >> difficulty;
}
// start the game
displayBoard board;
board.start();
board.printBoard();
cout << "X goes first."<<endl;
do {
// Ask for players move
cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
cin >> x >> y;
//
switch (difficulty) {
// 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')){
// 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);
case 1: {
// Change to player 2
turn++;
}else{
cout << "Invalid Move!"<<endl;
}
// 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')){
// Player moves
board.playerMove2(x,y);
// Check for flips in every direction
board.upCheck(x, y, 'O', true);
board.downCheck(x, y, 'O', true);
board.leftCheck(x, y, 'O', true);
board.rightCheck(x, y, 'O', true);
}
break;
case 2: {
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) {
aiX = 0;
aiY = 0;
AIturn.move2(aiX, aiY);
// 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();
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--;
} while (winner == false);
}
break;
case 3: {
do {
// Ask for players move
cout << "Enter the (X,Y) coordinates of where you would like to place a chip: " ;
cin >> x >> y;
}else{
// Prints invalid and then asks for another move
cout << "Invalid Move!"<<endl;
// 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')){
// 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) {
// Check fo valid move
if(board.isValidAdjacent(x-1, y-1, 'X') && board.isValidFlip(x-1, y-1, 'O')){
// Player moves
board.playerMove2(x,y);
// Check for flips in every direction
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--;
}else{
// Prints invalid and then asks for another move
cout << "Invalid Move!"<<endl;
}
}
board.printBoard();
}
} while (winner == false);
}
board.printBoard();
break;
} while (winner == false);
}
......
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