Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Checkers Game I already wrote the program but I feel that the AI code is wea

ID: 3833896 • Letter: C

Question

C++ Checkers Game

I already wrote the program but I feel that the AI code is weak. I want AI to avoid it's peices from being eaten and try it's best to win the Game instead of going foward with one piece all accross the board. Feel free to modify the code and Any help would be much appreciated.

This is part of my code:

#include "NelderAiClass.h"

bool NelderAiClass::areThereJumps()

{
   bool areThere = 0;
   checkerPiece testOppiece, testOponent, testpiece;
   numberOfMoves = 0;

   for (int j = 0; j < 12; j++)//for every oponent that might be jumped
   {
       if (!teamNotTurn->ispieceAlive(j)) continue;
       testOppiece = teamNotTurn->getpiece(j);

       for (int i = 0; i < 12; i++)//for every peice that might have a jump
       {
           if (!teamWhosTurn->ispieceAlive(i)) continue;


           for (int move = 1; move <= 4; move++)//for every way a peice might jump
           {
               testOponent = teamWhosTurn->getpiece(i);
               testpiece = teamWhosTurn->getpiece(i);

               if (move == 4)
               {
                   testOponent.downLeft();
                   if (testpiece.jumpdownLeft() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
                   {
                       areThere = 1;
                       availableMoves[numberOfMoves].setpiece(i);
                       availableMoves[numberOfMoves].setDirection(move);
                       numberOfMoves++;
                   }
               }
               else if (move == 2)
               {
                   testOponent.upLeft();
                   if (testpiece.jumpUpLeft() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
                   {
                       areThere = 1;
                       availableMoves[numberOfMoves].setpiece(i);
                       availableMoves[numberOfMoves].setDirection(move);
                       numberOfMoves++;
                   }
               }
               else if (move == 3)
               {
                   testOponent.downRight();
                   if (testpiece.jumpdownRight() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
                   {
                       areThere = 1;
                       availableMoves[numberOfMoves].setpiece(i);
                       availableMoves[numberOfMoves].setDirection(move);
                       numberOfMoves++;
                   }
               }
               else if (move == 1)
               {
                   testOponent.upRight();
                   if (testpiece.jumpUpRight() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
                   {
                       areThere = 1;
                       availableMoves[numberOfMoves].setpiece(i);
                       availableMoves[numberOfMoves].setDirection(move);
                       numberOfMoves++;
                   }
               }
           }
       }
   }


   return areThere;
  
}

void NelderAiClass::findMoves()
{
  
   checkerPiece testpiece;
   numberOfMoves = 0;

   for (int i = 0; i < 12; i++)//for every peice that might have a move
   {


       for (int move = 1; move <= 4; move++)//for every way a peice might move
       {
           if (!teamWhosTurn->ispieceAlive(i)) continue;//dead things should stay dead
           testpiece = teamWhosTurn->getpiece(i);

           if (move == 4)
           {

               if (testpiece.downLeft() && (!anyOneThere(testpiece)))
               {
                  
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 2)
           {

               if (testpiece.upLeft() && (!anyOneThere(testpiece)))
               {
                  
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 3)
           {

               if (testpiece.downRight() && (!anyOneThere(testpiece)))
               {
                  
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 1)
           {

               if (testpiece.upRight() && (!anyOneThere(testpiece)))
               {
                  
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
       }
   }


   return;
}

bool NelderAiClass::anyOneThere(checkerPiece test)
{

   for (int i = 0; i < 12; i++)
   {
       if ((teamWhosTurn->getpiece(i) == test && teamWhosTurn->ispieceAlive(i))
           || (teamNotTurn->getpiece(i) == test&&teamNotTurn->ispieceAlive(i)))
           return true;
   }

   return false;
}

bool NelderAiClass::findAditionalMove(int i)
{
   bool areThere = 0;
   checkerPiece testOppiece, testOponent, testpiece;
   numberOfMoves = 0;

   for (int j = 0; j < 12; j++)//for every oponent that might be jumped
   {
       if (!teamNotTurn->ispieceAlive(j)) continue;
       testOppiece = teamNotTurn->getpiece(j);

       for (int move = 1; move <= 4; move++)//for every way a peice might jump
       {
           testOponent = teamWhosTurn->getpiece(i);
           testpiece = teamWhosTurn->getpiece(i);

           if (move == 4)
           {
               testOponent.downLeft();
               if (testpiece.jumpdownLeft() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
               {
                   areThere = 1;
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 2)
           {
               testOponent.upLeft();
               if (testpiece.jumpUpLeft() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
               {
                   areThere = 1;
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 3)
           {
               testOponent.downRight();
               if (testpiece.jumpdownRight() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
               {
                   areThere = 1;
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
           else if (move == 1)
           {
               testOponent.upRight();
               if (testpiece.jumpUpRight() && (testOponent == testOppiece) && (!anyOneThere(testpiece)))
               {
                   areThere = 1;
                   availableMoves[numberOfMoves].setpiece(i);
                   availableMoves[numberOfMoves].setDirection(move);
                   numberOfMoves++;
               }
           }
       }
   }
   return areThere;
  
}

NelderAiClass::NelderAiClass()
{
}

checkermoves NelderAiClass::getMove(checkerTeam myTeam, checkerTeam otherTeam)
{
   teamWhosTurn = &myTeam;
   teamNotTurn = &otherTeam;
   bool checkForAditionalMove=0;

   if (aditionalMoveFlag)//if our last move gave us an aditional jump
   {
       aditionalMoveFlag = false;
       checkForAditionalMove = true;
       findAditionalMove(lastMove.getpiece());
       //find jumps for that last peice
   }  
   else if (areThereJumps())//if there are jumps
   {
       checkForAditionalMove = true;
       //check for aditional jumps
   }
   else
   {
       findMoves();
   }

   for (int i = 0; i < numberOfMoves; i++)
   {
       cout << availableMoves[i].getpiece() << endl;
       cout << availableMoves[i].getDirection() << endl<<endl;
   }
  
   checkermoves temp=availableMoves[0];
   //int playerPiece;
   //int playerDirection;
   //cin >> playerPiece >> playerDirection;
   //temp.setpiece(playerPiece);
   //temp.setDirection(playerDirection);


   lastMove = temp;
   if (checkForAditionalMove)
   {
       //find if our move causes an aditional jump
       if (findAditionalMove(lastMove.getpiece()))//there are aditional jumps
       {
           aditionalMoveFlag = true;
       }
   }
   return temp;
}

Explanation / Answer

#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>

//REGULAR PROTOTYPES

int getxcoord(void);
int getycoord(void);
void showboard(int matrix[8][8]);
char convert(int number);

//Checker Piece Class for Inheritance

class checkerpiece
{
protected:
int x;
int y;
bool king;
bool selected;
public:
int givexposition(void);
int giveyposition(void);
void select(void);
void unselect(void);
bool whetherselected(void);
//bool alljump(void); <- Implement This!
checkerpiece(int startx, int starty, bool kingship = 0);
};

int checkerpiece::givexposition(void)
{
return(x);
}

int checkerpiece::giveyposition(void)
{
return(y);
}

void checkerpiece::select(void)
{
selected = 1;
}

void checkerpiece::unselect(void)
{
selected = 0;
}

bool checkerpiece::whetherselected(void)
{
return(selected);
}

checkerpiece::checkerpiece(int startx, int starty, bool kingship)
{
x = startx;
y = starty;
king = kingship;
}

//White Piece

class wpiece : public checkerpiece
{
public:
bool moveup(int nextx, int nexty, int matrix[8][8]);
bool jumpup(int nextx, int nexty, int matrix[8][8]);
wpiece(int startx = -1, int starty = -1, bool kingship = 0);
};

bool wpiece::jumpup(int nextx, int nexty, int matrix[8][8])
{
if(nextx == x + 2 && nexty == y + 2 && matrix[x+1][y+1] == 2)
{
matrix[x][y] = 0;
matrix[x+1][y+1] = 0;
matrix[x+2][y+2] = 1;

x = x+2;
y = y+2;
cout << "jumped up right" << endl;
return(1);
}
if(nextx == x - 2 && nexty == y + 2 && matrix[x-1][y+1] == 2)
{
matrix[x][y] = 0;
matrix[x-1][y+1] = 0;
matrix[x-2][y+2] = 1;

x = x-2;
y = y+2;
cout << "jumped up left" << endl;
return(1);
}

cout << "No Jump - sorry" << endl;

return(0);
}

bool wpiece::moveup(int nextx, int nexty, int matrix[8][8])
{
if(nextx == x + 1 && nexty == y + 1 && matrix[x][y] == 0)
{
cout << matrix[x-1][y-1] << endl;
matrix[x-1][y-1] = 0;
matrix[x][y] = 1;

cout << matrix[x][y] << endl;

x++;
y++;

cout << matrix[x-1][y-1] << endl;
cout << "moved up right" << endl;

return(1);
}
if(nextx == x - 1 && nexty == y + 1 && matrix[x-2][y] == 0)
{
matrix[x-1][y-1] = 0;
matrix[x-2][y] = 1;

x--;
y++;
cout << "moved up left" << endl;
return(1);
}

cout << "no move - sorry" << endl;

return(0);
}

wpiece::wpiece(int startx, int starty, bool kingship):checkerpiece(startx, starty, king)
{
x = startx;
y = starty;
king = kingship;
}

//CLASS-RELATED PROTOTYPES

void printpieces(wpiece whitepiece[12]);
int findpiece(wpiece whitepiece[12], int x, int y);

////////
//MAIN//
////////

main()
{
int matrix[8][8] = { 1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,2,0,2,0,2,0,2,
2,0,2,0,2,0,2,0,
0,2,0,2,0,2,0,2};
int xposition;
int yposition;
int xposition2;
int yposition2;
int index;

wpiece whitepiece[12] = {wpiece(1,1,0),wpiece(3,1,0),wpiece(5,1,0),wpiece(7,1,0),wpiece(2,2,0),wpiece(4,2,0),wpiece(6,2,0),wpiece(8,2,0),wpiece(1,3,0),wpiece(3,3,0),wpiece(5,3,0),wpiece(7,3,0)};

printpieces(whitepiece);

bool turn = 0;

showboard(matrix);

while(turn == 0)
{
cout << "=-WHITE'S TURN-=" << endl;
cout << "Enter coordinates of piece to move" << endl;
xposition = getxcoord();
yposition = getycoord();


while(findpiece(whitepiece, xposition, yposition) == 12)
{
cout << "No White Piece found - Enter coordinates of piece to move" << endl;
xposition = getxcoord();
yposition = getycoord();
}

index = findpiece(whitepiece, xposition, yposition);

cout << "Where do you want to move it?" << endl;
xposition2 = getxcoord();
yposition2 = getycoord();

cout << xposition2 << ", " << yposition2 << endl;

while(findpiece(whitepiece, xposition2, yposition2) != 12)
{
cout << findpiece(whitepiece, xposition2, yposition2) << endl;
cout << "Already a Piece there - Enter different place to move" << endl;
xposition2 = getxcoord();
yposition2 = getycoord();
}

if(whitepiece[index].jumpup(xposition2, yposition2, matrix) == 0 && whitepiece[index].moveup(xposition2, yposition2, matrix) == 0)
{
cout<< "Not a valid move - Enter different place to move" << endl;
}

showboard(matrix);
}


return (0);
}


//FUNCTIONS

int getxcoord(void)
{
char letter;
int x = 9;

cout << "A - H: ";

while(x == 9)
{
cin >> letter;
letter = toupper(letter);
switch (letter)
{
case 'A': x = 1;
break;
case 'B': x = 2;
break;
case 'C': x = 3;
break;
case 'D': x = 4;
break;
case 'E': x = 5;
break;
case 'F': x = 6;
break;
case 'G': x = 7;
break;
case 'H': x = 8;
break;
default : cout << "Please give a letter from A to H: ";
}
}

return(x);
}

int getycoord(void)
{
int y = 9;

cout << "1 - 8: ";
cin >> y;
while(y < 1 || y > 8)
{
cout << "Please give a number from 1 to 8: ";
cin >> y;
}

return(y);
}

int findpiece(wpiece whitepiece[12], int x, int y)
{
int finder;

for (finder = 0; finder < 12; finder++)
{
if (whitepiece[finder].givexposition() == x && whitepiece[finder].giveyposition() == y)
{
whitepiece[finder].select();
return(finder);
}
}

return(finder);
}

void showboard(int matrix[8][8])
{
int bigcount;
int counter;

cout << "|----|----|----|----|----|----|----|----|" << endl;

for(bigcount = 7; bigcount >= 0; bigcount--)
{
for(counter = 0; counter < 8; counter++)
{
cout << "| " << convert(matrix[bigcount][counter]) << " ";
}

cout << "|" << endl << "|----|----|----|----|----|----|----|----|" << endl;
}
}

char convert(int number)
{
switch(number)
{
case 0 : return(' ');
break;
case 1 : return('w');
break;
case 2 : return('b');
break;
case 3 : return('W');
break;
case 4 : return('B');
break;
default : return('!');
}
}


void printpieces(wpiece whitepiece[12])
{
int count;

for(count = 0; count < 12; count++)
{
cout << "White Piece Index Number " << count << " has coordinates: " << whitepiece[count].givexposition() << ", " << whitepiece[count].giveyposition() << endl;
}
}