I've created a simple score game. The goal is improving my knowledge about OOP.
This is my Main
method:
using System;
namespace Snake
{
class Program
{
static void Main()
{
while (true)
{
Process.UI();
}
}
}
}
It's very simple and just calls a method named UI
.
This is my Snake
method (I named it Snake
because it's like a snake game):
using System;
using System.Collections.Generic;
using System.Threading;
namespace Snake
{
class Snake
{
static int headOfSnake;
public static int Score { get; set; }
public static void GenericRandomCircle()
{
Random test = new Random();
int index = test.Next(Process.ListOfDots.Count);
Process.ListOfDots[index] = '■';
}
public static void PositionOfSnake()
{
Random test = new Random();
headOfSnake = test.Next(Process.ListOfDots.Count);
Process.ListOfDots[headOfSnake] = '▒';
}
public static void Move(string move, int Step)
{
switch (move)
{
case "w":
ChangeSnakePosition(Step, -20);
break;
case "s":
ChangeSnakePosition(Step, 20);
break;
case "a":
ChangeSnakePosition(Step, -1);
break;
case "d":
ChangeSnakePosition(Step, 1);
break;
default:
break;
}
}
static void ChangeSnakePosition(int Step, int WhereToWhere)
{
try
{
int Counter = headOfSnake;
for (int i = 0; i < Step; i++)
{
Thread.Sleep(200);
if (Process.ListOfDots[Counter + WhereToWhere] == '■')
{
Score++;
GenericRandomCircle();
}
Process.ListOfDots[Counter] = '∙';
Counter = Counter + WhereToWhere;
Process.ListOfDots[Counter] = '▒';
RefreshGameBoard();
}
headOfSnake = Counter;
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
RefreshGameBoard();
Console.WriteLine("YOU LOSE!!! START GAME AGAIN");
Environment.Exit(0);
}
}
static void RefreshGameBoard()
{
Process.GameBoard();
}
}
}
And this is the last class called Process
to process the game:
using System;
using System.Collections.Generic;
namespace Snake
{
class Process
{
static List<char> _listOfDots = new List<char>();
static bool isGenerate;
static string userMove;
static int userStep;
public static List<char> ListOfDots
{
get { return _listOfDots; }
set { _listOfDots = value; }
}
public static void GameBoard()
{
Console.Clear();
Console.WriteLine("╔═════════════════════════════════════════╗");
for (int i = 0; i < ListOfDots.Count; i++)
{
if (i % 20 == 0 && i == 0)
{
Console.Write("║ {0} ", ListOfDots[i]);
continue;
}
else if (i % 20 == 0)
{
Console.Write("║\n║ {0} ", ListOfDots[i]);
continue;
}
Console.Write("{0} ", ListOfDots[i]);
if (i == ListOfDots.Count - 1)
{
Console.Write("║");
}
}
Console.WriteLine();
Console.Write("╚═════════════════════════════════════════╝");
Console.WriteLine();
}
public static void ShowGuide()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Your Score: {0}", Snake.Score);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("up: W \t down: S \t right: D \t left: A");
}
public static void UI()
{
if (!isGenerate)
{
FirstGenerate();
}
MoveChecks("Enter W S D A", "Put numbers");
Snake.Move(userMove, userStep);
}
static void MoveChecks(string wasdMessage, string stepMessage)
{
while (true)
{
GameBoard();
ShowGuide();
userMove = GetString("Enter W S D A to move: ");
if (userMove != "w" && userMove != "a" && userMove != "s" && userMove != "d")
{
GetError(String.Format("▌ {0} ▐", wasdMessage));
Console.ReadLine();
continue;
}
try
{
userStep = Convert.ToInt32(GetString("How Many Step: "));
break;
}
catch
{
GetError(String.Format("▌ {0} ▐", stepMessage));
Console.ReadLine();
continue;
}
}
}
static void FirstGenerate()
{
while (true)
{
string userInput = GetString("Please Enter a number can be divided by 100 to play the game (less than 400) :");
if (ListOfDots.Count > 0)
{
GetError("You Can't Insert to the BoardGame.");
return;
}
try
{
if (Convert.ToInt32(userInput) % 100 == 0 && Convert.ToInt32(userInput) <= 400)
{
for (int i = 0; i < Convert.ToInt32(userInput); i++)
{
ListOfDots.Add('∙');
}
Snake.GenericRandomCircle();
Snake.PositionOfSnake();
GameBoard();
Snake.Score = 0;
isGenerate = true;
break;
}
else
{
GetError("Please Enter a number divided by 100 and less than 400.");
}
}
catch
{
Console.WriteLine("Please enter a number");
}
}
}
// STATIC METHODS - Once Write * EveryWhere Use.
public static string GetString(string message)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(message.ToLower());
Console.ForegroundColor = ConsoleColor.White;
return Console.ReadLine();
}
public static void GetError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.White;
}
}
}
How do you think this program can be made more object-oriented and expanded? Please tell me the problems of this program so that I can improve my knowledge. Thanks.
GameState
,Rendering
,HandleUserInput
,UpdateState
, andStateEvaluation
. A good exercise would be to take my code and see if you can add internal walls or a computer-based snake opponent. Next step, console-based PacMan / Astroids =). Please let me know if you have any questions about my code. \$\endgroup\$