Code:Oware

From Predictive Chemistry
Jump to: navigation, search

Here is the code we're building in Intro. Scientific Computing, 2016 for playing Oware. There is a simple rule summary and a few example game openings at this site.

<source lang="python">

  1. objective: create a game of Owari
  2. ASCII Art: f e d c b a <-- (fed) # score[1]
  3. A B C D E F --> (ABC) # score[0]
  4. 0 1 2 3 4 5
  1. create a new board

def new_board(): # () -> ([int], [int], [int])

       ABC = [4]*6
       fed = [4]*6
       score = [0, 0]
       return ABC, fed, score
  1. move -- remove seeds from starting location
  2. and distribute one after the other counterclockwise
  3. Implements the pattern: (start) ABC fed (score) -> ABC fed (score)
  4. type = (start : int), board -> board
  5. (position F) [4,4,4,4,4,4] [4,4,4,4,4,4] (score) -> [4,4,4,4,4,0] [4,4,5,5,5,5] (score)
  6. (position e) [4,4,4,4,4,4] [4,4,4,4,4,4] (score) -> [5,5,5,4,4,4] [5,0,4,4,4,4] (score)
  7. (position A) [0,1,2,3,4,5] [2,2,2,2,2,2] (score) -> [0,1,2,3,4,5] [2,2,2,2,2,2] (score)
  8. (position a) [0,1,2,3,4,5] [2,2,2,2,2,2] (score) -> [0,1,2,3,4,5] [2,2,2,3,3,0] (score)
  9. move : int -> board -> board

def move(start, ABC, fed, score):

       # move pieces
       # compute board location
       board, pos = compute_board_location(start, ABC, fed)
       n = board[pos] # copy number of seeds there
       board[pos] = 0
       # loop over n
       for i in range(n): # 0, 1, ..., n-1
               update_pos = (start + i + 1) % 12
               board, loc = compute_board_location(update_pos, ABC, fed)
               # print i, loc, update_pos # trace the loop execution for debugging
               board[loc] += 1 # same as board[] = board[] + 1 
       print("Remember to check for capture")
       return ABC, fed, score
  1. Helper function to deal with our 0-11 based numbering system.

def compute_board_location(start, ABC, fed):

       if start < 6:
               board = ABC
               pos = start
       else:
               board = fed
               pos = 11 - start
       return board, pos


  1. print_board : ([int], [int], [int]) -> ()

def print_board(ABC, fed, score):

       print "Current board:"
       print "f e d c b a"
       print fed, score[1]
       print ABC, score[0]
       print "A B C D E F"

ABC, fed, score = new_board() print_board(ABC, fed, score) </source>