Permalänk
Medlem
Skrivet av Affeskate13:

Kan vi inte lägga ut några rader kod, och se vad folk tror att de står för?

Jag rekommenderar följande lilla perl snutt:

s//:o/;say length$1 while s/(.*):(.*)/$2:$1$2/

Visa signatur

Fractal Design Define R5 | MSI Z97-GD65 Gaming | MSI Geforce GTX 970 Gaming 4G | Intel i5 4690k | Cooler Master Hyper 212 EVO | EVGA Supernova G2 750W | 2x8GB Corsair Vengeance Low Profile DDR3 1600Mhz | Samsung 850 EVO | Seagate 1TB SATA3.5

Permalänk
Skrivet av Affeskate13:

Kan vi inte lägga ut några rader kod, och se vad folk tror att de står för?

såklart!

GraphicsWindow.KeyDown = HandleKey GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( 253, 252, 251 ) While "True" BOXES = 4 ' number of boxes per piece BWIDTH = 25 ' box width in pixels XOFFSET = 40 ' Screen X offset in pixels of where the board starts YOFFSET = 40 ' Screen Y offset in pixels of where the board starts CWIDTH = 10 ' Canvas Width, in number of boxes CHEIGHT = 20 ' Canvas Height, in number of boxes. STARTDELAY = 800 ENDDELAY = 175 PREVIEW_xpos = 13 PREVIEW_ypos = 2 GraphicsWindow.Clear() GraphicsWindow.Title = "Small Basic Tetris" GraphicsWindow.Height = 580 GraphicsWindow.Width = 700 GraphicsWindow.Show() SetupTemplates() SetupCanvas() MainLoop() GraphicsWindow.ShowMessage( "Game Over", "Small Basic Tetris" ) EndWhile Sub MainLoop template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h end = 0 sessionDelay = STARTDELAY While end = 0 If sessionDelay > ENDDELAY Then sessionDelay = sessionDelay - 1 EndIf delay = sessionDelay thisPiece = nextPiece template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h DrawPreviewPiece() h = thisPiece ypos = 0 done = 0 xpos = 3 ' always drop from column 3 CheckStop() ' in: ypos, xpos, h ret: done If done = 1 Then ypos = ypos - 1 MovePiece() 'in: ypos, xpos, h end = 1 EndIf yposdelta = 0 While done = 0 Or yposdelta > 0 MovePiece() 'in: ypos, xpos, h ' Delay, but break if the delay get set to 0 if the piece gets dropped delayIndex = delay While delayIndex > 0 And delay > 0 Program.Delay(10) delayIndex = delayIndex - 10 EndWhile If yposdelta > 0 Then yposdelta = yposdelta - 1 ' used to create freespin, when the piece is rotated Else ypos = ypos + 1 ' otherwise, move the piece down. EndIf ' Check if the piece should stop. CheckStop() ' in: ypos, xpos, h ret: done EndWhile EndWhile EndSub Sub HandleKey ' Stop game If GraphicsWindow.LastKey = "Escape" Then Program.End() EndIf ' Move piece left If GraphicsWindow.LastKey = "Left" Then moveDirection = -1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece right If GraphicsWindow.LastKey = "Right" Then moveDirection = 1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece down If GraphicsWindow.LastKey = "Down" or GraphicsWindow.LastKey = "Space" Then delay = 0 EndIf ' Rotate piece If GraphicsWindow.LastKey = "Up" Then basetemplate = Array.GetValue(h, -1) ' Array.GetValue(h, -1) = the template name template = "temptemplate" rotation = "CW" CopyPiece() 'in basetemplate, template, rotation Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name moveDirection = 0 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 ' See if it can be moved so that it will rotate. xposbk = xpos yposdelta = 0 While yposdelta = 0 And Math.Abs(xposbk - xpos) < 3 ' move up to 3 times only ' if the rotation move worked, copy the temp to "rotatedtemplate" and use that from now on If invalidMove = 0 Then basetemplate = template template = "rotatedtemplate" Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name rotation = "COPY" CopyPiece() 'in basetemplate, template, rotation yposdelta = 1 ' Don't move down if we rotate MovePiece() 'in: ypos, xpos, h ElseIf invalidMove = 2 Then ' Don't support shifting piece when hitting another piece to the right or left. xpos = 99 ' exit the loop Else ' if the rotated piece can't be placed, move it left or right and try again. xpos = xpos - invalidMove ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 EndIf EndWhile If invalidMove <> 0 Then xpos = xposbk Array.SetValue(h, -1, basetemplate) ' Array.GetValue(h, -1) = the template name template = "" EndIf EndIf EndSub Sub DrawPreviewPiece xpos = PREVIEW_xpos ypos = PREVIEW_ypos h = nextPiece XOFFSETBK = XOFFSET YOFFSETBK = YOFFSET XOFFSET = XOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewx") ' Array.GetValue(h, -1) = the template name YOFFSET = YOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewy") ' Array.GetValue(h, -1) = the template name MovePiece() 'in: ypos, xpos, h XOFFSET = XOFFSETBK YOFFSET = YOFFSETBK EndSub ' creates template that's a rotated basetemplate Sub CopyPiece 'in basetemplate, template, rotation L = Array.GetValue(basetemplate, "dim") If rotation = "CW" Then For i = 0 to BOXES - 1 ' x' = y y' = L - 1 - x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (Math.Remainder(v, 10)) y = (L - 1 - Math.Floor(v/10)) Array.SetValue(template, i, x * 10 + y) EndFor ' Count-Cockwise is not currently used ElseIf rotation = "CCW" Then For i = 0 to BOXES - 1 ' x' = L - 1 - y y' = x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (L - 1 - Math.Remainder(v, 10)) y = Math.Floor(v/10) Array.SetValue(template, i, x * 10 + y) EndFor ElseIf rotation = "COPY" Then For i = 0 to BOXES - 1 Array.SetValue(template, i, Array.GetValue(basetemplate, i)) EndFor Else GraphicsWindow.ShowMessage("invalid parameter", "Error") Program.End() EndIf ' Copy the remain properties from basetemplate to template. Array.SetValue(template, "color", Array.GetValue(basetemplate, "color")) Array.SetValue(template, "dim", Array.GetValue(basetemplate, "dim")) Array.SetValue(template, "pviewx", Array.GetValue(basetemplate, "pviewx")) Array.SetValue(template, "pviewy", Array.GetValue(basetemplate, "pviewy")) EndSub Sub CreatePiece ' in: template ret: h ' Create a new handle, representing an arrayName, that will represent the piece hcount = hcount + 1 h = Text.Append("piece", hcount) Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = Array.GetValue(template, "color") For i = 0 to BOXES - 1 s = Shapes.AddRectangle(BWIDTH, BWIDTH) Shapes.Move(s, -BWIDTH, -BWIDTH) ' move off screen Array.SetValue(h, i, s) EndFor EndSub Sub MovePiece 'in: ypos, xpos, h. ypos/xpos is 0-19, representing the top/left box coordinate of the piece on the canvas. h returned by CreatePiece For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name x = Math.Floor(v/10) y = Math.Remainder(v, 10) ' Array.GetValue(h, i) = box for piece h. ' xpos/ypos = are topleft of shape. x/y is the box offset within the shape. Shapes.Move(Array.GetValue(h, i), XOFFSET + xpos * BWIDTH + x * BWIDTH, YOFFSET + ypos * BWIDTH + y * BWIDTH) EndFor EndSub Sub ValidateMove ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 i = 0 invalidMove = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If (x + xpos + moveDirection) < 0 Then invalidMove = -1 i = BOXES ' force getting out of the loop EndIf If (x + xpos + moveDirection) >= CWIDTH Then invalidMove = 1 i = BOXES ' force getting out of the loop EndIf If Array.GetValue("c", (x + xpos + moveDirection) + (y + ypos) * CWIDTH) <> "." Then invalidMove = 2 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile EndSub Sub CheckStop ' in: ypos, xpos, h ret: done done = 0 i = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If y + ypos > CHEIGHT Or Array.GetValue("c", (x + xpos) + (y + ypos) * CWIDTH) <> "." Then done = 1 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile ' If we need to stop the piece, move the box handles to the canvas If done = 1 Then For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) Array.SetValue("c", (Math.Floor(v/10) + xpos) + (Math.Remainder(v, 10) + ypos - 1) * CWIDTH, Array.GetValue(h, i)) EndFor ' 1 points for every piece successfully dropped score = score + 1 PrintScore() ' Delete clared lines DeleteLines() EndIf EndSub Sub DeleteLines linesCleared = 0 ' Iterate over each row, starting from the bottom For y = CHEIGHT - 1 to 0 Step -1 ' Check to see if the whole row is filled x = CWIDTH While x = CWIDTH x = 0 While x < CWIDTH piece = Array.GetValue("c", x + y * CWIDTH) If piece = "." then x = CWIDTH EndIf x = x + 1 EndWhile ' if non of them were empty (i.e "."), then remove the line. If x = CWIDTH Then ' Delete the line For x1 = 0 to CWIDTH - 1 Shapes.Remove(Array.GetValue("c", x1 + y * CWIDTH)) EndFor linesCleared = linesCleared + 1 ' Move everything else down one. For y1 = y To 1 Step -1 For x1 = 0 to CWIDTH - 1 piece = Array.GetValue("c", x1 + (y1 - 1) * CWIDTH) Array.SetValue("c", x1 + y1 * CWIDTH, piece) Shapes.Move(piece, Shapes.GetLeft(piece), Shapes.GetTop(piece) + BWIDTH) EndFor EndFor EndIf EndWhile EndFor If linesCleared > 0 Then score = score + 100 * Math.Round(linesCleared * 2.15 - 1) PrintScore() EndIf EndSub Sub SetupCanvas ' GraphicsWindow.DrawResizedImage( Flickr.GetRandomPicture( "bricks" ), 0, 0, GraphicsWindow.Width, GraphicsWindow.Height) GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor GraphicsWindow.FillRectangle(XOFFSET, YOFFSET, CWIDTH*BWIDTH, CHEIGHT*BWIDTH) Program.Delay(200) GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Pink" For x = 0 To CWIDTH-1 For y = 0 To CHEIGHT-1 Array.SetValue("c", x + y * CWIDTH, ".") ' "." indicates spot is free GraphicsWindow.DrawRectangle(XOFFSET + x * BWIDTH, YOFFSET + y * BWIDTH, BWIDTH, BWIDTH) EndFor EndFor GraphicsWindow.PenWidth = 4 GraphicsWindow.PenColor = "Black" GraphicsWindow.DrawLine(XOFFSET, YOFFSET, XOFFSET, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH, YOFFSET, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET, YOFFSET + CHEIGHT*BWIDTH, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.PenColor = "Lime" GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET, XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 4, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 4) GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = "Pink" x = XOFFSET + PREVIEW_xpos * BWIDTH - BWIDTH y = YOFFSET + PREVIEW_ypos * BWIDTH - BWIDTH GraphicsWindow.FillRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.DrawRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.FillRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.DrawRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontItalic = "False" GraphicsWindow.FontName = "Comic Sans MS" GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x, y + 200, "Game control keys:") GraphicsWindow.DrawText(x + 25, y + 220, "Left Arrow = Move piece left") GraphicsWindow.DrawText(x + 25, y + 240, "Right Arrow = Move piece right") GraphicsWindow.DrawText(x + 25, y + 260, "Up Arrow = Rotate piece") GraphicsWindow.DrawText(x + 25, y + 280, "Down Arrow = Drop piece") GraphicsWindow.DrawText(x, y + 320, "Press to stop game") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontName = "Georgia" GraphicsWindow.FontItalic = "True" GraphicsWindow.FontSize = 36 GraphicsWindow.DrawText(x - 20, y + 400, "Small Basic Tetris") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x - 20, y + 440, "ver.0.1") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score score = 0 PrintScore() EndSub Sub PrintScore GraphicsWindow.PenWidth = 4 GraphicsWindow.BrushColor = "Pink" GraphicsWindow.FillRectangle(500, 65, 153, 50) GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawRectangle(500, 65, 153, 50) GraphicsWindow.FontItalic = "False" GraphicsWindow.FontSize = 32 GraphicsWindow.FontName = "Impact" GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawText(505, 70, Text.Append(Text.GetSubText( "00000000", 0, 8 - Text.GetLength( score ) ), score)) EndSub Sub SetupTemplates ' each piece has 4 boxes. ' the index of each entry within a piece represents the box number (1-4) ' the value of each entry represents to box zero-based box coordinate within the piece: tens place is x, ones place y '_X_ '_X_ '_XX Array.SetValue("template1", 0, 10) Array.SetValue("template1", 1, 11) Array.SetValue("template1", 2, 12) Array.SetValue("template1", 3, 22) Array.SetValue("template1", "color", "Yellow") Array.SetValue("template1", "dim", 3) Array.SetValue("template1", "pviewx", -12) Array.SetValue("template1", "pviewy", 12) '_X_ '_X_ 'XX_ Array.SetValue("template2", 0, 10) Array.SetValue("template2", 1, 11) Array.SetValue("template2", 2, 12) Array.SetValue("template2", 3, 02) Array.SetValue("template2", "color", "Magenta") Array.SetValue("template2", "dim", 3) Array.SetValue("template2", "pviewx", 12) Array.SetValue("template2", "pviewy", 12) '_X_ 'XXX '_ Array.SetValue("template3", 0, 10) Array.SetValue("template3", 1, 01) Array.SetValue("template3", 2, 11) Array.SetValue("template3", 3, 21) Array.SetValue("template3", "color", "Gray") Array.SetValue("template3", "dim", 3) Array.SetValue("template3", "pviewx", 0) Array.SetValue("template3", "pviewy", 25) 'XX_ 'XX_ '_ Array.SetValue("template4", 0, 00) Array.SetValue("template4", 1, 10) Array.SetValue("template4", 2, 01) Array.SetValue("template4", 3, 11) Array.SetValue("template4", "color", "Cyan") Array.SetValue("template4", "dim", 2) Array.SetValue("template4", "pviewx", 12) Array.SetValue("template4", "pviewy", 25) 'XX_ '_XX '_ Array.SetValue("template5", 0, 00) Array.SetValue("template5", 1, 10) Array.SetValue("template5", 2, 11) Array.SetValue("template5", 3, 21) Array.SetValue("template5", "color", "Green") Array.SetValue("template5", "dim", 3) Array.SetValue("template5", "pviewx", 0) Array.SetValue("template5", "pviewy", 25) '_XX 'XX_ '_ Array.SetValue("template6", 0, 10) Array.SetValue("template6", 1, 20) Array.SetValue("template6", 2, 01) Array.SetValue("template6", 3, 11) Array.SetValue("template6", "color", "Blue") Array.SetValue("template6", "dim", 3) Array.SetValue("template6", "pviewx", 0) Array.SetValue("template6", "pviewy", 25) '_X '_X '_X '_X Array.SetValue("template7", 0, 10) Array.SetValue("template7", 1, 11) Array.SetValue("template7", 2, 12) Array.SetValue("template7", 3, 13) Array.SetValue("template7", "color", "Red") Array.SetValue("template7", "dim", 4) Array.SetValue("template7", "pviewx", 0) Array.SetValue("template7", "pviewy", 0) EndSub

