Nov-23-2020, 09:29 PM
(This post was last modified: Nov-23-2020, 09:29 PM by Roidesflammes.
Edit Reason: Added code tags around dictionary
)
Hello !
sorry for my bad English :(
In class we are designing a corridor, here is a link for those interested in the game to explain the rules to you: https://www.youtube.com/watch?v=6ISruhN0Hc0
I'm trying to design a print_quoridor() function that displays the corridor, with the current position of both players and walls.
My function takes a dictionary type argument, example:
There is another "walls" key, which gives access to the "horizontal" and "vertical" keys. They define the walls that are currently on the ground.
This is what my function should look like, regarding my dictionary above:
By convention, a horizontal wall is located between lines 𝑦 - 1 and 𝑦, and blocks columns 𝑥 and 𝑥 + 1. Likewise, a vertical wall is located between columns 𝑥 - 1 and 𝑥, and blocks lines 𝑥 and 𝑥 + 1
for horizontal walls, we therefore have the constraints 1 ≤ 𝑥 ≤ 8 and 2 ≤ 𝑦 ≤ 9, but for vertical walls, they are rather 2 ≤ 𝑥 ≤ 9 and 1 ≤ 𝑦 ≤ 8.
For each change of player position or addition of wall in dictionary, the function must display it
My code looks like this for now:
I don't think that's the right way to build the empty checkerboard, and add the elements afterwards.
I think I am not on the right track.
I think about it, and I say to myself that maybe it would be better to take it step by step:
quoridor = "first line"
quoridor + = "second line"
Or it might be better to use lists within a list:
quoridor = [[], [], [], ...]
The problem is that I am a beginner and I do not know where to start, I need help and especially understanding ...
sorry for my bad English :(
In class we are designing a corridor, here is a link for those interested in the game to explain the rules to you: https://www.youtube.com/watch?v=6ISruhN0Hc0
I'm trying to design a print_quoridor() function that displays the corridor, with the current position of both players and walls.
My function takes a dictionary type argument, example:
dictionary = { "players": [ {"name": "player 1", "walls": 7, "pos": [5, 5]}, {"name": "player 2", "walls": 3, "pos": [8, 6]} ], "walls": { "horizontal": [[4, 4], [2, 6], [3, 8], [5, 8], [7, 8]], "vertical": [[6, 2], [4, 4], [2, 6], [7, 5], [7, 7]] } }Inside, we find a key "player 1" which gives access to a list where there is a key "name", a key "walls" (the number of walls that the players can still place) and the key "pos "(the player's current position). same for "player 2"
There is another "walls" key, which gives access to the "horizontal" and "vertical" keys. They define the walls that are currently on the ground.
This is what my function should look like, regarding my dictionary above:
Output:Legend:
1=player 1, walls=|||||||
2=Player 2, walls=|||
-----------------------------------
9 | . . . . . . . . . |
| |
8 | . . . . . . | . . . |
| ------- -------|------- |
7 | . | . . . . . | . . . |
| | |
6 | . | . . . . . | . 2 . |
| ------- | |
5 | . . . | . 1 . | . . . |
| | |
4 | . . . | . . . . . . |
| ------- |
3 | . . . . . | . . . . |
| | |
2 | . . . . . | . . . . |
| |
1 | . . . . . . . . . |
--|-----------------------------------
| 1 2 3 4 5 6 7 8 9
Noting that: A position is always represented by a pair of two coordinates (𝑥, 𝑦), where 1 ≤ 𝑥 ≤ 9 and 1 ≤ 𝑦 ≤ 9 and that the walls always have a length of 2 squares and their position is always relative to their lower left corner.By convention, a horizontal wall is located between lines 𝑦 - 1 and 𝑦, and blocks columns 𝑥 and 𝑥 + 1. Likewise, a vertical wall is located between columns 𝑥 - 1 and 𝑥, and blocks lines 𝑥 and 𝑥 + 1
for horizontal walls, we therefore have the constraints 1 ≤ 𝑥 ≤ 8 and 2 ≤ 𝑦 ≤ 9, but for vertical walls, they are rather 2 ≤ 𝑥 ≤ 9 and 1 ≤ 𝑦 ≤ 8.
For each change of player position or addition of wall in dictionary, the function must display it
My code looks like this for now:
def print_quoridor(dictionary): player_name = dictionary["players"][0]["name"] number_of_player_wall = dictionary["players"][0]["walls"] * "|" ordi_name = dictionary["players"][1]["name"] number_of_wall_of_ordi = dictionary["players"][1]["walls"] * "|" space = abs(len (name_of_player) - len (name_of_ordi)) * " " #this condition allows to align the walls even if we change the names of the players, the walls must will always be aligned in "Legend" if len (player_name) > len (ordi_name): checkerboard = f"Legend: \n 1={player_name}, walls={number_of_player_wall} \n 1={ordi_name}, {space} walls={number_of_wall_of_ordi}" else: checkerboard = f"Legend: \n 1={player_name}, {space} walls={number_of_player_wall} \n 1={ordi_name}, walls={number_of_wall_of_ordi}" i = 9 checkerboard + = "\n" + "-----------------------------------" + "\n" while i> = 1: checkerboard + = (f "{i} |......... |" + "\n" + "|......... |" + "\n") i -= 1 checkerboard + = "- | -----------------------------------" + "\n" checkerboard + = " | 1 2 3 4 5 6 7 8 9" return checkerboardAnd the terminal returns this to me :
Output:Legend:
1=player 1, walls=|||||||
1=player 2, walls=|||
-----------------------------------
9 | . . . . . . . . . |
| . . . . . . . . . |
8 | . . . . . . . . . |
| . . . . . . . . . |
7 | . . . . . . . . . |
| . . . . . . . . . |
6 | . . . . . . . . . |
| . . . . . . . . . |
5 | . . . . . . . . . |
| . . . . . . . . . |
4 | . . . . . . . . . |
| . . . . . . . . . |
3 | . . . . . . . . . |
| . . . . . . . . . |
2 | . . . . . . . . . |
| . . . . . . . . . |
1 | . . . . . . . . . |
| . . . . . . . . . |
--|-----------------------------------
| 1 2 3 4 5 6 7 8 9
I would like to put the position of my players and my walls in my checkers (quoridor), but I do not know how to doI don't think that's the right way to build the empty checkerboard, and add the elements afterwards.
I think I am not on the right track.
I think about it, and I say to myself that maybe it would be better to take it step by step:
quoridor = "first line"
quoridor + = "second line"
Or it might be better to use lists within a list:
quoridor = [[], [], [], ...]
The problem is that I am a beginner and I do not know where to start, I need help and especially understanding ...