31-3: Servlet RockPaperScissors

jk·2024년 2월 15일
0

kdt 풀스택

목록 보기
61/127



1. jsp 로 가위바위보 게임을 구현하시오.

<!-- code1 -->
<!-- .\WebContent\rockPaperScissors.html -->
<!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>
<!-- code2 -->
<!-- .\WebContent\rockPaperScissors.jsp -->
<%@ 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>
//code3
//.\src\game\RockPaperScissors.java
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;
	}
}


profile
Brave but clumsy

0개의 댓글