# A Case Study  Some Advice on How to Go about Solving Problems in Prolog  A Program to Play Connect-4 Game.

## Presentation on theme: "A Case Study  Some Advice on How to Go about Solving Problems in Prolog  A Program to Play Connect-4 Game."— Presentation transcript:

A Case Study  Some Advice on How to Go about Solving Problems in Prolog  A Program to Play Connect-4 Game

Some Advice on How to Go about Solving Problems in Prolog 1. Have a very clear understanding of the problem what are the initial conditions? what is the goal state? what are the rules and the constraints? 2. Find suitable data structures to represent the problem states

Some Advice on How to Go about Solving Problems in Prolog (cont) 3. Try to abstract the problem can it be divided into several sub-problems? is it similar to some problem that we already know how to solve? 4. When using recursion, to think about base case: how to terminate the program general case: how to define N from N-1 5. Write the code in a top-down fashion

Connect-4 Game Two players A 6 x 7 vertical board Each player drops one of his/her counters into any columns in turns The first player to get four of his/her counters in a row (vertically, horizontally or diagonally) wins the game.

Find suitable data structures to represent the problem states 6x7 board – use a list of lists [[_,_,_,_,_,_,_], [_,_,_,_,_,_,_], [_,_,_,_,_,_,_]] Variables represent empty slots o represents computer’s counters. x represents human player's counters.

Find suitable data structures to represent the problem states An optional consideration – To keep track of every step of a game, we can use a list to remember all the moves and the states of boards: [init_board, move1, board1, move2, board2, …] board is described as the above move is the form of [Who, Move]

How to access the board get_col(1, Board, Col):- Board = [[X6,_,_,_,_,_,_], [X5,_,_,_,_,_,_], [X4,_,_,_,_,_,_], [X3,_,_,_,_,_,_], [X2,_,_,_,_,_,_], [X1,_,_,_,_,_,_]], Col = [X1,X2,X3,X4,X5,X6]. get_row(1, Board, Row):- Board = [Row|_]. get_diagonal(3, Board, D):- Board = [[X6,_,_,_,_,_,_], [_,X5,_,_,_,_,_], [_,_,X4,_,_,_,_], [_,_,_,X3,_,_,_], [_,_,_,_,X2,_,_], [_,_,_,_,_,X1,_]], D = [X1,X2,X3,X4,X5,X6]. get_row(2, Board, Row):- Board = [_,Row|_].

How to update the board Q. How to add a new counter to the nth column of a board? A.Go through the nth column (from bottom), add the counter at the first empty slot. x add(C, [H|_]):- x x o -> x x o var(H), H = C. o x o o o x o o add(C, [H|T]):- x o x x o x o x x o nonvar(H), add(C, T).

The main loop for playing the game One termination case – If we can find a connect 4, then game is over. Two non-terminal cases – if it is human player’s turn, get a move from the player make the move, let computer carry on if it is computer’s turn computer selects a move make the move, let human player carry on

The main loop for playing the game c4_loop(Turn, CurrentBoard):- check4(CurrentBoard). % found a connect-4, game is over print_result(Turn). c4_loop(x, CurrentBoard):- % it is human player's turn get_move_fm_human(CurrentBoard, Move), move(x, Move, CurrentBoard, NewBoard), print_move('You', x, Move), print_board(NewBoard), c4_loop(o, NewBoard). c4_loop(o, CurrentBoard):- % otherwise computer_selects_a_move(CurrentBoard, Move), move(o, Move, CurrentBoard, NewBoard), print_move('I', o, Move), print_board(NewBoard), c4_loop(x, NewBoard).

Download ppt "A Case Study  Some Advice on How to Go about Solving Problems in Prolog  A Program to Play Connect-4 Game."

Similar presentations