Dold text

för långt?
536 rader

Visa signatur

Ursäktar för stavfel

Permalänk
Skrivet av JOANATAN5354:

såklart!

GraphicsWindow.KeyDown = HandleKey GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( 253, 252, 251 ) While "True" BOXES = 4 ' number of boxes per piece BWIDTH = 25 ' box width in pixels XOFFSET = 40 ' Screen X offset in pixels of where the board starts YOFFSET = 40 ' Screen Y offset in pixels of where the board starts CWIDTH = 10 ' Canvas Width, in number of boxes CHEIGHT = 20 ' Canvas Height, in number of boxes. STARTDELAY = 800 ENDDELAY = 175 PREVIEW_xpos = 13 PREVIEW_ypos = 2 GraphicsWindow.Clear() GraphicsWindow.Title = "Small Basic Tetris" GraphicsWindow.Height = 580 GraphicsWindow.Width = 700 GraphicsWindow.Show() SetupTemplates() SetupCanvas() MainLoop() GraphicsWindow.ShowMessage( "Game Over", "Small Basic Tetris" ) EndWhile Sub MainLoop template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h end = 0 sessionDelay = STARTDELAY While end = 0 If sessionDelay > ENDDELAY Then sessionDelay = sessionDelay - 1 EndIf delay = sessionDelay thisPiece = nextPiece template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h DrawPreviewPiece() h = thisPiece ypos = 0 done = 0 xpos = 3 ' always drop from column 3 CheckStop() ' in: ypos, xpos, h ret: done If done = 1 Then ypos = ypos - 1 MovePiece() 'in: ypos, xpos, h end = 1 EndIf yposdelta = 0 While done = 0 Or yposdelta > 0 MovePiece() 'in: ypos, xpos, h ' Delay, but break if the delay get set to 0 if the piece gets dropped delayIndex = delay While delayIndex > 0 And delay > 0 Program.Delay(10) delayIndex = delayIndex - 10 EndWhile If yposdelta > 0 Then yposdelta = yposdelta - 1 ' used to create freespin, when the piece is rotated Else ypos = ypos + 1 ' otherwise, move the piece down. EndIf ' Check if the piece should stop. CheckStop() ' in: ypos, xpos, h ret: done EndWhile EndWhile EndSub Sub HandleKey ' Stop game If GraphicsWindow.LastKey = "Escape" Then Program.End() EndIf ' Move piece left If GraphicsWindow.LastKey = "Left" Then moveDirection = -1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece right If GraphicsWindow.LastKey = "Right" Then moveDirection = 1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece down If GraphicsWindow.LastKey = "Down" or GraphicsWindow.LastKey = "Space" Then delay = 0 EndIf ' Rotate piece If GraphicsWindow.LastKey = "Up" Then basetemplate = Array.GetValue(h, -1) ' Array.GetValue(h, -1) = the template name template = "temptemplate" rotation = "CW" CopyPiece() 'in basetemplate, template, rotation Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name moveDirection = 0 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 ' See if it can be moved so that it will rotate. xposbk = xpos yposdelta = 0 While yposdelta = 0 And Math.Abs(xposbk - xpos) < 3 ' move up to 3 times only ' if the rotation move worked, copy the temp to "rotatedtemplate" and use that from now on If invalidMove = 0 Then basetemplate = template template = "rotatedtemplate" Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name rotation = "COPY" CopyPiece() 'in basetemplate, template, rotation yposdelta = 1 ' Don't move down if we rotate MovePiece() 'in: ypos, xpos, h ElseIf invalidMove = 2 Then ' Don't support shifting piece when hitting another piece to the right or left. xpos = 99 ' exit the loop Else ' if the rotated piece can't be placed, move it left or right and try again. xpos = xpos - invalidMove ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 EndIf EndWhile If invalidMove <> 0 Then xpos = xposbk Array.SetValue(h, -1, basetemplate) ' Array.GetValue(h, -1) = the template name template = "" EndIf EndIf EndSub Sub DrawPreviewPiece xpos = PREVIEW_xpos ypos = PREVIEW_ypos h = nextPiece XOFFSETBK = XOFFSET YOFFSETBK = YOFFSET XOFFSET = XOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewx") ' Array.GetValue(h, -1) = the template name YOFFSET = YOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewy") ' Array.GetValue(h, -1) = the template name MovePiece() 'in: ypos, xpos, h XOFFSET = XOFFSETBK YOFFSET = YOFFSETBK EndSub ' creates template that's a rotated basetemplate Sub CopyPiece 'in basetemplate, template, rotation L = Array.GetValue(basetemplate, "dim") If rotation = "CW" Then For i = 0 to BOXES - 1 ' x' = y y' = L - 1 - x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (Math.Remainder(v, 10)) y = (L - 1 - Math.Floor(v/10)) Array.SetValue(template, i, x * 10 + y) EndFor ' Count-Cockwise is not currently used ElseIf rotation = "CCW" Then For i = 0 to BOXES - 1 ' x' = L - 1 - y y' = x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (L - 1 - Math.Remainder(v, 10)) y = Math.Floor(v/10) Array.SetValue(template, i, x * 10 + y) EndFor ElseIf rotation = "COPY" Then For i = 0 to BOXES - 1 Array.SetValue(template, i, Array.GetValue(basetemplate, i)) EndFor Else GraphicsWindow.ShowMessage("invalid parameter", "Error") Program.End() EndIf ' Copy the remain properties from basetemplate to template. Array.SetValue(template, "color", Array.GetValue(basetemplate, "color")) Array.SetValue(template, "dim", Array.GetValue(basetemplate, "dim")) Array.SetValue(template, "pviewx", Array.GetValue(basetemplate, "pviewx")) Array.SetValue(template, "pviewy", Array.GetValue(basetemplate, "pviewy")) EndSub Sub CreatePiece ' in: template ret: h ' Create a new handle, representing an arrayName, that will represent the piece hcount = hcount + 1 h = Text.Append("piece", hcount) Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = Array.GetValue(template, "color") For i = 0 to BOXES - 1 s = Shapes.AddRectangle(BWIDTH, BWIDTH) Shapes.Move(s, -BWIDTH, -BWIDTH) ' move off screen Array.SetValue(h, i, s) EndFor EndSub Sub MovePiece 'in: ypos, xpos, h. ypos/xpos is 0-19, representing the top/left box coordinate of the piece on the canvas. h returned by CreatePiece For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name x = Math.Floor(v/10) y = Math.Remainder(v, 10) ' Array.GetValue(h, i) = box for piece h. ' xpos/ypos = are topleft of shape. x/y is the box offset within the shape. Shapes.Move(Array.GetValue(h, i), XOFFSET + xpos * BWIDTH + x * BWIDTH, YOFFSET + ypos * BWIDTH + y * BWIDTH) EndFor EndSub Sub ValidateMove ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 i = 0 invalidMove = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If (x + xpos + moveDirection) < 0 Then invalidMove = -1 i = BOXES ' force getting out of the loop EndIf If (x + xpos + moveDirection) >= CWIDTH Then invalidMove = 1 i = BOXES ' force getting out of the loop EndIf If Array.GetValue("c", (x + xpos + moveDirection) + (y + ypos) * CWIDTH) <> "." Then invalidMove = 2 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile EndSub Sub CheckStop ' in: ypos, xpos, h ret: done done = 0 i = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If y + ypos > CHEIGHT Or Array.GetValue("c", (x + xpos) + (y + ypos) * CWIDTH) <> "." Then done = 1 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile ' If we need to stop the piece, move the box handles to the canvas If done = 1 Then For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) Array.SetValue("c", (Math.Floor(v/10) + xpos) + (Math.Remainder(v, 10) + ypos - 1) * CWIDTH, Array.GetValue(h, i)) EndFor ' 1 points for every piece successfully dropped score = score + 1 PrintScore() ' Delete clared lines DeleteLines() EndIf EndSub Sub DeleteLines linesCleared = 0 ' Iterate over each row, starting from the bottom For y = CHEIGHT - 1 to 0 Step -1 ' Check to see if the whole row is filled x = CWIDTH While x = CWIDTH x = 0 While x < CWIDTH piece = Array.GetValue("c", x + y * CWIDTH) If piece = "." then x = CWIDTH EndIf x = x + 1 EndWhile ' if non of them were empty (i.e "."), then remove the line. If x = CWIDTH Then ' Delete the line For x1 = 0 to CWIDTH - 1 Shapes.Remove(Array.GetValue("c", x1 + y * CWIDTH)) EndFor linesCleared = linesCleared + 1 ' Move everything else down one. For y1 = y To 1 Step -1 For x1 = 0 to CWIDTH - 1 piece = Array.GetValue("c", x1 + (y1 - 1) * CWIDTH) Array.SetValue("c", x1 + y1 * CWIDTH, piece) Shapes.Move(piece, Shapes.GetLeft(piece), Shapes.GetTop(piece) + BWIDTH) EndFor EndFor EndIf EndWhile EndFor If linesCleared > 0 Then score = score + 100 * Math.Round(linesCleared * 2.15 - 1) PrintScore() EndIf EndSub Sub SetupCanvas ' GraphicsWindow.DrawResizedImage( Flickr.GetRandomPicture( "bricks" ), 0, 0, GraphicsWindow.Width, GraphicsWindow.Height) GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor GraphicsWindow.FillRectangle(XOFFSET, YOFFSET, CWIDTH*BWIDTH, CHEIGHT*BWIDTH) Program.Delay(200) GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Pink" For x = 0 To CWIDTH-1 For y = 0 To CHEIGHT-1 Array.SetValue("c", x + y * CWIDTH, ".") ' "." indicates spot is free GraphicsWindow.DrawRectangle(XOFFSET + x * BWIDTH, YOFFSET + y * BWIDTH, BWIDTH, BWIDTH) EndFor EndFor GraphicsWindow.PenWidth = 4 GraphicsWindow.PenColor = "Black" GraphicsWindow.DrawLine(XOFFSET, YOFFSET, XOFFSET, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH, YOFFSET, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET, YOFFSET + CHEIGHT*BWIDTH, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.PenColor = "Lime" GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET, XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 4, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 4) GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = "Pink" x = XOFFSET + PREVIEW_xpos * BWIDTH - BWIDTH y = YOFFSET + PREVIEW_ypos * BWIDTH - BWIDTH GraphicsWindow.FillRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.DrawRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.FillRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.DrawRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontItalic = "False" GraphicsWindow.FontName = "Comic Sans MS" GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x, y + 200, "Game control keys:") GraphicsWindow.DrawText(x + 25, y + 220, "Left Arrow = Move piece left") GraphicsWindow.DrawText(x + 25, y + 240, "Right Arrow = Move piece right") GraphicsWindow.DrawText(x + 25, y + 260, "Up Arrow = Rotate piece") GraphicsWindow.DrawText(x + 25, y + 280, "Down Arrow = Drop piece") GraphicsWindow.DrawText(x, y + 320, "Press to stop game") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontName = "Georgia" GraphicsWindow.FontItalic = "True" GraphicsWindow.FontSize = 36 GraphicsWindow.DrawText(x - 20, y + 400, "Small Basic Tetris") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x - 20, y + 440, "ver.0.1") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score score = 0 PrintScore() EndSub Sub PrintScore GraphicsWindow.PenWidth = 4 GraphicsWindow.BrushColor = "Pink" GraphicsWindow.FillRectangle(500, 65, 153, 50) GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawRectangle(500, 65, 153, 50) GraphicsWindow.FontItalic = "False" GraphicsWindow.FontSize = 32 GraphicsWindow.FontName = "Impact" GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawText(505, 70, Text.Append(Text.GetSubText( "00000000", 0, 8 - Text.GetLength( score ) ), score)) EndSub Sub SetupTemplates ' each piece has 4 boxes. ' the index of each entry within a piece represents the box number (1-4) ' the value of each entry represents to box zero-based box coordinate within the piece: tens place is x, ones place y '_X_ '_X_ '_XX Array.SetValue("template1", 0, 10) Array.SetValue("template1", 1, 11) Array.SetValue("template1", 2, 12) Array.SetValue("template1", 3, 22) Array.SetValue("template1", "color", "Yellow") Array.SetValue("template1", "dim", 3) Array.SetValue("template1", "pviewx", -12) Array.SetValue("template1", "pviewy", 12) '_X_ '_X_ 'XX_ Array.SetValue("template2", 0, 10) Array.SetValue("template2", 1, 11) Array.SetValue("template2", 2, 12) Array.SetValue("template2", 3, 02) Array.SetValue("template2", "color", "Magenta") Array.SetValue("template2", "dim", 3) Array.SetValue("template2", "pviewx", 12) Array.SetValue("template2", "pviewy", 12) '_X_ 'XXX '_ Array.SetValue("template3", 0, 10) Array.SetValue("template3", 1, 01) Array.SetValue("template3", 2, 11) Array.SetValue("template3", 3, 21) Array.SetValue("template3", "color", "Gray") Array.SetValue("template3", "dim", 3) Array.SetValue("template3", "pviewx", 0) Array.SetValue("template3", "pviewy", 25) 'XX_ 'XX_ '_ Array.SetValue("template4", 0, 00) Array.SetValue("template4", 1, 10) Array.SetValue("template4", 2, 01) Array.SetValue("template4", 3, 11) Array.SetValue("template4", "color", "Cyan") Array.SetValue("template4", "dim", 2) Array.SetValue("template4", "pviewx", 12) Array.SetValue("template4", "pviewy", 25) 'XX_ '_XX '_ Array.SetValue("template5", 0, 00) Array.SetValue("template5", 1, 10) Array.SetValue("template5", 2, 11) Array.SetValue("template5", 3, 21) Array.SetValue("template5", "color", "Green") Array.SetValue("template5", "dim", 3) Array.SetValue("template5", "pviewx", 0) Array.SetValue("template5", "pviewy", 25) '_XX 'XX_ '_ Array.SetValue("template6", 0, 10) Array.SetValue("template6", 1, 20) Array.SetValue("template6", 2, 01) Array.SetValue("template6", 3, 11) Array.SetValue("template6", "color", "Blue") Array.SetValue("template6", "dim", 3) Array.SetValue("template6", "pviewx", 0) Array.SetValue("template6", "pviewy", 25) '_X '_X '_X '_X Array.SetValue("template7", 0, 10) Array.SetValue("template7", 1, 11) Array.SetValue("template7", 2, 12) Array.SetValue("template7", 3, 13) Array.SetValue("template7", "color", "Red") Array.SetValue("template7", "dim", 4) Array.SetValue("template7", "pviewx", 0) Array.SetValue("template7", "pviewy", 0) EndSub

