I have one-dimensional array which means chess table. I would like to find all positions which knight could attack. For example, have 3x3 chess table (K is Knight, X is attack position):
---------------
| X | | |
---------------
| | | K |
---------------
| X | | |
---------------
and for this table, in prolog program I would like to have array:
-------------------------------------
| X | | | | | K | X | | |
-------------------------------------
I want to write universal program for all cases - 3x3 table, 4x4, 5x5, etc.
I tried:
control(Table, N, Pos, NewTable) :- insert(Table, Pos, 'k' , TempTable1, 1),
insert(TempTable1, Pos-N*2+1, 'x' , TempTable2, 1),
insert(TempTable2, Pos-N*2-1, 'x' , TempTable3, 1),
insert(TempTable3, Pos-N +2, 'x' , TempTable4, 1),
insert(TempTable4, Pos-N -2, 'x' , TempTable5, 1),
insert(TempTable5, Pos+N*2+1, 'x' , TempTable6, 1),
insert(TempTable6, Pos+N*2-1, 'x' , TempTable7, 1),
insert(TempTable7, Pos+N +2, 'x' , TempTable8, 1),
insert(TempTable8, Pos+N -2, 'x' , NewTable, 1).
There N - is table size (3), Pos - knight position. The "insert" is OK, but "control" doesn't work correctly:
?- control([0,0,0,0,0,0,0,0,0], 3, 6, R).
R = [x, 0, 0, 0, x, k, x, 0, 0].
should be R = [x, 0, 0, 0, 0, k, x, 0, 0]
.
Any ideas, how to change "control" predicate?
control
. - Cephalopod