Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choose Position on a game table

Status
Not open for further replies.

berlos

Programmer
Jun 21, 2003
1
PT
I have done the predicate choose_position but its not working :( when i choose the figure and the position that i want to place on the game table the code returns a new board with random figures and i want that the code returns the old board with the figure in the position that i have choose. Do you know where is the problem?Can you correct the situation?
One more thing do you know how do i place a new board with figures next to the existing one. This board is for the user see what figures are avairable to use in the game table and then to use it with the predicate choose_position.
here is the code....

symbol(o).
symbol('+').
symbol(x).
symbol('-').

:- dynamic(board/1).
:- dynamic(board_size/1).


start:- nl, writeln('Welcome to the game Figures '),
start_.

start_:- startgame(Option),
start(Option).

start(1):-
write('Size of the Board:'),
read(Size),nl,
number(Size),
Size>1,
retractall(board_size(_)),
retractall(board(_)),
asserta(board_size(Size)),
create_board(Size),
board(Board),
print_board3(Board), %here the actual game should start
choose_position(Symbol,X,Y),
make_move(Symbol,X,Y).

start(0).

startgame(Option):- repeat,
nl, writeln(' 1 - Start the game '),
writeln(' 0 - Quit '), nl,
write('-> '), nl,
read(Option),
Option =< 1, Option >= 0,
!;
start(Option).

create_board(Size) :-
make_rows(Size,0,Board),
init_board(Board),
asserta(board(Board)).

make_rows(N,N,[]).
make_rows(Size,N,[C|Tail]) :-
N1 is N+1,
N<Size,
make_columns(Size,0,C),
make_rows(Size,N1,Tail).

make_columns(N,N,[]).
make_columns(Size,N,[_|Tail]) :-
N1 is N+1,
N<Size,
make_columns(Size,N1,Tail).

init_board(Board) :-
init(o,Board),
init('+',Board),
init(x,Board),
init('-',Board).

init_board(Board) :-
init_board(Board).

init(Symbol,Board) :-
length(Board,L),
PosX is random(L),
PosY is random(L),
place_figure(Symbol,PosY,PosX,Board).

init_rest([]).
init_rest([C|Tail]) :-
init_rest_c(C),
init_rest(Tail).

init_rest_c([]).
init_rest_c(['*'|Tail]) :-
init_rest_c(Tail).
init_rest_c([P|Tail]) :-
ground(P), %Positin is taken
init_rest_c(Tail).


place_figure(Symbol,0,PosY,[C|_]) :-
place_figure_c(Symbol,PosY,C).
place_figure(Symbol,N,PosY,[_|Tail]) :-
N>0,
N1 is N-1,
place_figure(Symbol,N1,PosY,Tail).

place_figure_c(Symbol,0,[Symbol|_]).
place_figure_c(Symbol,N,[_|Tail]) :-
N>0,
N1 is N-1,
place_figure_c(Symbol,N1,Tail).

make_move(Symbol,X,Y) :-
board_size(Size),
X =< Size,
Y =< Size,
PosX is X-1,
PosY is Y-1,
board(B),
remove_figure(Symbol,B,Board),
place_figure(Symbol,PosY,PosX,Board),
retractall(board(_)),
asserta(board(Board)),
print_board3(Board).


remove_figure(Symbol,[H1|Tail],[H2|Tail]) :-
my_member(Symbol,H1),
remove_f(Symbol,H1,H2).
remove_figure(Symbol,[H|T1],[H|T2]) :-
not(member(Symbol,H)),
remove_figure(Symbol,T1,T2).

my_member(M,[H|_]) :-
ground(H),
M=H.
my_member(M,[_|T]) :-
my_member(M,T).

remove_f(S1,[H|Tail],[_|Tail]) :-
ground(H),
H=S1.
remove_f(S1,[H|T1],[H|T2]) :-
var(H),
remove_f(S1,T1,T2).
remove_f(S1,[H|T1],[H|T2]) :-
ground(H),
S1\=H,
remove_f(S1,T1,T2).


choose_position(Symbol,X,Y):- repeat,
nl, writeln(' What figure do you want to place on the table '),
write('-> '),
read(Symbol), nl,
writeln(' Where do you want to put the figure '), nl,
write('X -> '),
read(X), nl,
write('Y -> '),
read(Y), nl,
!;
start.





% ------------------ print_board 3rd version ----------------------

print_board3(Board) :-
length(Board,N),
draw_board(N,Board).


draw_board(N,Board) :-
draw_first_line(N),
draw_rows(1,N,Board),
nl.


draw_first_line(N) :-
nl,nl,
write_times(' ',4),
draw_first_line_rek(1,N),
nl,
draw_seperator_line(N).

draw_first_line_rek(I,N) :- I>N.
draw_first_line_rek(I,N) :-
I=<N,
write(' '),
write(I),
write(' '),
I1 is I+1,
draw_first_line_rek(I1,N).

draw_rows(_,_,[]).
draw_rows(I,N,[R|Tail]) :-
num_of_digits(I,D),
T is 1 + 2-D,
write_times(' ',T),
write(I),
write(' |'),
draw_row(R),
nl,
draw_seperator_line(N),
I1 is I+1,
draw_rows(I1,N,Tail).

draw_row([]).
draw_row([H|Tail]) :-
var(H),
write(' '),
write('*'),
write(' |'),
draw_row(Tail).
draw_row([H|Tail]) :-
ground(H),
write(' '),
write(H),
write(' |'),
draw_row(Tail).

draw_seperator_line(N) :-
write_times(' ',4),
T is N*(4)+1,
write_times('-',T),
nl.


num_of_digits(0,0).
num_of_digits(M,D) :-
M>0,
M1 is M//10,
num_of_digits(M1,D1),
D is D1+1.

write_times(_,0).
write_times(S,N) :-
N>0,
N1 is N-1,
write(S),
write_times(S,N1).

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top