JAVA/알고리즘
[JAVA & JSP] 미니게임(2)_LOTTO
YOU R PLANET
2021. 12. 14. 23:16
게임 원리 [인덱스 페이지] 1. 로또 당첨 번호 7개를 세팅한다. (중복 없이) ----------------------------------------------------------- [플레이 페이지] [선택 처리 페이지] 2. 로또 번호 선택 판을 만든다. (7행 46열로 0 인덱스는 사용하지 않는다.) * 각 행에서 서로 다른 번호를 눌러야 한다. * 각 행에서는 하나의 번호만 눌러야 한다. ------------------------------------------------------------- [결과 페이지] 3. 번호 선택 후, 당첨 여부를 판단한다. |
1. index
<%@page import="java.util.Arrays"%>
<%@page import="java.util.Random"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<!-- 게임 원리 -->
<!--
1. 로또 당첨 배열을 만든다. (중복 없이 7개)
2. 1 - 45 까지 수를 가진 7개 배열을 2차원으로 만든다. [7][45];
3. 각 버튼을 누르고, 7개를 모두 선택하면 당첨결과를 확인한다.
-->
<%
Random ran = new Random();
int lotto[][] = new int[7][46]; // 열 인덱스와 숫자가 일치하도록 45개가 아닌 46개로 만든다.
int[] jackpot = new int[7]; // 로또 당첨 번호를 담을 배열
int count = 0; // 눌린 버튼 개수를 셀 변수
for(int i = 0; i < jackpot.length; i++){
int num = ran.nextInt(45)+1; // 1~ 45 의 숫자 중 랜덤
jackpot[i] = num;
for(int j = 0; j < i; j++){
if(jackpot[j] == num){
i--;
break;
}
}
}
System.out.println("당첨 번호: " + Arrays.toString(jackpot));
session.setAttribute("lotto", lotto);
session.setAttribute("jackpot", jackpot);
session.setAttribute("count", count);
response.sendRedirect("LottoPlay.jsp");
%>
</body>
</html>
2. lottoplay
<body>
<%
int[][] lotto = (int[][])session.getAttribute("lotto");
int count = (int)session.getAttribute("count");
%>
<table border="1">
<tr align="center"><td colspan="7"><h1>LOTTO 7/45</h1></td></tr>
<tr>
<%for(int i = 0; i < lotto.length; i++){ %>
<td width="120px">
<%for(int j = 1; j < lotto[i].length; j++){ %>
<%if(lotto[i][j] == 1){ %> <!-- 누른 번호이면 id=checked 인 버튼을 생성-->
<button id="checked" onclick="window.location.href='LottoChoice.jsp?x=<%=i%>&&y=<%=j%>'"><%=j%></button>
<%}else{%> <!-- 아직 안 눌린 번호이면 일반 버튼 생성 -->
<button onclick="window.location.href='LottoChoice.jsp?x=<%=i%>&&y=<%=j%>'"><%=j%></button>
<%}%>
<%if(j % 5 == 0){%>
<br>
<%}%>
<%}%>
</td>
<%}%>
</tr>
<tr align="center"><td colspan="7">
<%if(count == 7){ %> <!-- 번호 7개를 다 골랐으면, 당첨확인 버튼 생성 -->
<input type="button" id="button2" onclick="window.location.href='LottoPro.jsp?'" value="당첨확인">
<%} %>
</td></tr>
</table>
</body>
스타일이 길어서 <body></body>만 코드블럭으로 만들었다.
3. lottoChoice
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
int state = 0; // 1 : 더 이상 누를 수 없음(중복값, 혹은 같은 행) , 2 : 이미 눌렀던 값을 또 누름
int[][] lotto = (int[][])session.getAttribute("lotto");
int x = Integer.parseInt(request.getParameter("x")); // 행 인덱스
int y = Integer.parseInt(request.getParameter("y")); // 열 인덱스
int count = (int)session.getAttribute("count");
for(int i =0; i < lotto.length; i++){
for(int j = 1; j < lotto[i].length; j++){
if(lotto[i][j] == 1){ // 누른 번호일 때,
if(i == x && y == j){ // 그리고 받은 인덱스와 완전히 동일할 때, state = 2 (이미 눌렀던 값)
state = 2;
break;
}else if(i == x || j == y){ // 그리고 행 번호(한 행 당 숫자는 하나) 혹은 열 번호가 같을 때(같은 숫자 선택 금지), state = 1
state = 1;
break;
}
}
}
if(state == 1 || state ==2){
break;
}
}
if(state == 0 && count < 7){ // 중복 X, 눌린 번호가 아니고 7개 보다 적게 눌렀을 때,
lotto[x][y] = 1; // 골랐다는 표시 해주고, count++
count++;
}else if(state == 2){ // 이미 눌렀던 값일 때,
lotto[x][y] = 0; // 다시 0처리 해주고, count--;
count--;
}
session.setAttribute("count", count);
response.sendRedirect("LottoPlay.jsp");
%>
</body>
</html>
눌린 값과 새로 들어온 값의
1. 행 인덱스 or 열 인덱스가 같을 때
2. 둘다 같을 때
3. 모두 다를 때
3가지 조건으로 나눠 처리했다.
4. lottoPro
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style>
span{
color: red;
}
</style>
</head>
<body>
<%
int[] jackpot = (int[])session.getAttribute("jackpot");
int[][] lotto = (int[][])session.getAttribute("lotto");
int[] me = new int[7];
for(int i = 0; i < lotto.length; i++){
for(int j = 0; j < lotto[i].length; j++){
if(lotto[i][j] == 1){ //누른 값이면 내 배열에 넣어라.
me[i] = j;
}
}
}
int count = 0;
for(int i = 0; i < jackpot.length; i++){
for(int j = 0; j < me.length; j++){
if(jackpot[i] == me[j]){ // 로또 번호와 내 번호가 같으면 count ++
count++;
break;
}
}
}
%>
<table>
<tr align="center"><td><h1>당첨 결과:
<%if(count < 3){%>
꽝
<%}else if(count == 3){ %>
5등
<%}else if(count == 4){ %>
4등
<%}else if(count == 5){ %>
3등
<%}else if(count == 6){ %>
2등
<%}else{%>
1등
<%} %>
</h1></td></tr>
<tr align="center"><td><h3>
<%for(int i = 0; i < jackpot.length; i++){ %>
<%=jackpot[i]%>
<%}%>
</h3></td></tr>
<tr align="center"><td><h1>내 번호</h1></td></tr>
<tr align="center"><td><h3>
<%for(int i = 0; i < me.length; i++){%>
<%=me[i]%>
<%}%>
</h3></td></tr>
<tr align="center"><td>
<a href="index.jsp">처음으로</a>
</td></tr>
</table>
</body>
</html>
실행 모습
> 첫 화면
> 버튼 선택
> 당첨 결과
12, 29, 34
3개의 번호를 맞췄기 때문에 5등!
더하기 게임과 마찬가지로,
간단한 구조를 갖고 있어서 한번쯤 만들어 보기 좋다.
게다가, 로또 번호를 컨닝해서 1등에 당첨될 수도 있다.