//Wayne You
//Bob Jones HS
//Intermediate 3
//Contest #3
#include <iostream>
class board{
public:
board();
char
checkLocation(int location);
bool
placePiece(int location, char color);
int
movePiece(int start, int end);
private:
char
grid[30];//B-Black piece W-White piece E-Empty O-Out of bounds
};
using namespace std;
board::board()
{
for(int
n=0;n<30;n++)
{
grid[n]='E';
}
}
char board::checkLocation(int location)
{
if(location<30&&location>=0)
{
return
grid[location];
}
else
{
return 'O';
}
}
bool board::placePiece(int location, char color)
{
if(checkLocation(location)=='E'&&(color=='B'||color=='W'))
{
grid[location]=color;
return true;
}
else
{
return false;
}
}
int board::movePiece(int start, int end)
{
char piece1,
piece2;
if(start<25)
{
piece1=checkLocation(start);
if(piece1=='O'||piece1=='E')
{
return 0;
}
if(end>25)
{
piece2=checkLocation(25);
if(piece2=='E')
{
grid[25]=piece1;
grid[start]='E';
return 1;
}
else if(piece1==piece2)
{
return 0;
}
else
{
grid[start]=piece2;
grid[25]=piece1;
return 1;
}
}
piece2=checkLocation(end);
if(piece2=='O')
{
return 0;
}
if(piece2=='E')
{
grid[end]=piece1;
grid[start]='E';
return 1;
}
else
if(piece1==piece2)
{
return 0;
}
else
{
grid[end]=piece1;
grid[start]=piece2;
return 1;
}
}
else
{
if(end==30)
{
grid[start]='E';
return 2;
}
else
if(start==25)
{
piece1=checkLocation(start);
piece2=checkLocation(end);
if(piece2=='O')
{
return 0;
}
if(end==26)
{
if(piece2!=piece1)
{
char piece3=checkLocation(14);
if(piece3!=piece1)
{
if(piece2=='E')
{
if(piece3=='E')
{
grid[14]=piece1;
grid[25]='E';
return 1;
}
else
{
grid[14]=piece1;
grid[26]=piece3;
grid[25]='E';
return 1;
}
}
else
{
if(piece3=='E')
{
grid[14]=piece1;
grid[25]=piece2;
grid[26]='E';
return 1;
}
else
{
grid[14]=piece1;
grid[25]=piece2;
grid[26]=piece3;
return 1;
}
}
}
else
{
grid[25]=piece2;
grid[26]=piece1;
return 1;
}
}
else
{
return 0;
}
}
if(piece2==piece1)
{
return 0;
}
grid[25]=piece2;
grid[end]=piece1;
return 1;
}
return 0;
}
}
int main(void)
{
board theBoard;
int temp;
int rodcounts[10];
cout<<"Enter the number of black pawns: ";
cin>>temp;
cout<<"Enter their locations:\n";
for(int
n=temp;n>0;n--)
{
cin>>temp;
while(!theBoard.placePiece(temp-1,'B'))
{
cout<<"Invalid input, try again.\n";
cin>>temp;
}
}
cout<<"\nEnter the number of white pawns: ";
cin>>temp;
cout<<"Enter their locations:\n";
for(int
n=temp;n>0;n--)
{
cin>>temp;
while(!theBoard.placePiece(temp-1,'W'))
{
cout<<"Invalid input, try again.\n";
cin>>temp;
}
}
cout<<"Enter the ten rod counts:\n";
for(int
n=0;n<10;n++)
{
cin>>temp;
while(temp<1&&temp>5)
{
cout<<"Invalid input, try again.\n";
cin>>temp;
}
rodcounts[n]=temp;
}
for(int
n=0;n<10;n++)
{
int
blackpawn;
int
whitepawn;
if(!(n%2))
{
bool
moved=false;
for(int
m=29;m>=0;m--)
{
if(theBoard.checkLocation(m)=='B')
{
int check=theBoard.movePiece(m,m+rodcounts[n]);
if(check==1)
{
moved=true;
if(m<25&&m+rodcounts[n]>25)
{
blackpawn=26;
}
else
{
blackpawn=m+rodcounts[n]+1;
}
break;
}
else if(check==2)
{
moved=true;
blackpawn=-1;
break;
}
}
}
if(!moved)
{
blackpawn=-2;
}
}
else
{
bool
moved=false;
for(int
m=29;m>=0;m--)
{
if(theBoard.checkLocation(m)=='W')
{
int check=theBoard.movePiece(m,m+rodcounts[n]);
if(check==1)
{
moved=true;
if(m<25&&m+rodcounts[n]>25)
{
whitepawn=26;
}
else
{
whitepawn=m+rodcounts[n]+1;
}
if(blackpawn==whitepawn)
{
blackpawn=m+1;
}
break;
}
else if(check==2)
{
moved=true;
whitepawn=-1;
break;
}
}
}
if(!moved)
{
whitepawn=-2;
}
if(blackpawn==-1)
{
cout<<"DONE, ";
}
else
if(blackpawn==-2)
{
cout<<"NO VALID MOVES, ";
}
else
{
cout<<blackpawn<<", ";
}
if(whitepawn==-1)
{
cout<<"DONE\n";
}
else
if(whitepawn==-2)
{
cout<<"NO VALID MOVES\n";
}
else
{
cout<<whitepawn<<"\n";
}
}
}
system("pause");
return 1;
}