A Case Study - PowerPoint PPT Presentation

About This Presentation
Title:

A Case Study

Description:

A Case Study. Some Advice on How to Go about Solving Problems in Prolog ... his/her counters in a row (vertically, horizontally or diagonally) wins the game. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 11
Provided by: rong94
Category:
Tags: case | diagonally | study

less

Transcript and Presenter's Notes

Title: A Case Study


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 6x7 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 computers 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
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_.

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.
  • x add(C, H_)-
  • x x o -gt x x o var(H), H C.
  • o x o o o x o o add(C, HT)-
  • x o x x o x o x x o nonvar(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 players turn,
  • get a move from the player
  • make the move, let computer carry on
  • if it is computers 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).
Write a Comment
User Comments (0)
About PowerShow.com