Skip to content

Commit 167165a

Browse files
committed
issue #30 A Chessboard Game java w/ Explanation
1 parent b575e5c commit 167165a

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed
Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
package hackerrank;
22

33
public class AChessboardGame {
4+
// https://www.hackerrank.com/challenges/a-chessboard-game-1/forum/comments/290655
5+
static String chessboardGame(int t, int x, int y) {
6+
// 체스게임을 하는 플레이어 2명 중의 승자를 리턴하는 문제
7+
// 아래 4가지 규칙으로만 이동이 가능하고 더이상 이동할 수 없는 플레이어가 지게된다.
8+
// 1. (x-2, y+1)
9+
// 2. (x-2, y-1)
10+
// 3. (x+1, y-2)
11+
// 4. (x-1, y-2)
12+
/* 15X15체스판에서 플레이어1이 이길 수 있는 칸과 플레이어2가 이길 수 있는 칸을 작성하면 아래와 같다.
13+
*
14+
2211 2211 2211 221
15+
2211 2211 2211 221
16+
1111 1111 1111 111
17+
1111 1111 1111 111
418
5-
static String chessboardGame(int x, int y) {
6-
for (int i = 0; i < 2; i++) {
7-
if (((x - 1) / 2) % 2 == 0 && ((y - 1) / 2) % 2 == 0) {
19+
2211 2211 2211 221
20+
2211 2211 2211 221
21+
1111 1111 1111 111
22+
1111 1111 1111 111
23+
24+
2211 2211 2211 221
25+
2211 2211 2211 221
26+
1111 1111 1111 111
27+
1111 1111 1111 111
28+
29+
2211 2211 2211 221
30+
2211 2211 2211 221
31+
1111 1111 1111 111
32+
* */
33+
34+
for(int i=0; i<t; i++){
35+
if ((x % 4 == 1 || x % 4 == 2) && (y % 4 == 1 || y % 4 == 2)) {
836
return "Second";
937
}
1038
}
11-
return "First";
39+
return "First";
1240
}
1341

1442
public static void main(String[] args) {
15-
System.out.println(chessboardGame(5, 2) + ", ans: Second");
16-
System.out.println(chessboardGame(5, 3) + ", ans: First");
17-
System.out.println(chessboardGame(8, 8) + ", ans: First");
43+
System.out.println(chessboardGame(3,5, 2) + ", ans: Second"); //3 1 3 3
44+
System.out.println(chessboardGame(3,5, 3) + ", ans: First");
45+
System.out.println(chessboardGame(3,8, 8) + ", ans: First");
46+
System.out.println(chessboardGame(3,7, 3) + ", ans: First");
47+
System.out.println(chessboardGame(3,8, 12) + ", ans: First");
48+
System.out.println(chessboardGame(3,9, 7) + ", ans: First");
1849
}
1950

2051
}

0 commit comments

Comments
 (0)