Delete GameEngine.cpp
This commit is contained in:
parent
f326187388
commit
ed598d0c8b
1 changed files with 0 additions and 560 deletions
560
GameEngine.cpp
560
GameEngine.cpp
|
@ -1,560 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "GameEngine.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
string myToUpper(string input)
|
|
||||||
{
|
|
||||||
string output;
|
|
||||||
|
|
||||||
for (int i = 0 ; i < input.size(); ++i)
|
|
||||||
{
|
|
||||||
int numeric;
|
|
||||||
|
|
||||||
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
|
|
||||||
{
|
|
||||||
numeric = input[i] - 32;
|
|
||||||
output.push_back((char)numeric);// = 'Q';//(char) numeric;
|
|
||||||
}
|
|
||||||
else output.push_back(input[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < output.size(); ++i)
|
|
||||||
{
|
|
||||||
cout<<output[i]<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct moves
|
|
||||||
{
|
|
||||||
int row;
|
|
||||||
char column;
|
|
||||||
string moveType;
|
|
||||||
|
|
||||||
moves(int linea, int columna, string m)
|
|
||||||
{
|
|
||||||
row = linea;
|
|
||||||
column = columna;
|
|
||||||
moveType = m;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Board
|
|
||||||
{
|
|
||||||
char boardArray [8][8];
|
|
||||||
char turn = 'O';
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
Board()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < 2; ++i)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < 8; ++j)
|
|
||||||
{
|
|
||||||
boardArray[i][j] = 'X';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int i = 2; i < 6; ++i)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < 8; ++j)
|
|
||||||
{
|
|
||||||
boardArray[i][j] = '_';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int i = 6; i <= 7; ++i)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < 8; ++j)
|
|
||||||
{
|
|
||||||
boardArray[i][j] = 'O';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
moves parse(string input)
|
|
||||||
{
|
|
||||||
|
|
||||||
input = myToUpper(input);
|
|
||||||
|
|
||||||
cout<<input<<endl;
|
|
||||||
|
|
||||||
int temp1;
|
|
||||||
char temp2;
|
|
||||||
string temp3;
|
|
||||||
|
|
||||||
temp2 = input[0];
|
|
||||||
temp1 = input[1] - '0';
|
|
||||||
|
|
||||||
if (input[3] == 'L')
|
|
||||||
{
|
|
||||||
temp3 = "LEFT";
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (input[3] == 'R')
|
|
||||||
{
|
|
||||||
temp3 = "RIGHT";
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
temp3 = "FWD";
|
|
||||||
}
|
|
||||||
|
|
||||||
moves output(temp1,temp2,temp3);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
char getTurn()
|
|
||||||
{
|
|
||||||
return turn;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isGameOver()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (boardArray[0][i] == 'O')
|
|
||||||
{
|
|
||||||
cout<<"\n\n\nplayer O wins!\n\n\n"<<endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (boardArray[7][i] == 'X')
|
|
||||||
{
|
|
||||||
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// else return false; this line was making this function return false no matter what, apparently...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void changeTurns()
|
|
||||||
{
|
|
||||||
if (turn == 'O') turn = 'X';
|
|
||||||
else turn = 'O';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void displayBoard()
|
|
||||||
{
|
|
||||||
cout<<"\n\n";
|
|
||||||
cout<<" A B C D E F G H"<<endl;
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
{
|
|
||||||
int mango = 8 - i;
|
|
||||||
cout<<mango<<" ";
|
|
||||||
for (int j = 0; j < 8; ++j)
|
|
||||||
{
|
|
||||||
cout<<boardArray[i][j]<<" ";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
cout<<'\n'<<endl;
|
|
||||||
cout<<"turn : "<<turn;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int charToIntColumn(char input) //converts column number to int
|
|
||||||
{
|
|
||||||
int kolumn;
|
|
||||||
|
|
||||||
switch (input)
|
|
||||||
{
|
|
||||||
case 'A': kolumn = 0; break;
|
|
||||||
case 'B': kolumn = 1; break;
|
|
||||||
case 'C': kolumn = 2; break;
|
|
||||||
case 'D': kolumn = 3; break;
|
|
||||||
case 'E': kolumn = 4; break;
|
|
||||||
case 'F': kolumn = 5; break;
|
|
||||||
case 'G': kolumn = 6; break;
|
|
||||||
case 'H': kolumn = 7; break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return kolumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
char intToCharColumn(int input) //converts column number to int
|
|
||||||
{
|
|
||||||
char kolumn;
|
|
||||||
|
|
||||||
switch (input)
|
|
||||||
{
|
|
||||||
case 1: kolumn = 'A'; break;
|
|
||||||
case 2: kolumn = 'B'; break;
|
|
||||||
case 3: kolumn = 'C'; break;
|
|
||||||
case 4: kolumn = 'D'; break;
|
|
||||||
case 5: kolumn = 'E'; break;
|
|
||||||
case 6: kolumn = 'F'; break;
|
|
||||||
case 7: kolumn = 'G'; break;
|
|
||||||
case 8: kolumn = 'H'; break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return kolumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void move(string inputMove)
|
|
||||||
{
|
|
||||||
|
|
||||||
moves jugada = parse(inputMove);
|
|
||||||
|
|
||||||
int row = 8 - (jugada.row);
|
|
||||||
int kolumn = charToIntColumn(jugada.column);
|
|
||||||
int temp = boardArray[row][kolumn];
|
|
||||||
int reflector = 1;
|
|
||||||
|
|
||||||
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
|
||||||
{
|
|
||||||
cout<<"ERROR: index out of bound!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (temp != turn)
|
|
||||||
{
|
|
||||||
cout<<"you can't move that piece at this turn!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (temp == '_')
|
|
||||||
{
|
|
||||||
cout<<"there's no piece in that spot!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (temp == 'X' || temp == 'O')
|
|
||||||
{
|
|
||||||
|
|
||||||
if (temp == 'O')
|
|
||||||
{
|
|
||||||
reflector *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (jugada.moveType == "FWD")
|
|
||||||
{
|
|
||||||
|
|
||||||
if(boardArray[row+reflector][kolumn] != '_')
|
|
||||||
{
|
|
||||||
cout<<"you can't move that piece forward"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (jugada.moveType == "LEFT")
|
|
||||||
{
|
|
||||||
if (kolumn == 0)
|
|
||||||
{
|
|
||||||
cout<<"Destination Spot out of range!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (boardArray[row+reflector][kolumn-1] == temp)
|
|
||||||
{
|
|
||||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn-1] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (jugada.moveType == "RIGHT")
|
|
||||||
{
|
|
||||||
if (kolumn == 7)
|
|
||||||
{
|
|
||||||
cout<<"Destination Spot out of range!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (boardArray[row+reflector][kolumn+1] == temp)
|
|
||||||
{
|
|
||||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn+1] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout<<"Unrecognized movetype!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout<<"Invalid piece!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void move(moves jugada)
|
|
||||||
{
|
|
||||||
|
|
||||||
int row = 8 - (jugada.row);
|
|
||||||
int kolumn = charToIntColumn(jugada.column);
|
|
||||||
|
|
||||||
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
|
||||||
{
|
|
||||||
cout<<"ERROR: index out of bound!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int temp = boardArray[row][kolumn];
|
|
||||||
if (temp != turn)
|
|
||||||
{
|
|
||||||
cout<<"you can't move that piece at this turn!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (temp == '_')
|
|
||||||
{
|
|
||||||
cout<<"there's no piece in that spot!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int reflector = 1;
|
|
||||||
|
|
||||||
if (temp == 'X' || temp == 'O')
|
|
||||||
{
|
|
||||||
|
|
||||||
if (temp == 'O')
|
|
||||||
{
|
|
||||||
reflector *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (jugada.moveType == "FWD")
|
|
||||||
{
|
|
||||||
|
|
||||||
if(boardArray[row+reflector][kolumn] != '_')
|
|
||||||
{
|
|
||||||
cout<<"you can't move that piece forward"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (jugada.moveType == "LEFT")
|
|
||||||
{
|
|
||||||
if (kolumn == 0)
|
|
||||||
{
|
|
||||||
cout<<"Destination Spot out of range!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (boardArray[row+reflector][kolumn-1] == temp)
|
|
||||||
{
|
|
||||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn-1] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (jugada.moveType == "RIGHT")
|
|
||||||
{
|
|
||||||
if (kolumn == 7)
|
|
||||||
{
|
|
||||||
cout<<"Destination Spot out of range!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (boardArray[row+reflector][kolumn+1] == temp)
|
|
||||||
{
|
|
||||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
boardArray[row][kolumn] = '_';
|
|
||||||
boardArray[row+reflector][kolumn+1] = temp;
|
|
||||||
|
|
||||||
changeTurns();
|
|
||||||
displayBoard();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout<<"Unrecognized movetype!"<<endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout<<"Invalid piece!"<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool isThisMovePossible(int r, int c, string moveType)
|
|
||||||
{
|
|
||||||
char pieceToMove = boardArray[r][c];
|
|
||||||
|
|
||||||
if (pieceToMove != turn) //trying to move invalid piece
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int reflector = 1;
|
|
||||||
|
|
||||||
if (pieceToMove == 'O')
|
|
||||||
{
|
|
||||||
reflector *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (moveType == "FWD")
|
|
||||||
{
|
|
||||||
|
|
||||||
if (boardArray[r+reflector][c] == '_') return true;
|
|
||||||
else return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (moveType == "RIGHT")
|
|
||||||
{
|
|
||||||
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true;
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (moveType == "LEFT")
|
|
||||||
{
|
|
||||||
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true;
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
vector<moves> viewPossibleMoves()
|
|
||||||
{
|
|
||||||
vector<moves> output;
|
|
||||||
|
|
||||||
for (int r = 0; r < 8; ++r)
|
|
||||||
{
|
|
||||||
for (int c = 0; c < 8; ++c)
|
|
||||||
{
|
|
||||||
if (boardArray[r][c] == turn)
|
|
||||||
{
|
|
||||||
if (isThisMovePossible(r,c,"FWD"))
|
|
||||||
{
|
|
||||||
moves temp(8-r,intToCharColumn(c+1),"FWD");
|
|
||||||
output.push_back(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isThisMovePossible(r,c,"LEFT"))
|
|
||||||
{
|
|
||||||
moves temp(8-r,intToCharColumn(c+1),"LEFT");
|
|
||||||
output.push_back(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isThisMovePossible(r,c,"RIGHT"))
|
|
||||||
{
|
|
||||||
moves temp(8-r,intToCharColumn(c+1),"RIGHT");
|
|
||||||
output.push_back(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void easyAI()
|
|
||||||
{
|
|
||||||
|
|
||||||
//1) see all possible movements
|
|
||||||
|
|
||||||
vector<moves> listOfMoves = viewPossibleMoves();
|
|
||||||
|
|
||||||
//2) pick a movement
|
|
||||||
|
|
||||||
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]);
|
|
||||||
|
|
||||||
//cout<<"\n\nMove executed by AI: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void displayPossibleMoves(vector<moves> input)
|
|
||||||
{
|
|
||||||
cout<<"\n\nList of possible Moves:"<<endl;
|
|
||||||
for (int i = 0; i < input.size(); ++i)
|
|
||||||
{
|
|
||||||
cout<<"possible move: "<<input[i].row<<" "<<input[i].column<<" "<<input[i].moveType<<endl;
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue