Skip to content

Instantly share code, notes, and snippets.

@taikomegane
Last active May 20, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikomegane/835e0c390dcd3b7a1b3206f358494137 to your computer and use it in GitHub Desktop.
Save taikomegane/835e0c390dcd3b7a1b3206f358494137 to your computer and use it in GitHub Desktop.
じゃんけんシミュレーション
public class Main {
public static void main(String[] args) {
//Refereeインスンタンスを生成
Referee r = new Referee(1000,5); //引数に実行回数と人数を指定
//Playerインスタンスを5つ生成
Player[] pl = new Player[5]; //人数を引数に指定
for(int i = 0; i < pl.length; i++){
pl[i] = new Player();
}
//num回シミュレーションを回す
for (int i = 0; i < r.getNum(); i++){
//人数を戻す
r.setGroup();
//全員に順位がつくまで
do {
//回数の初期化
r.clearCount();
//前回の勝ちと負けでそれぞれじゃんけんをする=2周回す
for (int j = 0; j < 2; j++){
if (r.getGroup().get(j) < 2){
continue; //人数が2人未満→スキップ
} else { //人数が2人以上→勝敗つくまでじゃんけん
do {
//じゃんけんの準備
r.clearRsp(); //rspの初期化
r.setCount(j); //カウントを1増やす
//じゃんけん
for (int k = 0; k < r.getGroup().get(j); k++){
pl[k].setRsp(); //じゃんけんの手を決める
r.countRsp(pl[k].getRsp()); //人数を数える
}
} while (r.judge(j)); //継続判定
r.add(); //勝敗人数を配列に追加
}
}
r.setTotal_count(r.getCount()); //総回数を更新
r.remove(); //終了した組を配列から削除
} while (r.getGroup().size() != 0); //全員に順位がつく
//System.out.println(r.getTotal_count() + "回で終わり!");
}
System.out.println("平均"+r.calcAvg(r.getTotal_count(), r.getNum())+"回です");
}
}
public class Player {
//Field
private int rsp = 0;
//Constructor
Player (){
}
//Setter, Getter
public int getRsp() {
return rsp;
}
public void setRsp() {
this.rsp = new java.util.Random().nextInt(3);
}
}
import java.util.ArrayList;
public class Referee {
//Field
private int num; //シミュレーション回数
private int total; //総人数
private ArrayList <Integer> group = new ArrayList<Integer>(); ////じゃんけんのグループ用の配列
private int win = 0; //勝者人数
private int lose = 0; //敗者人数
private int count[] = {0,0}; //じゃんけん1回が終了するまでの回数
private int total_count = 0; //じゃんけんの総回数
private int r; private int s; private int p; //rock scissors paper
//Constructor シミュレーション回数と人数を指定
public Referee (int num, int total){
this.num = num;
this.total = total;
this.win = total;
group.add(win);
group.add(lose);
}
//Setter, Getter
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public ArrayList<Integer> getGroup() {
return group;
}
public void setGroup() {
this.group.add(this.total);
this.group.add(0);
}
public int getWin() {
return this.win;
}
public void setWin(int win) {
this.win = win;
}
public int getLose() {
return this.lose;
}
public void setLose(int lose) {
this.lose = lose;
}
public int getTotal_count() {
return total_count;
}
public void setTotal_count(int[] count) {
this.total_count += Math.max(count[0], count[1]);
}
public int[] getCount() {
return count;
}
public void setCount(int i) {
this.count[i] += 1;
}
//Method
//初期化
//RSPの初期化
public void clearRsp(){
this.r = 0; this.s = 0; this.p = 0;
}
//カウントの初期化
public void clearCount(){
this.count[0] = 0;
this.count[1] = 0;
}
//じゃんけん
//RSPの人数を数える
public void countRsp(int rsp){
switch (rsp){
case 0:
this.r++; break;
case 1:
this.s++; break;
case 2:
this.p++; break;
}
}
//勝敗人数かあいこを返す
public boolean judge(int j){
if ((this.r == 0 ^ this.s == 0 ^ this.p == 0) ||
(this.r == 0 && this.s == 0 && this.p == 0)){
if (this.r == 0) {
// r==0なら、win=s, lose=p
this.win = this.s; this.lose = this.p;
} else if (this.s == 0) {
// s==0なら、win=p, lose=r
this.win = this.p; this.lose = this.r;
} else {
// p==0なら、win=r, lose=s
this.win = this.r; this.lose = this.s;
}
return false;
}
return true;
}
public void add(){
this.group.add(this.win);
this.group.add(this.lose);
}
public void remove(){
this.group.remove(1);
this.group.remove(0);
}
//シミュレーション
//平均値を出力
public double calcAvg(int total_number, int number){
return (double)total_number / (double)number;
}
}
@taikomegane
Copy link
Author

人数を指定して、結果が出るまでの回数を計算するじゃんけんシミュレーションです。
人数と実行回数を指定することで、結果が出るまでの平均値が算出されます。

注意:
複数グループでじゃんけんを行う場合、同時にじゃんけんをしています。
例:2人と3人でじゃんけんをし、それぞれ3回、4回で終了→4回だけが総数にカウントされる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment