LoginSignup
3
0

Javaで動的に変わるじゃんけんプログラミングの作成

Last updated at Posted at 2023-06-02

はじめに

じゃんけん初期画面.png

動的に変わるじゃんけんプログラミングを作ってみようと思い、サーブレット、JSPの練習にもなると思い、作成しました。

gitはこちら
https://github.com/TowaMurata/rock_paper_scissors

使用した技術

HTML
CSS
Java
サーブレット
JSP

作成したクラス

index.jsp
初期画面を表示するjsp

じゃんけん初期画面.png

rock_paper_scissorsServlet.java
選択した値を取得して、リクエストスコープに入れる。 ランダムで作った値(コンピュータの手)をリクエストスコープに入れる。 上の二つを勝ち負け判定処理のクラスに渡して、勝ち負け判定をリクエストスコープに入れ、結果を表示する画面にフォワードする。
rock_paper_scissorsLogic.java
受けっとった、選択した値とランダムの値をもとに勝ち負け判定して、結果をint型で返す。
rock_paper_scissors.jsp
じゃんけん結果の画像を表示するクラス。

じゃんけん_勝ち.png

勝ち負け判定処理処理

     //selectBottom,random == 0 グー
        //selectBottom,random == 1 チョキ
        //selectBottom,random == 2 パー
		//r = 0:引き分け 1:勝ち 2:負け 3:エラー
		int r = 0;
		if(selectBottom == random) {
			return r;
			
		}else if(selectBottom == 0 && random == 1 ||
				 selectBottom == 1 && random == 2 ||
				 selectBottom == 2 && random == 0
				)
		{
			r = 1;
			return r;		
			
		}else if(selectBottom == 0 && random == 2 ||
				 selectBottom == 1 && random == 0 ||
				 selectBottom == 2 && random == 1 
				)
		{
			r = 2;
		return r;
		}
		r = 3;
		return r;

選択した値によって表示画像を返す処理(jsp内でやっている。)

 <%
    String myImage = null;
    String mySelect = null;
    if(selectBottom == 0){
    	myImage = "image/RPS_Rock.png";
    	mySelect = "グー";
    }else if(selectBottom == 1){
    	myImage = "image/RPS_Scissors.png";
    	mySelect = "チョキ";
    }else if(selectBottom == 2){
    	myImage = "image/RPS_Paper.png";
    	mySelect ="パー";
    }
    
    String conImage = null;
    String conSelct = "null";
    if(random == 0){
    	conImage = "image/RPS_Rock.png";
    	conSelct = "グー";
    }else if(random == 1){
    	conImage = "image/RPS_Scissors.png";
    	conSelct = "チョキ";
    }else if(random == 2){
    	conImage = "image/RPS_Paper.png";
    	conSelct = "パー";
    }
    
    String messege = null;
    if(result == 0){
    	messege = "残念!あいこです。";
    }else if(result == 1){
    	messege = "おめでとうございます!あなたの勝ちです。";
    }else if(result == 2){
    	messege = "残念!あなたの負けです。";
    }
    %>

やってみた感想

選択した手の画像を出す処理を、jsp内で条件分岐してやっていて、これでいいのかどうかアドバイスをください。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0