【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core

时间:2023-02-28 14:58:01

Index page

【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core

Welcome page

【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core

【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core

生成很多不同的小人哦~我是如何实现这么stupid but interesting的程序呢?我用了ASP.NET Core

画小人的话,用了一个很stupid的辅助类, 自己写的,小人脸宽21,然后鼻子占1,剩下的眼睛,鼻子,脸,耳朵,分分看。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace game1.Controllers
{
public class DrawingHelper
{
public static string randomEye = @"-|@!~^+'o0O⊙";
public static string randomMouth = @"_x*oO~DVv";
public static string randomHair = randomEye + randomMouth;
public static List<StringBuilder> Draw(int FaceCount)
{
int RowWidth = ;
int hairWidth = ; string leftEar = "(";
string rightEar = ")";
string leftFace = "|";
string nose = "*"; var faces = new List<StringBuilder>(); for (int t = ; t <= FaceCount; t++)
{
StringBuilder sb = new StringBuilder();
string eye = GetRandomPart(randomEye, );
string mouth = GetRandomPart(randomMouth, );
string hairStyle = GetRandomPart(randomHair, );
DrawHair(RowWidth, hairWidth, hairStyle,sb);
DrawCartoon(leftEar, rightEar, leftFace, eye, nose, mouth,sb);
sb.Append("\n");
faces.Add(sb);
}
return faces; } private static void DrawHair(int RowWidth, int hairWidth, string hairStyle,StringBuilder sb)
{
StringBuilder hair = new StringBuilder();
for (int i = ; i <= hairWidth - ; i++)
{
hair.Append(hairStyle);
}
sb.AppendLine((" " + hair + " ").PadLeft((RowWidth - hairWidth) / + hairWidth, ' '));
sb.AppendLine(("|" + hair + "|").PadLeft((RowWidth - hairWidth) / + hairWidth, ' '));
}
public static string GetRandomPart(string pwdchars, int pwdlen)
{ string tmpstr = "";
int iRandNum;
Thread.Sleep();
long tick = DateTime.Now.Ticks;
Random rnd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> ));
for (int i = ; i < pwdlen; i++)
{
iRandNum = rnd.Next(pwdchars.Length);
tmpstr += pwdchars[iRandNum];
}
return tmpstr;
}
private static void DrawCartoon(string leftEar, string rightEar, string leftFace, string eye, string nose, string mouth, StringBuilder sb)
{
//Print left
PrintLeftParts(leftEar, ,sb);
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(eye, ,sb);
PrintLeftParts(nose, ,sb);
//Pring right
PrintLeftParts(eye, , sb);
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(rightEar, ,sb);
//Print down face
sb.AppendLine();
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(mouth, ,sb,'_');
PrintLeftParts(leftFace, , sb,'_');
sb.AppendLine();
sb.AppendLine(); } static void PrintLeftParts(string organName, int leftPad, StringBuilder sb,char paddingchar = ' ')
{
for( int i=; i < leftPad; i++)
{
sb.Append(paddingchar);
}
sb.Append(organName);
}
}
}

上一个HelloWorldController,也是整个程序唯一的controller。

using game1.Models;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web; // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace game1.Controllers
{
public class HelloWorldController : Controller
{
TelemetryClient tc = new TelemetryClient();
//
// GET: /HelloWorld/ [HttpPost]
public ActionResult Index(string userName, int count)
{
FaceInfo fi = new FaceInfo();
fi.Name = userName;
fi.FaceCount = count;
return RedirectToAction("Welcome",fi);
}
[HttpGet]
public ActionResult Index()
{ return View();
}
public ActionResult About()
{ return View();
}
//
// GET: /HelloWorld/Welcome/ //public string Welcome(string name, int ID = 1)
//{
// tc.TrackEvent("welcome");
// return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
//}
public ActionResult Welcome(FaceInfo fi)
{
tc.TrackEvent("Generating....");
ViewData["Message"] = "Hello " + fi.Name;
ViewData["FaceCount"] = fi.FaceCount;
List<String> faces = DrawingHelper.Draw(fi.FaceCount).Select(t => t.ToString()).ToList();
return View(faces);
}
}
}

omg~我还有一个model呢~ model好像是实现了在controller里传值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace game1.Models
{
public class FaceInfo
{
public int FaceCount { get; set; }
public string Name { get; set; }
}
}

Index.cshtml里很简单

俩输入框,一个按钮

@model game1.Models.FaceInfo
@{
ViewData["Title"] = "Index";
} <h2>--------------HELLO--------------</h2>
<hr />
@using (Html.BeginForm())
{
<p>User name:</p>
<input type="text" name="userName" />
<p>Face count:</p>
<input type="text" name="count" />
<p></p>
<input type="submit" value="Try It!"/>
}> <p>Hello to many little faces!</p>

Welcome.cshtml似乎更简单!

@{
ViewData["Title"] = "Welcome";
}
@model List<String>
<h2>Welcome to Winnie's app</h2> <p>This is my first web app.</p> @foreach(var element in Model)
{
<li>@ViewData["Message"]</li>
<pre>@Html.DisplayFor(m=>element)</pre>
}

嗯虽然简单,但是宝宝也忙乎了一天~