110 likes | 123 Views
Play a basic interactive game of Noughts and Crosses, choose your moves wisely, and compete against the computer. Enjoy the classic game in a digital environment!
E N D
A Basic Interactive Game play(Result) :- initialise(Game, Position, Player), display_game(Position, Player), play(Position, Player, Result). play(Position, Player, Result) :- game_over(Position, Player, Result),!, announce(Result). play(Position, Player, Result) :- choose_move(Position, Player, Move), move(Move, Position, NewPosition), display_game(NewPosition, Player), next_player(Player, NewPlayer), !, play(NewPosition, NewPlayer, Result). Sets initial state Displays game state Terminating state Display the result Decide on a move Update the state Choose who’s next
Noughts and Crosses play :- initialise(Game, Position, Player), display_game(Position, Player), play(Position, Player, Result). play(Position, Player, Result) :- game_over(Position, Player, Result),!, announce(Result). play(Position, Player, Result) :- choose_move(Position, Player, Move), move(Move, Position, NewPosition), display_game(NewPosition, Player), next_player(Player, NewPlayer), !, play(NewPosition, NewPlayer, Result). X
Noughts and Crosses play :- initialise(Game, Position, Player), display_game(Position, Player), play(Position, Player, Result). play(Position, Player, Result) :- game_over(Position, Player, Result),!, announce(Result). play(Position, Player, Result) :- choose_move(Position, Player, Move), move(Move, Position, NewPosition), display_game(NewPosition, Player), next_player(Player, NewPlayer), !, play(NewPosition, NewPlayer, Result). X X O X X
Noughts and Crosses play :- initialise(Game, Position, Player), display_game(Position, Player), play(Position, Player, Result). play(Position, Player, Result) :- game_over(Position, Player, Result),!, announce(Result). play(Position, Player, Result) :- choose_move(Position, Player, Move), move(Move, Position, NewPosition), display_game(NewPosition, Player), next_player(Player, NewPlayer), !, play(NewPosition, NewPlayer, Result). X O X
Noughts and Crosses play :- initialise(Game, Position, Player), display_game(Position, Player), play(Position, Player, Result). play(Position, Player, Result) :- game_over(Position, Player, Result),!, announce(Result). play(Position, Player, Result) :- choose_move(Position, Player, Move), move(Move, Position, NewPosition), display_game(NewPosition, Player), next_player(Player, NewPlayer), !, play(NewPosition, NewPlayer, Result). O X O X X
Representing Game State: Examples board(x, [x,e,o,e,x,o,e,e,x]). board(x, [x,_,o,_,x,o,_,_,x]). board(x, player(x,[1,5,9]), player(o,[3,6])). board(P, [S1,S2,S3,S4,S5,S6,S7,S8,S9], [ [S1,S2,S3], [S4,S5,S6], [S7,S8,S9], [S1,S4,S7], [S2,S5,S8], [S3,S6,S9], [S1,S5,S9], [S3,S5,S7] ]).
Input and Output (Prolog Terms) Reads a Prolog Term from the standard input | ?- read(X). : move(5). X = move(5) Writes a Prolog Term to the standard output Throws a new line on the standard input | ?- write([x,e,e,x,o,e,e,e,e]), nl. ([x,e,e,x,o,e,e,e,e])
A Simple Recursive Input Program | ?- read_until(X, 'Input data : ', stop). Input data : foo(a). Input data : baz. Input data : stop. X = [foo(a), baz] read_until([Input|Rest], Prompt, StopTerm) :- write(Prompt), read(Input), \+ Input = StopTerm, !, read_until(Rest, Prompt, StopTerm). read_until([], _, _).
Input (Characters) | ?- read_word_list(X). |: You can find this program in Sterling and Shapiro X = [‘You’,can,find,this,program,in,’Sterling’,and,’Shapiro’]
Input (Characters) read_word_list(Ws) :- get0(C), read_word_list(C, Ws). read_word_list(C, [W|Ws]) :- word_char(C), read_word(C, W, C1), read_word_list(C1, Ws). read_word_list(C, Ws) :- fill_char(C), get0(C1), read_word_list(C1, Ws). read_word_list(C, []) :- end_of_words_char(C). fill_char(32). end_of_words_char(46). end_of_words_char(63). Reads the first character from standard input Full stop Question mark
Reading a Word read_word(C, W, C1):- word_chars(C, Cs, C1), name(W, Cs). word_chars(C, [C|Cs], C0):- word_char(C), !, get0(C1), word_chars(C1, Cs, C0). word_chars(C, [], C):- \+ word_char(C). word_char(C):- 97 =< C, C =< 122. word_char(C):- 65 =< C, C =< 90. word_char(C):- 48 =< C, C =< 57. word_char(95). Lower case letter Upper case letter Number Underscore