Course:CPSC312-2017-TicTacToe

From UBC Wiki
Jump to: navigation, search

TicTacToe

Authors: Johnson S, Will W, Eric C

What is the problem?

Implement tic tac toe (https://en.wikipedia.org/wiki/Tic-tac-toe) using Prolog. Our project will have 2 game modes: Player vs. Player and Player vs. AI. Players will be able to make valid moves, providing us with a new board state, which is then checked with the multiple win conditions. When one of these win conditions is met, the result given will be either playerWin or computerWin (users assume that once no moves can be made, the result is a tie). Some queries to try:

?- game(x,[3,3],[x,e,e,e,x,e,o,o,e],R,Result). This should result in playerWin.

?- gameWithAi([],[e,e,e,e,e,e,e,e,e],R,R1,R2). User puts in an empty move, which allows the computer to make the first move.

What is the something extra?

Player vs. AI gameplay in which the computer attempts to make winning moves as well as block the opponent. This is done by creating predicate cases for all potential "losing cases" that would cause the AI to block off the user's winning move. Similarly, we created predicate cases that would cause the computer to attempt a winning move.

What did we learn from doing this?

Link to our code: https://github.com/willywwang/CPSC312-Project-1-Tic-Tac-Toe

Logic and relation-based programming in Prolog can be very versatile. It almost seems as if you could code any program using Prolog with just a few simple built-in predicates and propositional logic statements. For small scale projects like this, it is suitable to code certain functions in a way that accounts for every possible outcome. In our case, the rule 'determinWin' had eight different logical cases in which a player could win in a 3x3 game board. However, this way of programming would definitely be inefficient for more complex problems, or even just a larger game board.

We implemented 'game' and 'gameWithAi', the latter being a lot slower due to it's backtracking nature. Also when tracing it, you could clearly see the difference between the two. For example. tracing ?-gameWithAi([2,3],[o,o,e,e,x,e,e,e,x],R,R1,R2). will produce constant fails/redo's while ?- game(o,[1,3],[o,o,e,e,x,e,e,e,x],R,R1). all succeed.