Created
February 14, 2016 20:22
-
-
Save Ostapp/3f57943cfb6ee7910890 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_table(): | |
lookup = ['-', 'X', 'O'] | |
for row in T: | |
print ' '.join([lookup[cell] for cell in row]) | |
def move(player, row, column): | |
global next_player | |
row -= 1 | |
column -= 1 | |
assert player == next_player, 'player not valid' | |
assert row in range(table_size), 'row out of range' | |
assert column in range(table_size), 'column out of range' | |
assert T[row][column] == 0, 'avoid illegal move' | |
T[row][column] = player # writing to global variable, why is not a problem, | |
lookup_next_player = { | |
1: 2, | |
2: 1 | |
} | |
next_player = lookup_next_player[next_player] | |
print_table() | |
state = game_state() | |
if state in [1, 2]: | |
print 'winner: {}'.format(state) | |
if state == 3: | |
print 'draw' | |
def game_state(): | |
directions = [(1, 0), (0, 1), (1, 1), (-1, 1)] | |
can_continue = False | |
for row in range(table_size): | |
for column in range(table_size): | |
for dir_x, dir_y in directions: | |
p = check_cell(row, column, dir_x, dir_y) | |
if p: | |
return p | |
if T[row][column] == 0: | |
can_continue = True | |
if can_continue: | |
return 0 # can continue | |
return 3 # draw | |
def check_cell(row, column, dir_x, dir_y): | |
cell = T[row][column] | |
if cell == 0: | |
return 0 | |
# if not (0 <= column + dir_x * winning_size <= table_size): | |
# return 0 | |
if column + dir_x * winning_size not in range(table_size): | |
return 0 | |
if row + dir_y * winning_size not in range(table_size): | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + (i * dir_y)][column + (i * dir_x)] != cell: | |
return 0 | |
return cell | |
table_size = 4 | |
winning_size = 2 | |
T = [[0] * table_size for _ in range(table_size)] | |
next_player = 1 | |
print_table() | |
move(1, 2, 2) | |
move(2, 3, 2) | |
move(1, 3, 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_table(): | |
lookup = ['-', 'X', 'O'] | |
for row in T: | |
print ' '.join([lookup[cell] for cell in row]) | |
def move(player, row, column): | |
global next_player | |
row -= 1 | |
column -= 1 | |
assert player == next_player, 'player not valid' | |
assert row in range(table_size), 'row out of range' | |
assert column in range(table_size), 'column out of range' | |
assert T[row][column] == 0, 'avoid illegal move' | |
T[row][column] = player # writing to global variable, why is not a problem, | |
lookup_next_player = { | |
1: 2, | |
2: 1 | |
} | |
next_player = lookup_next_player[next_player] | |
print_table() | |
state = game_state() | |
if state in [1, 2]: | |
print 'winner: {}'.format(state) | |
if state == 3: | |
print 'draw' | |
def game_state(): | |
can_continue = False | |
for row in range(table_size): | |
for column in range(table_size): | |
p = check_state_horizontal_start(row, column) | |
if p: | |
return p | |
p = check_state_vertical_start(row, column) | |
if p: | |
return p | |
p = check_state_diag_se_start(row, column) | |
if p: | |
return p | |
p = check_state_diag_sw_start(row, column) | |
if p: | |
return p | |
if T[row][column] == 0: | |
can_continue = True | |
if can_continue: | |
return 0 # can continue | |
return 3 # draw | |
def check_state_horizontal_start(row, column): | |
cell = T[row][column] | |
if cell == 0: | |
return 0 | |
if column + winning_size > table_size: | |
return 0 | |
for i in range(1, winning_size): | |
if T[row][column + i] != cell: | |
return 0 | |
return cell | |
def check_state_vertical_start(row, column): | |
cell = T[row][column] | |
if row + winning_size > table_size: | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + i][column] != cell: | |
return 0 | |
return cell | |
def check_state_diag_se_start(row, column): | |
cell = T[row][column] | |
if row + winning_size > table_size or \ | |
column + winning_size > table_size: | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + i][column + i] != cell: | |
return 0 | |
return cell | |
def check_state_diag_sw_start(row, column): | |
cell = T[row][column] | |
if row + winning_size > table_size or \ | |
column - winning_size < 0: | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + i][column - i] != cell: | |
return 0 | |
return cell | |
table_size = 4 | |
winning_size = 2 | |
T = [[0] * table_size for _ in range(table_size)] | |
next_player = 1 | |
print_table() | |
move(1, 3, 3) | |
move(2, 1, 1) | |
move(1, 3, 4) | |
move(2, 2, 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def print_table(): | |
lookup = ['-', 'X', 'O'] | |
for row in T: | |
print ' '.join([lookup[cell] for cell in row]) | |
def move(player, row, column): | |
global next_player | |
assert player == next_player, 'player not valid' | |
assert row in range(table_size), 'row out of range' | |
assert column in range(table_size), 'column out of range' | |
assert T[row][column] == 0, 'avoid illegal move' | |
T[row][column] = player # writing to global variable, why is not a problem, | |
lookup_next_player = { | |
1: 2, | |
2: 1 | |
} | |
next_player = lookup_next_player[next_player] | |
print_table() | |
state = game_state() | |
if state in [1, 2]: | |
print 'winner: {}'.format(state) | |
if state == 3: | |
print 'draw' | |
def game_state(): | |
directions = [(1, 0), (0, 1), (1, 1), (-1, 1)] | |
can_continue = False | |
for row in range(table_size): | |
for column in range(table_size): | |
for dir_x, dir_y in directions: | |
p = check_cell(row, column, dir_x, dir_y) | |
if p: | |
return p | |
if T[row][column] == 0: | |
can_continue = True | |
if can_continue: | |
return 0 # can continue | |
return 3 # draw | |
def check_cell(row, column, dir_x, dir_y): | |
cell = T[row][column] | |
if cell == 0: | |
return 0 | |
# if not (0 <= column + dir_x * winning_size <= table_size): | |
# return 0 | |
if column + dir_x * winning_size not in range(table_size): | |
return 0 | |
if row + dir_y * winning_size not in range(table_size): | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + (i * dir_y)][column + (i * dir_x)] != cell: | |
return 0 | |
return cell | |
def ask_names_confirm(): | |
for i in ['first', 'second']: | |
confirm = '' | |
while confirm not in ['yes', 'y']: | |
name = raw_input('What is {} player\'s name? ' | |
.format(i)).strip().title() | |
confirm = raw_input('Are you "{}", is this correct? [yes/no] ' | |
.format(name)).strip().lower() | |
players.append(name) | |
def first_player(): | |
global next_player | |
next_player = random.randint(0, 1) | |
print 'First player is: {}'.format(players[next_player]) | |
def play(): | |
answer = raw_input('{} where do you move? "row column": ' | |
.format(players[next_player])) | |
# HOMEWORK | |
# constants | |
table_size = 4 | |
winning_size = 2 | |
# globals | |
T = [[0] * table_size for _ in range(table_size)] | |
players = [] | |
next_player = -1 | |
# script | |
ask_names_confirm() | |
first_player() | |
print_table() | |
play() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def print_table(): | |
lookup = ['-', 'X', 'O'] | |
for row in T: | |
print ' '.join([lookup[cell] for cell in row]) | |
def move(player, row, column): | |
global next_player | |
assert player == next_player, 'player not valid' | |
assert row in range(table_size), 'row out of range' | |
assert column in range(table_size), 'column out of range' | |
assert T[row][column] == 0, 'avoid illegal move' | |
T[row][column] = player # writing to global variable, why is not a problem, | |
lookup_next_player = { | |
1: 2, | |
2: 1 | |
} | |
next_player = lookup_next_player[next_player] | |
def game_state(): | |
directions = [(1, 0), (0, 1), (1, 1), (-1, 1)] | |
can_continue = False | |
for row in range(table_size): | |
for column in range(table_size): | |
for dir_x, dir_y in directions: | |
possible_winner = check_cell(row, column, dir_x, dir_y) | |
if possible_winner: | |
return possible_winner | |
if T[row][column] == 0: | |
can_continue = True | |
if can_continue: | |
return 0 # can continue | |
return 3 # draw | |
def check_cell(row, column, dir_x, dir_y): | |
cell = T[row][column] | |
if cell == 0: | |
return 0 | |
if not (0 <= column + dir_x * winning_size <= table_size): | |
return 0 | |
if not (0 <= row + dir_y * winning_size <= table_size): | |
return 0 | |
for i in range(1, winning_size): | |
if T[row + (i * dir_y)][column + (i * dir_x)] != cell: | |
return 0 | |
return cell | |
def ask_names(): | |
number_to_english = { | |
1: 'first', | |
2: 'second', | |
} | |
for i in [1, 2]: | |
confirm = '' | |
while confirm not in ['yes', 'y']: | |
name = raw_input('What is {} player\'s name? ' | |
.format(number_to_english[i])).strip().title() | |
confirm = raw_input('Are you "{}", is this correct? [yes/no] ' | |
.format(name)).strip().lower() | |
players[i] = name | |
def choose_first_player(): | |
global next_player | |
next_player = random.randint(1, 2) | |
print 'First player is: {}'.format(players[next_player]) | |
def play(): | |
while game_state() == 0: | |
print_table() | |
answer = raw_input('{} where do you move? "row column": ' | |
.format(players[next_player])) | |
row, column = answer.strip().split() | |
try: | |
move(next_player, int(row) - 1, int(column) - 1) | |
except Exception as e: | |
print 'Error with move, try again: {}'.format(e) | |
print_table() | |
final_state = game_state() | |
if final_state in [1, 2]: | |
print 'Winner is: {}'.format(players[final_state]) | |
return | |
print "It's a draw" | |
return | |
# constants | |
table_size = 5 | |
winning_size = 3 | |
# globals | |
T = [[0] * table_size for _ in range(table_size)] | |
players = {} | |
next_player = -1 | |
# script | |
ask_names() | |
choose_first_player() | |
play() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment