Difference between revisions of "Code:Oware"
From Predictive Chemistry
m |
|||
(One intermediate revision by the same user not shown) | |||
Line 34: | Line 34: | ||
# loop over n |
# loop over n |
||
for i in range(n): # 0, 1, ..., n-1 |
for i in range(n): # 0, 1, ..., n-1 |
||
− | update_pos = ( |
+ | update_pos = (start + i + 1) % 12 |
board, loc = compute_board_location(update_pos, ABC, fed) |
board, loc = compute_board_location(update_pos, ABC, fed) |
||
− | print i, loc, update_pos |
+ | # print i, loc, update_pos # trace the loop execution for debugging |
board[loc] += 1 # same as board[] = board[] + 1 |
board[loc] += 1 # same as board[] = board[] + 1 |
||
print("Remember to check for capture") |
print("Remember to check for capture") |
||
− | return ABC, |
+ | return ABC, fed, score |
# Helper function to deal with our 0-11 based numbering system. |
# Helper function to deal with our 0-11 based numbering system. |
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>