Добавил:
Andrew1992
Факультет ИКСС, группа ИКВТ-61
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include "chessboard.h"
#include <cstring>
using namespace std;
static short method1;
static short method2;
short cR = 9, cC = 9, fR = 9, fC = 9;
void ChessBoard :: initializeBoard ()
{
for ( short i = 0; i < 8; i++ )
for ( short j = 0; j < 8; j++)
board[i][j] = 0;
}
void ChessBoard :: setCurrentRow ( unsigned X )
{
currentRow = X;
}
void ChessBoard :: setCurrentColumn ( unsigned Y )
{
currentColumn = Y;
}
unsigned ChessBoard :: getCurrentRow ()
{
return currentRow;
}
unsigned ChessBoard :: getCurrentColumn ()
{
return currentColumn;
}
void ChessBoard :: setBoard ( int number )
{
board[currentRow][currentColumn] = number;
}
void ChessBoard :: setAccessibility( int accessibility[][8], int currentRow, int currentColumn )
{
short const n = makePossibleMove(0, currentRow, currentColumn);
accessibility[currentRow][currentColumn] = 0;
for ( short i = 0; i < n; i++)
{
makePossibleMove(i, currentRow, currentColumn);
for ( short j = 0; j < i; j++)
{
if ( horizontal[i] == horizontal[j] && vertical[i] == vertical[j] && j != i )
{
i--;
break;
}
}
}
for ( short i = 0; i < n; i++)
{
accessibility[currentRow + vertical[i]][currentColumn + horizontal[i]]--;
}
}
int ChessBoard :: makePossibleMove ( int counter, int currentRow, int currentColumn )
{
register int v[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };
register int h[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };
register short possible_moves = 8;
for (register short i = 0; i < 8; i++)
{
if ( ((currentRow + v[i]) < 0) || ((currentRow + v[i]) > 7) ||
((currentColumn + h[i]) < 0) || ((currentColumn + h[i]) > 7)
|| board[currentRow + v[i]][currentColumn + h[i]] != 0 )
{
v[i] = 0; h[i] = 0; possible_moves--;
}
}
if (possible_moves == 0)
return 0;
short random = rand()%8;
while (v[random] == 0)
random = rand()%8;
vertical[counter] = v[random];
horizontal[counter] = h[random];
return possible_moves;
}
void ChessBoard :: setPosition( int accessibility[][8], int counter )
{
if (method1 != 0)
{
register short min = accessibility[currentRow + vertical[counter]][currentColumn + horizontal[counter]];
register short minRow = vertical[counter], minColumn = horizontal[counter];
register short const n = makePossibleMove(counter, currentRow, currentColumn);
for ( short i = 0; i < n; i++)
{
makePossibleMove(i, currentRow, currentColumn);
for ( short j = 0; j < i; j++)
{
if ( horizontal[i] == horizontal[j] && vertical[i] == vertical[j] && j != i )
{
i--;
break;
}
}
}
for ( short i = 0; i < n; i++)
{
if ( min > accessibility[currentRow + vertical[i]][currentColumn + horizontal[i]] )
{
min = accessibility[currentRow + vertical[i]][currentColumn + horizontal[i]];
minRow = vertical[i];
minColumn = horizontal[i];
}
else if ( (min == accessibility[currentRow + vertical[i]][currentColumn + horizontal[i]]) &&
(minRow != vertical[i] || minColumn != horizontal[i]) && method2 == 1 )
{
if ( prediction(accessibility, minRow, minColumn, counter, currentRow, currentColumn) >
prediction(accessibility, vertical[i], horizontal[i], counter, currentRow, currentColumn))
{
min = accessibility[currentRow + vertical[i]][currentColumn + horizontal[i]];
minRow = vertical[i];
minColumn = horizontal[i];
}
}
}
vertical[counter] = minRow;
horizontal[counter] = minColumn;
}
currentRow += vertical[counter];
currentColumn += horizontal[counter];
board[currentRow][currentColumn] = counter + 1;
if (method1 != 0 && counter < 63)
setAccessibility( accessibility, currentRow, currentColumn );
}
void ChessBoard :: printBoard ()
{
cout << endl << endl;
for (short i = 0; i < 8; i++)
{
cout << "\t\t" << 8 - i;
for (short j = 0; j < 8; j++)
{
cout << setw(4) << board[i][j];
}
cout << endl << endl;
}
cout << "\t\t" << setw (5) << "a" << setw(4) << "b" << setw(4) << "c" << setw(4) << "d" << setw(4) << "e" <<
setw(4) << "f" << setw(4) << "g" << setw(4) << "h"<< endl << endl;
}
int ChessBoard :: prediction ( int accessibility[][8], int vertical, int horizontal, int counter, int currentRow, int currentColumn )
{
ChessBoard object1;
short min2;
short minRow2, minColumn2;
int A[8][8];
for ( short i = 0; i < 8; i++ )
for ( short j = 0; j < 8; j++)
A[i][j] = accessibility[i][j];
for ( short i = 0; i < 8; i++ )
for ( short j = 0; j < 8; j++)
object1.board[i][j] = board[i][j];
currentRow += vertical;
currentColumn += horizontal;
object1.board[currentRow][currentColumn] = counter + 1;
counter++;
object1.setAccessibility(A, currentRow, currentColumn);
short const n = object1.makePossibleMove(0, currentRow, currentColumn);
if ( object1.makePossibleMove(0, currentRow, currentColumn) == false )
{
return 8;
}
for ( short i = 0; i < n; i++)
{
object1.makePossibleMove(i, currentRow, currentColumn );
for ( short j = 0; j < i; j++)
{
if ( object1.horizontal[i] == object1.horizontal[j] && object1.vertical[i] == object1.vertical[j] && j != i )
{
i--;
break;
}
}
}
min2 = A[currentRow + object1.vertical[0]][currentColumn + object1.horizontal[0]];
static_cast<void>(minRow2 = object1.vertical[0]), minColumn2 = object1.horizontal[0];
for ( short i = 0; i < n; i++)
{
if ( (min2 > A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]]) && (A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]] >= 0) )
{
min2 = A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]];
minRow2 = object1.vertical[i];
minColumn2 = object1.horizontal[i];
}
else if ( (min2 == A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]]) &&
(minRow2 != object1.vertical[i] || minColumn2 != object1.horizontal[i]) &&
(A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]] >= 0) )
{
if ( object1.prediction(A, minRow2, minColumn2, counter, currentRow, currentColumn) >
object1.prediction (A, object1.vertical[i], object1.horizontal[i], counter, currentRow, currentColumn))
{
min2 = A[currentRow + object1.vertical[i]][currentColumn + object1.horizontal[i]];
minRow2 = object1.vertical[i];
minColumn2 = object1.horizontal[i];
}
}
}
return min2;
}
void ChessBoard :: interact(int argc, char **argv)
{
char begin[3], end[3];
string reply;
if (argc < 2){
cout << "Do you want to select the starting position? (Yes/No): "; cin >> reply;
}
else {
reply = argv[1];
}
if (reply == "Yes" || reply == "yes" || reply == "y" || reply == "Y")
{
if (argc < 2)
{
cout << "Enter the starting square: "; cin >> begin;
}
else {
strcpy(begin, argv[2]);
}
switch(begin[0])
{
case 'a': cC = 0; break;
case 'b': cC = 1; break;
case 'c': cC = 2; break;
case 'd': cC = 3; break;
case 'e': cC = 4; break;
case 'f': cC = 5; break;
case 'g': cC = 6; break;
case 'h': cC = 7; break;
default: cC = 9;
}
switch(begin[1])
{
case '1': cR = 7; break;
case '2': cR = 6; break;
case '3': cR = 5; break;
case '4': cR = 4; break;
case '5': cR = 3; break;
case '6': cR = 2; break;
case '7': cR = 1; break;
case '8': cR = 0; break;
default: cR = 9;
}
}
if (argc < 2){
cout << "Do you want to select the final position? (Yes/No): "; cin >> reply;
}
else
reply = argv[3];
if (reply == "Yes" || reply == "yes" || reply == "y" || reply == "Y")
{
do
{
if (argc < 2){
cout << "Enter the final position: "; cin >> end;
}
else
strcpy(end, argv[4]);
switch(end[0])
{
case 'a': fC = 0; break;
case 'b': fC = 1; break;
case 'c': fC = 2; break;
case 'd': fC = 3; break;
case 'e': fC = 4; break;
case 'f': fC = 5; break;
case 'g': fC = 6; break;
case 'h': fC = 7; break;
default: fC = 9;
}
switch(end[1])
{
case '1': fR = 7; break;
case '2': fR = 6; break;
case '3': fR = 5; break;
case '4': fR = 4; break;
case '5': fR = 3; break;
case '6': fR = 2; break;
case '7': fR = 1; break;
case '8': fR = 0; break;
default: fR = 9;
}
if (cR == fR && cC == fC)
cout << "Impossible path!" << endl;
}
while (cR == fR && cC == fC);
}
if (argc < 2){
cout << "Use Random pick method or Strategy? (Random/Strategy): "; cin >> reply;
}
else
reply = argv[5];
if (reply == "Random" || reply == "random")
{
method1 = 0;
}
else
{
method1 = 1;
if ( argc < 2 )
{
cout << "Shall I use Modified Warnsdorff's Algorithm or Normal? (Modified/Normal): "; cin >> reply;
}
else
reply = argv[6];
if (reply == "Modified" || reply == "modified")
{
method2 = 1;
}
else
{
method2 = 0;
}
}
}