1 / 11

A Basic Interactive Game

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!

lhylton
Download Presentation

A Basic Interactive Game

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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] ]).

  7. 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])

  8. 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([], _, _).

  9. 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’]

  10. 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

  11. 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

More Related