본문 바로가기

Java, Spring/07일

7일차 7

가위, 바위, 보 게임

  • gameform.jsp : 가위,바위,보 선택 페이지
  • Gamepro servlet : 사람 입력 값 받고 컴퓨터 랜덤 값으로 승패계산까지
  • gamepr.jsp : 결과 보여줘
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="../Gamepro" method="post">
<select name="you">
	<option value="1">가위</option>
	<option value="2">바위</option>
	<option value="3">보</option>
</select>
<input type="submit" value="선택"/>
</form>
</body>
</html>
 
package test.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Gamepro extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int you = Integer.parseInt(request.getParameter("you"));		
		int com = (int) (Math.random()*3)+1;

		String strYou = strRSP(you);
		String strCom = strRSP(com);
		String strWin = youWin(you,com);
		
		request.setAttribute("strYou", strYou);
		request.setAttribute("strCom", strCom);
		request.setAttribute("strWin", strWin);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/basic/gamepro.jsp");
		dispatcher.forward(request, response);
	}
		
	protected String youWin(int you, int com){		
/*		String strRtn = "";		
		if (you == com){
			strRtn = "비겼습니다.";
		} else if (you==1) {
			if (com == 2) strRtn = "당신의 패배";
			else strRtn = "당신의 승리";			
		}else if (you==2) {
			if (com == 3) strRtn = "당신의 패배";
			else strRtn = "당신의 승리";	
		}else  {
			if (com == 1) strRtn = "당신의 패배";				
			else strRtn = "당신의 승리";	
		}		
		return strRtn;*/
		
		switch(((you-com)+3)%3){
			case 0: return "비겼습니다";
			case 1: return "당신의 승리";
			case 2: return "당신의 패배";
			default: return "오류";			
		}
	}
	
	protected String strRSP(int num){
		String rtnStr = "";
		if (num == 1)
			rtnStr = "가위";
		else if(num==2)
			rtnStr = "바위";
		else 
			rtnStr = "보";
		
		return rtnStr;				
	}
}
 
  <servlet>
  	<servlet-name>Gamepro</servlet-name>
  	<servlet-class>test.servlet.Gamepro</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Gamepro</servlet-name>
  	<url-pattern>/Gamepro</url-pattern>
  </servlet-mapping>
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
You : ${strYou}<br/>
Com : ${strCom}<br/>
${strWin}
</body>
</html>

'Java, Spring > 07일' 카테고리의 다른 글

7일차 2  (0) 2012.09.25
7일차 3  (0) 2012.09.25
7일차 4  (0) 2012.09.25
7일차 5  (0) 2012.09.25
7일차 6  (0) 2012.09.25