Dold text

för långt?

"Några" rader

Visa signatur

I7 3770 | 2st R9 290 (Crossfire) | 32GB 1866Mhz Ram | Asus Rog Maximus IV Extreme | Cooler Master Nepton 280L | Cooler Master V1000 (1000W) | Cooler Master HAF XB Evo | 2st Seagate Barracuda 2TB 3.5 HDD's |

Permalänk

bara 536

Visa signatur

Ursäktar för stavfel

Permalänk
Medlem
Skrivet av JOANATAN5354:

såklart!

GraphicsWindow.KeyDown = HandleKey GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( 253, 252, 251 ) While "True" BOXES = 4 ' number of boxes per piece BWIDTH = 25 ' box width in pixels XOFFSET = 40 ' Screen X offset in pixels of where the board starts YOFFSET = 40 ' Screen Y offset in pixels of where the board starts CWIDTH = 10 ' Canvas Width, in number of boxes CHEIGHT = 20 ' Canvas Height, in number of boxes. STARTDELAY = 800 ENDDELAY = 175 PREVIEW_xpos = 13 PREVIEW_ypos = 2 GraphicsWindow.Clear() GraphicsWindow.Title = "Small Basic Tetris" GraphicsWindow.Height = 580 GraphicsWindow.Width = 700 GraphicsWindow.Show() SetupTemplates() SetupCanvas() MainLoop() GraphicsWindow.ShowMessage( "Game Over", "Small Basic Tetris" ) EndWhile Sub MainLoop template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h end = 0 sessionDelay = STARTDELAY While end = 0 If sessionDelay > ENDDELAY Then sessionDelay = sessionDelay - 1 EndIf delay = sessionDelay thisPiece = nextPiece template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h DrawPreviewPiece() h = thisPiece ypos = 0 done = 0 xpos = 3 ' always drop from column 3 CheckStop() ' in: ypos, xpos, h ret: done If done = 1 Then ypos = ypos - 1 MovePiece() 'in: ypos, xpos, h end = 1 EndIf yposdelta = 0 While done = 0 Or yposdelta > 0 MovePiece() 'in: ypos, xpos, h ' Delay, but break if the delay get set to 0 if the piece gets dropped delayIndex = delay While delayIndex > 0 And delay > 0 Program.Delay(10) delayIndex = delayIndex - 10 EndWhile If yposdelta > 0 Then yposdelta = yposdelta - 1 ' used to create freespin, when the piece is rotated Else ypos = ypos + 1 ' otherwise, move the piece down. EndIf ' Check if the piece should stop. CheckStop() ' in: ypos, xpos, h ret: done EndWhile EndWhile EndSub Sub HandleKey ' Stop game If GraphicsWindow.LastKey = "Escape" Then Program.End() EndIf ' Move piece left If GraphicsWindow.LastKey = "Left" Then moveDirection = -1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece right If GraphicsWindow.LastKey = "Right" Then moveDirection = 1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece down If GraphicsWindow.LastKey = "Down" or GraphicsWindow.LastKey = "Space" Then delay = 0 EndIf ' Rotate piece If GraphicsWindow.LastKey = "Up" Then basetemplate = Array.GetValue(h, -1) ' Array.GetValue(h, -1) = the template name template = "temptemplate" rotation = "CW" CopyPiece() 'in basetemplate, template, rotation Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name moveDirection = 0 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 ' See if it can be moved so that it will rotate. xposbk = xpos yposdelta = 0 While yposdelta = 0 And Math.Abs(xposbk - xpos) < 3 ' move up to 3 times only ' if the rotation move worked, copy the temp to "rotatedtemplate" and use that from now on If invalidMove = 0 Then basetemplate = template template = "rotatedtemplate" Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name rotation = "COPY" CopyPiece() 'in basetemplate, template, rotation yposdelta = 1 ' Don't move down if we rotate MovePiece() 'in: ypos, xpos, h ElseIf invalidMove = 2 Then ' Don't support shifting piece when hitting another piece to the right or left. xpos = 99 ' exit the loop Else ' if the rotated piece can't be placed, move it left or right and try again. xpos = xpos - invalidMove ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 EndIf EndWhile If invalidMove <> 0 Then xpos = xposbk Array.SetValue(h, -1, basetemplate) ' Array.GetValue(h, -1) = the template name template = "" EndIf EndIf EndSub Sub DrawPreviewPiece xpos = PREVIEW_xpos ypos = PREVIEW_ypos h = nextPiece XOFFSETBK = XOFFSET YOFFSETBK = YOFFSET XOFFSET = XOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewx") ' Array.GetValue(h, -1) = the template name YOFFSET = YOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewy") ' Array.GetValue(h, -1) = the template name MovePiece() 'in: ypos, xpos, h XOFFSET = XOFFSETBK YOFFSET = YOFFSETBK EndSub ' creates template that's a rotated basetemplate Sub CopyPiece 'in basetemplate, template, rotation L = Array.GetValue(basetemplate, "dim") If rotation = "CW" Then For i = 0 to BOXES - 1 ' x' = y y' = L - 1 - x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (Math.Remainder(v, 10)) y = (L - 1 - Math.Floor(v/10)) Array.SetValue(template, i, x * 10 + y) EndFor ' Count-Cockwise is not currently used ElseIf rotation = "CCW" Then For i = 0 to BOXES - 1 ' x' = L - 1 - y y' = x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (L - 1 - Math.Remainder(v, 10)) y = Math.Floor(v/10) Array.SetValue(template, i, x * 10 + y) EndFor ElseIf rotation = "COPY" Then For i = 0 to BOXES - 1 Array.SetValue(template, i, Array.GetValue(basetemplate, i)) EndFor Else GraphicsWindow.ShowMessage("invalid parameter", "Error") Program.End() EndIf ' Copy the remain properties from basetemplate to template. Array.SetValue(template, "color", Array.GetValue(basetemplate, "color")) Array.SetValue(template, "dim", Array.GetValue(basetemplate, "dim")) Array.SetValue(template, "pviewx", Array.GetValue(basetemplate, "pviewx")) Array.SetValue(template, "pviewy", Array.GetValue(basetemplate, "pviewy")) EndSub Sub CreatePiece ' in: template ret: h ' Create a new handle, representing an arrayName, that will represent the piece hcount = hcount + 1 h = Text.Append("piece", hcount) Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = Array.GetValue(template, "color") For i = 0 to BOXES - 1 s = Shapes.AddRectangle(BWIDTH, BWIDTH) Shapes.Move(s, -BWIDTH, -BWIDTH) ' move off screen Array.SetValue(h, i, s) EndFor EndSub Sub MovePiece 'in: ypos, xpos, h. ypos/xpos is 0-19, representing the top/left box coordinate of the piece on the canvas. h returned by CreatePiece For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name x = Math.Floor(v/10) y = Math.Remainder(v, 10) ' Array.GetValue(h, i) = box for piece h. ' xpos/ypos = are topleft of shape. x/y is the box offset within the shape. Shapes.Move(Array.GetValue(h, i), XOFFSET + xpos * BWIDTH + x * BWIDTH, YOFFSET + ypos * BWIDTH + y * BWIDTH) EndFor EndSub Sub ValidateMove ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 i = 0 invalidMove = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If (x + xpos + moveDirection) < 0 Then invalidMove = -1 i = BOXES ' force getting out of the loop EndIf If (x + xpos + moveDirection) >= CWIDTH Then invalidMove = 1 i = BOXES ' force getting out of the loop EndIf If Array.GetValue("c", (x + xpos + moveDirection) + (y + ypos) * CWIDTH) <> "." Then invalidMove = 2 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile EndSub Sub CheckStop ' in: ypos, xpos, h ret: done done = 0 i = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If y + ypos > CHEIGHT Or Array.GetValue("c", (x + xpos) + (y + ypos) * CWIDTH) <> "." Then done = 1 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile ' If we need to stop the piece, move the box handles to the canvas If done = 1 Then For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) Array.SetValue("c", (Math.Floor(v/10) + xpos) + (Math.Remainder(v, 10) + ypos - 1) * CWIDTH, Array.GetValue(h, i)) EndFor ' 1 points for every piece successfully dropped score = score + 1 PrintScore() ' Delete clared lines DeleteLines() EndIf EndSub Sub DeleteLines linesCleared = 0 ' Iterate over each row, starting from the bottom For y = CHEIGHT - 1 to 0 Step -1 ' Check to see if the whole row is filled x = CWIDTH While x = CWIDTH x = 0 While x < CWIDTH piece = Array.GetValue("c", x + y * CWIDTH) If piece = "." then x = CWIDTH EndIf x = x + 1 EndWhile ' if non of them were empty (i.e "."), then remove the line. If x = CWIDTH Then ' Delete the line For x1 = 0 to CWIDTH - 1 Shapes.Remove(Array.GetValue("c", x1 + y * CWIDTH)) EndFor linesCleared = linesCleared + 1 ' Move everything else down one. For y1 = y To 1 Step -1 For x1 = 0 to CWIDTH - 1 piece = Array.GetValue("c", x1 + (y1 - 1) * CWIDTH) Array.SetValue("c", x1 + y1 * CWIDTH, piece) Shapes.Move(piece, Shapes.GetLeft(piece), Shapes.GetTop(piece) + BWIDTH) EndFor EndFor EndIf EndWhile EndFor If linesCleared > 0 Then score = score + 100 * Math.Round(linesCleared * 2.15 - 1) PrintScore() EndIf EndSub Sub SetupCanvas ' GraphicsWindow.DrawResizedImage( Flickr.GetRandomPicture( "bricks" ), 0, 0, GraphicsWindow.Width, GraphicsWindow.Height) GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor GraphicsWindow.FillRectangle(XOFFSET, YOFFSET, CWIDTH*BWIDTH, CHEIGHT*BWIDTH) Program.Delay(200) GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Pink" For x = 0 To CWIDTH-1 For y = 0 To CHEIGHT-1 Array.SetValue("c", x + y * CWIDTH, ".") ' "." indicates spot is free GraphicsWindow.DrawRectangle(XOFFSET + x * BWIDTH, YOFFSET + y * BWIDTH, BWIDTH, BWIDTH) EndFor EndFor GraphicsWindow.PenWidth = 4 GraphicsWindow.PenColor = "Black" GraphicsWindow.DrawLine(XOFFSET, YOFFSET, XOFFSET, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH, YOFFSET, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET, YOFFSET + CHEIGHT*BWIDTH, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.PenColor = "Lime" GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET, XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 4, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 4) GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = "Pink" x = XOFFSET + PREVIEW_xpos * BWIDTH - BWIDTH y = YOFFSET + PREVIEW_ypos * BWIDTH - BWIDTH GraphicsWindow.FillRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.DrawRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.FillRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.DrawRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontItalic = "False" GraphicsWindow.FontName = "Comic Sans MS" GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x, y + 200, "Game control keys:") GraphicsWindow.DrawText(x + 25, y + 220, "Left Arrow = Move piece left") GraphicsWindow.DrawText(x + 25, y + 240, "Right Arrow = Move piece right") GraphicsWindow.DrawText(x + 25, y + 260, "Up Arrow = Rotate piece") GraphicsWindow.DrawText(x + 25, y + 280, "Down Arrow = Drop piece") GraphicsWindow.DrawText(x, y + 320, "Press to stop game") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontName = "Georgia" GraphicsWindow.FontItalic = "True" GraphicsWindow.FontSize = 36 GraphicsWindow.DrawText(x - 20, y + 400, "Small Basic Tetris") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x - 20, y + 440, "ver.0.1") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score score = 0 PrintScore() EndSub Sub PrintScore GraphicsWindow.PenWidth = 4 GraphicsWindow.BrushColor = "Pink" GraphicsWindow.FillRectangle(500, 65, 153, 50) GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawRectangle(500, 65, 153, 50) GraphicsWindow.FontItalic = "False" GraphicsWindow.FontSize = 32 GraphicsWindow.FontName = "Impact" GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawText(505, 70, Text.Append(Text.GetSubText( "00000000", 0, 8 - Text.GetLength( score ) ), score)) EndSub Sub SetupTemplates ' each piece has 4 boxes. ' the index of each entry within a piece represents the box number (1-4) ' the value of each entry represents to box zero-based box coordinate within the piece: tens place is x, ones place y '_X_ '_X_ '_XX Array.SetValue("template1", 0, 10) Array.SetValue("template1", 1, 11) Array.SetValue("template1", 2, 12) Array.SetValue("template1", 3, 22) Array.SetValue("template1", "color", "Yellow") Array.SetValue("template1", "dim", 3) Array.SetValue("template1", "pviewx", -12) Array.SetValue("template1", "pviewy", 12) '_X_ '_X_ 'XX_ Array.SetValue("template2", 0, 10) Array.SetValue("template2", 1, 11) Array.SetValue("template2", 2, 12) Array.SetValue("template2", 3, 02) Array.SetValue("template2", "color", "Magenta") Array.SetValue("template2", "dim", 3) Array.SetValue("template2", "pviewx", 12) Array.SetValue("template2", "pviewy", 12) '_X_ 'XXX '_ Array.SetValue("template3", 0, 10) Array.SetValue("template3", 1, 01) Array.SetValue("template3", 2, 11) Array.SetValue("template3", 3, 21) Array.SetValue("template3", "color", "Gray") Array.SetValue("template3", "dim", 3) Array.SetValue("template3", "pviewx", 0) Array.SetValue("template3", "pviewy", 25) 'XX_ 'XX_ '_ Array.SetValue("template4", 0, 00) Array.SetValue("template4", 1, 10) Array.SetValue("template4", 2, 01) Array.SetValue("template4", 3, 11) Array.SetValue("template4", "color", "Cyan") Array.SetValue("template4", "dim", 2) Array.SetValue("template4", "pviewx", 12) Array.SetValue("template4", "pviewy", 25) 'XX_ '_XX '_ Array.SetValue("template5", 0, 00) Array.SetValue("template5", 1, 10) Array.SetValue("template5", 2, 11) Array.SetValue("template5", 3, 21) Array.SetValue("template5", "color", "Green") Array.SetValue("template5", "dim", 3) Array.SetValue("template5", "pviewx", 0) Array.SetValue("template5", "pviewy", 25) '_XX 'XX_ '_ Array.SetValue("template6", 0, 10) Array.SetValue("template6", 1, 20) Array.SetValue("template6", 2, 01) Array.SetValue("template6", 3, 11) Array.SetValue("template6", "color", "Blue") Array.SetValue("template6", "dim", 3) Array.SetValue("template6", "pviewx", 0) Array.SetValue("template6", "pviewy", 25) '_X '_X '_X '_X Array.SetValue("template7", 0, 10) Array.SetValue("template7", 1, 11) Array.SetValue("template7", 2, 12) Array.SetValue("template7", 3, 13) Array.SetValue("template7", "color", "Red") Array.SetValue("template7", "dim", 4) Array.SetValue("template7", "pviewx", 0) Array.SetValue("template7", "pviewy", 0) EndSub

