Impressum
< Laufendes O - Teil 5 Inhalt Laufendes O - Teil 7>

Das laufende O - Teil 6 (Modularisierung)

Beschreibung

Wenn man sich im Quellcode unten die Main durchliest, dann versteht sogar ein Laie relativ genau was passiert. Dies wurde erreicht, indem man Codeteile in Unterprogramme mit aussagekräftigen Namen ausgelagert hat.
Doch nicht nur die Lesbarkeit steigtert sich durch die Modualrisierung auch Veränderungen und Erweiterungen lassen sich einfacher durchführen, wenn man eine sinnvolle Aufteilung in Unterprogramme vornimmt und diese sprechend benennt.

Youtube Video: Das laufende O Teil 6

Der Quellcode

using System; class Program { static int[,] feld = { {1,1,1,1,1,1,1,1,1,1,1 }, {1,0,2,0,1,0,0,1,0,0,1 }, {1,0,0,0,1,0,0,1,0,0,1 }, {1,0,0,0,1,0,3,0,0,0,1 }, {1,0,0,0,4,2,1,5,1,1,1 }, {1,0,0,0,4,2,1,5,1,1,1 }, {1,0,0,0,4,2,1,5,5,1,1 }, {1,0,0,0,0,0,0,0,0,0,1 }, {1,0,0,0,4,2,1,5,1,1,1 }, {1,0,0,0,4,2,1,1,1,1,1 }, {1,5,1,1,1,0,0,0,0,0,1 }, {1,0,0,1,3,0,2,1,0,1,1 }, {1,0,2,2,0,0,0,0,0,0,1 }, {1,1,1,1,1,1,1,1,1,1,1 }, }; static char[] aussehen = { ' ', ' ', '*', '$', ' ', ' ' }; static int[] wertInPunkten = { 0 , 0 , 1 , 10, 0 , 0 }; static int[] wirdZu = { 0 , 1 , 0 , 2 , 1 , 0 }; static bool[] laufbar = {true,false, true, true, true, true }; static ConsoleColor[] farbe = { ConsoleColor.White, ConsoleColor.Gray, ConsoleColor.Yellow, ConsoleColor.DarkGreen, ConsoleColor.White, ConsoleColor.Red, }; static ConsoleColor[] bg = { ConsoleColor.Black, ConsoleColor.White, ConsoleColor.Black, ConsoleColor.Red, ConsoleColor.Black, ConsoleColor.DarkGray, }; static int x, y, punkte=0; static void zeichneFeld() { for (int x=0; x<feld.GetLength(0); x++) for (int y = 0; y < feld.GetLength(1); y++) { Console.SetCursorPosition(x, y); int index = feld[x, y]; Console.ForegroundColor = farbe[index]; Console.BackgroundColor = bg[index]; Console.Write(aussehen[index]); } } static void Main(string[] args) { zeichneFeld(); x = 4; y = 3; char c; Console.CursorVisible = false; do { Console.SetCursorPosition(feld.GetLength(0) + 2, 3); Console.Write("Punkte: " + punkte); Console.SetCursorPosition(x, y); Console.ForegroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Black; Console.Write("O"); c = Console.ReadKey(true).KeyChar; Console.SetCursorPosition(x, y); int index = feld[x, y]; Console.ForegroundColor = farbe[index]; Console.BackgroundColor = bg[index]; Console.Write(aussehen[index]); int oldX = x; int oldY = y; if (c == 'w') y--; if (c == 's') y++; if (c == 'a') x--; if (c == 'd') x++; index = feld[x, y]; if (laufbar[index]==false) { x = oldX; y = oldY; } index = feld[x, y]; punkte = punkte + wertInPunkten[index]; feld[x, y] = wirdZu[index]; } while (c!='q'); } }