:Java Applet プログラム{〜01/29(Mon)}
Java Applet/AWTについて学習し、Java Appletのオリジナルプログラムを作成し解説せよ。
ソースコード
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.applet.*; public class marubatsu extends Applet implements ActionListener{ boolean flag; //勝敗判定フラグ Button b[],res; int [][] map; //勝敗判定に使う二次元配列 int count = 1; //先攻、後攻を判断するカウンタ Label label = new Label("対戦型マルバツゲーム。"); public void init() { Panel p = new Panel(); //ボタンを設置するパネル res = new Button("リセット"); //リセットボタン res.addActionListener((ActionListener) this); //リセットボタンをイベント登録 p.setLayout(new GridLayout(3,3,2,2)); //パネルpのレイアウトをグリッドレイアウトに設定 b = new Button[9]; for(int i = 0;i<9;i++){ //ボタンをパネルに設置 p.add(b[i] = new Button()); b[i].addActionListener(this); //イベント登録 b[i].setFont(new Font("Gothic",Font.BOLD,48)); } setLayout(new BorderLayout()); //レイアウトをボーダーレイアウトに設定 add("Center",p); //パネルpをCenterに設置 add("South",res); //resボタンを下に設置 add("North",label); //ラベルlabelを上に設置 map = new int[3][3]; } public void actionPerformed(ActionEvent e){ //ボタンが押されたときに呼び出されたメソッド if(e.getSource()==b[0]){ //押したボタンはb[0]か? if((b[0].getLabel().length()==0)&&(!flag)){ //ボタンに何か表示されてるか フラグはfalseか? if(count%2!=0){ //カウンタが偶数か奇数かで先攻か後攻か判定 b[0].setLabel("o"); //ボタン上にラベルを設置 map[0][0]=1; //勝敗判定のために二次元配列に値を代入 count++; //カウンタをインクリメント if((map[0][0]==1&&map[0][1]==1&&map[0][2]==1)||(map[0][0]==\1&&map[1][1]==1 &&map[2][2]==1)||(map[0][0]==1&&map[1][0]==1&&map[2][0]==1)){ //二次元配列に代入されてる数値を見て勝敗判定 flag = true; //勝敗判定が完了したのでflagをtrueに label.setText("先攻の勝ちです。"); //ラベルに勝利メッセージを表示させる } else if(count==10) //ボタンが全部押されたか判定 label.setText("引き分けです。"); //ラベルに引き分けメッセージを表示 } else { b[0].setLabel("x"); map[0][0]=2; //後攻の場合は2を代入 count++; if((map[0][0]==2&&map[0][1]==2&&map[0][2]==2)||(map[0][0] ==\2&&map[1][1]==2&&map[2][2]==2)||(map[0][0]==2&&map[1][0]==2&&map[2][0]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[1]){ if((b[1].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[1].setLabel("o"); map[0][1]=1; count++; if((map[0][0]==1&&map[0][1]==1&&map[0][2]==1)||(map[0][1]== 1&&map[1][1]==1&&map[2][1]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[1].setLabel("x"); map[0][1]=2; count++; if((map[0][0]==2&&map[0][1]==2&&map[0][2]==2)||(map[0][1]== 2&&map[1][1]==2&&map[2][1]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[2]){ if((b[2].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[2].setLabel("o"); map[0][2]=1; count++; if((map[0][0]==1&&map[0][1]==1&&map[0][2]==1)||(map[0][2]==\1&& map[1][2]==1&&map[2][2]==1)||(map[0][2]==1&&map[1][1]==1&&map[2][0]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[2].setLabel("x"); map[0][2]=2; count++; if((map[0][0]==2&&map[0][1]==2&&map[0][2]==2)||(map[0][2]==\2&&map[1][2] ==2&&map[2][2]==2)||(map[0][2]==2&&map[1][1]==2&&map[2][0]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[3]){ if((b[3].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[3].setLabel("o"); map[1][0]=1; count++; if((map[1][0]==1&&map[1][1]==1&&map[1][2]==1)|| (map[0][0]==\1&&map[1][0]==1&&map[2][0]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[3].setLabel("x"); map[1][0]=2; count++; if((map[1][0]==2&&map[1][1]==2&&map[1][2]==2)|| (map[0][0]==\2&&map[1][0]==2&&map[2][0]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[4]){ if((b[4].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[4].setLabel("o"); map[1][1]=1; count++; if((map[1][0]==1&&map[1][1]==1&&map[1][2]==1)||(map[0][1]==\1&&map[1][1] ==1&&map[2][1]==1)||(map[0][0]==1&&map[1][1]==1&&map[2][2]==1)|| (map[0][2]==1&&map[1][1]==1&&map[2][0]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[4].setLabel("x"); map[1][1]=2; count++; if((map[1][0]==2&&map[1][1]==2&&map[1][2]==2)||(map[0][1]==2&&map[1][1] ==2&&map[2][1]==2)||(map[0][0]==2&&map[1][1]==2&&map[2][2]==2)|| (map[0][2]==2&&map[1][1]==2&&map[2][0]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[5]){ if((b[5].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[5].setLabel("o"); map[1][2]=1; count++; if((map[1][0]==1&&map[1][1]==1&&map[1][2]==1)|| (map[0][2]==1&&map[1][2]==1&&map[2][2]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[5].setLabel("x"); map[1][2]=2; count++; if((map[1][0]==2&&map[1][1]==2&&map[1][2]==2)||(map[0][2]==2&& map[1][2]==2&&map[2][2]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[6]){ if((b[6].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[6].setLabel("o"); map[2][0]=1; count++; if((map[0][0]==1&&map[1][0]==1&&map[2][0]==1) ||(map[0][2]==\1&&map[1][1]==1&&map[2][0]==1) ||(map[2][0]==1&&map[2][1]==1&&map[2][2]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[6].setLabel("x"); map[2][0]=2; count++; if((map[0][0]==2&&map[1][0]==2&&map[2][0]==2)|| (map[0][2]==\2&&map[1][1]==2&&map[2][0]==2)|| (map[2][0]==2&&map[2][1]==2&&map[2][2]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[7]){ if((b[7].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[7].setLabel("o"); map[2][1]=1; count++; if((map[0][1]==1&&map[1][1]==1&&map[2][1]==1)|| (map[2][0]==1&&map[2][1]==1&&map[2][2]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[7].setLabel("x"); map[2][1]=2; count++; if((map[0][1]==2&&map[1][1]==2&& map[2][1]==2)||(map[2][0]==2&& map[2][1]==2&&map[2][2]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==b[8]){ if((b[8].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[8].setLabel("o"); map[2][2]=1; count++; if((map[0][0]==1&&map[1][1]==1&&map [2][2]==1)||(map[0][2]==1&&map[1][2]==1&&map[2][2]==1)|| (map[2][0]==1&&map[2][1]==1&&map[2][2]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[8].setLabel("x"); map[2][2]=2; count++; if((map[0][0]==2&&map[1][1]==2&&map [2][2]==2)||(map[0][2]==\2&&map[1][2]==2&&map[2][2]==2) ||(map[2][0]==2&&map[2][1]==2&&map[2][2]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } if(e.getSource()==res){ //resボタンが押された flag = false; //全て初期状態に戻す count = 1; label.setText("対戦型マルバツゲーム。"); for(int i=0;i<9;i++) b[i].setLabel(""); for(int j=0;j<3;j++){ for(int k=0;k<3;k++) map[j][k]=0; } } } } |
ソースの方にもコメントでちょっとした考察というか説明みたいなものを記入しています。ソースのほうは あるページからお借りしてきました。このアプレットはマルバツゲームです。全体的な形は前の課題のgui プログラムに似た形になっています。パネルを作ったりリセットボタンを作ったりしてできています。また コンピュータとの対戦などはまだ勉強していないところでの科目になるのでいれてはいません。countとい う関数で先攻と後攻を決めています。マルバツゲームは二人プレイでマスが9マスしかのいので奇数、偶数 で先攻と後攻の判断ができ、先攻の時はマルが表示、後攻だとバツが表示されるようになっている。また countが10になるとすべてのマスが埋まっていることになるのでそのときは引き分けになるように設定 している。 if文のところでは各ボタンごとの設定をしている。 例えば if(e.getSource()==b[1]){ if((b[1].getLabel().length()==0)&&(!flag)){ if(count%2!=0){ b[1].setLabel("o"); map[0][1]=1; count++; if((map[0][0]==1&&map[0][1]==1&&map[0][2]==1)||(map[0][1] ==1&&map[1][1]==1&&map[2][1]==1)){ flag = true; label.setText("先攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } else{ b[1].setLabel("x"); map[0][1]=2; count++; if((map[0][0]==2&&map[0][1]==2&&map[0][2]==2)||(map[0][1] ==2&&map[1][1]==2&&map[2][1]==2)){ flag = true; label.setText("後攻の勝ちです。"); } else if(count==10) label.setText("引き分けです。"); } } } こんな場合だとb[1]のボタンがマルのときこのボタンと一列になる可能性のあるところに全てマルがあるか 調べて一列になっているなら先攻の勝ちだと表示する。このボタンでcountが10になったら引き分けなの で引き分けだと表示する。またこれがバツの時に成り立つかを調べて同じように表示にさせる。どの条件も 満たさない時はそのままにして次にどれかのボタンを押すまでは何もしない。この繰り返しをしている。 またリセットボタンを押されたときはすべての場所のマルやバツを何もない状態にする。