Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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<> 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions