-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
connect: Implement the exercise "connect" #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b2bc11e
connect: Implement the exercise "connect"
yunchih 10bc1f0
connect: Follow flake8
yunchih 79d57e0
connect: Match solution with testfile
yunchih 3ce3c99
connect: Add testfile version string
yunchih ad82337
connect: Fix test file
yunchih d7fe0a0
Merge branch 'master' into master
ilya-khadykin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Connect | ||
|
||
Compute the result for a game of Hex / Polygon. | ||
|
||
The abstract boardgame known as | ||
[Hex](https://en.wikipedia.org/wiki/Hex_%28board_game%29) / Polygon / | ||
CON-TAC-TIX is quite simple in rules, though complex in practice. Two players | ||
place stones on a rhombus with hexagonal fields. The player to connect his/her | ||
stones to the opposite side first wins. The four sides of the rhombus are | ||
divided between the two players (i.e. one player gets assigned a side and the | ||
side directly opposite it and the other player gets assigned the two other | ||
sides). | ||
|
||
Your goal is to build a program that given a simple representation of a board | ||
computes the winner (or lack thereof). Note that all games need not be "fair". | ||
(For example, players may have mismatched piece counts.) | ||
|
||
The boards look like this (with spaces added for readability, which won't be in | ||
the representation passed to your code): | ||
|
||
``` | ||
. O . X . | ||
. X X O . | ||
O O O X . | ||
. X O X O | ||
X O O O X | ||
``` | ||
|
||
"Player `O`" plays from top to bottom, "Player `X`" plays from left to right. In | ||
the above example `O` has made a connection from left to right but nobody has | ||
won since `O` didn't connect top and bottom. | ||
|
||
### Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory. | ||
|
||
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`. | ||
|
||
|
||
For more detailed information about running tests, code style and linting, | ||
please see the [help page](http://exercism.io/languages/python). | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
class ConnectGame: | ||
def __init__(self, board): | ||
pass | ||
|
||
def get_winner(self): | ||
pass |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import unittest | ||
|
||
import connect | ||
|
||
|
||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 | ||
|
||
testcases = [ | ||
{ | ||
"description": "an empty board has no winner", | ||
"board": | ||
""" . . . . . | ||
. . . . . | ||
. . . . . | ||
. . . . . | ||
. . . . .""", | ||
"winner": "" | ||
}, | ||
{ | ||
"description": "O can win on a 1x1 board", | ||
"board": "O", | ||
"winner": "O" | ||
}, | ||
{ | ||
"description": "X can win on a 1x1 board", | ||
"board": "X", | ||
"winner": "X" | ||
}, | ||
{ | ||
"description": "only edges does not make a winner", | ||
"board": | ||
""" O O O X | ||
X . . X | ||
X . . X | ||
X O O O""", | ||
"winner": "" | ||
}, | ||
{ | ||
"description": "illegal diagonal does not make a winner", | ||
"board": | ||
""" X O . . | ||
O X X X | ||
O X O . | ||
. O X . | ||
X X O O""", | ||
"winner": "" | ||
}, | ||
{ | ||
"description": "nobody wins crossing adjacent angles", | ||
"board": | ||
""" X . . . | ||
. X O . | ||
O . X O | ||
. O . X | ||
. . O .""", | ||
"winner": "" | ||
}, | ||
{ | ||
"description": "X wins crossing from left to right", | ||
"board": | ||
""" . O . . | ||
O X X X | ||
O X O . | ||
X X O X | ||
. O X .""", | ||
"winner": "X" | ||
}, | ||
{ | ||
"description": "X wins using a convoluted path", | ||
"board": | ||
""" . X X . . | ||
X . X . X | ||
. X . X . | ||
. X X . . | ||
O O O O O""", | ||
"winner": "X" | ||
}, | ||
{ | ||
"description": "O wins crossing from top to bottom", | ||
"board": | ||
""" . O . . | ||
O X X X | ||
O O O . | ||
X X O X | ||
. O X .""", | ||
"winner": "O" | ||
}, | ||
{ | ||
"description": "X wins using a spiral path", | ||
"board": | ||
""" O X X X X X X X X | ||
O X O O O O O O O | ||
O X O X X X X X O | ||
O X O X O O O X O | ||
O X O X X X O X O | ||
O X O O O X O X O | ||
O X X X X X O X O | ||
O O O O O O O X O | ||
X X X X X X X X O """, | ||
"winner": "X" | ||
}, | ||
] | ||
|
||
|
||
class ConnectTest(unittest.TestCase): | ||
def test_game(self): | ||
for testcase in testcases: | ||
game = connect.ConnectGame(testcase["board"]) | ||
winner = game.get_winner() | ||
expected = testcase["winner"] if testcase["winner"] else "None" | ||
got = winner if winner else "None" | ||
self.assertEqual(winner, testcase["winner"], | ||
"Test failed: %s, expected winner: %s, got: %s" | ||
% (testcase["description"], expected, got)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
class ConnectGame: | ||
|
||
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, -1), (-1, 1)] | ||
white = "O" | ||
black = "X" | ||
none = "" | ||
|
||
def __init__(self, lines): | ||
self.board = self.make_board(lines) | ||
assert len(self.board) > 0 | ||
|
||
self.width = len(self.board[0]) | ||
self.height = len(self.board) | ||
assert self.width > 0 and self.height > 0 | ||
|
||
for l in self.board: | ||
assert len(l) == self.width | ||
|
||
def valid(self, x, y): | ||
return x >= 0 and x < self.width and y >= 0 and y < self.height | ||
|
||
def make_board(self, lines): | ||
return ["".join(l.split()) for l in lines.splitlines()] | ||
|
||
def player_reach_dest(self, player, x, y): | ||
if player == self.black: | ||
return x == self.width - 1 | ||
if player == self.white: | ||
return y == self.height - 1 | ||
|
||
def walk_board(self, player, x, y, visited=[]): | ||
if (x, y) in visited: | ||
return False | ||
|
||
if (not self.valid(x, y)) or self.board[y][x] != player: | ||
return False | ||
|
||
if self.player_reach_dest(player, x, y): | ||
return True | ||
|
||
for d in self.directions: | ||
if self.walk_board(player, x + d[0], y + d[1], visited + [(x, y)]): | ||
return True | ||
|
||
def check_player_is_winner(self, player): | ||
if player == self.black: | ||
for y in range(self.height): | ||
if self.walk_board(player, 0, y): | ||
return True | ||
if player == self.white: | ||
for x in range(self.width): | ||
if self.walk_board(player, x, 0): | ||
return True | ||
|
||
def get_winner(self): | ||
if self.check_player_is_winner(self.black): | ||
return self.black | ||
if self.check_player_is_winner(self.white): | ||
return self.white | ||
return self.none |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The solution template doesn't match its usage in
connect_test.py
. The solution template should only contain placeholders for all classes and methods referenced in the tests.connect_test.py
does not referenceconnect.ConnectGame
, but it does referenceconnect.play()
, so that should be in the template.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out! (-:
Please check the updated one.