Pages

Monday, May 19, 2014

TicTacToe Game using SWI-Prolog

TicTacToe Game using SWI-Prolog

 

TicTacToe Game
Using SWI-Prolog
AI language

Coding help by :
S. Tanimoto
and others

DL:
http://www.mediafire.com/download/65377ioodfkkyjq/TicTacToe.rar


Video :


Intro Muisc :
    Music by: Lino Rise
    Title: Beyond R.( Teaser Version )
    Source: www.linorise.com

15 comments:

  1. Hello,
    I've been looking at your code (currently learning ProLog). I understand all of the code, except the end (which seems to be the important part).

    can_x_win(Brd) :- omove(Brd, x, NewBrd), win(NewBrd, x).

    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd),
    win(NewBrd, o),!.
    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd),
    not(can_x_win(NewBrd)).
    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd).
    oplay(Brd,NewBrd) :-
    not(member(a,Brd)),!,
    write('Game Ended without Winner!'), nl,
    NewBrd = Brd.

    I'm unsure how to understand that. I saw that every time I play 1, it always play 2, so the IA is pretty basic (which is not a problem for learning). How does the omove work?

    Thanks

    ReplyDelete
    Replies
    1. you are right this AI is very simple, it's unable to learn (learning from failures), however omove is simply make few check in order to play
      first check is (if O played in a position and win)
      second check is (if O played where you wont be able to win)
      third check is (if O played in a position that O will be near to win)
      and lastly O is just play without checking anything which properly happen at the first play

      Delete
  2. How did you create the executable file?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. it was build by my supervisor at that time, he asked me to share

      Delete
  3. Hi,
    i have a problem understanding this part of the code :
    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd),
    not(can_x_win(NewBrd)).

    why you put not before can_x_win ? and how the AI know the right position where the user wont be able to win?

    ReplyDelete
    Replies
    1. in the part the AI, will search if it can make a move to stop x from winning. that's why I put "not"
      and the AI knows that by studying the blocks at the beginning of the code.

      Delete
  4. السلام عليكم ورحمه الله وبركاته
    جزاك الله خيراً على الشروحات المفيده
    آخي الفاضل لو تفضلت بالمساعده اكون شاكره جدا لك
    عندي اسايمنت عن
    Animal Identifier و Working Memory و SWI Prolog
    آذا تستطيع مساعدتي في شرح مالمطلوب او كيف اقوم بالحل او ان كان لديك مراجع استطيع ان افهم منها المطلوب اكون لك من الشاكرين ..

    آذا تستطيع مساعدتي سا آرسل لك ال
    PDF
    الموجود فيه الاسايمنت و المحاضره

    شكرا جزيلا
    وفقك الله

    ReplyDelete
  5. Good afternoon, my friend, do you have any complete explanation of this code?

    ReplyDelete
    Replies
    1. Hi,

      I am afraid, I don't. Sorry.
      However, I will do my best to remember the purpose of a specific lines if you got a trouble in some.

      Delete
  6. No problems, man! I have trouble understanding two parts, my friend.
    The first is: What change would be made so that the human player could never win, that is, there will always be a draw or victory of player 2.
    The second is:
    can_x_win(Brd) :- omove(Brd, x, NewBrd), win(NewBrd, x).

    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd),
    win(NewBrd, o),!.
    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd),
    not(can_x_win(NewBrd)).
    oplay(Brd,NewBrd) :-
    omove(Brd, o, NewBrd).
    oplay(Brd,NewBrd) :-
    not(member(a,Brd)),!,
    write('Game Ended without Winner!'), nl,
    NewBrd = Brd.

    ReplyDelete
    Replies
    1. Regarding your Q1:
      the answer is allow player 2 (AI player) to learn from you. this nowadays called machine learning. at the beginning the AI player will be easy to beat. However, once it has enough data (mistakes to learn from) it will become difficult to win which ends either in draw or AI wins.

      Q2:
      this part of the code checks the next move for the AI. so first if AI can win in one single move then AI will wins. if not, then AI will see if it can block you from winning in the next step. If not, AI will make a move. Else, AI will consider this game as a draw.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. hello Motaz, can u help me to tranlate your program game tictactoe become a program visual prolog? i have a last task, please help me :(

    ReplyDelete