dポイントプレゼントキャンペーン実施中!

こんにちは。プログラミング言語初心者です。

三目並べをJavaで作りたいのですが、サンプルコードなどを見せていただければ幸いです。

GridLayoutを使うのはわかっているのですが、そこから進まない・・・涙

A 回答 (2件)

 以下は、思考ルーチンを除いた三目並べの例です。



import java.awt.*;
import java.awt.event.*;

public class sanmoku extends Frame implements ActionListener {

private Button B[];
private Label ML;
private boolean F;

public sanmoku() { super("Sanmoku Narabe");
int i;
setSize(200, 200);
Panel P = new Panel(new GridLayout(10, 10));
add(P, BorderLayout.CENTER);
for (i = 0, B = new Button[100]; i < 100; i ++) {
B[i] = new Button();
B[i].addActionListener(this);
B[i].setActionCommand(Integer.toString(i));
P.add(B[i]);
};
ML = new Label();
add(ML, BorderLayout.SOUTH);
F = true;
};

synchronized boolean SetF(boolean nf) {
boolean of = F;
F = nf;
return(of);
};

public void actionPerformed(ActionEvent ae) {

int n, i;

if (!SetF(false)) return;

try {
n = Integer.parseInt(ae.getActionCommand());
if ((n>=0) && (n<100)) if (B[n].getLabel().length()==0) {
B[n].setLabel("o");
if (WinCheck(n, "o")) {
ML.setText("You Win");
return;
}
else {
n=(int)(Math.random()*100.0);
for (i=0;i<100;i++,n++) if (B[++n>=100?n=0:n].getLabel().length()==0) break;
if (i==100) {
ML.setText("Even");
return;
};
B[n].setLabel("x");
if (WinCheck(n, "x")) {
ML.setText("I Win");
return;
};
};
};
} catch (Exception e) {};

SetF(true);

};

private static final int CI[][] = {{-2,-1,1,2},{-40,-20,20,40},{-42,-21,21,42},{-38,-19,19,38}};

private boolean WinCheck(int n, String s) {

int i, j, k, l;

for (l=0;l<4;l++) for (k=0;k<3;k++) {
for (i=k;i<k+2;i++) {
j=n/10*20+n%10+CI[l][i];
if ((j<0) || (j>=200) || (((j/10)%2) != 0)) break;
if (s.compareTo(B[j/20*10+j%10].getLabel()) != 0) break;
};
if (i==(k+2)) return(true);
};

return(false);

};

public static void main(String args[]) {

sanmoku S = new sanmoku();
S.setVisible(true);

};

}
    • good
    • 0

TicTacToeは、sunの開発キット(sdk)中に


「アプレット・デモ」
として付属してますよ。

sunのサイトでも公開されてます。
http://java.sun.com/j2se/1.4/ja/docs/ja/relnotes …

参考URL:http://java.sun.com/j2se/1.4/ja/docs/ja/relnotes …
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!