Difference between revisions of "Code:Oware"

From Predictive Chemistry
Jump to: navigation, search
m
 
(2 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
return ABC, fed, score
 
return ABC, fed, score
   
# move pieces (TODO)
 
  +
# move -- remove seeds from starting location
def move(ABC, fed, start):
 
  +
# and distribute one after the other counterclockwise
# player 1, assume start is at position F
 
  +
#
ABC[5] = 0 #assign old position to zero
 
  +
# Implements the pattern: (start) ABC fed (score) -> ABC fed (score)
fed[5] = fed[5] + 1 # distribute seeds
 
  +
# type = (start : int), board -> board
fed[4] = fed[4] + 1 # distribute seeds
 
  +
# (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)
fed[3] = fed[3] + 1 # distribute seeds
 
  +
# (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)
fed[2] = fed[2] + 1 # distribute seeds
 
  +
# (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)
# TODO: check for capture
 
  +
# (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)
return 0, 0 # return score update
 
  +
#
  +
# 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
  +
  +
# 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
  +
   
 
# print_board : ([int], [int], [int]) -> ()
 
# print_board : ([int], [int], [int]) -> ()

Latest revision as of 11:55, 1 February 2016

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>