0
アプレットで交互にターンが表示できません
javaアプレットで○×ゲームを作成していますが、交互にターンを表示させようとしても一方のターンしか表示されず困っています。playerメソッドでturnChangeを呼び出しても表示がされないようです。どなたかご回答お願いします。
import java.applet.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
//marubatuクラスはアプレットを継承し、マウスイベントを処理できる
public class marubatu extends Applet implements ActionListener {
int board[][]=new int[3][3];
int xpos,ypos,c=1;
int maru=1,batu=-1,kara=0;
int turn;
String s="";
String message="";
String tmessage="";
public marubatu() {
}
//アプレットが開始されたとき、最初に実行されるメソッド
public void init(){
c=1;
for (int i = 0 ; i < 3 ; i++)
for (int j = 0 ; j < 3 ; j++)
board[i][j]=kara;
turn=maru;
message="";
tmessage="";
s="";
repaint();
button();
}//最初に実行されるメソッド終了
//マウスイベントのメソッド
public boolean mouseDown(Event evt,int x,int y){
//クリック地点のマス目
xpos=x/60;ypos=y/60;
if(s=="先攻"||s=="後攻"){
if(xpos>=0&&xpos<3&&ypos>=0&&ypos<3){
player();
think();
judge();
repaint();
}
}
return true;
} //マウスイベント終了
//ボタン作成メソッド
public void button(){
Button sb =new Button("先攻");
Button tb =new Button("後攻");
add(tb);
add(sb);
setLayout(null);
tb.setBounds(50,250,70,30);
sb.setBounds(50,200,70,30);
tb.addActionListener(this);
sb.addActionListener(this);
}//ボタン作成メソッド終了
//ボタンアクションイベント
public void actionPerformed(ActionEvent e) {
s = e.getActionCommand();
if(s=="先攻"){
tmessage="あなたのターンです";
}
if(s=="後攻"){
tmessage="CPUのターンです";
think();
}
repaint();
}//アクションイベント終了
//○×を置くメソッド
public void player(){
//マスの範囲内でマスに何もなく、messageに何も入ってなければ以下を実行
if(xpos>=0&&xpos<3&&ypos>=0&&ypos<3&&board[xpos][ypos]==kara&&message==""){
board[xpos][ypos]=turn;
turn*=-1;
c++;
turnChange();
}
}//○×を置くメソッド終了
//どちらのターンか表示させるメソッド
public void turnChange(){
if(message==""&&(s=="先攻"||s=="後攻")){
if(turn==1)
tmessage="あなたのターンです";
else if(turn==-1)
tmessage="CPUのターンです";
}
repaint();
}//どちらのターンか表示させるメソッド終了
//cpuの思考メソッド
public void think(){
judge();
int a=0;
Random rnd = new Random();
while(a==0&&message==""){
int ran1 = rnd.nextInt(3);
int ran2 = rnd.nextInt(3);
xpos=ran1; ypos=ran2;
if(xpos>=0&&xpos<3&&ypos>=0&&ypos<3&&board[xpos][ypos]==kara){
board[xpos][ypos]=turn;
turn*=-1;
a++;
c++;
turnChange();
}
}
}//思考メソッド終了
//勝利判定のメソッド
public void judge(){
int i,j,a=0;
int lnaname=0,rnaname=0;
for(i=0;i<3;i++){
int yoko=0,tate=0;
for(j=0;j<3;j++){
yoko+=board[i][j];
tate+=board[j][i];
}
if(yoko==3||tate==3){
if(s=="先攻")
message="You win!!";
else if(s=="後攻")
message="You loose!!";
tmessage="";
}else if(yoko==-3||tate==-3){
if(s=="後攻")
message="You win!!";
else if(s=="先攻")
message="You loose!!";
tmessage="";
}
}
for(i=0;i<3;i++){
lnaname+=board[i][i];
rnaname+=board[i][2-i];
}
if(lnaname==3||rnaname==3){
if(s=="先攻")
message="You win!!";
else if(s=="後攻")
message="You loose!!";
tmessage="";
}else if(lnaname==-3||rnaname==-3){
if(s=="後攻")
message="You win!!";
else if(s=="先攻")
message="You loose!!";
tmessage="";
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(board[i][j]!=0&&message=="")
a++;
if(a==9){
message="Draw";
tmessage="";
}
}
}
}//勝利判定のメソッド終了
//グラフィックのアップデートメソッド
public void update(Graphics g){
paint(g);
}//アップデートメソッド終了
//グラフィックメソッド
public void paint(Graphics g){
g.setColor(Color.black);
for(int i=0;i<=180;i+=60){
g.drawLine(i,0,i,180);
g.drawLine(0,i,180,i);
}
//マスを調べて○か×か置く
for(xpos=0;xpos<3;xpos++){
for(ypos=0;ypos<3;ypos++){
if(board[xpos][ypos]==maru){
g.setColor(Color.black);
g.drawOval(xpos*60+2,ypos*60+2,57,57);
}else if(board[xpos][ypos]==batu){
g.setColor(Color.red);
g.drawOval(xpos*60+2,ypos*60+2,57,57);
}}}
//文字のスタイル(PLAIN,BOLD,ITALICがある)と色とメッセージをどこに表示させるか決める
Font font=g.getFont();
g.setFont(new Font(font.getName(),Font.ITALIC,30));
g.setColor(Color.red);
g.drawString(message,10,80);
g.setFont(new Font(font.getName(),Font.ITALIC,16));
g.setColor(Color.red);
g.drawString(tmessage,200,15*c);
}//グラフィックメソッド終了
}//クラス終了