Commit fdf33081 authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

4. prednaska

parent 1af34f37
using System;
using NPuzzle.Core;
namespace ConsoleUi.App
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Field field;
}
}
}
......@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NPuzzleCore", "NPuzzleCore\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NPuzzleConsole", "NPuzzleConsole\NPuzzleConsole.csproj", "{202AE2BB-A1CD-42F1-8ABD-4BF8F865A1BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NPuzzleTest", "NPuzzleTest\NPuzzleTest.csproj", "{E97D948D-31A5-40EF-A9E2-2D732254FA89}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -21,6 +23,10 @@ Global
{202AE2BB-A1CD-42F1-8ABD-4BF8F865A1BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{202AE2BB-A1CD-42F1-8ABD-4BF8F865A1BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{202AE2BB-A1CD-42F1-8ABD-4BF8F865A1BB}.Release|Any CPU.Build.0 = Release|Any CPU
{E97D948D-31A5-40EF-A9E2-2D732254FA89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E97D948D-31A5-40EF-A9E2-2D732254FA89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E97D948D-31A5-40EF-A9E2-2D732254FA89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E97D948D-31A5-40EF-A9E2-2D732254FA89}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Text;
using NPuzzle.Core;
using NPuzzleCore.Entity;
using NPuzzleCore.Service;
namespace NPuzzle.ConsoleUI
{
......@@ -9,6 +11,8 @@ namespace NPuzzle.ConsoleUI
{
private readonly Field _field;
private readonly IScoreService _scoreService = new ScoreServiceFile();
public ConsoleUI(Field field)
{
_field = field;
......@@ -16,12 +20,17 @@ namespace NPuzzle.ConsoleUI
public void Play()
{
PrintTopScores();
do
{
PrintField();
ProcessInput();
} while (!_field.IsSolved());
_scoreService.AddScore(
new Score {Player = Environment.UserName, Points = _field.GetScore(), PlayedAt = DateTime.Now});
PrintField();
Console.WriteLine("Game solved!");
......@@ -44,6 +53,8 @@ namespace NPuzzle.ConsoleUI
Console.WriteLine();
}
Console.WriteLine("Score: {0}", _field.GetScore());
}
private void ProcessInput()
......@@ -51,7 +62,7 @@ namespace NPuzzle.ConsoleUI
Console.Write("Enter command: ");
var line = Console.ReadLine().ToUpper();
if("X" == line)
if ("X" == line)
Environment.Exit(0);
try
......@@ -63,5 +74,18 @@ namespace NPuzzle.ConsoleUI
//Jaro: ocakvana chyba
}
}
private void PrintTopScores()
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("--------------------- TOP SCORES ------------------------");
Console.WriteLine("----------------------------------------------------------");
foreach (var score in _scoreService.GetTopScores())
{
Console.WriteLine("{0} {1}", score.Player, score.Points);
}
Console.WriteLine("----------------------------------------------------------");
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPuzzleCore.Core;
......@@ -15,13 +16,16 @@ namespace NPuzzle.Core
private Coordinate _emptyTileCoordinate;
private DateTime startTime;
public Field(int rowCount, int columnCount)
{
RowCount = rowCount;
ColumnCount = columnCount;
_tiles = new Tile[rowCount, columnCount];
Initialize();
Shuffle();
//Shuffle();
startTime = DateTime.Now;
}
public Tile GetTile(int row, int column)
......@@ -111,5 +115,9 @@ namespace NPuzzle.Core
return true;
}
public int GetScore()
{
return RowCount * ColumnCount * 10 - (DateTime.Now - startTime).Seconds;
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
namespace NPuzzleCore.Entity
{
[Serializable]
public class Score
{
public string Player { get; set; }
public int Points { get; set; }
public DateTime PlayedAt { get; set; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using NPuzzleCore.Entity;
namespace NPuzzleCore.Service
{
public interface IScoreService
{
void AddScore(Score score);
IList<Score> GetTopScores();
void ResetScore();
}
}
using NPuzzleCore.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace NPuzzleCore.Service
{
public class ScoreServiceFile : IScoreService
{
private const string FileName = "score.bin";
private IList<Score> _scores = new List<Score>();
void IScoreService.AddScore(Score score)
{
_scores.Add(score);
SaveScores();
}
IList<Score> IScoreService.GetTopScores()
{
LoadScores();
return _scores.OrderByDescending(s => s.Points).Take(3).ToList();
//return (from s in _scores orderby s.Points descending select s).Take(3).ToList();
}
void IScoreService.ResetScore()
{
_scores.Clear();
File.Delete(FileName);
}
private void SaveScores()
{
using (var fs = File.OpenWrite(FileName))
{
var bf = new BinaryFormatter();
bf.Serialize(fs, _scores);
}
}
private void LoadScores()
{
if (File.Exists(FileName))
{
using (var fs = File.OpenRead(FileName))
{
var bf = new BinaryFormatter();
_scores = (List<Score>)bf.Deserialize(fs);
}
}
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NPuzzleCore\NPuzzleCore.csproj" />
</ItemGroup>
......
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPuzzleCore.Service;
using System;
using NPuzzleCore.Entity;
namespace NPuzzleTest
{
[TestClass]
public class ScoreServiceTest
{
[TestMethod]
public void AddTest1()
{
var service = CreateService();
service.AddScore(new Score {Player = "Jaro", Points = 100, PlayedAt = DateTime.Now});
Assert.AreEqual(1, service.GetTopScores().Count);
Assert.AreEqual("Jaro", service.GetTopScores()[0].Player);
Assert.AreEqual(100, service.GetTopScores()[0].Points);
}
[TestMethod]
public void AddTest3()
{
var service = CreateService();
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Peter", Points = 200, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jozo", Points = 50, PlayedAt = DateTime.Now });
Assert.AreEqual(3, service.GetTopScores().Count);
Assert.AreEqual("Peter", service.GetTopScores()[0].Player);
Assert.AreEqual(200, service.GetTopScores()[0].Points);
Assert.AreEqual("Jaro", service.GetTopScores()[1].Player);
Assert.AreEqual(100, service.GetTopScores()[1].Points);
Assert.AreEqual("Jozo", service.GetTopScores()[2].Player);
Assert.AreEqual(50, service.GetTopScores()[2].Points);
}
[TestMethod]
public void AddTest5()
{
var service = CreateService();
service.AddScore(new Score { Player = "Zuzka", Points = 20, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Anca", Points = 10, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Peter", Points = 200, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jozo", Points = 50, PlayedAt = DateTime.Now });
Assert.AreEqual(3, service.GetTopScores().Count);
Assert.AreEqual("Peter", service.GetTopScores()[0].Player);
Assert.AreEqual(200, service.GetTopScores()[0].Points);
Assert.AreEqual("Jaro", service.GetTopScores()[1].Player);
Assert.AreEqual(100, service.GetTopScores()[1].Points);
Assert.AreEqual("Jozo", service.GetTopScores()[2].Player);
Assert.AreEqual(50, service.GetTopScores()[2].Points);
}
[TestMethod]
public void ResetTest()
{
var service = CreateService();
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.ResetScore();
Assert.AreEqual(0, service.GetTopScores().Count);
}
private IScoreService CreateService()
{
return new ScoreServiceFile();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment