Quantcast

Maximum PC

It is currently Wed Aug 27, 2014 9:11 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Trouble with an assignment
PostPosted: Sat Oct 16, 2004 5:51 pm 
8086
8086

Joined: Sat Oct 16, 2004 5:40 pm
Posts: 2
Hey guys,

I am trying to debug this program that I wrote for class and am stumped. Any suggestions?

//Fig 3.54: fig04_24.cpp
//Chess Knight
#include <iostream>
#include <iomanip>

using std::cout; //program uses cout
using std::endl; //program uses endl



// Create globals

int moveknight (int);

static int nomoves = 1;


int main()
{
while (nomoves == 1){ //while loop ends tour
moveknight;
}
return 0;
}

// Moves knight around chessboard
int moveknight (nomoves)

{

static int curhor = 0;
static int curvert = 0;
static int moves = 1;
static int board [8] [8] = {0};
int newhor [8] = {2,1,-1,-2,-2,-1,1,2}; //possible horizental half of moves
int newvert [8] = {-1,-2,-2,-1,1,2,2,1}; //possible horizental half of moves

for (int cntr = 0; cntr<=7; cntr++, moves++){ //for loop to run through possible moves

if (curhor + newhor < 0 || curhor + newhor > 7 || curvert + newvert < 0 || curvert + newvert > 7)
return; //keeps peice on the board
if (cntr == 7 && (board [ curhor + newhor[cntr] ] [ curvert + newvert [cntr] ] == 1)
nomoves = 0; //sets variable to end tour

while (board [ curhor + newhor[cntr] ],[ curvert + newvert [cntr] ] == 0){ //While loop for when move is possible
switch (curvert){ //converts to chessboard units for print
case 0:
cout << "Move " << moves << "\t A";
break;
case 1:
cout << "Move " << moves << "\t B";
break;
case 2:
cout << "Move " << moves << "\t C";
break;
case 3:
cout << "Move " << moves << "\t D";
break;
case 4:
cout << "Move " << moves << "\t E";
break;
case 5:
cout << "Move " << moves << "\t F";
break;
case 6:
cout << "Move " << moves << "\t G";
break;
case 7:
cout << "Move " << moves << "\t H";
break;
default:
break;
}
switch (curhor){ //converts to chessboard units for print
case 0:
cout << "1 to ";
break;
case 1:
cout << "2 to ";
break;
case 2:
cout << "3 to ";
break;
case 3:
cout << "4 to ";
break;
case 4:
cout << "5 to ";
break;
case 5:
cout << "6 to ";
break;
case 6:
cout << "7 to ";
break;
case 7:
cout << "8 to ";
break;
default:
break;
}

curhor = curhor + newhor; //move piece horizontally
curvert = curvert + newvert; //move piece vertically

switch (curvert){ //converts to chessboard units for print
case 0:
cout << "A";
break;
case 1:
cout << "B";
break;
case 2:
cout << "C";
break;
case 3:
cout << "D";
break;
case 4:
cout << "E";
break;
case 5:
cout << "F";
break;
case 6:
cout << "G";
break;
case 7:
cout << "H";
break;
default:
break;
}
switch (curhor){ //converts to chessboard units for print
case 0:
cout << "1\n";
break;
case 1:
cout << "2\n";
break;
case 2:
cout << "3\n";
break;
case 3:
cout << "4\n";
break;
case 4:
cout << "5\n";
break;
case 5:
cout << "6\n";
break;
case 6:
cout << "7\n";
break;
case 7:
cout << "8\n";
break;
default:
break;
}
break;
}

}

return nomoves;

}


Top
  Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 6:10 pm 
I judge you GUILTY!
I judge you GUILTY!
User avatar

Joined: Tue May 25, 2004 4:38 pm
Posts: 162
Location: New York City
Move the main function to the bottom for starters.
Seoond your function
int moveknight(nomoves) is missing the int specifiier
it should be
Code:
int moveknight(int nomoves)


Your code has other syntax errors like mssing parantheses.
You might have to use a * for the newver and newhor calculations since the new positions are arrays.
example

Code:
if (curhor + *newhor < 0 || curhor + *newhor > 7 || curvert + *newvert < 0 || curvert + *newvert > 7)


Top
  Profile  
 
 Post subject: Got it
PostPosted: Sat Oct 16, 2004 8:08 pm 
8086
8086

Joined: Sat Oct 16, 2004 5:40 pm
Posts: 2
Dude,
I figured it out like 5 minutes before your post. Of course, a half dozen other bugs came up next, but I have it running now. Thanks.

G


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group