PERL   74
Tic-Tac-Toe program
Guest on 16th August 2022 01:08:49 AM


  1. % A Tic-Tac-Toe program in Prolog.   S. Tanimoto.
  2. % To play a game with the computer, type
  3. % playo.
  4. % To watch the computer play a game with itself, type
  5. % selfgame.
  6.  
  7. % Predicates that define the winning conditions:
  8.  
  9. win(Board, Player) :- rowwin(Board, Player).
  10. win(Board, Player) :- colwin(Board, Player).
  11. win(Board, Player) :- diagwin(Board, Player).
  12.  
  13. rowwin(Board, Player) :- Board = [Player,Player,Player,_,_,_,_,_,_].
  14. rowwin(Board, Player) :- Board = [_,_,_,Player,Player,Player,_,_,_].
  15. rowwin(Board, Player) :- Board = [_,_,_,_,_,_,Player,Player,Player].
  16.  
  17. colwin(Board, Player) :- Board = [Player,_,_,Player,_,_,Player,_,_].
  18. colwin(Board, Player) :- Board = [_,Player,_,_,Player,_,_,Player,_].
  19. colwin(Board, Player) :- Board = [_,_,Player,_,_,Player,_,_,Player].
  20.  
  21. diagwin(Board, Player) :- Board = [Player,_,_,_,Player,_,_,_,Player].
  22. diagwin(Board, Player) :- Board = [_,_,Player,_,Player,_,Player,_,_].
  23.  
  24. % Helping predicate for alternating play in a "self" game:
  25.  
  26. other(x,o).
  27. other(o,x).
  28.  
  29. game(Board, Player) :- win(Board, Player), !, write([player, Player, wins]).
  30. game(Board, Player) :-
  31.   other(Player,Otherplayer),
  32.   move(Board,Player,Newboard),
  33.   !,
  34.   display(Newboard),
  35.   game(Newboard,Otherplayer).
  36.  
  37. move([b,B,C,D,E,F,G,H,I], Player, [Player,B,C,D,E,F,G,H,I]).
  38. move([A,b,C,D,E,F,G,H,I], Player, [A,Player,C,D,E,F,G,H,I]).
  39. move([A,B,b,D,E,F,G,H,I], Player, [A,B,Player,D,E,F,G,H,I]).
  40. move([A,B,C,b,E,F,G,H,I], Player, [A,B,C,Player,E,F,G,H,I]).
  41. move([A,B,C,D,b,F,G,H,I], Player, [A,B,C,D,Player,F,G,H,I]).
  42. move([A,B,C,D,E,b,G,H,I], Player, [A,B,C,D,E,Player,G,H,I]).
  43. move([A,B,C,D,E,F,b,H,I], Player, [A,B,C,D,E,F,Player,H,I]).
  44. move([A,B,C,D,E,F,G,b,I], Player, [A,B,C,D,E,F,G,Player,I]).
  45. move([A,B,C,D,E,F,G,H,b], Player, [A,B,C,D,E,F,G,H,Player]).
  46.  
  47. display([A,B,C,D,E,F,G,H,I]) :- write([A,B,C]),nl,write([D,E,F]),nl,
  48.  write([G,H,I]),nl,nl.
  49.  
  50. selfgame :- game([b,b,b,b,b,b,b,b,b],x).
  51.  
  52. % Predicates to support playing a game with the user:
  53.  
  54. x_can_win_in_one(Board) :- move(Board, x, Newboard), win(Newboard, x).
  55.  
  56. % The predicate orespond generates the computer's (playing o) reponse
  57. % from the current Board.
  58.  
  59. orespond(Board,Newboard) :-
  60.  move(Board, o, Newboard),
  61.  win(Newboard, o),
  62.  !.
  63. orespond(Board,Newboard) :-
  64.  move(Board, o, Newboard),
  65.  not(x_can_win_in_one(Newboard)).
  66. orespond(Board,Newboard) :-
  67.  move(Board, o, Newboard).
  68. orespond(Board,Newboard) :-
  69.  not(member(b,Board)),
  70.  !,
  71.  write('Cats game!'), nl,
  72.  Newboard = Board.
  73.  
  74. % The following translates from an integer description
  75. % of x's move to a board transformation.
  76.  
  77. xmove([b,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
  78. xmove([A,b,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
  79. xmove([A,B,b,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
  80. xmove([A,B,C,b,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
  81. xmove([A,B,C,D,b,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
  82. xmove([A,B,C,D,E,b,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
  83. xmove([A,B,C,D,E,F,b,H,I], 7, [A,B,C,D,E,F,x,H,I]).
  84. xmove([A,B,C,D,E,F,G,b,I], 8, [A,B,C,D,E,F,G,x,I]).
  85. xmove([A,B,C,D,E,F,G,H,b], 9, [A,B,C,D,E,F,G,H,x]).
  86. xmove(Board, N, Board) :- write('Illegal move.'), nl.
  87.  
  88. % The 0-place predicate playo starts a game with the user.
  89.  
  90. playo :- explain, playfrom([b,b,b,b,b,b,b,b,b]).
  91.  
  92. explain :-
  93.   write('You play X by entering integer positions followed by a period.'),
  94.   nl,
  95.   display([1,2,3,4,5,6,7,8,9]).
  96.  
  97. playfrom(Board) :- win(Board, x), write('You win!').
  98. playfrom(Board) :- win(Board, o), write('I win!').
  99. playfrom(Board) :- read(N),
  100.   xmove(Board, N, Newboard),
  101.   display(Newboard),
  102.   orespond(Newboard, Newnewboard),
  103.   display(Newnewboard),
  104.   playfrom(Newnewboard).

Raw Paste

Login or Register to edit or fork this paste. It's free.