Download - 17-Othello

Transcript
Page 1: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 1

A Game of Othello Othello: popular board game (often known as

Reversi)

8x8 board, black and white tokens

Today, we will use it as a design example

Page 2: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 2

Othello: Rules and Game Play

The object of the game is to have the majority of your colour discs on the board at the end of the game

Rules Black places two black discs and White

places two white discs as shown in here. The game always begins with this setup.

A move consists of "outflanking" your opponent's disc(s), then flipping the outflanked disc(s) to your colour.

To outflank means to place a disc on the board so that your opponent's row (or rows) of disc(s) is bordered at each end by a disc of your colour. (A "row" may be made up of one or more discs).

Here's one example: White disc A was already in place on the board. The placement of white disc B outflanks the row of three black discs.

Page 3: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 3

Outflanking Example

White disc A was already in place on the board. The placement of white disc B outflanks the row of three black discs.

White flips the outflanked discs and now the row looks like this:

Page 4: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 4

Rules

Black always moves first.

If on your turn you cannot outflank and flip at least one opposing disc, your turn is forfeited and your opponent moves again. However, if a move is available to you, you may not forfeit your turn.

A disc may outflank any number of discs in one or more rows in any number of directions at the same time - horizontally, vertically or diagonally.

You may not skip over your own color disc to outflank an opposing disc.

Page 5: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 5

Rules

Discs may only be outflanked as a direct result of a move and must fall in the direct line of the disc placed down.

All discs outflanked in any one move must be flipped, even if it is to the player's advantage not to flip them at all.

Once a disc is placed on a square, it can never be moved to another square later in the game.

Page 6: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 6

Rules

When it is no longer possible for either player to move, the game is over. Discs are counted and the player with the majority of his or her colour discs on the board is the winner.

Page 7: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 7

Our Design Problem

Design an Othello Board and Gamekeeper. The gamekeeper will

Keep track of the score and state of the board Indicate whose move it is Indicate where legal moves can be made Accept and make a legal move

Flip all the discs who have been outflanked

Focus of today’s lecture Game engine and logic alone We will assume

Display device for an 8x8 board Input device which tells us where to move on an 8x8 board

Page 8: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 8

Global Picture of Our SystemCircuit in Each Square to:1. Keep State of the Square2. Compute whether move in

square is legalGame Controller

1. Global Game State

2. Orchestrates individual move logic

Page 9: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example - 9

Game Controller State Machine

FlipCurrentColor

LegalMove?

EnableMove

UpdateBoard

Flip CurrentColor

LegalMove?

Game Over

CurrentColor =White

Y

N

Y

N

Move selected’

Move selected

Page 10: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

10

Square FSM

Basic Functions Store state of square (Empty, White, Black) Report when move is legal Report when user moves into square Update state of square in response to a move

Square

Current Color

Move Enabled

Legal Move

Move Selected

Page 11: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

11

Square Finite State Machine

State=none

State=black State=white

Set s

tate

=bla

ck

Set state=white

Flip white

Flip black

Finite State Machine (Macro) per Othello

Square

Page 12: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

12

What’s a Legal Move?

Decided in each square

Square state must be empty

“Run” of colors Straight line of squares of one color bordered by a square of

the other color White run: line of white squares terminated by black square Black run: line of black squares terminated by white square Current run: line of squares of current color terminated by

square of other color

Legal move Square is empty and neighbor square is part of current run

Page 13: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

13

Key Consideration for Cell

Is it on a “run” in any direction? Originates a white (black) run:

Cell is white (black); and Neighbor in direction is black (white);

Continues a white (black) run Cell is white (black); and Neighbor in direction continues or originates a white (black)

run

Move to a square is legal if and only if Current Mover is white (black) Current State is empty Some neighbor continues or orginates a black (white)

run

Page 14: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

14

Originating and Continuing a Run

Begins a white run

Continues a white run

Begins a black run

Page 15: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

15

Legal Moves

Legal to move black

Legal to move white

How do we build a circuit to pick this up?

Page 16: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

16

Two Functions Per Color and direction

Remote Black: this square is white all the squares in some direction are white until we hit a

black Reverse black/white for Remote White

