340010F 足利 誠
ジャンケンゲームをつくってみました。
上のグー、チョキ、パーのいずれかを選べば勝ったり負けたり引き分けたりします。
グー、チョキ、パーは実は勝率が異なっており、出し方によって合計得点が変わってきます。
ちなみに勝ちが5点、引き分けが2点、敗けが0点です。
飽きた場合はリセットボタンを押せばカウントが戻ります。
import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*; class MyCanvas extends Canvas { String message=""; int wsum=0; int esum=0; int lsum=0; int count=0; int point=0; public MyCanvas(){ super(); setSize(200,200); } public void paint(Graphics g){ g.setColor(Color.blue); g.fillRect(0,0,200,200); g.setColor(Color.white); g.setFont(new Font("Courier",Font.BOLD,30)); g.drawString(message,70,100); g.setFont(new Font("Courier",Font.BOLD,10)); g.drawString("win="+wsum,30,160); g.drawString("even="+esum,80,160); g.drawString("lose="+lsum,130,160); g.drawString("挑戦回数"+count,30,180); g.drawString("point="+point,120,180); } public void butt1(){ int secret=(new Random()).nextInt(3)+1; if(secret==1){ message="win!"; wsum = wsum+1; } else if(secret==2){ message="even"; esum = esum+1; } else if(secret==3){ message="lose"; lsum = lsum+1; } count=wsum+esum+lsum; point=wsum*5+esum*2; repaint(); } public void butt2(){ int secret=(new Random()).nextInt(5)+1; if(secret==2){ message="win!"; wsum = wsum+1; } else if(secret==3){ message="even"; esum = esum+1; } else if(secret==4){ message="even"; esum = esum+1; } else if(secret==5){ message="even"; esum = esum+1; } else if(secret==1){ message="lose"; lsum = lsum+1; } count=wsum+esum+lsum; point=wsum*5+esum*2; repaint(); } public void butt3(){ int secret=(new Random()).nextInt(6)+1; if(secret==3){ message="win!"; wsum = wsum+1; } else if(secret==4){ message="win!"; wsum = wsum+1; } else if(secret==1){ message="even"; esum = esum+1; } else if(secret==2){ message="lose"; lsum = lsum+1; } else if(secret==5){ message="lose"; lsum = lsum+1; } else if(secret==6){ message="lose"; lsum = lsum+1; } count=wsum+esum+lsum; point=wsum*5+esum*2; repaint(); } public void butt4(){ message="reset"; wsum=0; esum=0; lsum=0; count=0; point=0; repaint(); } } public class Kadai1216 extends Applet implements ActionListener{ MyCanvas myCanvas; Button button1, button2, button3, button4; public void init(){ Panel panel=new Panel(); panel.setLayout(new FlowLayout()); panel.add(button1=new Button("グー")); button1.addActionListener(this); panel.add(button2=new Button("チョキ")); button2.addActionListener(this); panel.add(button3=new Button("パー")); button3.addActionListener(this); panel.add(button4=new Button("reset")); button4.addActionListener(this); setLayout(new BorderLayout()); add(panel,"North"); add(myCanvas=new MyCanvas(),"South"); setSize(getPreferredSize()); setVisible(true); } public void actionPerformed(ActionEvent e){ System.out.println(e); Object source=e.getSource(); if(source.equals(button1)){ myCanvas.butt1(); } if(source.equals(button2)){ myCanvas.butt2(); } if(source.equals(button3)){ myCanvas.butt3(); } if(source.equals(button4)){ myCanvas.butt4(); } } }