#include <stdio.h>
#include <stdlib.h>
void showBoard(int pLife[],int pType[]) {// WORKS NO-TOUCHY!
int i;
printf("\n*-----------------------------------*\n");
printf("| | | | | | |\n");
for(i=0;i<=5;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("|-----+-----+-----+-----+-----+-----|\n");
printf("| | | | | | |\n");
for(i=6;i<=11;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("|-----+-----+-----+-----+-----+-----|\n");
printf("| | | | | | |\n");
for(i=12;i<=17;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("|-----+-----+-----+-----+-----+-----|\n");
printf("| | | | | | |\n");
for(i=18;i<=23;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("|-----+-----+-----+-----+-----+-----|\n");
printf("| | | | | | |\n");
for(i=24;i<=29;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("|-----+-----+-----+-----+-----+-----|\n");
printf("| | | | | | |\n");
for(i=30;i<=35;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n");
printf("| | | | | | |\n");
printf("*-----------------------------------*\n\n");
}
void manualEntry(int pLife[],int pType[],int pPos,int wantedLife,int wantedType) {// WORKS NO-TOUCHY!
pLife[pPos]=wantedLife;
pType[pPos]=wantedType;
}
void resetBoardLife(int pLife[],int wantedLife) {// WORKS NO-TOUCHY!
short i;
for(i=0;i<=35;i++) {
pLife[i]=wantedLife;
}
}
void resetBoardType(int pType[],int wantedType) {// WORKS NO-TOUCHY!
short i;
for(i=0;i<=35;i++) {
pType[i]=wantedType;
}
}
int getPieceCount(int pLife[]) {// WORKS NO-TOUCHY!
int i;
int pieceCount=0;
for(i=0;i<=35;i++) {
if(pLife[i]!=0) {
pieceCount+=1;
}
}
return pieceCount;
}
void boardPOut(int pType[],int pLife[],int pOut[36][36],int pOutLength[]) { //WORKS NO TOUCHY
int i;
for(i=0;i<36;i++) {
if(pLife[i]>0) {
moveList(pType[i],i,pOut,pOutLength);
}
}
}
void showPOut(int pPos,int pOut[36][36],int pOutLength[]) { //WORKS NO TOUCHY
int i;
for(i=0;i<pOutLength[pPos];i++) {
printf("%d:%d ",i,pOut[pPos][i]);
}
}
void setStartingPosition(int startingPosition[],int *startingPositionAmount) {// Testime
int i=0;
int exitArray=1;
int enteredValue1;
printf("%d: ",i+1);
scanf("%d",&enteredValue1);
startingPosition[i]=enteredValue1;
do {
printf("Add more?(YES=1 NO=0)");
scanf("%d",&exitArray);
i+=1;
if(exitArray==1) {
printf("%d: ",i+1);
scanf("%d",&enteredValue1);
startingPosition[i]=enteredValue1;
} else {
*startingPositionAmount=i;
}
} while(exitArray==1);
printf("\n");
}
void testStartPos(int startingPosition[],int startingPositionAmount) {// Testime
int i;
for(i=0;i<startingPositionAmount;i++) {
printf("%d:%d ",i,startingPosition[i]);
}
printf("\n");
}
void solver1(int pLife[],int pOut[36][36],int pOutLength[],int startingPosition[],int startingPositionAmount,int bestPath[36],int *bestPathLength) { // incomplete of course
int i,x,a,b,c,d,e;
int currentPos;int currentPath[36];
int dummyPLife[36];
int hasDecreased=0;
for(e=0;e<36;dummyPLife[e]=pLife[e],e++);
*bestPathLength=0;
for(i=0;i<startingPositionAmount;i++){
if(dummyPLife[startingPosition[i]]>0) {
currentPath[currentPos]=startingPosition[i];
dummyPLife[startingPosition[i]]-=1;
currentPos+=1;
for(x=0;x<999;x++) { // 999 is a devil standing on its head so i had to pick it :D
if(currentPos==0){ //exit this loop if currentPos is at 0
break;
}
if(x<pOutLength[currentPath[currentPos-1]]) {
if(dummyPLife[pOut[currentPath[currentPos-1]][x]]>0) { //go up 1 level
currentPath[currentPos]=pOut[currentPath[currentPos-1]][x];
dummyPLife[currentPath[currentPos]]-=1; //reduce 1 dummyPLife
currentPos+=1; //new currentPos
x=0; //reseted to 0 for loop
}
} else { // go down 1 level
if(currentPos>*bestPathLength) { //update bestPathLength and bestPath (if its better)
*bestPathLength=currentPos;
for(a=0;a<*bestPathLength;a++) {
bestPath[a]=currentPath[a];
}
}
currentPos-=1; //currentPos lower by 1
dummyPLife[currentPath[currentPos]]+=1; //put back latest dummyPLife
for(c=0;c<pOutLength[currentPath[currentPos]];c++) {
if(currentPath[currentPos]==pOut[currentPath[currentPos]][c]) {
x=c;
break;
}
}
}
}
}
}
printf("\n");
printf("bestPathLength = %d\n",*bestPathLength);
printf("%d",bestPath[0]);
for(d=1;d<*bestPathLength;d++) {
printf("-%d",bestPath[d]);
}
}
void testMoveList(int pType,int pPosition,int pOut[][],int pOutLength[]) {
switch(pType) {
case 0:
switch(pPosition) {
case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return;
default:printf("too far >:D");
}
case 1:
switch(pPosition) {
case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return;
default:printf("too far >:D");
}
case 2:
switch(pPosition) {
case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return;
default:printf("too far >:D");
}
case 3:
switch(pPosition) {
case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return;
case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return;
default:printf("too far >:D");
}
default:printf("too far >:D");
}
}
int main(int argc,char *argv[]) {
int quit=0;
int pLife[36];
int pType[36];
int startingPosition[36];
int startingPositionAmount=0;
int pOut[36][36];
int pOutLength[36];
int i;
int bestPath[36];
int bestPathLength;
printf("Welcome...\n");
resetBoardLife(pLife,0);
resetBoardType(pType,0);
showBoard(pLife,pType);
do {
int enteredCommand;
int enteredValue1;
int enteredValue2;
int enteredValue3;
printf("+----(1)----+-----(2)-----+-----(3)------+-----(4)------+-----(5)-----+\n");
printf("|manualEntry| setStartPos |resetBoardLife|resetBoardType| solver1 |\n");
printf("+====(6)====+=====(7)=====+=====(8)======+=====(9)======+=====(10)====+\n");
printf("| | boardPOut | showPOut | moveList | |\n");
printf("+----(11)---+-----(12)----+-----(13)-----+-----(14)-----+-----(15)----+\n");
printf("| |getPieceCount| testStartPos | testMoveList | quit |\n");
printf("+-----------+-------------+--------------+--------------+-------------+\n");
scanf("%d",&enteredCommand);
switch (enteredCommand) {
case 1:printf("Location:");
scanf("%d",&enteredValue1);
printf(" pLife:");
scanf("%d",&enteredValue2);
printf(" pType:");
scanf("%d",&enteredValue3);
manualEntry(pLife,pType,enteredValue1,enteredValue2,enteredValue3);
break;
case 2:setStartingPosition(startingPosition,&startingPositionAmount);
break;
case 3:printf(" pLife:");
scanf("%d",&enteredValue1);
resetBoardLife(pLife,enteredValue1);
break;
case 4:printf(" pType:");
scanf("%d",&enteredValue1);
resetBoardType(pType,enteredValue1);
break;
case 5:solver1(pLife,pOut,pOutLength,startingPosition,startingPositionAmount,bestPath,&bestPathLength);
break;
case 7:
boardPOut(pType,pLife,pOut,pOutLength);
break;
case 8:printf("pPosition to printf array:");
scanf("%d",&enteredValue1);
showPOut(enteredValue1,pOut,pOutLength);
break;
case 9:printf("pType(0-8):");
scanf("%d",&enteredValue1);
printf("pPosition(0-35):");
scanf("%d",&enteredValue2);
moveList(enteredValue1,enteredValue2,pOut,pOutLength);
break;
case 12:printf("There is %d pieces left.",getPieceCount(pLife));
break;
case 13:testStartPos(startingPosition,startingPositionAmount);
break;
case 14:printf("pType(0-8):");
scanf("%d",&enteredValue1);
printf("pPosition(0-5):");
scanf("%d",&enteredValue2);
testMoveList(enteredValue1,enteredValue2,pOut,pOutLength);
break;
case 15:quit=1;
break;
default:printf("error\n\n");
}
showBoard(pLife,pType);
} while(quit==0);
return 0;
}
I removed function moveList.. and uploaded it here. http://s219376922.onlinehome.us/misc/moveList.txt
Ok heres the code, now heres where the problem is..
void solver1(int pLife[],int pOut[36][36],int pOutLength[],int startingPosition[],int startingPositionAmount,int bestPath[36],int *bestPathLength) { // incomplete of course
int i,x,a,b,c,d,e;
int currentPos;int currentPath[36];
int dummyPLife[36];
int hasDecreased=0;
for(e=0;e<36;dummyPLife[e]=pLife[e],e++);
*bestPathLength=0;
for(i=0;i<startingPositionAmount;i++){
if(dummyPLife[startingPosition[i]]>0) {
currentPath[currentPos]=startingPosition[i];
dummyPLife[startingPosition[i]]-=1;
currentPos+=1;
for(x=0;x<999;x++) { // 999 is a devil standing on its head so i had to pick it :D
if(currentPos==0){ //exit this loop if currentPos is at 0
break;
}
if(x<pOutLength[currentPath[currentPos-1]]) {
if(dummyPLife[pOut[currentPath[currentPos-1]][x]]>0) { //go up 1 level
currentPath[currentPos]=pOut[currentPath[currentPos-1]][x];
dummyPLife[currentPath[currentPos]]-=1; //reduce 1 dummyPLife
currentPos+=1; //new currentPos
x=0; //reseted to 0 for loop
}
} else { // go down 1 level
if(currentPos>*bestPathLength) { //update bestPathLength and bestPath (if its better)
*bestPathLength=currentPos;
for(a=0;a<*bestPathLength;a++) {
bestPath[a]=currentPath[a];
}
}
currentPos-=1; //currentPos lower by 1
dummyPLife[currentPath[currentPos]]+=1; //put back latest dummyPLife
for(c=0;c<pOutLength[currentPath[currentPos]];c++) {
if(currentPath[currentPos]==pOut[currentPath[currentPos]][c]) {
x=c;
break;
}
}
}
}
}
}
printf("\n");
printf("bestPathLength = %d\n",*bestPathLength);
printf("%d",bestPath[0]);
for(d=1;d<*bestPathLength;d++) {
printf("-%d",bestPath[d]);
}
}
The error is: An access violation(segmentational fault) on line 20 of this block down here.
This program checks a grid of 6x6, each square has a life(0~2) and a piece type(1,2,3,4,horse,bishop,tower,queen,and teleport to any other piece). The goal of this is to hit the most pieces possible...
Im pretty new to programming, so this error must come from pointers.. or bad logistic. :[
Please guide me on what it could be. Thanks :)
Any tips welcome! Even if it doesnt solve this. =D