Skip to content

Instantly share code, notes, and snippets.

@JamesJinPark
Last active August 29, 2015 14:06
Show Gist options
  • Save JamesJinPark/668c14445fa1523cbc8d to your computer and use it in GitHub Desktop.
Save JamesJinPark/668c14445fa1523cbc8d to your computer and use it in GitHub Desktop.
Three_Musketeers
# The Three Musketeers Game
# by James Park and Joo Kim.
# In all methods,
# A 'location' is a two-tuple of integers, each in the range 0 to 4.
# The first integer is the row number, the second is the column number.
# A 'direction' is one of the strings "up", "down", "left", or "right".
# A 'board' is a list of 5 lists, each containing 5 strings: "M", "R", or "-".
# "M" = Musketeer, "R" = Cardinal Richleau's man, "-" = empty.
# Each list of 5 strings is a "row"
# A 'player' is one of the strings "M" or "R" (or sometimes "-").
#
# For brevity, Cardinal Richleau's men are referred to as "enemy".
# 'pass' is a no-nothing Python statement. Replace it with actual code.
def create_board():
global board
"""Creates the initial Three Musketeers board. 'M' represents
a Musketeer, 'R' represents one of Cardinal Richleau's men,
and '-' denotes an empty space."""
m = 'M'
r = 'R'
board = [ [r, r, r, r, m],
[r, r, r, r, r],
[r, r, m, r, r],
[r, r, r, r, r],
[m, r, r, r, r] ]
def set_board(new_board):
"""Replaces the global board with new_board."""
global board
board = new_board
def get_board():
"""Just returns the board. Possibly useful for unit tests."""
return board
def string_to_location(s):
"""Given a two-character string (such as 'A5') return the designated
location as a 2-tuple (such as (0, 4))."""
assert s[0] >= 'A' and s[0] <= 'E'
assert s[1] >= '1' and s[1] <= '5'
row = ['A', 'B', 'C', 'D', 'E']
column = [1, 2, 3, 4, 5]
for i in range(0, len(row)):
if s[0] == row[i]:
break
for y in range (0, len(column)):
if int(s[1]) == column[y]:
break
return (i, y)
def location_to_string(location):
"""Return the string representation of a location."""
assert location[0] >= 0 and location[0] <= 4
assert location[1] >= 0 and location[1] <= 4
row = ['A', 'B', 'C', 'D', 'E']
column = ['1', '2', '3', '4', '5']
for i in range(0, len(row)):
if (location[0] == i):
break
for y in range (0, len(column)):
if (location[1] == y):
break
return (row[i] + column[y])
def at(location):
"""Returns the contents of the board at the given location."""
return board[location[0]][location[1]]
def all_locations():
"""Returns a list of all 25 locations on the board."""
create_board()
return_list = []
for i in range(0, len(board)):
for j in range(0, len(board[i])):
return_list.append((i, j))
return return_list
def adjacent_location(location, direction):
"""Return the location next to the given one, in the given direction.
Does not check if the location returned is legal on a 5x5 board."""
(row, column) = location
if(direction == 'left'):
location_left = (location[0], location[1] - 1)
return location_left
if(direction == 'right'):
location_right = (location[0], location[1] + 1)
return location_right
if(direction == 'up'):
location_up = (location[0] - 1, location[1])
return location_up
if(direction == 'down'):
location_down = (location[0] + 1, location[1])
return location_down
def is_legal_move_by_musketeer(location, direction):
"""Tests if the Musketeer at the location can move in the direction."""
assert at(location) == 'M'
if(is_within_board(location, direction)):
if(direction == 'right' and board[location[0]][location[1] + 1] == 'R'):
return True
if(direction == 'left' and board[location[0]][location[1] - 1] == 'R'):
return True
if(direction == 'up' and board[location[0] - 1][location[1]] == 'R'):
return True
if(direction == 'down' and board[location[0] + 1][location[1]] == 'R'):
return True
else:
return False
def is_legal_move_by_enemy(location, direction):
"""Tests if the enemy at the location can move in the direction."""
assert at(location) == 'R'
if(is_within_board(location, direction)):
if(direction == 'right' and board[location[0]][location[1] + 1] == '-'):
return True
if(direction == 'left' and board[location[0]][location[1] - 1] == '-'):
return True
if(direction == 'up' and board[location[0] - 1][location[1]] == '-'):
return True
if(direction == 'down' and board[location[0] + 1][location[1]] == '-'):
return True
else:
return False
def is_legal_move(location, direction):
"""Tests whether it is legal to move the piece at the location
in the given direction."""
if at(location) == 'M':
return is_legal_move_by_musketeer(location, direction)
if at(location) == 'R':
return is_legal_move_by_enemy(location, direction)
def has_some_legal_move_somewhere(who):
"""Tests whether a legal move exists for player "who" (which must
be either 'M' or 'R'). Does not provide any information on where
the legal move is."""
#1st: determine 'who'
#2nd: scan board to find pieces (while loop)
#3rd: after a piece is found, ask "is_legal_move_by_musketeer" and "is_legal_move_by_enemy" (for loop)
#4th: if true, for loop stops, while loop breaks, and return true.
#5th: if false, for loop stops, while loop breaks, and return false.
#6th: ask if this is correct.
direction_list = ['right', 'left', 'up', 'down']
if who == 'M':
while True:
for i in range(0, len(board)):
for j in range(0, len(board[i])):
if (board[i][j] == 'M'):
for k in range(0, len(direction_list)):
if(is_legal_move_by_musketeer((i,j), direction_list[k]) == True):
return is_legal_move_by_musketeer((i,j), direction_list[k])
break
if who == 'R':
while True:
for i in range(0, len(board)):
for j in range(0, len(board[i])):
if (board[i][j] == 'R'):
for k in range(0, len(direction_list)):
if(is_legal_move_by_enemy((i,j),direction_list[k]) == True):
return is_legal_move_by_enemy((i,j),direction_list[k])
break
def possible_moves_from(location):
"""Returns a list of directions ('left', etc.) in which it is legal
for the player at location to move. If there is no player at
location, returns the empty list, []."""
direction_list = ['right', 'left', 'up', 'down']
"""
possible_moves = []
for i in range(0, len(board)):
for j in range(0, len(board[i])):
if at(location) == 'M':
for k in range(0, len(direction_list)):
if(is_legal_move_by_musketeer((i, j), direction_list[k]) == True):
possible_moves.append(direction_list[k])
if at(location) == 'R':
for k in range (0, len(direction_list)):
if (is_legal_move_by_enemy((i, j), direction_list[k]) == True):
possible_moves.append(direction_list[k])
return possible_moves()
"""
#I give up...
save_possible_moves = []
if(at(location) == 'M'):
if(is_within_board(location, 'left') == True):
if(board[location[0]][location[1] - 1] == 'R'):
save_possible_moves.append('left')
if(is_within_board(location, 'right') == True):
if(board[location[0]][location[1] + 1] == 'R'):
save_possible_moves.append('right')
if(is_within_board(location, 'up') == True):
if(board[location[0] - 1][location[1]] == 'R'):
save_possible_moves.append('up')
if(is_within_board(location, 'down') == True):
if(board[location[0] + 1][location[1]] == 'R'):
save_possible_moves.append('down')
if(at(location) == 'R'):
if(is_within_board(location, 'left') == True):
if(board[location[0]][location[1] - 1] == '-'):
save_possible_moves.append('left')
if(is_within_board(location, 'right') == True):
if(board[location[0]][location[1] + 1] == '-'):
save_possible_moves.append('right')
if(is_within_board(location, 'up') == True):
if(board[location[0] - 1][location[1]] == '-'):
save_possible_moves.append('up')
if(is_within_board(location, 'down') == True):
if(board[location[0] + 1][location[1]] == '-'):
save_possible_moves.append('down')
if(at(location) == '-'):
return []
return save_possible_moves
def can_move_piece_at(location):
"""Tests whether the player at the location has at least one move available."""
if possible_moves_from(location) == []:
return False
else:
return True
def is_legal_location(location):
"""Tests if the location is legal on a 5x5 board."""
#Use "all_locations". If the inputted location is not in the "all_locations" list then, not legal.
if location in all_locations():
return True
else:
return False
def is_within_board(location, direction):
"""Tests if the move stays within the boundaries of the board."""
if(direction == 'right'):
if(location[1] < 4):
return True
else:
return False
if(direction == 'left'):
if(location[1] > 0):
return True
else:
return False
if(direction == 'up'):
if(location[0] > 0):
return True
else:
return False
if(direction == 'down'):
if(location[0] < 4):
return True
else:
return False
def all_possible_moves_for(player):
"""Returns every possible move for the player ('M' or 'R') as a list
(location, direction) tuples."""
"""
if player == 'M':
#scan board to find M pieces.
#tests to see what the legal moves are and then put them in empty list.
#return the contents of the list.
#use possible_moves function.
if player == 'R':
"""
pass
def make_move(location, direction):
"""Moves the piece in location in the indicated direction."""
if is_legal_move(location, direction):
if at(location) =='M':
board[adjacent_location(location, direction)[0]][adjacent_location(location, direction)[1]] = 'M'
board[location[0]][location[1]] = '-'
if at(location) == 'R':
board[adjacent_location(location, direction)[0]][adjacent_location(location, direction)[1]] = 'R'
board[location[0]][location[1]] = '-'
return board[adjacent_location(location, direction)[0]][adjacent_location(location, direction)[1]]
def choose_computer_move(who):
"""The computer chooses a move for a Musketeer (who = 'M') or an
enemy (who = 'R') and returns it as the tuple (location, direction),
where a location is a (row, column) tuple as usual."""
while True
computer_move = random.randint ('left', 'right', 'up', 'down')
def is_enemy_win():
"""Returns True if all 3 Musketeers are in the same row or column."""
#---------- Communicating with the user ----------
def print_board():
print " 1 2 3 4 5"
print " ---------------"
ch = "A"
for i in range(0, 5):
print ch, "|",
for j in range(0, 5):
print board[i][j] + " ",
print
ch = chr(ord(ch) + 1)
print
def print_instructions():
print
print """To make a move, enter the location of the piece you want to move,
and the direction you want it to move. Locations are indicated as a
letter (A, B, C, D, or E) followed by an integer (1, 2, 3, 4, or 5).
Directions are indicated as left, right, up, or down (or simply L, R,
U, or D). For example, to move the Musketeer from the top right-hand
corner to the row below, enter 'A5 left' (without quotes).
For convenience in typing, you may use lowercase letters."""
print
def choose_users_side():
"""Returns 'M' if user is playing Musketeers, 'R' otherwise."""
user = ""
while user != 'M' and user != 'R':
answer = raw_input("Would you like to play Musketeer (M) or enemy (R)? ")
answer = answer.strip()
if answer != "":
user = answer.upper()[0]
return user
def get_users_move():
"""Gets a legal move from the user, and returns it as a
(location, direction) tuple."""
directions = {'L':'left', 'R':'right', 'U':'up', 'D':'down'}
move = raw_input("Your move? ").upper().replace(' ', '')
if move[0] in 'ABCDE' and move[1] in '12345' and move[2] in 'LRUD':
location = string_to_location(move[0:2])
direction = directions[move[2]]
if is_legal_move(location, direction):
return (location, direction)
print "Illegal move--'" + move + "'"
print_instructions()
return get_users_move()
def move_musketeer(users_side):
"""Gets the Musketeer's move (from either the user or the computer)
and makes it."""
if users_side == 'M':
(location, direction) = get_users_move()
if at(location) == 'M':
if is_legal_move(location, direction):
make_move(location, direction)
describe_move("Musketeer", location, direction)
else:
print "You can't move there!"
return move_musketeer(users_side)
else: # Computer plays Musketeer
(location, direction) = choose_computer_move('M')
make_move(location, direction)
describe_move("Musketeer", location, direction)
def move_enemy(users_side):
"""Gets the enemy's move (from either the user or the computer)
and makes it."""
if users_side == 'R':
(location, direction) = get_users_move()
if at(location) == 'R':
if is_legal_move(location, direction):
make_move(location, direction)
describe_move("Enemy", location, direction)
else:
print "You can't move there!"
return move_enemy(users_side)
else: # Computer plays enemy
(location, direction) = choose_computer_move('R')
make_move(location, direction)
describe_move("Enemy", location, direction)
return board
def describe_move(who, location, direction):
"""Prints a sentence describing the given move."""
new_location = adjacent_location(location, direction)
print who, 'moves', direction, 'from',\
location_to_string(location), 'to',\
location_to_string(new_location) + ".\n"
def start():
"""Plays the Three Musketeers Game."""
users_side = choose_users_side()
board = create_board()
print_instructions()
print_board()
while True:
if has_some_legal_move_somewhere('M'):
board = move_musketeer(users_side)
print_board()
if is_enemy_win():
print "Cardinal Richleau's men win!"
break
else:
print "The Musketeers win!"
break
if has_some_legal_move_somewhere('R'):
board = move_enemy(users_side)
print_board()
else:
print "The Musketeers win!"
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment