Columns Algorithmus

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

gekkonier

Betreff: Columns Algorithmus

BeitragMi, Jul 02, 2008 15:20
Antworten mit Zitat
Benutzer-Profile anzeigen
Columns ist ein Tetrisähnliches Game, in dem es darum geht Reihen von mindestens 3 gleichfarbigen Steinen zu bilden. Nähere Informationen:

http://de.wikipedia.org/wiki/Columns

Hier ein kleines Snippet (Daten und Verarbeitung) wie man so etwas in Blitzmax realisieren könnte.
Das Programm hat keinerlei grafische Ausgabe:

Code: [AUSKLAPPEN]
SuperStrict
SeedRnd MilliSecs()

Const sizex:Int = 5
Const sizey:Int = 10

Const empty:Int = 0
Const full:Int = 1
Const marked:Int = 2

Global triples:Int = True


Type Feld
   Field color:Int
   Field state:Int
EndType

Type Matrix
   Field data:Feld[sizex , sizey]
   
   Method New()
      For Local x:Int = 0 To sizex - 1
         For Local y:Int = 0 To sizey -1
            data[x , y] = New Feld
         Next
      Next
   EndMethod
   
   Method random()
      For Local x:Int = 0 To sizex -1
         For Local y:Int = 0 To sizey - 1
            data[x , y].color = Rand(1 , 5)
            data[x , y].state = full
         Next
      Next
   EndMethod
   
   Method debug(whathappened:String)
      Print
      Print whathappened
      For Local y:Int = 0 To sizey - 1
         Local line:String = ""
         For Local x:Int = 0 To sizex - 1
            Local mark:String = "  "
            If data[x , y].state = marked
               mark = "* "
            EndIf
            If data[x , y].state = empty
               line = line + "   "
            Else
               line = line + String(data[x , y].color) + mark
            EndIf
            
         Next
         Print line
      Next
   EndMethod
   
   Method mark()
      triples = False
      For Local x:Int = 0 To sizex - 1
         For Local y:Int = 0 To sizey - 1
            If x > 0 And x < sizex - 1
               If cmp(data[x , y] , data[x - 1 , y] , data[x + 1 , y])
                  data[x , y].state = marked
                  data[x - 1 , y].state = marked
                  data[x + 1 , y].state = marked
                  triples = True
               EndIf
            EndIf
            If y > 0 And y < sizey - 1
               If cmp(data[x , y] , data[x , y - 1] , data[x , y + 1])
                  data[x , y].state = marked
                  data[x , y - 1].state = marked
                  data[x , y + 1].state = marked
                  triples = True
               EndIf
            EndIf
            If x > 0 And y > 0 And x < sizex - 1 And y < sizey - 1
               If cmp(data[x , y] , data[x - 1 , y + 1] , data[x + 1 , y - 1])
                  data[x , y].state = marked
                  data[x - 1 , y + 1].state = marked
                  data[x + 1 , y - 1].state = marked
                  triples = True
               EndIf
               If cmp(data[x , y] , data[x - 1 , y - 1] , data[x + 1 , y + 1])
                  data[x , y].state = marked
                  data[x - 1 , y - 1].state = marked
                  data[x + 1 , y + 1].state = marked
                  triples = True
               EndIf
            EndIf
         Next
      Next
   EndMethod
   
   Method remove()
      For Local x:Int = 0 To sizex - 1
         For Local y:Int = 0 To sizey - 1
            If data[x , y].state = marked
               data[x , y].state = empty
            EndIf
         Next
      Next
   EndMethod
   
   Method collapse()
      Local buffer:Int[sizey]
      Local bufferpos:Int
      Local datapos:Int
      
      For Local x:Int = 0 To sizex - 1
         
         bufferpos = 0
         For Local y:Int = sizey - 1 To 0 Step - 1
            If data[x , y].state = full
               buffer[bufferpos] = data[x , y].color
               bufferpos = bufferpos + 1
            EndIf
         Next
         
         datapos = sizey - 1
         
         For Local y:Int = 0 To sizey - 1
            data[x , y].state = empty
         Next
         
         bufferpos = 0
         While bufferpos < sizey
            If buffer[bufferpos] > 0
               data[x , datapos].state = full
               data[x , datapos].color = buffer[bufferpos]
               datapos = datapos - 1
            EndIf
            bufferpos = bufferpos + 1
         Wend
         
         For Local y:Int = 0 To sizey - 1
            buffer[y] = 0
         Next
      
      Next

   EndMethod
   
   Function cmp:Int(a:Feld , b:Feld , c:Feld)
      Local retval:Int = False
      If a.state <> 0 And b.state <> 0 And c.state <> 0
         If a.color = b.color And b.color = c.color
            retval = True
         EndIf
      EndIf
      Return retval
   EndFunction
   
EndType

Local m:Matrix = New Matrix
m.random
m.mark

Repeat
   m.debug("markierte triples: ")
   m.remove
   m.debug("rausgeloescht: ")
   m.collapse
   m.debug("nachgerueckt: ")
   m.mark
Until triples = False

Print
Print "nichts mehr zu loeschen, ich bin fertig!"


Mögliche Ausgabe:

Code: [AUSKLAPPEN]
markierte triples:
1  3  4  5  3 
2* 1  5* 2  2 
2* 1  5* 5  1 
2* 2* 5* 1  3 
1  2* 4  1  2 
2  2* 5  3* 1 
2  4  3* 5  5 
1  3* 1  3  3 
3* 2  5  1  2 
1  3  2  1  3 

rausgeloescht:
1  3  4  5  3 
   1     2  2 
   1     5  1 
         1  3 
1     4  1  2 
2     5     1 
2  4     5  5 
1     1  3  3 
   2  5  1  2 
1  3  2  1  3 

nachgerueckt:
            3 
         5  2 
         2  1 
         5  3 
1  3  4  1  2 
1  1  4  1  1 
2  1  5  5  5 
2  4  1  3  3 
1  2  5  1  2 
1  3  2  1  3 

markierte triples:
            3 
         5  2 
         2  1 
         5  3 
1  3  4  1  2 
1* 1  4  1  1 
2  1* 5* 5* 5*
2* 4  1* 3  3 
1  2* 5  1* 2 
1  3  2* 1  3 

rausgeloescht:
            3 
         5  2 
         2  1 
         5  3 
1  3  4  1  2 
   1  4  1  1 
2             
   4     3  3 
1     5     2 
1  3     1  3 

nachgerueckt:
               
            3 
            2 
         5  1 
         2  3 
         5  2 
1  3     1  1 
2  1  4  1  3 
1  4  4  3  2 
1  3  5  1  3 

nichts mehr zu loeschen, ich bin fertig!


Geht bestimmt alles viel schöner (ganz sicher vorallem die Methode collapse), aber bevor ich daran gar nix mehr herumschraube dachte ich, dass ich es euch zugänglich mache.

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group