Difference between revisions of "Code:Oware"
From Predictive Chemistry
(Created page with "Here is the code we're building in Intro. Scientific Computing, 2016 for playing [https://en.wikipedia.org/wiki/Oware Oware]. There is a simple rule summary and a few example ...") |
m |
||
(3 intermediate revisions by the same user not shown) | |||
Line 15: | Line 15: | ||
return ABC, fed, score |
return ABC, fed, score |
||
⚫ | |||
+ | # move -- remove seeds from starting location |
||
⚫ | |||
+ | # 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) |
||
⚫ | |||
+ | # (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) |
||
⚫ | |||
+ | # |
||
+ | # move : int -> board -> board |
||
⚫ | |||
⚫ | |||
+ | # 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 |
||
+ | |||
⚫ | |||
+ | |||
⚫ | |||
+ | |||
+ | # 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]) -> () |
||
Line 34: | Line 62: | ||
print "A B C D E F" |
print "A B C D E F" |
||
− | ABC, fed, score = |
+ | ABC, fed, score = new_board() |
print_board(ABC, fed, score) |
print_board(ABC, fed, score) |
||
</source> |
</source> |
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">
- objective: create a game of Owari
- ASCII Art: f e d c b a <-- (fed) # score[1]
- A B C D E F --> (ABC) # score[0]
- 0 1 2 3 4 5
- create a new board
def new_board(): # () -> ([int], [int], [int])
ABC = [4]*6 fed = [4]*6 score = [0, 0] return ABC, fed, score
- move -- remove seeds from starting location
- and distribute one after the other counterclockwise
- Implements the pattern: (start) ABC fed (score) -> ABC fed (score)
- type = (start : int), board -> board
- (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)
- (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)
- (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)
- (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)
- 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]) -> ()
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>