luturol / jokenpo

A Jokenpo solution built with .NET Core 2.0. Check my blog post about the project for more info

Home Page:https://luturol.github.io/csharp/CSharp-Jokenpo-Challenge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jokenpo Challenge

This is a repository with the solution for this problemfrom DojoPuzzles. My solution was made .Net Core 2.1.

How to run

To run the tests:

  1. Access JokenpoTests folder and after on your terminal use the following command:
dotnet test

How I solved the problem

Jokenpo consist of a game that has at least 3 hand form and which one has it's own rule to win, tie or lose. For this I thought that I must have some way of validation. The validation I wanted was to make it more "generic" and easy to create. Each validation only works for that one had form. To do it, I made an Enum for Hand forms:

public enum HandForm
{
        Rock,
        Paper,
        Scissor
}

Now the problem is: How to validate the Enum? How the Enum will have it's own Rule?

The way I thought of solving was: create an extension for this enum that will return the Rule Class

Rule class will implement the AbstractRule class that will contain an abstract method that returns the result of that play, this means that the Rule class will have the checks for that hand form against the opponent hand form.

public abstract GameStatus WinAgainst(Position opponent);

Now the Judge of the Jokenpo will have only one line of code:

public GameResult ValidateGame(Player player1, Player player2)
{
    return player1.HandForm.ToRule().WinAgainst(player2.HandForm);             
}

So, whenever someone call the Judge class to validate the game, it will only return the result of the game based on the first Player passed as Parameter.

About

A Jokenpo solution built with .NET Core 2.0. Check my blog post about the project for more info

https://luturol.github.io/csharp/CSharp-Jokenpo-Challenge


Languages

Language:C# 100.0%