[구디아카데미][IT국비지원][TIL] 가위 바위 보 게임

Seokhyun Jeong·2023년 4월 19일
0

과제

목록 보기
6/19
post-thumbnail

[구디아카데미][IT국비지원]

코드

rspForm.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>rspForm.html</title>
</head>
<body>
	<h1>가위바위보를 선택하세요</h1>
	<form action="./rspAction.jsp" method="post">
		<div>
			<input type="radio" name="userRsp" value="가위">
			<image src="./img/가위.jpg"> <!-- 가위 이미지 -->
			<input type="radio" name="userRsp" value="바위">
			<image src="./img/바위.jpg"> <!-- 바위 이미지 -->
			<input type="radio" name="userRsp" value="">
			<image src="./img/보.jpg"> <!-- 보 이미지 -->
		</div>
		<div>
			<button type="submit">가위바위보</button>
		</div>
	</form>
</body>
</html>

rspAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>rspAction.jsp</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf8");
		String userRsp = request.getParameter("userRsp"); // "가위" or "바위" or "보"
		
		int r = (int) (Math.random() * 3); // 0, 1, 2
		
		String comRsp = null;
		if (r == 0) {
			comRsp = "가위";
		} else if(r == 1) {
			comRsp = "바위";
		} else {
			comRsp = "보";
		}
		
		String result = null;
		if(userRsp.equals(comRsp)) {
			result = "비겼다";
		} else if((userRsp.equals("가위") && comRsp.equals("보")) || (userRsp.equals("바위") && comRsp.equals("가위")) || (userRsp.equals("보") && comRsp.equals("바위"))) {
			result = "이겼다";
		} else {
			result = "졌다";
		}
	%>
	<table>
		<tr>
			<td>사용자 : </td>
			<td><image src="./img/<%=userRsp %>.jpg"></td>
			<td>컴퓨터 : </td>
			<td><image src="./img/<%=comRsp %>.jpg"></td>
		</tr>
		
		<tr>
			<td>결과 : </td>
			<td><%=result %></td>
			<!-- <td></td> -->
			<!-- <td></td> -->
		</tr>
	</table>
</body>
</html>

결과화면


0개의 댓글