Dold text

för långt?
536 rader

Tetris

Visa signatur

sebbeboy -Vad är det som är så roligt med att sitta och navigera där med muspekaren? För det är väl det enda vettiga man kan göra i startmenyn?
Tvelander -För den är 10000000000x mer logisk än att sitta leta runt i ett satans fan fullscreen fönster med massa grafik och skit bajs som funkar kiss dåligt på PC.
Kör start8 nu annars får jag FLIPP på windows 8

Permalänk

ja

Visa signatur

Ursäktar för stavfel

Permalänk
Medlem

GraphicsWindow.Title = "Small Basic Tetris"

lol ..

Permalänk
Medlem

Beror nog på paradigm, utvecklarens kunskap, språket osv hur lätt det är för en "dödlig" att förstå.

Visa signatur

Huvudriggen är en Gigabyte Aorus Xtreme | 128gb DDR5 6000 | Ryzen 7950X | 3080Ti
Utöver det är det för många datorer, boxar och servar för att lista :P

Permalänk
Medlem
Skrivet av JOANATAN5354:

Vilket språk var det? VB? Java? Har tyvärr bara läst C, C++ än så länge men mer språk ska det bli!

Visa signatur

OS: MacOS/ Windows 10 Pro 64-bit MB: ASUS-Z97-A CPU: i7 4790k
NÄTAGG: EVGA SUPERNOVA G2
RAM: 32768 MiB GPU: 1070 FTW Chassi: Fractal Design R4
MBP 13" i5 | 256GB | 16GB RAM | MID 2014

Permalänk
Skrivet av ChickenNoodles4U:

Själv så har man hållit på med HTML och CSS i ett tag i skolan och kollat runt lite på Java, Python och C# så jag vet ju på ett ungefär vad de kan stå på en kod remsa/linje.

