[JSP] 가위바위보 게임
2016. 11. 2. 18:10ㆍProgramming/JSP
<index.jsp: 메인 화면>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <%@ 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>HTML 연습</title> </head> <body> <% try{ //전적을 세션에서 받아옴 int win_you = (int)session.getAttribute("win_you"); int win_com = (int)session.getAttribute("win_com"); %> <h2>현재 전적: <%=win_you%>:<%=win_com%></h2> <% if(win_com>2 || win_you>2){ %> <h1>시합 끝났습니다</h1> <% if(win_com>win_you){ %> <h1>당신의 패배!</h1> <p><img src="./img/lose.PNG"></p> <% }else{ %> <h1>당신의 승리!</h1> <p><img src="./img/win.PNG"></p> <% } } }catch(Exception e){e.printStackTrace();} %> <h1>가위바위보 게임</h1> <p><img src="./img/main.PNG"></p> <form action="./result.jsp" method="get"> <select name="you"> <option value="0">가위</option> <option value="1">바위</option> <option value="2">보</option> </select> <input type="submit" value="제출"> </body> </html> | cs |
<result.jsp:결과 표시 화면>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.Random" %> <!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>결과 화면</title> </head> <body> <h1>당신이 낸 것</h1> <% switch(Integer.parseInt(request.getParameter("you"))){ case 0:%><p><img src="./img/sc.PNG"></p> <%break; case 1:%><p><img src="./img/gu.PNG"></p> <%break; case 2:%><p><img src="./img/pa.PNG"></p> <%break; } %> <% //컴퓨터 난수 설정 int com=0; String result=null; Random random = new Random(); com = random.nextInt(3); //이긴 횟수 계산(컴퓨터/당신) int win_com, win_you; if(session.getAttribute("win_you")==null){ win_com=0; win_you=0; }else{ win_com=(int)session.getAttribute("win_com"); win_you=(int)session.getAttribute("win_you"); %> <h1>컴퓨터가 낸 것</h1> <% switch(com){ case 0:%><p><img src="./img/sc.PNG"></p> <%break; case 1:%><p><img src="./img/gu.PNG"></p> <%break; case 2:%><p><img src="./img/pa.PNG"></p> <%break; } //가위바위보 게임 결과 저장 switch(Integer.parseInt(request.getParameter("you"))){ case 0: if(com==1){result = "패배";win_com++;break; }else if(com==2){result = "승리";win_you++;break; }else{result = "무승부";break; } case 1:if(com==1){result = "무승부";break; }else if(com==2){result = "패배";win_com++;break; }else{result = "승리";win_you++;break; } case 2:if(com==1){result = "승리";win_you++;break; }else if(com==2){result = "무승부";break; }else{result = "패배";win_com++;break; } } } %> <%//전적을 세션에 저장 session.setAttribute("win_you",win_you); session.setAttribute("win_com",win_com); %> <h1><%=result%></h1> <p><a href="./index.jsp">다시 하기</a></p> </body> </html> | cs |
<정리>
1. Random 객체를 활용해 난수(0~2) 발생시켜 가위바위보를 대응시킴
2. 세션을 통해 화면이 변해도 값은 남아있도록 함
3. 초기화를 단 한 번 수행하기 위해서
session.getAttribute("win_com")이 null일 때만 초기화하도록 함.
4. 메인 화면에서 session.getAttribute("win_com")할 때 NullPointerException 발생하여 try~catch 처리
<미해결 오류>
1. 첫 번째 게임의 결과 화면에서 '컴퓨터가 낸 패'가 NULL로 표시됨.
'Programming > JSP' 카테고리의 다른 글
JSTL 태그 part2 (0) | 2016.11.09 |
---|---|
MVC 모델 (0) | 2016.11.07 |
JSTL 태그 part1 (0) | 2016.10.30 |
L09 EL 태그 활용, 내장 객체 종류, 예외처리 (0) | 2016.10.28 |
L08 Bean으로 값(parameter) 전달 (0) | 2016.10.27 |