1 Replies - 8841 Views - Last Post: 04 October 2011 - 08:08 AM Rate Topic: -----

#1 judeabao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-October 11

Can you help me determining a winner in my Tic Tac Toe program ?

Posted 04 October 2011 - 03:37 AM

Here's my program :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author Jayson Jude
 */
public class JavaApplication11 implements ActionListener {
private JFrame window = new JFrame("Tic-Tac-Toe");
	private JPanel inputPanel = new JPanel();
	private JPanel boardPanel = new JPanel();
	private JButton button3 = new JButton("3 by 3");
	private JButton button4 = new JButton("4 by 4");
	private JButton button5 = new JButton("5 by 5");
	private JLabel label = new JLabel ("Click a button to choose game board size:");
	private JButton buttons[][];
	private int boardSize=0;
	private int count = -1;
	private String letter = "";
	private boolean win = false;

	public JavaApplication11(){
	//Create Window
	window.setSize(500,500);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	
	//Add Label and buttons to inputPanel
	inputPanel.add(label);
	inputPanel.add(button3);
	inputPanel.add(button4);
	inputPanel.add(button5);
	button3.addActionListener(this);
	button4.addActionListener(this);
	button5.addActionListener(this);
	inputPanel.setSize(500,200);
	window.add(inputPanel,"North");
	
	
	
	//Make The Window Visible
	window.setVisible(true);
	}
	
	/**
	 When an object is clicked, perform an action.
	 @param a action event object
	 */
	public void actionPerformed(ActionEvent a) {
		if(count==-1){
			letter="-";
			count=0;
		}
		else{
		count++;
		
		//Calculate whose turn it is
		
		if(count % 2 == 0){
			letter = "O";
		} else {
			letter = "X";
		}
		}
		
		if (a.getSource() == button3) {
			boardSize=3;
		}
		else if (a.getSource()== button4){
			boardSize=4;
		}
		else if (a.getSource() == button5){
			boardSize=5;
		}
		if ((a.getSource() == button3)||(a.getSource()==button4)||(a.getSource()==button5)){
			button3.setEnabled(false);
			button4.setEnabled(false);
			button5.setEnabled(false);
			buttons = new JButton[boardSize][boardSize];
			for(int i=0; i<boardSize; i++)
				for (int j=0; j<boardSize; j++){
					buttons[i][j] = new JButton();
					boardPanel.add(buttons[i][j]);
					buttons[i][j].addActionListener(this);
				}
			
			boardPanel.setSize(300,300);
			boardPanel.setLayout(new GridLayout(boardSize,boardSize));
			window.add(boardPanel,"Center");
		}
		
		//Write the letter to the button and deactivate it
		 JButton pressedButton = (JButton)a.getSource(); 
		 pressedButton.setText(letter);
		 pressedButton.setEnabled(false);
		
		//Determine winner
		if (a.getSource()==button3)
                {
                   if (buttons[0]==buttons[1]&&buttons[1]==buttons[2])
                   {
                       win=true;
                   }
                }
                
                
                 
                 
                 
		//Show a dialog when game is over
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " wins the game!");
			JOptionPane.showInputDialog("Do you want to play again? Y/N");
                        char choice = 0;
                        if (choice=='Y'||choice=='y')
                        {
                            new JavaApplication11();
                        }
                        else if (choice=='N'||choice=='n')
                        {
                            JOptionPane.showMessageDialog(null,"GOODBYE!");
                            System.exit(0);
                        }
                            
                                
		} else if(count == (boardSize*boardSize) && win == false){
			JOptionPane.showMessageDialog(null, "The game was tie!");
			
                            
		}	
		
                            
	}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new JavaApplication11();
    }
}






here's my solution but it didnt work ..

if (a.getSource()==button3)
                {
                   if (buttons[0]==buttons[1]&&buttons[1]==buttons[2])
                   {
                       win=true;
                   }
                }




Can you help me please ?I dont know what to do for this ?

Is This A Good Question/Topic? 0
  • +

Replies To: Can you help me determining a winner in my Tic Tac Toe program ?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Can you help me determining a winner in my Tic Tac Toe program ?

Posted 04 October 2011 - 08:08 AM

I already told you this is NOT a GridWorld question. Please STOP opening multiple threads and in the wrong forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1