Fast hur ser andra på programmering. Folk som inte har lärt sig något om de eller bara hört talas om de fast inte använder de. Hur ser de på koderna, vet de på ett ungefär vad de olika koderna betyder?

здравствуйте Мой русский не очень хорошо, но вы можете прочитать это ?

kan du förstå det utan att använda google translate? eller finns det vissa ord som man kan gissa sig till vad det betyder.

Москва
библиотека
метро
водка
автомобиль
полиция
кофе
карта

Visa signatur

hej Achmed Länken till bästa tråden #15549644

Permalänk
Entusiast

Intressant är också hur andra programmerare ser på ens egen programmering.
Det som jag själv anser vara ordning och reda, klartext och självklart, kan ses som spagetti, röra och anarki i en annan programmerares ögon.
Det är där kommentarer kommer in i bilden, så man får skriva av sig i hur man tänker. Efter en lång harang av kommentarer, så trillar tioöringen ner hos en del andra programmerare. -"Aha! Du tänker SÅÅÅ!"

Sedan stöter man på de där "elitisterna" (I brist på bättre beskrivning) som anser att alla som inte har samma tänk som dem själva är rörigt och otydligt. Sedan spelar det ingen roll hur logiskt och klart det i själva verket är. De har själva inte skrivit koden = total röra och måste göras om från scratch.

Visa signatur

Bästa programmen till Linux - v2.0
Linux-guide: Val av grafisk miljö. (Att välja distribution).
-
Everyone should have a SGoC in their systems (SGoC: SysGhost on a Chip)

Permalänk
Medlem
Skrivet av ChickenNoodles4U:

Själv så har man hållit på med HTML och CSS i ett tag i skolan och kollat runt lite på Java, Python och C# så jag vet ju på ett ungefär vad de kan stå på en kod remsa/linje.

Fast hur ser andra på programmering. Folk som inte har lärt sig något om de eller bara hört talas om de fast inte använder de. Hur ser de på koderna, vet de på ett ungefär vad de olika koderna betyder?

Min första tanke är ju att HTML och CSS inte är programmering, det är markup (textformatering).

Jag personligen är inte så fixerad vid hur kod skrivs, men blir förvirrad av hög abstraktionsgrad och arg när koden inte är uppdelad. Det viktigaste är att koden är läsbar och modifieringsbar. Ett kodstycke som är svåranpassat är ett dåligt kodstycke och bör skrivas om till en mer hanterbar standard.

Visa signatur

Desktop: | Win10 | InWin 303 | ASUS TUF X570 | AMD Ryzen 5 3600 | Noctua NH-U12S (PP) | Intel 600p 256GB | Gigabyte GTX 670 | 32GB DDR4 2400Mhz | Corsair RM650x | 3x 1080 Screens |
Datacenter: | 1x Physical | 1x Virtual |
Laptop: | 2x |

Dell Certified Technician

Permalänk
Avstängd
Skrivet av SysGhost:

Intressant är också hur andra programmerare ser på ens egen programmering.
Det som jag själv anser vara ordning och reda, klartext och självklart, kan ses som spagetti, röra och anarki i en annan programmerares ögon.
Det är där kommentarer kommer in i bilden, så man får skriva av sig i hur man tänker. Efter en lång harang av kommentarer, så trillar tioöringen ner hos en del andra programmerare. -"Aha! Du tänker SÅÅÅ!"

Sedan stöter man på de där "elitisterna" (I brist på bättre beskrivning) som anser att alla som inte har samma tänk som dem själva är rörigt och otydligt. Sedan spelar det ingen roll hur logiskt och klart det i själva verket är. De har själva inte skrivit koden = total röra och måste göras om från scratch.

Kod ska vara självdokumenterande, klassnamn, metodnamn, längd på klasser och metoder etc ska vara anpassade så att kommentarer inte krävs. Tex istället för att skriva en komentar bryt ut koden till en metod som heter något vettigt så blir kommentaren öveflödig

Visa signatur
Permalänk
Medlem
Skrivet av CyberVillain:

Kod ska vara självdokumenterande, klassnamn, metodnamn, längd på klasser och metoder etc ska vara anpassade så att kommentarer inte krävs. Tex istället för att skriva en komentar bryt ut koden till en metod som heter något vettigt så blir kommentaren öveflödig

Håller med där. Sen kan det vara nice med kommentarer som förklarar den övergripande tanken och anledningar till varför man implementerat något så som man gjort, men själva koden skall vara tydlig ändå. Finns inget mer irriterande än en tydlig och kort metod som är fylld med onödiga kommentarer.

Visa signatur

Fractal Design Define R5 | MSI Z97-GD65 Gaming | MSI Geforce GTX 970 Gaming 4G | Intel i5 4690k | Cooler Master Hyper 212 EVO | EVGA Supernova G2 750W | 2x8GB Corsair Vengeance Low Profile DDR3 1600Mhz | Samsung 850 EVO | Seagate 1TB SATA3.5

Permalänk
Avstängd

Då är det bättre med något Visio-flödesdiagram eller så separat från koden

Visa signatur
Permalänk
Skrivet av Tobberoth:

Håller med där. Sen kan det vara nice med kommentarer som förklarar den övergripande tanken och anledningar till varför man implementerat något så som man gjort, men själva koden skall vara tydlig ändå. Finns inget mer irriterande än en tydlig och kort metod som är fylld med onödiga kommentarer.

har du något exempel JOANATAN5354 kod så står det ju "GraphicsWindow.Title = "Small Basic Tetris"" längst upp då blir det svårt för en som inte kan programmering att INTE förstå.

tanken var väl att man inte bara ska läsa texten utan förstå koden utan att det står i klarspråk vad den gör. sen går det väl att ha en miniräknare där det står Small Basic Tetris det gör ju inte att det är tetris ?

Visa signatur

hej Achmed Länken till bästa tråden #15549644

Permalänk
Medlem
Skrivet av ChickenNoodles4U:

Själv så har man hållit på med HTML och CSS i ett tag i skolan och kollat runt lite på Java, Python och C# så jag vet ju på ett ungefär vad de kan stå på en kod remsa/linje.

Fast hur ser andra på programmering. Folk som inte har lärt sig något om de eller bara hört talas om de fast inte använder de. Hur ser de på koderna, vet de på ett ungefär vad de olika koderna betyder?

Jag tror att det blir lättare att förstå koden om man samtidigt har tillgång till programmet. En väldigt simpel konsolapplikation där man har tillgång till programmet och koden kan nog en hel del förstå, tror jag. Men att vem som helst skulle förstå någonting av lite mer komplex kod utan någon som helst förklaring, det känns orimligt.

Visa signatur

Bästa trådstarten någonsin.

Asus Zenbook UX430: 8550U, MX150, 16 GiB, 1 TB

Permalänk
Medlem
Skrivet av CyberVillain:

Kod ska vara självdokumenterande, klassnamn, metodnamn, längd på klasser och metoder etc ska vara anpassade så att kommentarer inte krävs. Tex istället för att skriva en komentar bryt ut koden till en metod som heter något vettigt så blir kommentaren öveflödig

Håller inte med. Även om man förstår syftet så betyder det inte att man vet om metoden kastar ett exception ifall man ger ett nullvärde som parameter eller om det accepterar nullvärdet. Om det accepteras vad händer med det då? Självbeskrivande kod är viktigt, det ska vara så lätt som möjligt att läsa koden, men det är också viktigt att bakgrundsdetaljer beskrivs i kodens dokumentation.

Permalänk
Avstängd

du ser ju rätt enkelt om du går in i metoden vad den har för Code Contracts

Visa signatur
Permalänk
Datavetare
Skrivet av CyberVillain:

du ser ju rätt enkelt om du går in i metoden vad den har för Code Contracts

Kommenterar ska inte svara på frågan vad koden gör, det står ju redan (om det är vettig skrivet). Undantaget är kommenterar för funktioner/metoder som måste tala om vad funktionen/metoden faktiskt gör.

Kommenterar i koden ska svara på frågan varför, det är något du har väldigt svårt att beskriva med något som code contracts. En viss konstruktion kanske måste se ut på ett viss sätt för att en standard säger så, underliggande HW kanske har ett viss beteende (ARM kräver att saker flushas före/efter DMA, men krävs inte på t.ex. x86) etc.

Saker som Contract.Ensures() kommer med stor sannolikhet bara leda till "sloppy programming" precis som undantag väldigt ofta leder till detta. Som stressad programmerare tar man den enkla vägen ut och använder sig av "Ensures" eller drar i väg ett exception, men då har man bara skapat ett NAP (Någon Annans Problem).

Är faktiskt helt fantastiskt att hur ofta man faktiskt kan skriva koden så att den helt enkelt inte kan gå fel om man har tillräckligt starkt incitament, t.ex. om man skriver kod för säkerhetskritiska system (safety critical systems) där i de högst klassade systemen saker bara få gå fel i en initieringsfas, lyckas denna måste den tjänst programmet utför alltid lyckas (och det ska vara bevisat att det är så). Går att skriva förvånansvärt komplexa program under dessa restriktioner.

Visa signatur

Care About Your Craft: Why spend your life developing software unless you care about doing it well? - The Pragmatic Programmer

Permalänk
Avstängd

exemplet med Code contracts var enbart för att 2infinity pratade om hurvida en metod tog null eller icke (Skicka runt null gör man väll sällan btw?)

Jo visst, jag skriver också kommentarer ibland, men det är extremt sällan. Exempel på en sådan här tex

https://github.com/AndersMalmgren/DuoCode.SimpleInjector/blob...

Visa signatur
Permalänk
Datavetare
Skrivet av CyberVillain:

exemplet med Code contracts var enbart för att 2infinity pratade om hurvida en metod tog null eller icke (Skicka runt null gör man väll sällan btw?)

Jo visst, jag skriver också kommentarer ibland, men det är extremt sällan. Exempel på en sådan här tex

https://github.com/AndersMalmgren/DuoCode.SimpleInjector/blob...

Verkar som vi har rätt olika kodstil i så fall, kan inte posta koden (är från jobbet), men här är kod kontra kommentarer på senaste filen jag skrivit

http://cloc.sourceforge.net v 1.53 T=1.0 s (1.0 files/s, 4068.0 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C 1 411 1706 1951 -------------------------------------------------------------------------------

Varje funktion har kommentar för API-referensmanual och det är väldigt mycket kommentarer kring varför då detta implementerar en viss standard.

Visa signatur

Care About Your Craft: Why spend your life developing software unless you care about doing it well? - The Pragmatic Programmer

Permalänk
Medlem
Skrivet av Tobberoth:

Snyggt trollat. Som systemutvecklare tjänar du inte bara duktigt mycket mer än medellönen i Sverige, du har också hur lätt som helst att få jobb, tom om du vill jobba utomlands. Jämfört med de flesta yrken är det jackpot. Visst, läkare och advokater tjänar mer, men de måste också genomgå bra mycket tyngre utbildningar och har i läkares fall inga större möjligheter att jobba utomlands.

Inte för att vara dryg men läkare har exceptionella möjligheter att jobba utomlands

Visa signatur

Min Dator: AMD 3600 | GTX 680 | 16 GB RAM | Asus X570 Prime | Fractal Design Arc R2 | Thermalright Silver Arrow | Dell U2412M | Ibm Model M

Permalänk
Skrivet av Yoshman:

Verkar som vi har rätt olika kodstil i så fall, kan inte posta koden (är från jobbet), men här är kod kontra kommentarer på senaste filen jag skrivit

http://cloc.sourceforge.net v 1.53 T=1.0 s (1.0 files/s, 4068.0 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C 1 411 1706 1951 -------------------------------------------------------------------------------

