[informatica] [Python] voorstelling spelbord

Moderators: ArcherBarry, Fuzzwood

Reageer
Gebruikersavatar
Berichten: 112

[Python] voorstelling spelbord

Beste, 
 
is er iemand die me kan uitleggen hoe je een board kunt maken met python?

Gebruikersavatar
Berichten: 2.609

Re: [Python] voorstelling spelbord

Als ik zou weten wat "een board" betekent waarschijnlijk wel.

Gebruikersavatar
Pluimdrager
Berichten: 3.505

Re: [Python] voorstelling spelbord

Wat is de precieze opdracht?
"Mathematics is a gigantic intellectual construction, very difficult, if not impossible, to view in its entirety." Armand Borel

Gebruikersavatar
Berichten: 112

Re: [Python] voorstelling spelbord

Xenion schreef: Als ik zou weten wat "een board" betekent waarschijnlijk wel.
een bord spel in 2D, gelijk een bord van het spel Candy Crush 
dat is het opdracht :
 
 
def init_board(dimension, given_board=()):
"""
Return a board with given dimension.

A board consists of a visible board (with size dimension x dimension) and an (optional) set of invisible rows on top of the visible board,
which is used to fill the visible board after explosions.

- If given_board contains a list of colors, these colors are used to fill the board. The first color in given_board is the bottom-left color of the (visible) board.
The board is filled from left to right and bottom to top according to the specified dimension (= height and width of visible board).
given_board can also contain additional "invisible" rows.
given_board will always contain a multitude of the given dimension (full rows), and contain at least enough candy to fill the visible board.
- If no given_board is provided, the board is filled with randomly generated rows of colors according to the provided dimension using extend_board_with_row.
(only the visible board is created)
"""

Gebruikersavatar
Berichten: 2.609

Re: [Python] voorstelling spelbord

OK. Wat je nodig hebt is dus een 2D-array. Typisch stel je dat voor als een lijst van lijsten.
Stel dat de variabele L zo een lijst is, dan is L op zich weer een lijst. L stelt dan de i'de rij van je spelbord voor.
L is op zich weer een lijst dus je kan die ook indexeren en dan is bijvoorbeeld L[j] het element op rij i en kolom j in het spelbord.
(Als je de numpy library zou gebruiken dan zou je daar de ndarray klasse kunnen gebruiken, maar een eenvoudige lijst van lijsten is meestal voldoende.)
 
Dit is één voorbeeldje om zo'n matrix vol nullen aan te maken:

Code: Selecteer alles

L = []
for i in range(aantal_rijen):
    nieuwe_rij = [0]*aantal_kolommen
    L.append(nieuwe_rij)

Opmerking moderator

Ik pas ook even de titel van dit topic aan om het wat duiderlijker te maken.

Reageer