RemoteOrLocal Black: this square is black OR remoteBlack is true for this

square

Note that if a square is empty both remote and remoteOrLocal are false.

Page 17: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

17

Originating and Continuing a Run

remoteOrLocalWhite (all directions)

remoteOrLocalBlackWest

remoteBlackWest

remoteOrLocalWhite (all directions)remoteOrLocalBlackWestremoteBlackWest

remoteWhiteEast

remoteOrLocalBlack (all directions)

remoteOrLocalBlack East

Page 18: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

18

Cell circuit picture

OthelloCell

West Neighbor

Cell has remote blackCell has remote whiteCell has localOrRemote blackCell has localOrRemote whiteFlip blackFlip white

Local Inputs and Outputs (Connections to Adjoining cells)

Global Outputs

Cell color (black, white, none)Legal to move to current color

NW Neighbor

North Neighbor

NE Neighbor

East Neighbor

SWNeighbor

SouthNeighbor

SE Neighbor

Page 19: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

19

RemoteBlack (Continue White Run)

White run from SE

White run to NW

Cell Color = white Remote black to (NW neighbor)

Computation of remote (8x2, one to each neighbor x one per color)

SE Neighbor localOrRemote black

Page 20: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

20

RemoteOrLocal black (on white run or neighbor can start white run)

remoteBlack

Cell Color = black local orRemoteblack to (NW neighbor)

Computation of localOrRemote (8x2, one to each neighbor x one

per color)

Page 21: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

21

Cell circuit picture

OthelloCell

West Neighbor

Cell has remote blackCell has remote whiteCell has localOrRemote blackCell has localOrRemote whiteFlip blackFlip white

Local Inputs and Outputs (Connections to Adjoining cells)

Global Outputs

Cell color (black, white, none)Legal to move to current color

NW Neighbor

North Neighbor

NE Neighbor

East Neighbor

SWNeighbor

SouthNeighbor

SE Neighbor

On each link:

2 wires in

4 wires out

Page 22: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

22

Legal Move

Move to a square is legal if and only if Current Mover is white (black) Current State is empty Some neighbor continues or orginates a black (white)

run

Translate into our circuit Current Mover is white (black) Current State is empty For some direction: neighbor’s remoteWhite (black)

Page 23: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

23

Computation of Legal

Current Mover = black

NW remote black

Current Mover = white

NW remote white

8 inputs, one per neighbor

Cell color = none

Computation of legal

legal

Computation from one direction

Replicate here from each neighbor

Page 24: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

24

What’s the delay?

Worst case is on edge or corner At most 7 AND or OR gates on remote chain Computation of legal is ~6 gates (figure 3 gate delays for

8-input OR) Total delay is 13 gates

Note (Synchronous Mealy) we want to latch the output of legal!

What about the edges and corners? More later…

Page 25: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

25

How much logic in a cell?

8x2 AND gates for remote = 16

8x2 OR gates for localOrRemote = 16

3 gates for leaf of legal computation (8 leaves), so 8x3 = 24

7 OR gates + 1 AND gate for rest of legal computation

Total 64 gates/cell (so far)

Also need at least 2 latches for color + one for legal

More logic to come

Page 26: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

26

Doing The Move

This is easy One external select (keyed by button or multiplexer from

joystick – not our problem today) Select & legal (previously computed)

Still have to flip…

Page 27: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

27

FlippingMove

Flip

How can we build a flip function?

Page 28: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

28

Flipping

Move = black

Square selected

RemoteBlackNorth = True

RemoteBlackEast = True

Page 29: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

29

Key

Mover sends out a flip signal, with color and direction, to each neighbor 8x2 wires

