MVCに分割して以下のようなジャンケンゲームを作成してみよう。勝敗数はゲーム中保持される。
セッションスコープを利用すること。
●実行すると以下のような画面が表示される。
●手を選んで送信を押すと結果が表示される
●カウントをリセットするをクリックするとリセットされる
1.新規動的プロジェクトでjyan2を作成する。
2.modelパッケージにRspクラスを以下のように作成する。
●model.Jyan.java
package model; import java.io.Serializable; public class Jyan implements Serializable{ private String userHand; private String pcHand; private String result; private int win; private int lose; public String getUserHand() { return userHand; } public void setUserHand(String userHand) { this.userHand = userHand; } public String getPcHand() { return pcHand; } public void setPcHand(String pcHand) { this.pcHand = pcHand; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public int getWin() { return win; } public void setWin(int win) { this.win = win; } public int getLose() { return lose; } public void setLose(int lose) { this.lose = lose; } }
3.modelパッケージにRspLogicクラスを以下のように作成する
●model.JyanLogic.java
package model; import java.util.Random; public class JyanLogic { public void execute(Jyan jyan,int userHand){ String[] hands={"グー","チョキ","パー"}; jyan.setUserHand(hands[userHand]); int pcHand=new Random().nextInt(hands.length); jyan.setPcHand(hands[pcHand]); int diff=(userHand+3 - pcHand) % 3; String result; if(diff==0){ result="あいこです"; }else if(diff==2){ result="あなたの勝ち"; jyan.setWin(jyan.getWin()+1); }else{ result="あなたの負け"; jyan.setLose(jyan.getLose()+1); } jyan.setResult(result); } }
4.controllerパッケージの中にMainクラスを作成する。
●controller.Main.java
package controller; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import model.Jyan; import model.JyanLogic; /** * Servlet implementation class Main */ @WebServlet("/Main") public class Main extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action=request.getParameter("action"); if(action != null && action.equals("reset")){ HttpSession session=request.getSession(); session.removeAttribute("jyan"); } RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/main.jsp"); rd.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String hand=request.getParameter("hand"); HttpSession session=request.getSession(); Jyan j=(Jyan)session.getAttribute("jyan"); if(j==null){ j=new Jyan(); } JyanLogic jl=new JyanLogic(); jl.execute(j,Integer.parseInt(hand)); session.setAttribute("jyan", j); doGet(request, response); } }
5.WebContent/WEB-INF/の中にviewフォルダを作成しその中にmain.jspを以下のように作成する。
●WebContent/WEB-INF/view/main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="model.*"%> <% Jyan j=(Jyan)session.getAttribute("jyan"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> <h1>ジャンケンゲーム!</h1> <form action="/jyan2/Main" method="post"> <input type="radio" name="hand" value="0" <%=j !=null && j.getUserHand().equals("グー") ? "checked":"" %>>グー <input type="radio" name="hand" value="1" <%=j !=null && j.getUserHand().equals("チョキ") ? "checked":"" %>>チョキ <input type="radio" name="hand" value="2" <%=j !=null && j.getUserHand().equals("パー") ? "checked":"" %>>パー <input type="submit" value="送信"> </form> <% if (j != null){ %> <p>あなたは<%=j.getUserHand() %></p> <p>PCは<%=j.getPcHand() %></p> <p><%=j.getResult() %></p> <p>Win:<%=j.getWin() %></p> <p>Lose:<%=j.getLose() %></p> <a href="/jyan2/Main?action=reset">カウントをリセットする</a> <%} %> </body> </html>
6.実行してみよう。
コメント