• 締切済み

じゃんけんプログラミング Java

以下のプログラムでは、2人でのじゃんけんプログラムになっています。 このプログラムの /** * 「ジャンケン、ポン!」と声をかけ、 * プレイヤーの手を見て、どちらが勝ちかを判定する。 * * @param player1 判定対象プレイヤー1 * @param player2 判定対象プレイヤー2 * @return 勝ったプレイヤー。引き分けの場合は null を返す。 */ private Player judgeJanken(Player player1, Player player2) { Player winner = null; // プレイヤー1の手を出す int player1hand = player1.showHand(); // プレイヤー2の手を出す int player2hand = player2.showHand(); // それぞれの手を表示する printHand(player1hand); System.out.print(" vs. "); printHand(player2hand); System.out.print("\n"); // プレイヤー1が勝つ場合 if ((player1hand == Player.STONE && player2hand == Player.SCISSORS) || (player1hand == Player.SCISSORS && player2hand == Player.PAPER) || (player1hand == Player.PAPER && player2hand == Player.STONE)) { winner = player1; } // プレイヤー2が勝つ場合 else if ( (player1hand == Player.STONE && player2hand == Player.PAPER) || (player1hand == Player.SCISSORS && player2hand == Player.STONE) || (player1hand == Player.PAPER && player2hand == Player.SCISSORS)) { winner = player2; } // どちらでもない場合は引き分け(nullを返す) return winner; } と /** * オブジェクト指向によるジャンケンプログラム */ public class ObjectJanken { public static void main(String[] args) { // 審判(斎藤さん)のインスタンス生成 Judge saito = new Judge(); // プレイヤー1(村田さん)の生成 Player murata = new Player("村田さん"); // プレイヤー2(山田さん)の生成 Player yamada = new Player("山田さん"); // 村田さんと山田さんをプレイヤーとしてジャンケンを開始する saito.startJanken(murata, yamada); } } を変更してn人のじゃんけんプログラムにしたいと思っています。どのように変更すればよいでしょうか?

みんなの回答

noname#208507
noname#208507
回答No.1

1.n人のPlayer配列を引数にとるstartJanken()を新たに作ります。 2.そのメソッドの中に二重ループを作り、それぞれPlayer配列の要素について繰り返します。外側のループで走査するPlayer配列の要素をplayer1、内側のそれをplayer2とします。 3.ループの最も内側で、player1とplayer2が異なるプレーヤーかどうか条件分岐します。 4.player1とplayer2が別人の場合に限り、ループの最も内側でjudgeJanken()を呼びます。 5.外側のループの最後で、player1が勝利した回数をカウントします。 6.このとき、もしn-1回勝ったら(自分以外の全員に勝ったら)、player1の勝ちで終了。そうでなければ次のplayer1でループを続行。 7.外側のループが終了したら、引き分け。 おおむね、こんなところでしょう。 コーディングは自分でがんぱってください。

関連するQ&A

  • じゃんけん プログラミング

    以下のプログラムでは、2人でのじゃんけんプログラムになっています。 このプログラムの /** * 「ジャンケン、ポン!」と声をかけ、 * プレイヤーの手を見て、どちらが勝ちかを判定する。 * * @param player1 判定対象プレイヤー1 * @param player2 判定対象プレイヤー2 * @return 勝ったプレイヤー。引き分けの場合は null を返す。 */ private Player judgeJanken(Player player1, Player player2) { Player winner = null; // プレイヤー1の手を出す int player1hand = player1.showHand(); // プレイヤー2の手を出す int player2hand = player2.showHand(); // それぞれの手を表示する printHand(player1hand); System.out.print(" vs. "); printHand(player2hand); System.out.print("\n"); // プレイヤー1が勝つ場合 if ((player1hand == Player.STONE && player2hand == Player.SCISSORS) || (player1hand == Player.SCISSORS && player2hand == Player.PAPER) || (player1hand == Player.PAPER && player2hand == Player.STONE)) { winner = player1; } // プレイヤー2が勝つ場合 else if ( (player1hand == Player.STONE && player2hand == Player.PAPER) || (player1hand == Player.SCISSORS && player2hand == Player.STONE) || (player1hand == Player.PAPER && player2hand == Player.SCISSORS)) { winner = player2; } // どちらでもない場合は引き分け(nullを返す) return winner; } と /** * オブジェクト指向によるジャンケンプログラム */ public class ObjectJanken { public static void main(String[] args) { // 審判(斎藤さん)のインスタンス生成 Judge saito = new Judge(); // プレイヤー1(村田さん)の生成 Player murata = new Player("村田さん"); // プレイヤー2(山田さん)の生成 Player yamada = new Player("山田さん"); // 村田さんと山田さんをプレイヤーとしてジャンケンを開始する saito.startJanken(murata, yamada); } } を変更してn人のじゃんけんプログラムにしたいと思っています。どのように変更すればよいでしょうか?

  • Cプログラミング

    作成したジャンケンをするゲームに対して、人間かコンピュータのどちらかが先に5勝するまで繰り返すように改造せよ。(それぞれの対戦者に対する変数を用意し、勝つごとにその変数に1を加えていく。どちらかが5になれば終了するような反復構造にすればよい。) #include <stdio.h> #include <stdlib.h> main(){ int jibunn,aite; printf("Select rock(0) scissors(1) paper(2)\n"); printf("jibunn="); scanf("%d",&jibunn); srand(time(NULL)); aite=rand()%3; printf("aite=%d\n",aite); while(jibunn==aite){ printf("Drow\n"); printf("Select rock(0) scissors(1) paper(2)\n"); printf("jibunn="); scanf("%d",&jibunn); srand(time(NULL)); aite=rand()%3; printf("aite=%d\n",aite); } if(jibunn==aite){ printf("Drow\n"); } else if((jibunn==2)&&(aite==0)){ printf("You win!\n"); } else if((jibunn==0)&&(aite==1)){ printf("You win!\n"); } else if((jibunn==1)&&(aite==2)){ printf("You win!\n"); } else { printf("You lose…\n"); } } ここまでは出来たのですが、これにどのように付け足せばよいのでしょうか? 回答よろしくお願いします。

  • 正規表現の表現方法について

    じゃんけんの手を正規表現での表現方法について。 それぞれの手を定数で表現しています。場にじゃんけんの手が012と出たときに並び替えた120や210もパターン認識させたいと考えています。 正規表現の記述について教えていただきたいです。 現在のコード package game; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { // ユーザーの手を格納する変数 private static int playerhand; private static int user1hand; private static int user2hand; // 各々の勝数 private static int playercount = 0; private static int user1count = 0; private static int user2count = 0; // ジャンケンの手を表す定数を宣言する private static final int STONE = 0; private static final int SCISSORS = 1; private static final int PAPER = 2; // 判定する文字列 private static String checkhand = null; public static void main(String[] args) { // ここでじゃんけんさせる playerhand = STONE; user1hand = PAPER; user2hand = STONE; // 判定する文字列 checkhand = String.valueOf(playerhand) + String.valueOf(user1hand) + String.valueOf(user2hand); System.out.println(checkhand); // 判定するパターンを用意 // 引き分けパターン Pattern draw = Pattern.compile("([0]&[1]&[2])|([0]&[0]&[0])|([1]&[1]&[1])|([2]&[2]&[2])"); // 2人勝ちパターン Pattern win2 = Pattern.compile("([0]&[0]&[1])|([1]&[1]&[2])|([2]&[2]&[0])"); // 1人勝ちパターン Pattern win1 = Pattern.compile("([0]&[1]&[1])|([1]&[2]&[2])|([2]&[0]&[0])"); // パターンとの一致を確認するための準備 Matcher checkdraw = draw.matcher(checkhand); Matcher checkwin2 = win2.matcher(checkhand); Matcher checkwin1 = win1.matcher(checkhand); if (checkdraw.find()) { System.out.println("引き分けパターン"); } else if (checkwin2.find()) { System.out.println("2人勝ちのパターン"); checkWin2(); } else if (checkwin1.find()) { System.out.println("1人勝ちのパターン"); checkWin1(); } } /** * 1人勝ちで勝者の勝数を増やすメソッド * */ public static void checkWin1() { if (playerhand == user1hand) { user2count++; } else if (user1hand == user2hand) { playercount++; } else { user1hand++; } } /** * 2人勝ちで勝者の勝数を増やすメソッド */ public static void checkWin2() { if (playerhand == user1hand) { playercount++; user1count++; } else if (user1hand == user2hand) { user1hand++; user2hand++; } else { playercount++; user2count++; } } } ※OKWAVEより補足:「Webシステム開発」についての質問です。

    • ベストアンサー
    • Java
  • C言語-エラーの位置

    いろんなものを見ながらじゃんけんのプログラムを作ったのですが、 どうしても最後でエラーが出てしまいます。 その原因がどうしてもわかりません。 エラーの出る部分は、最後の「}」、「return」ですが・・・。 以下がプログラムです。(printf内の\nは省略してます) この場合、}とreturnの位置はどうすればいいでしょうか? #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int player_hand,cpu_hand_result; srand((unsigned) time(NULL)); do{ printf("1:グー 2:ちょき・・・・"); scanf("%d",&player_hand); if(player_hand<1 || player_hand>3)result-1; player_hand--; cpu_hand = rand()%3; switch(cpu_hand){ case 0:printf("グー"); break; case 1:printf("チョキ"); break; case 2:printf("パー"); break; } result-(cpu_hand-player_hand+3)%3; if(result==2)result=-1; switch(result){ case 1:printf("あなたの勝ち"); break; case -1:printf("あなたの負け"); break; case 0:printf("あいこ"); break; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • データを検索するJavaプログラムについての質問です。

    データを検索するJavaプログラムについての質問です。 EmpShowクラス(入力した、idがファイルの中のidと等しいならばその他(名前、事業部、社員番号)を返す)の一部 --------------------- Employee emp; DataRead dr = new DataRead("Emp.dat"); //データ読み込みオブジェクト生成 if ((emp = dr.getEmployee(Integer.parseInt(tId.getText()))) != null) { //検索 tName.setText(emp.getName()); tDep.setText(emp.getDep()); tTel.setText(emp.getTel()); } ---------------------- Emp.dat(ファイル)の中身→これが間違っている!! --------------------------------- 7705 山田太郎 ××事業部 2407 7701 山田五朗 ○○事業部 2480 --------------------------------- DataRead(データを読み込んで、正しいかどうかをEmpShowに返す)クラスの一部 ---------------------------------- private DataInputStream dis; public DataRead(String fname) { try { //入力ストリーム作成 dis = new DataInputStream(new FileInputStream(fname)); //ファイルが見つからない } catch (FileNotFoundException e) { System.err.println("Opening error"); System.exit(1); } } public Employee getEmployee(int eid){ int id; String name,dep, tel; try { while (true) { id = dis.readInt();// int として読み込む name = dis.readUTF(); // 文字列として読み込む dep = dis.readUTF(); // 文字列として読み込む tel = dis.readUTF();// 文字列として読み込む if(eid == id) break;// 社員番号が見つかったらループを抜ける } return new Employee(id,name,dep,tel); // 該当社員オブジェクト返却 } catch (EOFException e) {// 読み込み終了 return null; } catch (IOException e) { System.err.println("IO error"); return null; } -------------------------------------- このプログラム(一部ですが…)の流れとしては、 「if ((emp = dr.getEmployee(Integer.parseInt(tId.getText()))) != null) { //検索」によりEmp.datファイルの中で、idが合う 項目を検索し続けます(whileにより)。もし、結果が合えば該当する社員オブジェクトを返却し、合わないならばnullを返却するような流れです。 質問としては、 id = dis.readInt();// int として読み込む name = dis.readUTF(); // 文字列として読み込む dep = dis.readUTF(); // 文字列として読み込む tel = dis.readUTF();// 文字列として読み込む で読み込まれるような、datファイルの形式を知りたいです。現状では、上記のように 7705 山田太郎 ××事業部 2407 7701 山田五朗 ○○事業部 2480 になっています。 たとえば、IDを7705と読み込ませても、nullが返却されてしまいます。 どのようなdatファイルの中をどのような並べ方にすればちゃんと読み込まれるのでしょうか? よろしくお願いします。

    • ベストアンサー
    • Java
  • Javaでのカンマで配列を分ける方法

    こんにちは。 カンマ区切りで入力されているデータを2次元配列に格納したいのですがよくわかりません。 それとデータをtxtファイルから入力する方法で配列を入力した分だけ作ることはできますでしょうか? 現在のプログラムは最初に配列を1000個用意する形になっているので効率的とは言えないと思うのですが・・・。 以下プログラムを載せます。 public String[] datain(){ String[] data = new String[1000]; String inputFileName; int i = 0; // 読み込むファイルの名前 inputFileName = "D:\\a.txt"; // ファイルオブジェクトの生成 File inputFile = new File(inputFileName); try { // 入力ストリームの生成 FileInputStream fis = new FileInputStream(inputFile); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); // テキストファイルからの読み込み String d; while ( ( d = br.readLine()) != null ) { data[i] = d; i += 1; } br.close(); } catch(Exception e) { e.printStackTrace(); } return data; } というメソッドです。 戻り値を返した後でも前でもいいので、 --データ-- (a.txt) あ,い,う,え,お か,き,く,け,こ さ,し,す,せ,そ ↓このように取り込みたいのです。 --配列-- [0,0] = あ  [0,1] = い  [0,2] = う  [0,3] = え  [0,4] = お [1,0] = か  [1,1] = き  [1,2] = く  [1,3] = け  [1,4] = こ [2,0] = さ  [2,1] = し  [2,2] = す  [2,3] = せ  [2,4] = そ よろしくお願いします。

    • ベストアンサー
    • Java
  • C言語 じゃんけんゲーム 急いでいます

    じゃんけんゲームを作成しましたが 以下のプログラムに ・コンピュータ(CP)の手を記録し一つ前と同じ手を出さずにランダムで手を出す (例:前回CPがパーなら、今回CPはチョキかグーをだす) ・あいこの場合次のじゃんけんで同じ手を再び出す (例:今回CPもプレイヤーもグーの場合は次のじゃんけんでCPはグーを出す) という2つを追加したいのですが恐縮ですが 私ならこう、という模範解答をお願いします… #include <stdio.h> int main() { int i,n; int player; int computer; //勝敗数変数 int p_kati, p_make,p_draw; int q_kati=0, q_make=0,q_draw=0; //繰り返し条件 do { //変数の初期化 p_kati=0; p_make=0; p_draw = 0; //タイトル printf("あなたが出した手は・・・\n"); //乱数の初期化 srand(time(NULL)); //最低5回繰り返す for(i=1; i<=5; i++){ //コンピュータの手 computer = rand() %3; //プレイヤーの手を表記 printf("(グー: 0 チョキ: 1 パー: 2)→ "); do{ scanf("%d", &player); //プレイヤーの手 if(player<0 || 2<player){ //0、1、2以外を入力すると printf("もう一度入力してください");//これを表示 } }while(player<0 || 2<player);//0、1、2を入力で下記を実行 //勝ち負けの判定 if(player==0 && computer==0){ printf("playerも私もグーでした…引き分けです\n"); p_draw++; } else if(player==0 && computer==1){ printf("playerはグーで、私はチョキです…あなたの勝利です\n"); p_kati++; } else if(player==0 && computer==2){ printf("playerはグーで、私はパーです…あなたの負けです\n"); p_make++; } else if(player==1 && computer==0){ printf("playerはチョキで、私はグーです…あなたの負けです\n"); p_make++; (全パターン長いので省略) else if(player==2 && computer==2){ printf("playerも私もパーでした…引き分けです\n"); p_draw++; } } //結果表示 printf("%d 勝 %d 敗 %d 引き分けでした。\n",p_kati,p_make,p_draw); printf("このまま続けますか?続ける場合は何か数字を入力し、続けない場合は -1 を入力してください > "); scanf("%d", &n); //トータル点数を計算 q_kati += p_kati; q_make += p_make; q_draw += p_draw; //n(入力された数字)が-1以外なら続ける }while(n != -1); printf("トータルで %d 勝 %d 敗 %d 引き分けでした。\n",q_kati,q_make,q_draw); return 0; }

  • C言語初心者です。 ジャンケンゲーム

    今、授業の課題でジャンケンゲームを作成していますが、なかなかできません。もし、良かったら何処が違うのか教えてください。 // main.c #include <stdio.h> #include <time.h> //#include <time.h> #include "my.h" main(int argc, char* argv[]){ int i; char s[7]; printf("これはジャンケンゲームです。\n"); printf("手を入力します(グー:1 チョキ:2 パー:3)\n"); scanf("%s",&s[7]); srand((unsigned)time(NULL)); printf("%s\n",jyan(dice(3))); if(s == jyan){ printf("あいこです。\n"); }else if(s == 1 && jyan ==2){ printf("あなたの勝ちです。\n"); }else if(s == 2 && jyan == 3){ printf("あなたの勝ちです。\n"); }else if(s == 3 && jyan ==1){ printf("あなたの勝ちです。\n"); }else{ printf("コンピュターの勝ちです。\n"); } } // dice.c #include <time.h> int dice(int n){ srand((unsigned)time(NULL)); return(rand()%n+1); } // jyan.c char *jyan(int n){ static char s[][7]={"グー","チョキ","パー"}; return s[--n]; } // my.h #include <stdio.h> char *jyan(int hand); //int dice(int n); 見づらくて申し訳ありません。4つのファイルに分けて作成しています。上記のプログラムだと自分の手、コンピューターの手がランダムに出てくるのですが判定が出来ませんでした。アドバイス、よろしくお願いします。 長文になってしまい申し訳ありません。

  • PHP5でクラスを作成しています。

    PHP5でクラスを作成しています。 コンストラクタの段階で論理エラーにしたくて、インスタンス値をnullにしたいです。 つまり直ちにプログラムを止めずに、クラスの生成(インスタンス化)を失敗させる 方法を__construct()関数内でどのように書けば良いのでしょうか? class Sample { function __construct() { // この中で処理の異常が発生! // インスタンス作成を失敗させる or インスタンス値をヌルにする。 //? //?どのように書けば?? //? } } $a = new Sample(); if ($a === null) { printf("正しくインスタンスの生成ができませんでした。"); } よろしくお願いします。

    • 締切済み
    • PHP
  • 2次元配列とじゃんけんアルゴリズムについて質問

    以下の、過去に私が質問した、2次元配列とじゃんけんアルゴリズムの質問のURLの見た上で私の質問に答えてください。 URL:ttp://okwave.jp/qa/q7038056.html 質問: public static int janken(int n){ int[][]tb1={ {9,9,9,9}, {9,0,1,2}, {9,2,0,1}, {9,1,2,0} }; int m=rand3(); System.out.println(m+" "); return tb1[n][m]; } 上記ソースコードの2次元配列について、何故「9」という数字があるのか、又1次元目と2次元目の要素数が「4」あるのかわかりませんでした。 上記のURL先で頂いた回答を元に、私は理解に努めました。その理解が正しいか判定してください。 「この勝敗表をあらわす2次元配列について、それぞれのプレイヤーのジャンケンの『手』を要素番号『1,2,3』に対応させている。つまり要素番号『0』は使っていないので、要素数が4つ必要。 また、要素番号『0』は、このjankenプログラムでは不要なので、何の値が入っても構わないので、『たまたま』9が入ってるだけで、9という数字に特に意味はない。因みに、その2つの要素番号に対応する要素が勝敗の結果の番号になる。」 こういうことでしょうか?

    • ベストアンサー
    • Java