Commit df4d29e4 authored by Andres Käver's avatar Andres Käver
Browse files

fix git

parent 99ac7234
#office
*.tmp
# Word temporary
~$*.doc*
# Word Auto Backup File
Backup of *.doc*
# Excel temporary
~$*.xls*
# Excel Backup File
*.xlk
# PowerPoint temporary
~$*.ppt*
# Visio autosave temporary files
*.~vsd*
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Course Materials
Delete CSharp 2020 Fall / Course Materials
\ No newline at end of file
# Common IntelliJ Platform excludes
.idea/
*.suo
*.user
.vs/
[Bb]in/
[Oo]bj/
_UpgradeReport_Files/
[Pp]ackages/
Thumbs.db
Desktop.ini
.DS_Store
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\GameBrain\GameBrain.csproj" />
<ProjectReference Include="..\GameConsoleUI\GameConsoleUI.csproj" />
<ProjectReference Include="..\MenuSystem\MenuSystem.csproj" />
</ItemGroup>
</Project>
using System;
using System.Runtime.CompilerServices;
using GameBrain;
using GameConsoleUI;
using MenuSystem;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=========> TIC-TAC-TOE AKAVER <================");
var menuB = new Menu(MenuLevel.Level2Plus);
menuB.AddMenuItem(new MenuItem("Sub 2.", "1", DefaultMenuAction));
var menuA = new Menu(MenuLevel.Level1);
menuA.AddMenuItem(new MenuItem("Go to submenu 2", "1", menuB.RunMenu));
menuA.AddMenuItem(new MenuItem("testing", "2", DefaultMenuAction));
var menu = new Menu(MenuLevel.Level0);
menu.AddMenuItem(new MenuItem("Go to submenu 1", "s", menuA.RunMenu));
menu.AddMenuItem(new MenuItem("New game human vs human. Pointless.", "1", TicTacToe));
menu.AddMenuItem(new MenuItem("New game puny human vs mighty AI", "2", DefaultMenuAction));
menu.AddMenuItem(new MenuItem("New game mighty AI vs superior AI", "3", DefaultMenuAction));
menu.RunMenu();
}
static string DefaultMenuAction()
{
Console.WriteLine("Not implemented yet!");
return "";
}
static string TicTacToe()
{
var game = new TicTacToe();
TicTacToeConsoleUI.DrawBoard(game.GetBoard());
var menu = new Menu(MenuLevel.Level0);
menu.AddMenuItem(new MenuItem(
$"Player {(game.NextMoveByX ? "X" : "O")} make a move",
userChoice: "p",
() =>
{
var (x, y) = GetMoveCordinates(game);
game.MakeAMove(x, y);
TicTacToeConsoleUI.DrawBoard(game.GetBoard());
return "";
})
);
menu.AddMenuItem(new MenuItem(
$"Exit game",
userChoice: "e",
DefaultMenuAction)
);
var userChoice = menu.RunMenu();
return userChoice;
}
static (int x, int y) GetMoveCordinates(TicTacToe game)
{
Console.WriteLine("Upper left corner is (1,1)!");
Console.Write("Give X (1-3), Y (1-3):");
var userValue = Console.ReadLine().Split(',');
var x = int.Parse(userValue[0].Trim()) - 1;
var y = int.Parse(userValue[1].Trim()) - 1;
return (x, y);
}
}
}
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{9AEE246B-9E4E-408C-9939-26CE884B4473}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MenuSystem", "MenuSystem\MenuSystem.csproj", "{7ACAA3D8-5E24-4A7A-93AA-C00723BC0B81}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution items", "Solution items", "{AC44475D-74E1-4166-B560-FC9A99692AF0}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
.gitignore = .gitignore
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameBrain", "GameBrain\GameBrain.csproj", "{FF509682-3B24-4B9B-8C77-1E59D560F825}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameConsoleUI", "GameConsoleUI\GameConsoleUI.csproj", "{BE921800-8FDA-4C04-9516-05B99199B17D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9AEE246B-9E4E-408C-9939-26CE884B4473}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AEE246B-9E4E-408C-9939-26CE884B4473}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AEE246B-9E4E-408C-9939-26CE884B4473}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AEE246B-9E4E-408C-9939-26CE884B4473}.Release|Any CPU.Build.0 = Release|Any CPU
{7ACAA3D8-5E24-4A7A-93AA-C00723BC0B81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7ACAA3D8-5E24-4A7A-93AA-C00723BC0B81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7ACAA3D8-5E24-4A7A-93AA-C00723BC0B81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7ACAA3D8-5E24-4A7A-93AA-C00723BC0B81}.Release|Any CPU.Build.0 = Release|Any CPU
{FF509682-3B24-4B9B-8C77-1E59D560F825}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF509682-3B24-4B9B-8C77-1E59D560F825}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF509682-3B24-4B9B-8C77-1E59D560F825}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF509682-3B24-4B9B-8C77-1E59D560F825}.Release|Any CPU.Build.0 = Release|Any CPU
{BE921800-8FDA-4C04-9516-05B99199B17D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE921800-8FDA-4C04-9516-05B99199B17D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE921800-8FDA-4C04-9516-05B99199B17D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE921800-8FDA-4C04-9516-05B99199B17D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace GameBrain
{
public enum CellState
{
Empty,
X,
O,
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
namespace GameBrain
{
// _, X, O, /X, /O
public class TicTacToe
{
private readonly CellState[,] _board = new CellState[3,3];
private bool _nextMoveByX = true;
public CellState[,] GetBoard()
{
var res = new CellState[3,3];
Array.Copy(_board, res, _board.Length );
return res;
}
public bool NextMoveByX => _nextMoveByX;
public bool MakeAMove(int x, int y)
{
if (_board[x, y] == CellState.Empty)
{
_board[x, y] = _nextMoveByX ? CellState.X : CellState.O;
_nextMoveByX = !_nextMoveByX;
return true;
}
return false;
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\GameBrain\GameBrain.csproj" />
</ItemGroup>
</Project>
using System;
using GameBrain;
namespace GameConsoleUI
{
public static class TicTacToeConsoleUI
{
public static void DrawBoard(CellState[,] board)
{
// add plus 1, since this is 0 based. length 0 is returned as -1;
var width = board.GetUpperBound(0) + 1; // x
var height = board.GetUpperBound(1) + 1; // y
for (int colIndex = 0; colIndex < width; colIndex++)
{
Console.Write($"+---+");
}
Console.WriteLine();
for (int rowIndex = 0; rowIndex < height; rowIndex++)
{
for (int colIndex = 0; colIndex < width; colIndex++)
{
Console.Write($"| {CellString(board[colIndex, rowIndex])} |");
}
Console.WriteLine();
for (int colIndex = 0; colIndex < width; colIndex++)
{
Console.Write($"+---+");
}
Console.WriteLine();
}
}
public static string CellString(CellState cellState)
{
switch (cellState)
{
case CellState.Empty: return " ";
case CellState.O: return "O";
case CellState.X: return "X";
}
return "-";
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace MenuSystem
{
public enum MenuLevel
{
Level0,
Level1,
Level2Plus
}
public class Menu
{
// list is not really optimal choice
//private List<MenuItem> MenuItems { get; set; } = new List<MenuItem>();
private Dictionary<string, MenuItem> MenuItems { get; set; } = new Dictionary<string, MenuItem>();
private readonly MenuLevel _menuLevel;
private readonly string[] reservedActions = new[] {"x", "m", "r"};
public Menu(MenuLevel level)
{
_menuLevel = level;
}
public void AddMenuItem(MenuItem item)
{
if (item.UserChoice == "")
{
throw new ArgumentException($"UserChoice cannot be empty");
}
MenuItems.Add(item.UserChoice, item);
}
public string RunMenu() // need to be of type Func<string>
{
var userChoice = "";
do
{
Console.Write("");
foreach (var menuItem in MenuItems)
{
Console.WriteLine(menuItem.Value);
}
switch (_menuLevel)
{
case MenuLevel.Level0:
Console.WriteLine("X) eXit");
break;
case MenuLevel.Level1:
Console.WriteLine("M) return to Main");
Console.WriteLine("X) eXit");
break;
case MenuLevel.Level2Plus:
Console.WriteLine("R) Return to pervious");
Console.WriteLine("M) return to Main");
Console.WriteLine("X) eXit");
break;
default:
throw new Exception("Unknown menu depth!");
}
Console.Write(">");
userChoice = Console.ReadLine()?.ToLower().Trim() ?? "";
// is it a reserved keyword
if (!reservedActions.Contains(userChoice))
{
// no it wasn't, try to find keyword in MenuItems
if (MenuItems.TryGetValue(userChoice, out var userMenuItem))
{
userChoice = userMenuItem.MethodToExecute();
}
else
{
Console.WriteLine("I don't have this option!");
}
}
if (userChoice == "x")
{
if (_menuLevel == MenuLevel.Level0)
{
Console.WriteLine("Closing down......");
}
break;
}
if (_menuLevel != MenuLevel.Level0 && userChoice == "m")
{
break;
}
if (_menuLevel == MenuLevel.Level2Plus && userChoice == "r")
{
break;
}
} while (true);
return userChoice;
}
}
}
\ No newline at end of file
using System;
namespace MenuSystem
{
public class MenuItem
{
public virtual string Label { get; set; }
public virtual string UserChoice { get; set; }
public virtual Func<string> MethodToExecute { get; set; }
public MenuItem(string label, string userChoice, Func<string> methodToExecute)
{
Label = label.Trim();
UserChoice = userChoice.Trim();
MethodToExecute = methodToExecute;
}
public override string ToString()
{
return UserChoice + ") " + Label;
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
</Project>
# C# Homeworks
## HW01
***Deadline: 2020-09-30 23:59:59***
Implement menu system for console applications.
Allow N depth of sub-menus. Different levels of menu must have predefined functionalities:
0) Exit from program
1) Exit from program, Return to Main
2) Exit from program, Return to Main, Return to previous
Do not allow user to specify user choices that are already taken (including the predefined ones!).
And think out more fancy functionalities to implement:
min & max lengths, keyboard based navigation, colors, no duplicate items, default user choice, etc.
Markdown is supported
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