Wer hat das TicTacToe spielbar bekommen?
Das war die Aufgabe
Postet hier eure Ergebnisse, Codes, Gedanken. Lernt von den anderen, seht euch deren Quelltext an und versucht euren eigenen zu verbessern.
Diskussion
Postet zu euren Codes stets eine kurze Erklärung mit euren Gedanken in denen ihr simpel gesagt die Frage "Wieso habe ich XY auf diese Art gelöst?" beantwortet. Beiträge, die nur den Code enthalten werden wir aus dem Thread entfernen.
Nächste Aufgabe
In einer Woche wird die Musterlösung nach editiert und in 2 die nächste Aufgabe eingestellt.
Viel Spaß & viel Erfolg!
Musterlösung:
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN]
AppTitle = "TicTacToe" Graphics 200, 200
Global grid:Int[3, 3]
Local timer:TTimer Local x:Int, y:Int, player:Int, mx:Int, my:Int, pos_x:Int, pos_y:Int, win:Int, mh1:Int Local message:String
SetClsColor(128, 0, 0) timer = TTimer.Create(60) SeedRnd MilliSecs()
player=Rand(1,2)
Repeat mx=MouseX() my=MouseY() mh1=MouseHit(1) Cls SetColor(0, 0, 255) DrawLine 25, 75, 175, 75 DrawLine 25, 125, 175, 125 DrawLine 75, 25, 75, 175 DrawLine 125, 25, 125, 175 For y=0 To 2 For x=0 To 2 If grid[x, y] > 0 Then Select grid[x, y] Case 1; SetColor 255, 0, 0 Case 2; SetColor 0, 128, 0 End Select DrawOval(30 + (x * 50), 30 + (y * 50), 40, 40) EndIf Next Next Select player Case 1 SetColor 255, 0, 0 Case 2 SetColor 0, 128, 0 End Select DrawOval(mx - 20, my - 20, 40, 40) If mh1 And win=False If mx>=25 And mx<=175 If my>=25 And my<=175 pos_x=(mx-25)/50 pos_y=(my-25)/50 If grid[pos_x, pos_y] = 0 grid[pos_x, pos_y] = player win=CheckLine(player) If player=1 Then player=2 Else player=1 EndIf EndIf EndIf EndIf If win>0 Then If win=3 Then message="Unentschieden!" Else message="Player "+win+" hat gewonnen!" EndIf SetColor 255, 255, 255 DrawText(message, 10, 25) DrawText("Leertaste für neues Spiel", 10, 100) If KeyHit(57) Then NewGame() win=False player=Rand(1,2) EndIf EndIf Flip 0 WaitTimer(timer) Until KeyHit(KEY_ESCAPE) Or AppTerminate()
End
Function CheckLine(player) Local x:Int, y:Int For y=0 To 2 If grid[0, y] = player And grid[1, y] = player And grid[2, y] = player Then Return player EndIf Next For x=0 To 2 If grid[x, 0] = player And grid[x, 1] = player And grid[x, 2] = player Then Return player EndIf Next If grid[0, 0] = player And grid[1, 1] = player And grid[2, 2] = player Then Return player EndIf If grid[2,0]=player And grid[1,1]=player And grid[0,2]=player Then Return player EndIf For y=0 To 2 For x=0 To 2 If grid[x,y]=0 Then Return False Next Next Return 3 End Function
Function NewGame() Local x:Int, y:Int For y=0 To 2 For x=0 To 2 grid[x, y] = 0 Next Next End Function
|