1 / 10

A Case Study

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?

barb
Download Presentation

A Case Study

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 Case Study Some Advice on How to Go about Solving Problems in Prolog A Program to Play Connect-4 Game

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

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

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

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

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

  7. 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 access the board

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

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

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

More Related