nice ang netbeans nagpractice pa ko.
asa ta ka download ug API?
Unsa nga version? 1.5?
J2SE 5.0 API Download <-- edit nako ang title. :mrgreen:
API? or API Documentation? if the latter, the post above this one is the anwer...if the former, its already included with the jdk you are using..the standard api, at least..Originally Posted by apart
guys, naa lang jud unta koy i konsulta, regarding ani akong program!!
koan ni siya, tictactoe game ni nga program! but, ang akong problema kay dli mag cge nalng ug balik ang x turn ani!!
can u help me solve this problem!?
morag sa kanang gi bold nako nga code man ang sayop! galibog jud ko unsaon nako pag compare between x and y nga turns..!!
tanx...!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Jerus extends JFrame implements MouseListener,ActionListener{
public static final int height = 250;
public static final int width = 250;
int numClicks;
boolean isDone = false;
boolean xTurn = true;
JLabel status = new JLabel("Welcome to my Game.!");
JLabel[][] grid = new JLabel[3][3];
char[][] game = new char[3][3];
JButton restart = new JButton("restart");
public Jerus(){
JPanel top;
JPanel bottom;
JPanel center;
setSize(width,height);
setTitle("Jerus' Project");
addWindowListener(new WindowTerminator());
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
top = new JPanel();
top.setLayout(new FlowLayout());
top.add(status);
center = new JPanel();
center.setLayout(new GridLayout(3,3,3,3));
center.setBackground(Color.BLACK);
for(int row=0; row<3; row++){
for(int col=0; col<3; col++){
grid[row][col] = new JLabel(" ",JLabel.CENTER);
grid[row][col].addMouseListener(this);
grid[row][col].setBackground(Color.WHITE);
center.add(grid[row][col]);
game[row][col] = ' ';
}
}
bottom = new JPanel();
bottom.setLayout(new FlowLayout());
bottom.add(restart);
contentPane.add(top,BorderLayout.NORTH);
contentPane.add(center,BorderLayout.CENTER);
contentPane.add(bottom,BorderLayout.SOUTH);
}
public void mouseClicked(MouseEvent e){
if(isDone){
resetGame();
}
JLabel clicked = (JLabel)e.getSource();
next:
for(int i=0; i<3; i++){
for(int j=0; j<3; j++ ){
if(clicked == grid[i][j]){
if(clicked != grid[i][j]){
status.setText("Invalid Move");
break next;
}
if((clicked == grid[i][j]) && (xTurn==false)){
clicked.setText("x");
clicked.setForeground(Color.RED);
game[i][j] = 'x';
}
else{
clicked.setText("o");
clicked.setForeground(Color.BLUE);
game[i][j] = 'o';
}
xTurn = true;
numClicks++;
gameOver();
}// end of if.
}//end of inner loop
}//end of outer loop
}//end of mouseClicked
public void resetGame(){
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
grid[i][j] = new JLabel(" ");
game[i][j] = ' ';
}
}
numClicks = 0;
xTurn = true;
}
public void gameOver(){
char winner = ' ';
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2])){
winner = game[0][0];}
else if((game[2][0] == game[1][1]) && (game[2][0] == game[0][2])){
winner = game[2][0];}
else{
for(int i=0; i<3; i++){
if((game[i][0] != ' ') && (game[i][0] == game[i][1]) && game[i][1] == game[i][2]){
winner = game[i][0];
}
else if((game[0][i] != ' ') && (game[0][i] == game[1][i]) && (game[1][i] == game[2][i])){
winner = game[0][1];
}
}
}
isDone = true;
if(winner == ' ' && numClicks == 9){
status.setText("Tie Game!!");
}
else if(winner != ' '){
status.setText("Game Over "+ winner +" Won!!");
}
else
status.setText((xTurn ? "X's Turn" : "O's Turn"));
isDone = false;
}
public void actionPerformed(ActionEvent ex){
resetGame();
}
public static void main(String[] args){
Jerus r = new Jerus();
r.setVisible(true);
}
public void mouseEntered(MouseEvent e){ }
public void mousePressed(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
public void mouseMoved(MouseEvent e){ }
public void mouseDragged(MouseEvent e){ }
}
your if(clicked != grid[j]) statement is not necessary. it wont pass through that because click == grid[j] always.if(clicked == grid[j]){
if(clicked != grid[j]){
status.setText("Invalid Move");
break next;
}
if((clicked == grid[j]) && (xTurn==false)){
clicked.setText("x");
clicked.setForeground(Color.RED);
game[j] = 'x';
}
else{
clicked.setText("o");
clicked.setForeground(Color.BLUE);
game[j] = 'o';
}
xTurn = true;
numClicks++;
gameOver();
}// end of if.
what's the use of the variable i?
@jerx bay gamiti daw ug code tags imong code.. i dont think mo compile sad ni ug tarong nga code kay you are comparing a JLabel to an array of JLabels.. if(clicked != grid[j]).. anyways, try daw gamit ug code tags..
@jerx: TIC TAC TOE ba ni nga game? Dili man cguro kailangan ang if(clicked == grid[j]).
If mouse_clicked gani ang label then assign dayon og value sa label 'X' ba or 'O'. Then check dayon ang matrix if naay na ba'y naka-form og straight 'X' or 'O'. If naay na'y naka-form report dayon kung kinsa ang player nga winner. Maka-ila ra man ka kung kinsa ang ni-daog ka'y each symbol 'X' or 'O' is assigned to each player man.
BTW parehas mo og pattern sa code ni sweetz (usa nga mga friend nako nga istoryan diri).
dili sad jud pwede ang clicked == grid[j] bay kay mag error na kay nag compare man na ug array nga object ug dili array nga object..incomparable data types..
Ang i og j ka'y indices sa iyang matrix which is grid.Originally Posted by boxingfan529
Similar Threads |
|