Varje funktion har kommentar för API-referensmanual och det är väldigt mycket kommentarer kring varför då detta implementerar en viss standard.

har du nån del där det inte uttryckligen står "tetris" utan att man ska läsa coden för att kunna fatta.

Visa signatur

hej Achmed Länken till bästa tråden #15549644

Permalänk
Datavetare
Skrivet av Grönahunden:

har du nån del där det inte uttryckligen står "tetris" utan att man ska läsa coden för att kunna fatta.

Få språk är lika lätta som Lisp, så borde vara en barnlek att lura ut vad detta gör (strippat kommenterar men har kvar funktionshjälpen)

(require 'cl) (setq ops '(:add (:prio 1 :assoc :left :repr "+" :op +) :sub (:prio 1 :assoc :left :repr "-" :op -) :mul (:prio 2 :assoc :left :repr "*" :op *) :div (:prio 2 :assoc :left :repr "/" :op /) :pow (:prio 3 :assoc :right :repr "^" :op expt))) (defun op-attr (op attr) (plist-get (plist-get ops op) attr)) (defun op-assoc (op) (op-attr op :assoc)) (defun op-prio (op) (op-attr op :prio)) (defun op-repr (op) (op-attr op :repr)) (defun op-op (op) (op-attr op :op)) (defun ch-type (c) (cond ((string-match "[[:space:]]" (string c)) :space) ((and (>= c ?0) (<= c ?9)) :number) ((or (eq c ?\() (eq c ?\))) :par) (t :operator))) (defun str-to-strtok (str) "Turns a string of numbers and operators into a list of tokens \"1+2\" -> '(1 + 2)" (let ((tokens) (prev-tok :number)) (dolist (ch (string-to-list str)) (let ((ct (ch-type ch))) (if (not (eq ct :space)) (progn (setq tokens (if (eq prev-tok ct) (cons (concat (car tokens) (string ch)) (cdr tokens)) (cons (string ch) tokens))) (setq prev-tok ct))))) (nreverse tokens))) (defun create-tok (tok) "Converts a string into a number or operator keyword, \"+\" -> :add" (cond ((string-match "\\`[-+]?[0-9]+\\'" tok) (string-to-number tok)) ((string-equal tok ")") :rpar) ((string-equal tok "(") :lpar) (t (let ((result-op)) (dolist (o ops result-op) (if (and (keywordp o) (equal tok (op-repr o))) (setq result-op o))))))) (defun strtok-to-tok (strtok) "Converts a list of string tokens into a list of integers and operator keywords, (\"1\" \"+\" \"2\") -> (1 :add 2)" (mapcar 'create-tok strtok)) (defun shunting-yard (tokens) "Converts from infix to postfix notion, (1 :add 2) -> (1 2 :add)" (let ((out) (stack)) (dolist (tok tokens) (cond ((numberp tok) (setq out (cons tok out))) ((eq tok :lpar) (setq stack (cons tok stack))) ((eq tok :rpar) (while (let ((ts (pop stack))) (if (eq ts :lpar) nil (setq out (cons ts out)))))) (t (progn (while (let* ((o2 (car stack)) (o2-prio (op-prio o2)) (o1-prio (op-prio tok)) (o1-assoc (op-assoc tok))) (if (and o2 (not (eq o2 :lpar)) (or (and (eq o1-assoc :right) (< o1-prio o2-prio)) (and (not (eq o1-assoc :right)) (<= o1-prio o2-prio)))) (progn (pop stack) (push o2 out)) nil))) (setq stack (cons tok stack)))))) (append (nreverse out) stack))) (defun rpn-to-sexp (tokens) "Converts a list of tokens in RPN order to a S-expression, (1 2 3 :add :mul) -> (+ 1 (* 2 3))" (let ((sexp)) (car (dolist (tok tokens sexp) (if (numberp tok) (push tok sexp) (let ((a (pop sexp)) (b (pop sexp))) (push (list (op-op tok) b a) sexp))))))) (defun ecalc (str) (interactive "M") (print (eval (rpn-to-sexp (shunting-yard (strtok-to-tok (str-to-strtok str)))))))

Dold text
Visa signatur

Care About Your Craft: Why spend your life developing software unless you care about doing it well? - The Pragmatic Programmer

Permalänk
Skrivet av JOANATAN5354:

såklart!

GraphicsWindow.KeyDown = HandleKey GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( 253, 252, 251 ) While "True" BOXES = 4 ' number of boxes per piece BWIDTH = 25 ' box width in pixels XOFFSET = 40 ' Screen X offset in pixels of where the board starts YOFFSET = 40 ' Screen Y offset in pixels of where the board starts CWIDTH = 10 ' Canvas Width, in number of boxes CHEIGHT = 20 ' Canvas Height, in number of boxes. STARTDELAY = 800 ENDDELAY = 175 PREVIEW_xpos = 13 PREVIEW_ypos = 2 GraphicsWindow.Clear() GraphicsWindow.Title = "Small Basic Tetris" GraphicsWindow.Height = 580 GraphicsWindow.Width = 700 GraphicsWindow.Show() SetupTemplates() SetupCanvas() MainLoop() GraphicsWindow.ShowMessage( "Game Over", "Small Basic Tetris" ) EndWhile Sub MainLoop template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h end = 0 sessionDelay = STARTDELAY While end = 0 If sessionDelay > ENDDELAY Then sessionDelay = sessionDelay - 1 EndIf delay = sessionDelay thisPiece = nextPiece template = Text.Append("template", Math.GetRandomNumber(7)) CreatePiece() ' in: template ret: h nextPiece = h DrawPreviewPiece() h = thisPiece ypos = 0 done = 0 xpos = 3 ' always drop from column 3 CheckStop() ' in: ypos, xpos, h ret: done If done = 1 Then ypos = ypos - 1 MovePiece() 'in: ypos, xpos, h end = 1 EndIf yposdelta = 0 While done = 0 Or yposdelta > 0 MovePiece() 'in: ypos, xpos, h ' Delay, but break if the delay get set to 0 if the piece gets dropped delayIndex = delay While delayIndex > 0 And delay > 0 Program.Delay(10) delayIndex = delayIndex - 10 EndWhile If yposdelta > 0 Then yposdelta = yposdelta - 1 ' used to create freespin, when the piece is rotated Else ypos = ypos + 1 ' otherwise, move the piece down. EndIf ' Check if the piece should stop. CheckStop() ' in: ypos, xpos, h ret: done EndWhile EndWhile EndSub Sub HandleKey ' Stop game If GraphicsWindow.LastKey = "Escape" Then Program.End() EndIf ' Move piece left If GraphicsWindow.LastKey = "Left" Then moveDirection = -1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece right If GraphicsWindow.LastKey = "Right" Then moveDirection = 1 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 If invalidMove = 0 Then xpos = xpos + moveDirection EndIf MovePiece() 'in: ypos, xpos, h EndIf ' Move piece down If GraphicsWindow.LastKey = "Down" or GraphicsWindow.LastKey = "Space" Then delay = 0 EndIf ' Rotate piece If GraphicsWindow.LastKey = "Up" Then basetemplate = Array.GetValue(h, -1) ' Array.GetValue(h, -1) = the template name template = "temptemplate" rotation = "CW" CopyPiece() 'in basetemplate, template, rotation Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name moveDirection = 0 ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 ' See if it can be moved so that it will rotate. xposbk = xpos yposdelta = 0 While yposdelta = 0 And Math.Abs(xposbk - xpos) < 3 ' move up to 3 times only ' if the rotation move worked, copy the temp to "rotatedtemplate" and use that from now on If invalidMove = 0 Then basetemplate = template template = "rotatedtemplate" Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name rotation = "COPY" CopyPiece() 'in basetemplate, template, rotation yposdelta = 1 ' Don't move down if we rotate MovePiece() 'in: ypos, xpos, h ElseIf invalidMove = 2 Then ' Don't support shifting piece when hitting another piece to the right or left. xpos = 99 ' exit the loop Else ' if the rotated piece can't be placed, move it left or right and try again. xpos = xpos - invalidMove ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 EndIf EndWhile If invalidMove <> 0 Then xpos = xposbk Array.SetValue(h, -1, basetemplate) ' Array.GetValue(h, -1) = the template name template = "" EndIf EndIf EndSub Sub DrawPreviewPiece xpos = PREVIEW_xpos ypos = PREVIEW_ypos h = nextPiece XOFFSETBK = XOFFSET YOFFSETBK = YOFFSET XOFFSET = XOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewx") ' Array.GetValue(h, -1) = the template name YOFFSET = YOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewy") ' Array.GetValue(h, -1) = the template name MovePiece() 'in: ypos, xpos, h XOFFSET = XOFFSETBK YOFFSET = YOFFSETBK EndSub ' creates template that's a rotated basetemplate Sub CopyPiece 'in basetemplate, template, rotation L = Array.GetValue(basetemplate, "dim") If rotation = "CW" Then For i = 0 to BOXES - 1 ' x' = y y' = L - 1 - x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (Math.Remainder(v, 10)) y = (L - 1 - Math.Floor(v/10)) Array.SetValue(template, i, x * 10 + y) EndFor ' Count-Cockwise is not currently used ElseIf rotation = "CCW" Then For i = 0 to BOXES - 1 ' x' = L - 1 - y y' = x v = Array.GetValue(basetemplate, i) 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) ' new x and y x = (L - 1 - Math.Remainder(v, 10)) y = Math.Floor(v/10) Array.SetValue(template, i, x * 10 + y) EndFor ElseIf rotation = "COPY" Then For i = 0 to BOXES - 1 Array.SetValue(template, i, Array.GetValue(basetemplate, i)) EndFor Else GraphicsWindow.ShowMessage("invalid parameter", "Error") Program.End() EndIf ' Copy the remain properties from basetemplate to template. Array.SetValue(template, "color", Array.GetValue(basetemplate, "color")) Array.SetValue(template, "dim", Array.GetValue(basetemplate, "dim")) Array.SetValue(template, "pviewx", Array.GetValue(basetemplate, "pviewx")) Array.SetValue(template, "pviewy", Array.GetValue(basetemplate, "pviewy")) EndSub Sub CreatePiece ' in: template ret: h ' Create a new handle, representing an arrayName, that will represent the piece hcount = hcount + 1 h = Text.Append("piece", hcount) Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = Array.GetValue(template, "color") For i = 0 to BOXES - 1 s = Shapes.AddRectangle(BWIDTH, BWIDTH) Shapes.Move(s, -BWIDTH, -BWIDTH) ' move off screen Array.SetValue(h, i, s) EndFor EndSub Sub MovePiece 'in: ypos, xpos, h. ypos/xpos is 0-19, representing the top/left box coordinate of the piece on the canvas. h returned by CreatePiece For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name x = Math.Floor(v/10) y = Math.Remainder(v, 10) ' Array.GetValue(h, i) = box for piece h. ' xpos/ypos = are topleft of shape. x/y is the box offset within the shape. Shapes.Move(Array.GetValue(h, i), XOFFSET + xpos * BWIDTH + x * BWIDTH, YOFFSET + ypos * BWIDTH + y * BWIDTH) EndFor EndSub Sub ValidateMove ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0 i = 0 invalidMove = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If (x + xpos + moveDirection) < 0 Then invalidMove = -1 i = BOXES ' force getting out of the loop EndIf If (x + xpos + moveDirection) >= CWIDTH Then invalidMove = 1 i = BOXES ' force getting out of the loop EndIf If Array.GetValue("c", (x + xpos + moveDirection) + (y + ypos) * CWIDTH) <> "." Then invalidMove = 2 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile EndSub Sub CheckStop ' in: ypos, xpos, h ret: done done = 0 i = 0 While i < BOXES v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x/y is the box offset within the shape. x = Math.Floor(v/10) y = Math.Remainder(v, 10) If y + ypos > CHEIGHT Or Array.GetValue("c", (x + xpos) + (y + ypos) * CWIDTH) <> "." Then done = 1 i = BOXES ' force getting out of the loop EndIf i = i + 1 EndWhile ' If we need to stop the piece, move the box handles to the canvas If done = 1 Then For i = 0 to BOXES - 1 v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name 'x = Math.Floor(v/10) 'y = Math.Remainder(v, 10) Array.SetValue("c", (Math.Floor(v/10) + xpos) + (Math.Remainder(v, 10) + ypos - 1) * CWIDTH, Array.GetValue(h, i)) EndFor ' 1 points for every piece successfully dropped score = score + 1 PrintScore() ' Delete clared lines DeleteLines() EndIf EndSub Sub DeleteLines linesCleared = 0 ' Iterate over each row, starting from the bottom For y = CHEIGHT - 1 to 0 Step -1 ' Check to see if the whole row is filled x = CWIDTH While x = CWIDTH x = 0 While x < CWIDTH piece = Array.GetValue("c", x + y * CWIDTH) If piece = "." then x = CWIDTH EndIf x = x + 1 EndWhile ' if non of them were empty (i.e "."), then remove the line. If x = CWIDTH Then ' Delete the line For x1 = 0 to CWIDTH - 1 Shapes.Remove(Array.GetValue("c", x1 + y * CWIDTH)) EndFor linesCleared = linesCleared + 1 ' Move everything else down one. For y1 = y To 1 Step -1 For x1 = 0 to CWIDTH - 1 piece = Array.GetValue("c", x1 + (y1 - 1) * CWIDTH) Array.SetValue("c", x1 + y1 * CWIDTH, piece) Shapes.Move(piece, Shapes.GetLeft(piece), Shapes.GetTop(piece) + BWIDTH) EndFor EndFor EndIf EndWhile EndFor If linesCleared > 0 Then score = score + 100 * Math.Round(linesCleared * 2.15 - 1) PrintScore() EndIf EndSub Sub SetupCanvas ' GraphicsWindow.DrawResizedImage( Flickr.GetRandomPicture( "bricks" ), 0, 0, GraphicsWindow.Width, GraphicsWindow.Height) GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor GraphicsWindow.FillRectangle(XOFFSET, YOFFSET, CWIDTH*BWIDTH, CHEIGHT*BWIDTH) Program.Delay(200) GraphicsWindow.PenWidth = 1 GraphicsWindow.PenColor = "Pink" For x = 0 To CWIDTH-1 For y = 0 To CHEIGHT-1 Array.SetValue("c", x + y * CWIDTH, ".") ' "." indicates spot is free GraphicsWindow.DrawRectangle(XOFFSET + x * BWIDTH, YOFFSET + y * BWIDTH, BWIDTH, BWIDTH) EndFor EndFor GraphicsWindow.PenWidth = 4 GraphicsWindow.PenColor = "Black" GraphicsWindow.DrawLine(XOFFSET, YOFFSET, XOFFSET, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH, YOFFSET, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.DrawLine(XOFFSET, YOFFSET + CHEIGHT*BWIDTH, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH) GraphicsWindow.PenColor = "Lime" GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET, XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 6) GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 4, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 4) GraphicsWindow.PenColor = "Black" GraphicsWindow.BrushColor = "Pink" x = XOFFSET + PREVIEW_xpos * BWIDTH - BWIDTH y = YOFFSET + PREVIEW_ypos * BWIDTH - BWIDTH GraphicsWindow.FillRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.DrawRectangle(x, y, BWIDTH * 5, BWIDTH * 6) GraphicsWindow.FillRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.DrawRectangle(x - 20, y + 190, 310, 170) GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontItalic = "False" GraphicsWindow.FontName = "Comic Sans MS" GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x, y + 200, "Game control keys:") GraphicsWindow.DrawText(x + 25, y + 220, "Left Arrow = Move piece left") GraphicsWindow.DrawText(x + 25, y + 240, "Right Arrow = Move piece right") GraphicsWindow.DrawText(x + 25, y + 260, "Up Arrow = Rotate piece") GraphicsWindow.DrawText(x + 25, y + 280, "Down Arrow = Drop piece") GraphicsWindow.DrawText(x, y + 320, "Press to stop game") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontName = "Georgia" GraphicsWindow.FontItalic = "True" GraphicsWindow.FontSize = 36 GraphicsWindow.DrawText(x - 20, y + 400, "Small Basic Tetris") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score GraphicsWindow.FontSize = 16 GraphicsWindow.DrawText(x - 20, y + 440, "ver.0.1") Program.Delay(200) ' without this delay, the above text will use the fontsize of the score score = 0 PrintScore() EndSub Sub PrintScore GraphicsWindow.PenWidth = 4 GraphicsWindow.BrushColor = "Pink" GraphicsWindow.FillRectangle(500, 65, 153, 50) GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawRectangle(500, 65, 153, 50) GraphicsWindow.FontItalic = "False" GraphicsWindow.FontSize = 32 GraphicsWindow.FontName = "Impact" GraphicsWindow.BrushColor = "Black" GraphicsWindow.DrawText(505, 70, Text.Append(Text.GetSubText( "00000000", 0, 8 - Text.GetLength( score ) ), score)) EndSub Sub SetupTemplates ' each piece has 4 boxes. ' the index of each entry within a piece represents the box number (1-4) ' the value of each entry represents to box zero-based box coordinate within the piece: tens place is x, ones place y '_X_ '_X_ '_XX Array.SetValue("template1", 0, 10) Array.SetValue("template1", 1, 11) Array.SetValue("template1", 2, 12) Array.SetValue("template1", 3, 22) Array.SetValue("template1", "color", "Yellow") Array.SetValue("template1", "dim", 3) Array.SetValue("template1", "pviewx", -12) Array.SetValue("template1", "pviewy", 12) '_X_ '_X_ 'XX_ Array.SetValue("template2", 0, 10) Array.SetValue("template2", 1, 11) Array.SetValue("template2", 2, 12) Array.SetValue("template2", 3, 02) Array.SetValue("template2", "color", "Magenta") Array.SetValue("template2", "dim", 3) Array.SetValue("template2", "pviewx", 12) Array.SetValue("template2", "pviewy", 12) '_X_ 'XXX '_ Array.SetValue("template3", 0, 10) Array.SetValue("template3", 1, 01) Array.SetValue("template3", 2, 11) Array.SetValue("template3", 3, 21) Array.SetValue("template3", "color", "Gray") Array.SetValue("template3", "dim", 3) Array.SetValue("template3", "pviewx", 0) Array.SetValue("template3", "pviewy", 25) 'XX_ 'XX_ '_ Array.SetValue("template4", 0, 00) Array.SetValue("template4", 1, 10) Array.SetValue("template4", 2, 01) Array.SetValue("template4", 3, 11) Array.SetValue("template4", "color", "Cyan") Array.SetValue("template4", "dim", 2) Array.SetValue("template4", "pviewx", 12) Array.SetValue("template4", "pviewy", 25) 'XX_ '_XX '_ Array.SetValue("template5", 0, 00) Array.SetValue("template5", 1, 10) Array.SetValue("template5", 2, 11) Array.SetValue("template5", 3, 21) Array.SetValue("template5", "color", "Green") Array.SetValue("template5", "dim", 3) Array.SetValue("template5", "pviewx", 0) Array.SetValue("template5", "pviewy", 25) '_XX 'XX_ '_ Array.SetValue("template6", 0, 10) Array.SetValue("template6", 1, 20) Array.SetValue("template6", 2, 01) Array.SetValue("template6", 3, 11) Array.SetValue("template6", "color", "Blue") Array.SetValue("template6", "dim", 3) Array.SetValue("template6", "pviewx", 0) Array.SetValue("template6", "pviewy", 25) '_X '_X '_X '_X Array.SetValue("template7", 0, 10) Array.SetValue("template7", 1, 11) Array.SetValue("template7", 2, 12) Array.SetValue("template7", 3, 13) Array.SetValue("template7", "color", "Red") Array.SetValue("template7", "dim", 4) Array.SetValue("template7", "pviewx", 0) Array.SetValue("template7", "pviewy", 0) EndSub

