Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes to tic-tac-toe #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions tictactoe.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,41 @@
'
*/
/* a little wrapper to make starting a game convenient */
tictactoe :- game([b,b,b,b,b,b,b,b,b], x).
tictactoe :- write('Enter 1..9 at > to make a move.'),nl,game([b,b,b,b,b,b,b,b,b], x).

/* the core recursive call to play the game */
game(Board , _) :- win(x, Board),write('you win!').
game(Board , _) :- win(o, Board),write('I win!').
game(Board , _) :- win(x, Board),showBoard(Board),write('You win!'),nl.
game(Board , _) :- win(o, Board),showBoard(Board),write('I win!'),nl.
game(Board , _) :- cat(Board). /* no more moves - not technically cat */
game(Board , x) :- moveUser(Board , NewBoard) , game(NewBoard , o).
game(Board , o) :- moveComputer(Board , NewBoard), game(NewBoard , x).

moveUser(Board , NewBoard) :- showBoard(Board), put('*'),get_char(Y), get_char(_), name(Y , [X]),
moveUser(Board , NewBoard) :- showBoard(Board), put('>'),get_char(Y), get_char(_), name(Y , [X]),
Loc is X - 48, !,procUserMove(Loc,Board,NewBoard).

procUserMove(Loc,Board,NewBoard) :- Loc > 0, Loc < 10, playAt(x , Loc , Board , NewBoard).
/* this handles the invalid case */
procUserMove(_,Board,_) :- write('invalid move'),!, game(Board,x).
procUserMove(_,Board,_) :- write('Invalid move, try again.'),nl,!, game(Board,x).

/* lists must be same length tim- these were getting triggered, how?
playAt(_ , _ , [_|_] , []) :- !,fail.
```````````````````````````````````````````````````playAt(_ , _ , [_|_] , []) :-
!,fail.
playAt(_ , _ , [] , [_|_]) :- !,fail. */
playAt(Player , 1 , [b|Tail] , [Player|Tail]).
playAt(Player , Loc , [H|TBoard] , [H|TNewBoard]) :-
Loc > 1,
M is Loc - 1 ,
playAt(Player , M , TBoard , TNewBoard).

%showBoard([]).
%showBoard([H|T]) :- put(H),showBoard1(T).
%showBoard1([H|T]) :- put(H),showBoard2(T).
%showBoard2([H|T]) :- put(H),nl,showBoard(T),nl.
showBoard([]).
showBoard([H|T]) :- put(H),showBoard1(T).
showBoard1([H|T]) :- put(H),showBoard2(T).
showBoard2([H|T]) :- put(H),nl,showBoard(T).
showBoard([A,B,C|T]) :- put(A),put(B),put(C),nl,showBoard1(T). %vp change
showBoard1([A,B,C|T]) :- put(A),put(B),put(C),nl,showBoard2(T). %vp change
showBoard2([A,B,C|T]) :- put(A),put(B),put(C),nl,showBoard(T). %vp change


win(A , [A,A,A,_,_,_,_,_,_]).
win(A , [_,_,_,A,A,A,_,_,_]).
Expand Down