/* ************************************************************************** * Program name : 045_Knights_tour (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/27 - first version * ************************************************************************** * Description : (a) Initial setting * * (b) Where to start the new game * * (c) Show current status * * (d) Find any possible move * * (e) Ask for the next move * * (f) Try again ? * ************************************************************************** */ #include #include void Displayboard(int ChessArray[][8], int Arraysize) // Note (1) { int Index_1,Index_2; cout << "\n\n\n\n\t 1 2 3 4 5 6 7 8" << "\n\t +-+-+-+-+-+-+-+-+"; for (Index_1=0; Index_1<=7; Index_1++) { cout << "\n\t " << Index_1+1 << " |"; for (Index_2=0; Index_2<=7; Index_2++) if (ChessArray[Index_1][Index_2]==1) cout << "*|"; else cout << " |"; cout << "\n\t +-+-+-+-+-+-+-+-+"; } } int main() { // part 1 : declaration const int Arraysize = 8; int ChessArray[Arraysize][Arraysize]; int Pos_x; // Location X (old) int Pos_y; // Location Y (old) int NewPos_x; // Location X (new) int NewPos_y; // Location Y (new) int Index_1, Index_2; // Loop counters int Endofgame; // End of game flag int Placed; // No. of successful pass char Again; do { // part 2 : Initial setting for (Index_1=0; Index_1<=7; Index_1++) for (Index_2=0; Index_2<=7; Index_2++) (ChessArray[Index_1][Index_2]=0); Endofgame = 0; // part 3 : Where to start do { cout << "\n" << "\n\t\t\tKnight's Tour\n" << "\n\tCan the chess piece called the Knight move around an" << "\n\tempty chessboard and touch each of the 64 squares once" << "\n\tand only once ?"; cout << "\n\n\tPlease enter the position where you want to start" << "\n\n\tthis game. Whenever you want to stop the game, please" << "\n\n\tinput zero as the position of both X and Y." << "\n\tInput position X (1-8) : "; cin >> Pos_x; cout << "\n\tInput position Y (1-8) : "; cin >> Pos_y; } while (Pos_x < 1 || Pos_x > 8 || Pos_y < 1 || Pos_y > 8) ; cout << "\n\n"; ChessArray[Pos_x-1][Pos_y-1] = 1; Placed = 1; do { do { Endofgame = 1; // part 4a : Show current status Displayboard(ChessArray,Arraysize); cout << "\n\n\tCurrent Pass : " << Placed+1 // Note (2) << "\tCurrent Position is : " << Pos_x << "," << Pos_y << "\n\tPossible positions are : "; // part 4b : Find any possible move if (Pos_x+2<9 && Pos_x+2>0 && Pos_y+1<9 && Pos_y+1>0 && ChessArray[Pos_x+1][Pos_y] != 1) { cout << Pos_x+2 << "," << Pos_y+1 << " "; Endofgame = 0; } if (Pos_x+2<9 && Pos_x+2>0 && Pos_y-1<9 && Pos_y-1>0 && ChessArray[Pos_x+1][Pos_y-2] != 1) { cout << Pos_x+2 << "," << Pos_y-1 << " "; Endofgame = 0; } if (Pos_x-2<9 && Pos_x-2>0 && Pos_y+1<9 && Pos_y+1>0 && ChessArray[Pos_x-3][Pos_y] != 1) { cout << Pos_x-2 << "," << Pos_y+1 << " "; Endofgame = 0; } if (Pos_x-2<9 && Pos_x-2>0 && Pos_y-1<9 && Pos_y-1>0 && ChessArray[Pos_x-3][Pos_y-2] != 1) { cout << Pos_x-2 << "," << Pos_y-1 << " "; Endofgame = 0; } if (Pos_x+1<9 && Pos_x+1>0 && Pos_y+2<9 && Pos_y+2>0 && ChessArray[Pos_x][Pos_y+1] != 1) { cout << Pos_x+1 << "," << Pos_y+2 << " "; Endofgame = 0; } if (Pos_x+1<9 && Pos_x+1>0 && Pos_y-2<9 && Pos_y-2>0 && ChessArray[Pos_x][Pos_y-3] != 1) { cout << Pos_x+1 << "," << Pos_y-2 << " "; Endofgame = 0; } if (Pos_x-1<9 && Pos_x-1>0 && Pos_y+2<9 && Pos_y+2>0 && ChessArray[Pos_x-2][Pos_y+1] != 1) { cout << Pos_x-1 << "," << Pos_y+2 << " "; Endofgame = 0; } if (Pos_x-1<9 && Pos_x-1>0 && Pos_y-2<9 && Pos_y-2>0 && ChessArray[Pos_x-2][Pos_y-3] != 1) { cout << Pos_x-1 << "," << Pos_y-2 << " "; Endofgame = 0; } if (Endofgame == 1) break; // Note (3) // part 4c : Ask for the next move cout << "\n\tInput position X (1-8) of your move : "; cin >> NewPos_x; cout << "\n\tInput position Y (1-8) of your move : "; cin >> NewPos_y; if (NewPos_x==0 && NewPos_y==0) // Note (3) { Endofgame=1; break; } } while (NewPos_x < 1 || NewPos_x > 8 || // Note (4) NewPos_y < 1 || NewPos_y > 8 || fabs(fabs(Pos_x-NewPos_x)+fabs(Pos_y-NewPos_y))!=3 || Pos_x==NewPos_x || Pos_y==NewPos_y || ChessArray[NewPos_x-1][NewPos_y-1] == 1 ); ChessArray[NewPos_x-1][NewPos_y-1] = 1; Pos_x = NewPos_x; // update current position Pos_y = NewPos_y; Placed++; // update the no. of sucessful move if (Placed==64) // Complete the game cout << "\n\n\t\aW E L L D O N E !!!"; } while (Endofgame==0 || Placed==64); // Note (3) // part 5 : try again ? cout << "\n\n\tTry again (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) Show the current status of the chessboard : (a) Blank --> not yet touch (b) * --> already touch Example : 1 2 3 4 5 6 7 8 +-+-+-+-+-+-+-+-+ 1 | | | | | | | | | +-+-+-+-+-+-+-+-+ 2 | | | | | | | | | +-+-+-+-+-+-+-+-+ 3 | | | | | | | | | +-+-+-+-+-+-+-+-+ 4 | | | | | | | | | +-+-+-+-+-+-+-+-+ 5 | | | | | |*| | | +-+-+-+-+-+-+-+-+ 6 | | | | | | | | | +-+-+-+-+-+-+-+-+ 7 | | | | | | |*| | +-+-+-+-+-+-+-+-+ 8 | | | | |*| | | | +-+-+-+-+-+-+-+-+ (2) Show current status (a) Current pass (b) Current position (c) Position of possible move Example : Current Pass : 4 Current Position is : 8,5 Possible positions are : 6,6 6,4 7,3 (3) The game will stop : (a) When the value of both position X and Y is 0 (Zero). (b) When the value of "Endofgame" is 1. Before checking the position of possible move, the value of "Endofgame" is setted to 1. When the program finds any possible move, the the value of "Endofgame" is setted to 0 (zero). (c) When the no. of successful move (or squares touched) is 64. (4) The new position is invalid when : (a) too large ( > 8) (b) too small ( < 1) (c) the distance between new position and current position is not equal to 3 (d) the X co-ordinate or Y co-ordinate of New Position is same as that of Current Position. (e) or that square (the inputted new position) was already touched. */