Instructions
Rock Paper Scissors Let’s play! You have to return which player won! In case of a draw return Draw!.
Examples:
1
2
3
rps('scissors','paper') // Player 1 won!
rps('scissors','rock') // Player 2 won!
rps('paper','paper') // Draw!
My Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class Kata
{
public int getState(string str)
{
switch (str)
{
case "rock": return 1;
case "scissors": return 2;
case "paper": return 3;
}
return 0;
}
public string Rps(string p1, string p2)
{
// "rock", "scissors", "paper"
int diff = getState(p1) - getState(p2);
if (diff == -1 || diff == 2)
{
return "Player 1 won!";
}
else if (diff == 0)
{
return "Draw!";
}
return "Player 2 won!";
}
}
- 가독성이 떨어진다.
- 베스트 솔루션 방식처럼 케이스 바이 케이스 별로 스트링을 구해주는게 더 나은 방법 인듯..
Best Practices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class Kata
{
public string Rps(string p1, string p2)
{
if (p1 == p2)
return "Draw!";
if (((p1 == "rock") && (p2 == "scissors")) ||
((p1 == "scissors") && (p2 == "paper")) ||
((p1 == "paper") && (p2 == "rock")))
{
return "Player 1 won!";
}
else
{
return "Player 2 won!";
}
}
}
-
Previous
[Codewars #56] Remove First and Last Character (8kyu) -
Next
[Codewars #58] Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!! (6kyu)