making an engine
This commit is contained in:
parent
b1f3496981
commit
f4d63382fe
6 changed files with 125 additions and 95 deletions
24
Board.cpp
24
Board.cpp
|
@ -522,27 +522,3 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
|
|||
|
||||
inputVec.push_back(inputBoard);
|
||||
}
|
||||
|
||||
//move this to its own file
|
||||
void Board::easyAI()
|
||||
{
|
||||
//1) see all possible movements
|
||||
|
||||
vector<moves> listOfMoves = viewPossibleMoves();
|
||||
|
||||
//obvious moves
|
||||
if (false){
|
||||
return;
|
||||
}
|
||||
|
||||
//random
|
||||
else {
|
||||
srand(time(NULL));
|
||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
|
||||
|
||||
//3) execute movement
|
||||
int temp = randomChoice;
|
||||
|
||||
move(listOfMoves[randomChoice]);
|
||||
}
|
||||
}
|
1
Board.h
1
Board.h
|
@ -40,5 +40,4 @@ public:
|
|||
void undo(Board& tablero);
|
||||
void interpret(string input, Board& tablero);
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||
void easyAI();
|
||||
};
|
109
Engine.cpp
Executable file
109
Engine.cpp
Executable file
|
@ -0,0 +1,109 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "Engine.h"
|
||||
|
||||
|
||||
Engine::Engine(){
|
||||
Board b = new Board();
|
||||
}
|
||||
|
||||
void Engine::startGame(){
|
||||
cout<<"WELCOME\n";
|
||||
|
||||
cout<<"1. Play against AI?\n";
|
||||
cout<<"2. Play against a human?\n";
|
||||
//cout<<"CHANGE THIS TO PARSE THINGS\n";
|
||||
cout<<"Enter choice: \n";
|
||||
|
||||
int choice;
|
||||
cin >> choice;
|
||||
cout << "OK" << endl;
|
||||
|
||||
string move;
|
||||
|
||||
bool gameOver = false;
|
||||
vector<Board> record;
|
||||
b.snapshot(record,b);
|
||||
|
||||
while (gameOver != true)
|
||||
{
|
||||
gameOver = b.isGameOver();
|
||||
|
||||
while(b.getTurn() == 'O' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter command: ";
|
||||
cin>>move;
|
||||
b.interpret(move,b);
|
||||
}
|
||||
|
||||
|
||||
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||
|
||||
if (choice == 1)
|
||||
{
|
||||
cout << "a";
|
||||
b.easyAI();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
while(b.getTurn() == 'X' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter command: ";
|
||||
cout<<"OK\n";
|
||||
cin>>move;
|
||||
b.interpret(move,b);
|
||||
}
|
||||
}
|
||||
|
||||
//b.snapshot();
|
||||
gameOver = b.isGameOver();
|
||||
|
||||
b.snapshot(record,b);
|
||||
}
|
||||
|
||||
//for debugging purposes
|
||||
cout<<"Record:"<<endl;
|
||||
|
||||
|
||||
cout<<record.size();
|
||||
for (int i = 0; i < record.size(); ++i)
|
||||
{
|
||||
record[i].displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
void Board::easyAI()
|
||||
{
|
||||
//1) see all possible movements
|
||||
|
||||
vector<moves> listOfMoves = viewPossibleMoves();
|
||||
|
||||
//obvious moves
|
||||
if (false){
|
||||
return;
|
||||
}
|
||||
|
||||
//random
|
||||
else {
|
||||
srand(time(NULL));
|
||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
|
||||
|
||||
//3) execute movement
|
||||
int temp = randomChoice;
|
||||
|
||||
move(listOfMoves[randomChoice]);
|
||||
}
|
||||
}
|
||||
|
||||
void Board::AI(){
|
||||
//do things here
|
||||
}
|
||||
|
||||
void Board::minMax(){
|
||||
//do more things here
|
||||
}
|
14
Engine.h
Executable file
14
Engine.h
Executable file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include "Board.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Engine {
|
||||
Board b;
|
||||
|
||||
public:
|
||||
Engine();
|
||||
void startGame();
|
||||
void easyAI();
|
||||
};
|
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
72
test.cpp
72
test.cpp
|
@ -1,76 +1,8 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "Board.h"
|
||||
#include "Engine.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<"WELCOME\n";
|
||||
|
||||
cout<<"1. Play against AI?\n";
|
||||
cout<<"2. Play against a human?\n";
|
||||
//cout<<"CHANGE THIS TO PARSE THINGS\n";
|
||||
cout<<"Enter choice: \n";
|
||||
|
||||
int choice;
|
||||
cin >> choice;
|
||||
cout << "OK" << endl;
|
||||
|
||||
Board b;
|
||||
string move;
|
||||
|
||||
bool gameOver = false;
|
||||
vector<Board> record;
|
||||
b.snapshot(record,b);
|
||||
|
||||
while (gameOver != true)
|
||||
{
|
||||
gameOver = b.isGameOver();
|
||||
|
||||
while(b.getTurn() == 'O' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter command: ";
|
||||
cin>>move;
|
||||
b.interpret(move,b);
|
||||
}
|
||||
|
||||
|
||||
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||
|
||||
if (choice == 1)
|
||||
{
|
||||
b.easyAI();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
while(b.getTurn() == 'X' )
|
||||
{
|
||||
b.displayBoard();
|
||||
cout<<"\nEnter command: ";
|
||||
cout<<"OK\n";
|
||||
cin>>move;
|
||||
b.interpret(move,b);
|
||||
}
|
||||
}
|
||||
|
||||
//b.snapshot();
|
||||
gameOver = b.isGameOver();
|
||||
|
||||
b.snapshot(record,b);
|
||||
}
|
||||
|
||||
//for debugging purposes
|
||||
cout<<"Record:"<<endl;
|
||||
|
||||
|
||||
cout<<record.size();
|
||||
for (int i = 0; i < record.size(); ++i)
|
||||
{
|
||||
record[i].displayBoard();
|
||||
}
|
||||
//
|
||||
}
|
||||
|
|
Reference in a new issue