Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:ffprg1-2018:ffprg1-2018 [2018/06/19 14:59] Ivo Blöchliger [Dienstag 19. 6. 2018] |
lehrkraefte:blc:informatik:ffprg1-2018:ffprg1-2018 [2018/06/26 17:51] (current) Ivo Blöchliger |
||
---|---|---|---|
Line 39: | Line 39: | ||
TicTacToe: | TicTacToe: | ||
* Gegeben ein 3x3-Array mit Einträgen 0, 1 oder 2 (leer, x, o). | * Gegeben ein 3x3-Array mit Einträgen 0, 1 oder 2 (leer, x, o). | ||
- | <code python> | ||
- | def ausgabe(feld): | ||
- | pass # Hier fehlt einiges an Code | ||
- | |||
- | feld = [[1, | ||
- | ausgabe(feld) | ||
- | </ | ||
- | * Gewünschte Text-Ausgabe: | ||
- | +---+---+---+ | + | <code python tictactoe.py> |
- | | x | | o | | + | from gpanel import * |
- | +---+---+---+ | + | |
- | | x | o | | | + | |
- | +---+---+---+ | + | |
- | | | + | |
- | +---+---+---+ | + | |
- | Hinweis: " | + | # leeres Feld |
+ | dim = 10 # Anzahl Felder | ||
+ | winlen = 5 # Anzahl fuer Gewinn | ||
+ | feld = [[0 for y in range(dim)] for x in range(dim)] | ||
+ | player = 1 | ||
+ | filled = 0 | ||
+ | winner = 0 | ||
+ | |||
+ | # Startpunkt x,y | ||
+ | # Vektor vx,vy | ||
+ | def testLine(x, | ||
+ | global winlen,dim | ||
+ | what = feld[x][y] | ||
+ | count = 1 if what>0 else 0 | ||
+ | while True: | ||
+ | x+=vx | ||
+ | y+=vy | ||
+ | if x<0 or y<0 or x>=dim or y>=dim: | ||
+ | break | ||
+ | if feld[x][y]==what: | ||
+ | count+=1 | ||
+ | else: | ||
+ | what = feld[x][y] | ||
+ | count=1 if what>0 else 0 | ||
+ | if feld[x][y]==0: | ||
+ | count = 0 | ||
+ | if count==winlen: | ||
+ | return what | ||
+ | return 0 | ||
+ | | ||
+ | def win(feld): | ||
+ | for x in range(dim): | ||
+ | combos = [[x, | ||
+ | [0,x,1,0],\ | ||
+ | [0,x,1,1],\ | ||
+ | [x,0,1,1],\ | ||
+ | [x, | ||
+ | [0,x,1,-1]] | ||
+ | for combo in combos: | ||
+ | winner = testLine(combo[0], | ||
+ | combo[2], combo[3], | ||
+ | if winner> | ||
+ | return winner | ||
+ | return 0 | ||
+ | |||
+ | |||
+ | def ausgabe(feld): | ||
+ | clear() | ||
+ | dim = len(feld) | ||
+ | for i in range(1, | ||
+ | line(0, | ||
+ | line(i, | ||
+ | for x in range(dim): | ||
+ | for y in range(dim): | ||
+ | if (feld[x][y]==1): | ||
+ | line(x+0.1, y+0.1, x+0.9, | ||
+ | line(x+0.1, y+0.9, x+0.9, | ||
+ | elif (feld[x][y]==2): | ||
+ | move(x+0.5, y+0.5) | ||
+ | circle(0.4) | ||
+ | | ||
+ | def onMousePressed(x, | ||
+ | global feld | ||
+ | global player | ||
+ | global filled | ||
+ | global winner | ||
+ | global dim | ||
+ | x = int(x) | ||
+ | y = int(y) | ||
+ | if winner>0 or filled == dim*dim: | ||
+ | feld = [[0 for y in range(dim)] for x in range(dim)] | ||
+ | player = 1 | ||
+ | filled = 0 | ||
+ | winner = 0 | ||
+ | elif feld[x][y]==0: | ||
+ | filled+=1 | ||
+ | feld[x][y] = player | ||
+ | player = 3-player | ||
+ | ausgabe(feld) | ||
+ | winner = win(feld) | ||
+ | if (winner> | ||
+ | print(" | ||
+ | elif (filled == dim*dim): | ||
+ | print(" | ||
+ | |||
+ | makeGPanel(0, | ||
+ | lineWidth(5) | ||
+ | ausgabe(feld) | ||
+ | |||
+ | </ | ||
* Gegeben ein 3x3-Array wie oben. Stellen Sie fest, ob schon jemand gewonnen hat. | * Gegeben ein 3x3-Array wie oben. Stellen Sie fest, ob schon jemand gewonnen hat. | ||
* Programmieren Sie ein Spiel für 2 menschliche Spieler, die abwechslungsweise die Koordinaten eingeben. Die Eingabe soll überprüft werden. | * Programmieren Sie ein Spiel für 2 menschliche Spieler, die abwechslungsweise die Koordinaten eingeben. Die Eingabe soll überprüft werden. |