実技(C#プログラミング実習)3

1.bootstrapを使ったジャンケンアプリの作成
2.bootstrapを使ったジャンケンアプリの作成
3.bootstrapを使ったジャンケンアプリ解説
〇model.Jyanken.java

package model;

import java.io.Serializable;

public class Jyanken implements Serializable{
	private String myHand;
	private String pcHand;
	private String result;
	public Jyanken() {}
	public Jyanken(String myHand) {
		this.myHand=myHand;
	}
	public String getMyHand() {
		return myHand;
	}
	public void setMyHand(String myHand) {
		this.myHand = myHand;
	}
	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;
	}

}

〇model.JyankenLogic.java

package model;

public class JyankenLogic {
	public void execute(Jyanken j) {
		String[] hands= {"グー","チョキ","パー"};
		String pcHand=hands[(int)(Math.random()*hands.length)];
		j.setPcHand(pcHand);
		String result;
		if(j.getMyHand().equals(pcHand)) {
			result="Draw";
		}else if(
				j.getMyHand().equals("グー") && pcHand.equals("チョキ")
				|| j.getMyHand().equals("チョキ") && pcHand.equals("パー")
				|| j.getMyHand().equals("パー") && pcHand.equals("グー")
				) {
			result="Win";
		}else {
			result="Lose";
		}
		j.setResult(result);

	}
}

〇controller.Main.java

package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 model.Jyanken;
import model.JyankenLogic;

/**
 * Servlet implementation class Main
 */
@WebServlet("/main")
public class Main extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/result.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application=this.getServletContext();
		List<Jyanken> list=(List<Jyanken>)application.getAttribute("data");
		if(list == null) {
			list=new ArrayList<>();
		}
		request.setCharacterEncoding("UTF-8");
		String myHand=request.getParameter("hand");
		if(myHand == null) {
			request.setAttribute("err", "手を選択してください!");
		}else {
			Jyanken jk=new Jyanken(myHand);
			JyankenLogic logic=new JyankenLogic();
			logic.execute(jk);
			request.setAttribute("jk",jk);
			list.add(0,jk);
			application.setAttribute("data", list);
		}

		doGet(request, response);
	}

}

〇/WEB-INF/view/result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="model.*,java.util.*"%>
<%
	List<Jyanken> list = (List<Jyanken>) application.getAttribute("data");
	Jyanken jk = (Jyanken) request.getAttribute("jk");
	String err = (String) request.getAttribute("err");
%>
<!doctype html>
<html lang="ja">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet"
	href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
	integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
	crossorigin="anonymous">

<title>ジャンケンゲーム</title>
</head>
<body style="background: #eee;">
	<div class="container p-4 mt-2 bg-white">
		<form action="/jyanken2/main" method="post" class="mx-auto"
			style="width: 300px; text-align: center; font-size: 27px;">
			<div class="form-check form-check-inline">
				<input class="form-check-input" type="radio"
					name="hand" id="inlineRadio1" value="グー"> <label
					class="form-check-label" for="inlineRadio1">グー</label>
			</div>
			<div class="form-check form-check-inline">
				<input class="form-check-input" type="radio"
					name="hand" id="inlineRadio2" value="チョキ">
				<label class="form-check-label" for="inlineRadio2">チョキ</label>
			</div>
			<div class="form-check form-check-inline">
				<input class="form-check-input" type="radio"
					name="hand" id="inlineRadio3" value="パー"> <label
					class="form-check-label" for="inlineRadio3">パー</label>
			</div>
			<button type="submit" class="btn btn-primary d-block mx-auto mt-4 "
				style="width: 230px;">いざ勝負</button>
		</form>
		<%if(err != null){%>
		<div class="alert alert-danger col-8 mx-auto mt-4 text-center" role="alert">
  <%=err %>
		</div>
		<%} %>
		<%if(jk != null){%>
		<div class="alert alert-success col-8 mx-auto mt-4 text-center" role="alert" style="font-size:20px;">
  あなたは<%=jk.getMyHand() %>,PCは<%=jk.getPcHand() %>:<%=jk.getResult() %>
		</div>
		<%} %>
		<%if(list != null){%>
<table class="table table-striped mt-4">
<tr><th>あなたの手</th><th>PCの手</th><th>勝敗</th></tr>
<%for(Jyanken j:list) {%>
<tr><td><%=j.getMyHand() %></td><td><%=j.getPcHand() %></td><td><%=j.getResult() %></td></tr>
<%} %>
</table>
<%} %>

	</div>

	<!-- Optional JavaScript -->
	<!-- jQuery first, then Popper.js, then Bootstrap JS -->
	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
		integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
		crossorigin="anonymous"></script>
	<script
		src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
		integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
		crossorigin="anonymous"></script>
	<script
		src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
		integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
		crossorigin="anonymous"></script>
</body>
</html>

4.DB接続
5.DB接続