Dold text

för långt?
536 rader

Ja den typen kan jag i alla fall fatta lite av. Tack vare WOT, när man ska ändra moddars beteende (ibland så är koden rätt dålig, så den inte lyssnar på det jag ändrar, även förklaringar rätt dåliga plus inbäddat med ryska).

Visa signatur

Min spel rigg:FD Define R4|VX 550W|i5 2500K|Corsair LP 4GBX2|Mammabräda P67 Extreme4|GTX 670 windforce|23tum u2312hm
Min gamla/HTPC:AMD 6000+|Ram 2GbX2|Radeon HD5770| XFX 450/nu XFX 550
Mitt bygge: ByggloggFri frakt INET:Fraktfritt sweclockers vid köp över 500kr

#Gilla inlägg som är bra & Använd citera/@"namn" vid snabbt svar

Permalänk
Skrivet av Yoshman:

Få språk är lika lätta som Lisp, så borde vara en barnlek att lura ut vad detta gör (strippat kommenterar men har kvar funktionshjälpen)

(require 'cl) (setq ops '(:add (:prio 1 :assoc :left :repr "+" :op +) :sub (:prio 1 :assoc :left :repr "-" :op -) :mul (:prio 2 :assoc :left :repr "*" :op *) :div (:prio 2 :assoc :left :repr "/" :op /) :pow (:prio 3 :assoc :right :repr "^" :op expt))) (defun op-attr (op attr) (plist-get (plist-get ops op) attr)) (defun op-assoc (op) (op-attr op :assoc)) (defun op-prio (op) (op-attr op :prio)) (defun op-repr (op) (op-attr op :repr)) (defun op-op (op) (op-attr op :op)) (defun ch-type (c) (cond ((string-match "[[:space:]]" (string c)) :space) ((and (>= c ?0) (<= c ?9)) :number) ((or (eq c ?\() (eq c ?\))) :par) (t :operator))) (defun str-to-strtok (str) "Turns a string of numbers and operators into a list of tokens \"1+2\" -> '(1 + 2)" (let ((tokens) (prev-tok :number)) (dolist (ch (string-to-list str)) (let ((ct (ch-type ch))) (if (not (eq ct :space)) (progn (setq tokens (if (eq prev-tok ct) (cons (concat (car tokens) (string ch)) (cdr tokens)) (cons (string ch) tokens))) (setq prev-tok ct))))) (nreverse tokens))) (defun create-tok (tok) "Converts a string into a number or operator keyword, \"+\" -> :add" (cond ((string-match "\\`[-+]?[0-9]+\\'" tok) (string-to-number tok)) ((string-equal tok ")") :rpar) ((string-equal tok "(") :lpar) (t (let ((result-op)) (dolist (o ops result-op) (if (and (keywordp o) (equal tok (op-repr o))) (setq result-op o))))))) (defun strtok-to-tok (strtok) "Converts a list of string tokens into a list of integers and operator keywords, (\"1\" \"+\" \"2\") -> (1 :add 2)" (mapcar 'create-tok strtok)) (defun shunting-yard (tokens) "Converts from infix to postfix notion, (1 :add 2) -> (1 2 :add)" (let ((out) (stack)) (dolist (tok tokens) (cond ((numberp tok) (setq out (cons tok out))) ((eq tok :lpar) (setq stack (cons tok stack))) ((eq tok :rpar) (while (let ((ts (pop stack))) (if (eq ts :lpar) nil (setq out (cons ts out)))))) (t (progn (while (let* ((o2 (car stack)) (o2-prio (op-prio o2)) (o1-prio (op-prio tok)) (o1-assoc (op-assoc tok))) (if (and o2 (not (eq o2 :lpar)) (or (and (eq o1-assoc :right) (< o1-prio o2-prio)) (and (not (eq o1-assoc :right)) (<= o1-prio o2-prio)))) (progn (pop stack) (push o2 out)) nil))) (setq stack (cons tok stack)))))) (append (nreverse out) stack))) (defun rpn-to-sexp (tokens) "Converts a list of tokens in RPN order to a S-expression, (1 2 3 :add :mul) -> (+ 1 (* 2 3))" (let ((sexp)) (car (dolist (tok tokens sexp) (if (numberp tok) (push tok sexp) (let ((a (pop sexp)) (b (pop sexp))) (push (list (op-op tok) b a) sexp))))))) (defun ecalc (str) (interactive "M") (print (eval (rpn-to-sexp (shunting-yard (strtok-to-tok (str-to-strtok str)))))))

Dold text

enkelt för dig kanske.. njet! jag fattar ingenting.
"let car do list if number push let a b push" ? jaha men blir inte begripligt

har man inga förkunskaper över huvud taget så är det nog lika svårt som att läsa text på ett språk man inte förstår.

ja tog din kod i Google translate den sa att det var engelska och översatt till svenska så förstår man det inte heller

polletten / tokens ramlade inte ner

(Kräver "cl)

(setq ops "(: add (: PRIO 1: Assoc: vänster: repr" + ": op +)
: Sub (: prio 1: Assoc: vänster: repr "-": op -)
: Mul (: prio 2: Assoc: vänster: repr "*": op *)
: Div (: prio 2: Assoc: vänster: repr "/": op /)
: Pow (: prio 3: Assoc: höger: repr "^": op Expt)))

(Defun op-attr (op attr) (plist-get (plist-get ops op) attr))
(Defun op-Assoc (op) (op-attr op: assoc))
(Defun op-prio (op) (op-attr op: prio))
(Defun op-repr (op) (op-attr op: repr))
(Defun op-op (op) (op-attr op: op))

(Defun ch-typ (c)
(Cond ((string-match "[[: space:]]" (string c)): utrymme)
(? (Och (> = c 0) (<= c 9)):? Nummer)
(? (Eller (eq c \ () (eq c \))): par)
(T: operatör)))

(Defun str-till-strtok (str)
"Slår en rad siffror och operatörer i en lista med polletter
\ "1 + 2 \" -> '(1 + 2) "
(Låt ((tokens) (prev-tok: nummer))
(Dolist (lm (sträng-till-lista str))
(Låt ((ct (lm-typ ch)))
(Om (inte (eq CT: utrymme))
(Progn
(setq polletter
(If (eq föreg-tok CT)
(Cons (concat (bil tokens) (string ch))
(CDR tokens))
(Cons (sträng ch) symboliska)))
(Setq prev-tok ct)))))
(nreverse polletter)))

(Defun skapa-tok (tok)
"Konverterar en sträng till ett nummer eller operatörs sökord, \" + \ "->: lägg till"
(Cond ((string-match "\\` [- +] [0-9] + \\ '? "Tok) (string-till-nummer tok))
((String-lika tok ")"): rpar)
((String-lika tok "("): LPAR)
(T (låt ((resultat-op))
(Dolist (o ops resultat op)
(Om (och (keywordp o)
(Lika tok (op-repr o)))
(Setq resultat op o)))))))

(Defun strtok-till-tok (strtok)
"Konverterar en lista med sträng polletter i en lista med heltal och
operatörs sökord, (\ "1 \" \ "+ \" \ "2 \") -> (1: tillsätt 2) "
(Mapcar 'create-tok strtok))

(Defun växling gård (tokens)
"Konverterar från infix till postfix begreppet, (1: tillsätt 2) -> (1 2: lägg)"
(Låt ((ut) (stacken))
(Dolist (tok tokens)
(Cond ((numberp tok) (setq ut (cons tok ut)))
((Eq tok: LPAR) (setq stacken (cons tok stacken)))
((Eq tok: rpar) (medan (låt ((ts (pop stacken)))
(If (eq ts: LPAR-)
noll
(Setq ut (cons ts ut))))))
(T (progn (medan (låt * ((o2 (bil stacken))
(O2-prio (op-prio o2))
(O1-prio (op-prio tok))
(O1-Assoc (op-assoc tok)))
(Om (och o2
(Inte (eq o2: LPAR))
(Eller (och (eq o1-Assoc: höger)
(<O1-prio o2-prio))
(Och (inte (eq O1-Assoc: höger))
(<= O1-prio o2-prio))))
(Progn (pop stacken)
(Push-o2 ut))
noll)))
(Setq stacken (cons tok stacken))))))
(Append (nreverse ut) stack)))

(Defun RPN-till-sexp (tokens)
"Konverterar en lista över polletter i RPN för att en S-uttryck,
(1 2 3: lägg: mul) -> (+ 1 (* 2 3)) "
(Låt ((sexp))
(Bil
(Dolist (tok polletter sexp)
(Om (numberp tok)
(Skjut tok sexp)
(Låt ((a (pop sexp))
(B (pop sexp)))
(Push (lista (op-op tok) b a) sexp)))))))

(Defun eCalc (str)
(Interaktiv "M")
(Trycket (eval (RPN-till-sexp (växling-yard (strtok-till-tok (str-till-strtok str)))))))

Dold text
Visa signatur

hej Achmed Länken till bästa tråden #15549644

Permalänk
Datavetare
Skrivet av Grönahunden:

enkelt för dig kanske.. njet! jag fattar ingenting.
"let car do list if number push let a b push" ? jaha men blir inte begripligt

har man inga förkunskaper över huvud taget så är det nog lika svårt som att läsa text på ett språk man inte förstår.

ja tog din kod i Google translate den sa att det var engelska och översatt till svenska så förstår man det inte heller

polletten / tokens ramlade inte ner

(Kräver "cl)

(setq ops "(: add (: PRIO 1: Assoc: vänster: repr" + ": op +)
: Sub (: prio 1: Assoc: vänster: repr "-": op -)
: Mul (: prio 2: Assoc: vänster: repr "*": op *)
: Div (: prio 2: Assoc: vänster: repr "/": op /)
: Pow (: prio 3: Assoc: höger: repr "^": op Expt)))

(Defun op-attr (op attr) (plist-get (plist-get ops op) attr))
(Defun op-Assoc (op) (op-attr op: assoc))
(Defun op-prio (op) (op-attr op: prio))
(Defun op-repr (op) (op-attr op: repr))
(Defun op-op (op) (op-attr op: op))

(Defun ch-typ (c)
(Cond ((string-match "[[: space:]]" (string c)): utrymme)
(? (Och (> = c 0) (<= c 9)):? Nummer)
(? (Eller (eq c \ () (eq c \))): par)
(T: operatör)))

(Defun str-till-strtok (str)
"Slår en rad siffror och operatörer i en lista med polletter
\ "1 + 2 \" -> '(1 + 2) "
(Låt ((tokens) (prev-tok: nummer))
(Dolist (lm (sträng-till-lista str))
(Låt ((ct (lm-typ ch)))
(Om (inte (eq CT: utrymme))
(Progn
(setq polletter
(If (eq föreg-tok CT)
(Cons (concat (bil tokens) (string ch))
(CDR tokens))
(Cons (sträng ch) symboliska)))
(Setq prev-tok ct)))))
(nreverse polletter)))

(Defun skapa-tok (tok)
"Konverterar en sträng till ett nummer eller operatörs sökord, \" + \ "->: lägg till"
(Cond ((string-match "\\` [- +] [0-9] + \\ '? "Tok) (string-till-nummer tok))
((String-lika tok ")"): rpar)
((String-lika tok "("): LPAR)
(T (låt ((resultat-op))
(Dolist (o ops resultat op)
(Om (och (keywordp o)
(Lika tok (op-repr o)))
(Setq resultat op o)))))))

(Defun strtok-till-tok (strtok)
"Konverterar en lista med sträng polletter i en lista med heltal och
operatörs sökord, (\ "1 \" \ "+ \" \ "2 \") -> (1: tillsätt 2) "
(Mapcar 'create-tok strtok))

(Defun växling gård (tokens)
"Konverterar från infix till postfix begreppet, (1: tillsätt 2) -> (1 2: lägg)"
(Låt ((ut) (stacken))
(Dolist (tok tokens)
(Cond ((numberp tok) (setq ut (cons tok ut)))
((Eq tok: LPAR) (setq stacken (cons tok stacken)))
((Eq tok: rpar) (medan (låt ((ts (pop stacken)))
(If (eq ts: LPAR-)
noll
(Setq ut (cons ts ut))))))
(T (progn (medan (låt * ((o2 (bil stacken))
(O2-prio (op-prio o2))
(O1-prio (op-prio tok))
(O1-Assoc (op-assoc tok)))
(Om (och o2
(Inte (eq o2: LPAR))
(Eller (och (eq o1-Assoc: höger)
(<O1-prio o2-prio))
(Och (inte (eq O1-Assoc: höger))
(<= O1-prio o2-prio))))
(Progn (pop stacken)
(Push-o2 ut))
noll)))
(Setq stacken (cons tok stacken))))))
(Append (nreverse ut) stack)))

(Defun RPN-till-sexp (tokens)
"Konverterar en lista över polletter i RPN för att en S-uttryck,
(1 2 3: lägg: mul) -> (+ 1 (* 2 3)) "
(Låt ((sexp))
(Bil
(Dolist (tok polletter sexp)
(Om (numberp tok)
(Skjut tok sexp)
(Låt ((a (pop sexp))
(B (pop sexp)))
(Push (lista (op-op tok) b a) sexp)))))))

(Defun eCalc (str)
(Interaktiv "M")
(Trycket (eval (RPN-till-sexp (växling-yard (strtok-till-tok (str-till-strtok str)))))))

Dold text

Det var en enklare miniräknare i emacs-lisp. En lite kortare, den räknar något (är Erlang).

lists:foldl(fun(Elem, OldDict) -> dict:update_counter(Elem, 1, OldDict) end, dict:new(), [a,b,a,c,b,b])).

Visa signatur

Care About Your Craft: Why spend your life developing software unless you care about doing it well? - The Pragmatic Programmer

Permalänk
Skrivet av Yoshman:

Det var en enklare miniräknare i emacs-lisp. En lite kortare, den räknar något (är Erlang).

lists:foldl(fun(Elem, OldDict) -> dict:update_counter(Elem, 1, OldDict) end, dict:new(), [a,b,a,c,b,b])).

ja skulle ju kunna googla på vad Erlang betyder fast då går man ju förbi hela syftet med tråden.
borde man vet vad det är för något ? finns det nån användning av detta förutom i programmering ?

gissning har det nånting med översättning av text att göra ?

Visa signatur

hej Achmed Länken till bästa tråden #15549644

Permalänk
Avstängd
Skrivet av Yoshman:

Verkar som vi har rätt olika kodstil i så fall, kan inte posta koden (är från jobbet), men här är kod kontra kommentarer på senaste filen jag skrivit

http://cloc.sourceforge.net v 1.53 T=1.0 s (1.0 files/s, 4068.0 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C 1 411 1706 1951 -------------------------------------------------------------------------------

Varje funktion har kommentar för API-referensmanual och det är väldigt mycket kommentarer kring varför då detta implementerar en viss standard.

I mina system kan flera 100 utvecklare passera under dess livslängd, man kan kommentera hur mycket man vill men i slutet är det ändå tiden det tar för en ny utvecklare att sätta sig in i koden som räknas. Då är ett lätt använt API samt en tydlig och skalbar domän viktigast

Skickades från m.sweclockers.com

Visa signatur