Flip black if Flip black signal from one direction (SE); and Color is white; and remoteBlack is true in other direction (NW) FlipBlackNW = [FlipBlackFromSE AND Color=white AND

RemoteBlackNW Send FlipBlackNW out to NW neighbor OR all FlipBackDirections to get Direction

Page 30: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

30

Flip Calculation

SE Flip black

Flip black to NW Neighbor

Cell color = white

Cell mover = black

NW Remote black

Move

Computation of flip (8x2, one per neighbor x one per color)

Page 31: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

31

Computation of Next Cell State

Current Mover=black

Move

Flip black to each

neighbor

Next state = black (repeat for white)

Page 32: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

32

Cell circuit picture

OthelloCell

West Neighbor

Cell has remote blackCell has remote whiteCell has localOrRemote blackCell has localOrRemote whiteFlip blackFlip white

Local Inputs and Outputs (Connections to Adjoining cells)

Global Outputs

Cell color (black, white, none)Legal to move to current color

NW Neighbor

North Neighbor

NE Neighbor

East Neighbor

SWNeighbor

SouthNeighbor

SE Neighbor

On each link:

4 wires in

6 wires out

Page 33: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

33

Gate Delay Calculation for Clip

7x2 gates to propagate = 14 gate delays

But need to consider the remote chain! Adds another 7 delays (from slide 24)

3 gate delays through 8-way OR + 1 gate delay to comput next state

Worst-case is 25 gate delays

Can reduce to 18 by latching remote signals computed in legal-move phase

Page 34: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

34

How Many Gates

2 gates/direction-color x 2 colors x 8 directions = 32 gates

2 gates for current move (one black, one white)

8 gates/color for upper end of next-state tree = 16

Total 50 gates

Add to 64 from slide 25

Total 114 gates/cell

64 cells = 7296 gates for design

But what about the corners and edges?

Page 35: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

35

Two choices

Cell design assumes neighbors in all directions Note true at edges and corners

One: special-case cells on the edge Now have 9 different types of cell!

1/49 cells in center of board (type 1) – cell we’ve designed 4/7 cells each on each edge (types 2-5) 4/7 cells each for each corner (types 6-9)

Note each specialty type is simpler than general case, but…

9 cell types to design!

Two: Surround the board with shadow squares Less efficient, but much simpler

Page 36: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

36

Revised Board

Shadow Cells

Normal Cells we’ve designed

Page 37: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

37

Shadow Cells

Always empty (no next-state logic)

Can’t be selected

localOrRemote, local = false for all colors and directions

Just a small collection of 6 wires connected to ground

Key advantage: only two cell types, one trivial

Disadvantage: lose a little efficiency from specialization of edge, corner cells Always worth it! Let a synthesizer optimize away the

constants

Page 38: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

38

Timing Diagram for Each Move

Set Current MoverLatch Legal Moves,

Remote ValuesSelect Square

Update Current State of All

Squares

Page 39: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

39

Logic for controller

Start

Make No Legal Move ControllerTransition

Read Move From

Controller

Update Board

Make Legal Move

Controller Transition

No Le

gal

Mov

e

Legal

Moves

Move

is

Leg

al

No

Lega

l M

ove

Page 40: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

40

Game Control Logic

Move=white Move=black

Move=white(no black

move)

Move=black(no white

move)

Game Over

Move white

No

legal m

ove

No legal m

ove

No legal

move

No

legal M

ove

Move blackMove black

Mov

eW

hite

Othello Game Controller FSM

Page 41: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

41

Key Steps to Making the Design Work

Software implementation first! I did it in Smalltalk

Tastes Differ, but… OO programming model tends to fit circuits well Map each object onto a circuit

Variables tend to map to latches Functions tend to map to logic circuits

Unit test, unit test, unit test! Design test circuits for each component Synthesize test circuits as part of the design

Audit, audit, audit! Pin out internal state where possible E.g., Legal should be displayed visually

Page 42: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

42

Timing Bugs

Nastiest, hardest to catch

Two common examples: Read-Before-Wirte and Write-Before-Read

Read-Before-Write Reader reads sequential value before writer has updated it Acts on old value E.G. no legal move but controller sees legal move from

previous value

Write-Before-Read Writer writes before old value has been acted on Reader doesn’t act on value

Page 43: 17-Othello

CS 150 – Spring 2008 – Lec #22: Othello Design Example -

43

Two Solution

Dirty/Clean bits Writer sets dirty bit, reader cleans it when read Writer checks dirty bits clean before writing, reader

checks set before reading Error raised if condition not met

FIFO Queues Writer writes, reader reads Decouples send/receive asymmetries by a cycle or so Can become event-driven: Reader only reads when new

value Still have to check overflow, etc Automatically implemented in V++