1. jsp 로 가위바위보 게임을 구현하시오.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RockPaperScissors</title>
</head>
<body>
<form action = "rockPaperScissors.jsp" method = "post">
<h1>Select one of Rock, Paper, Scissors</h1>
<select name = "rps">
<option value = "Rock" >Rock</option>
<option value = "Paper">Paper</option>
<option value = "Scissors">Scissors</option>
</select>
<input type = "submit" value = "FIGHT">
</form>
</body>
</html>
<%@ page import = "game.RockPaperScissors" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RockPaperScissors</title>
</head>
<body>
<%
RockPaperScissors rps = new RockPaperScissors();
String hand1 = request.getParameter("rps");
String hand2 = rps.newHandRandom();
String print = rps.fight(hand1, hand2);
%>
<%="Yours is " + hand1%><br>
<%="Mine is " + hand2%><br>
<%=print%><br>
<a href="rockPaperScissors.html">
Try again?
</a>
</body>
</html>
package game;
import java.util.concurrent.ThreadLocalRandom;
class Const {
static final String ROCK = "Rock";
static final String PAPER = "Paper";
static final String SCISSORS = "Scissors";
static final String[] HANDS = {ROCK, PAPER, SCISSORS};
}
public class RockPaperScissors {
public RockPaperScissors() {
}
public String newHandRandom() {
return Const.HANDS[ThreadLocalRandom.current().nextInt(0, Const.HANDS.length)];
}
public String fight(String hand1, String hand2) {
String print;
if (hand1.equals(hand2)) {
print = "Nobody won.";
} else if ((hand1.equals(Const.ROCK) && hand2.equals(Const.SCISSORS))
|| (hand1.equals(Const.PAPER) && hand2.equals(Const.ROCK))
|| (hand1.equals(Const.SCISSORS) && hand2.equals(Const.PAPER)))
{
print = "You Won.";
} else {
print = "You Lost.";
};
return print;
}
}