hex - math.unipd.it

8
© Renato Conte – HEX - 1 / 8- Hex is a board game for two players. It is played on a board that is an array of hexagons forming a rhombus pattern. The rules are simple enough that anybody can play, but the game is also of interest to mathematicians, game theorists, and computer scientists. Begin with a board, two players, and a supply of pieces or markers in two different colors. Each player chooses a color of markers and a pair of opposite sides which is shown here in red and blue. Basic strategy for this game is twofold: block your opponent and build your own bridge. If you do either well enough, you will accomplish the other. Remember to move sideways if they block you.

Transcript of hex - math.unipd.it

Page 1: hex - math.unipd.it

© Renato Conte – HEX - 1 / 8-

Hex is a board game for two players.

It is played on a board that is an array of hexagons forming a rhombus

pattern.

The rules are simple enough that anybody can play, but the game is also of

interest to mathematicians, game theorists, and computer scientists.

Begin with a board, two players, and a supply of pieces or markers in two

different colors.

Each player chooses a color of markers and a pair of opposite sides which is

shown here in red and blue.

Basic strategy for this

game is twofold:

block your opponent and build

your own bridge.

If you do either well enough,

you will accomplish the other.

Remember to move sideways

if they block you.

Page 2: hex - math.unipd.it

© Renato Conte – HEX - 2 / 8-

Page 3: hex - math.unipd.it

© Renato Conte – HEX - 3 / 8-

Utilizzando un diagramma delle classi in UML,

definire la struttura dati necessaria per il

funzionamento del gioco a livello model.

Realizzare poi un diagramma delle attività per

impostare un metodo che controlli se il giocatore

corrente, con l'ultima mossa, ha realizzato una

sequenza di pedine contigue che unisce le due

sponde a lui associate, e quindi ha vinto.

Page 4: hex - math.unipd.it

© Renato Conte – HEX - 4 / 8-

Page 5: hex - math.unipd.it

© Renato Conte – HEX - 5 / 8-

c

r

c - 1 c + 1

r - 1

r + 1

Page 6: hex - math.unipd.it

© Renato Conte – HEX - 6 / 8-

win(player: char , r: int , c: int ): bool

r: intr: intr: intr: int

Per ognuna delle Per ognuna delle Per ognuna delle Per ognuna delle sei caselle adiacentisei caselle adiacentisei caselle adiacentisei caselle adiacentiplayer: player: player: player:

[vinto ]

Controlla seControlla seControlla seControlla seentrambe le sponde entrambe le sponde entrambe le sponde entrambe le sponde sono state raggiuntesono state raggiuntesono state raggiuntesono state raggiunte

[sponde opposte raggiunte ]

result: boolresult: boolresult: boolresult: boolfalse false false false

[non esistonopiu' coordinateadiacenti] result:result:result:result:

A WinWinWinWin

A

result: boolresult: boolresult: boolresult: booltrue true true true

c: intc: intc: intc: int [ se la casella non è stata visitata AND è dello stesso colore della corrente ]

Coordinate Coordinate Coordinate Coordinate casella casella casella casella adiacenteadiacenteadiacenteadiacente

Chiamata ricorsiva

Marca casella Marca casella Marca casella Marca casella attuale visitataattuale visitataattuale visitataattuale visitata

[ se non ha vinto, riprova con un'altra casella]

[no ]

Result:Result:Result:Result:true true true true

Page 7: hex - math.unipd.it

© Renato Conte – HEX - 7 / 8-

/**Vittoria Rosso o Vittoria BluVerifica se esiste un percorso che congiunge due spondeopposte della matrice a partire dall'ultimo inserimento*/

bool HexBoard::Win( char player, int r, int c ){ // cout <<r<<c<<" "; visited[r][c]= true; // matrice da mettere a false prima della chiamata a win

if (player=='R') // se il giocatore e' Rosso: 1^ e ultima colonna { if (c==dim-1) sideRight= true; // raggiunta sponda destra else if (c==0) sideLeft= true; if (sideRight && sideLeft) return true; // sponde unite } else // se il giocatore e' Blu: 1^ e ultima riga { if(r==dim-1)sideDown = true; else if(r==0)sideUp = true; if (sideDown&&sideUp) return true; // sponde unite }

...

Page 8: hex - math.unipd.it

© Renato Conte – HEX - 8 / 8-

// destraif ( (c-1 >=0) && (M[r][c-1]== player) && !visited[r][c-1] ) if ( Win(player, r, c-1) ) return true;

//sinistraif ( (c+1 < dim) && (M[r][c+1]== player) && !visited[r][c+1] ) if ( Win(player, r, c+1) return true;

// altoif ( (r-1 >=0) && (M[r-1][c]== player) && !visited[r-1][c] ) if ( Win(player, r-1, c) return true;

//bassoif ( (r+1 <dim) && (M[r+1][c]== player) && !visited[r+1][c] ) if ( Win(player, r+1, c) return true;

// alto-sinistraif ( (r-1 >=0) && (c-1 >=0) && (M[r-1][c-1]==player) && !visited[r-1][c-1] ) if ( Win(player, r-1, c-1) return true;

//basso-destraif ( (r+1 < dim) && (c+1 < dim) && (M[r+1][c+1]==player) && !visited[r+1][c+1]) if ( Win(player, r+1, c+1) return true;

return false;}//Win

c

r

c - 1 c + 1

r - 1

r + 1