+ Reply to Thread
Page 3 of 5 FirstFirst 12345 LastLast
Results 21 to 30 of 48

Thread: Java:Tutorial - Tic-Tac-Toe

  1. #21
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31
    P.S For future refrence, he thanks you for doing his assignment

    Nice code Sidewinder *pulls out a dusty javadoc refrence and begins to read - yes, no swing just AWT...say something about age?*

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #22
    phenoma is offline Newbie
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0
    sorry about that...

  4. #23
    daremeister is offline Newbie
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Question Help please

    Sorry I know this thread is over a year old, but however it has been a big help for me while writing my own java program. I have read through all of the posts, and tried to implement your ideas. However, my instructor requires that our program use two files to get the job done, not just one. Any ideas or pointers in the right direction as to how I could accomplish this? Any help would be appreciated. I included the two files I have tried to do already. Whenever I run it, I recieve no build messages but get some weird exception, (Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException) and am not sure how to fix this. Thanks.
    Attached Files Attached Files

  5. #24
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    A NullPointerException means you have tried to instantiate an object that hasn't been given a value.

  6. #25
    chrontiger is offline Newbie
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Smile need help in my tictactoe

    hello...i have a program tictactoe in which a host and a client can play but i got this problem that i wanted to create a server for the two that controls the game. i tried to create one but the other client does not connect to both server and host can you help me out plssss.... i really need it...

  7. #26
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    I've wanted to implement RMI into this tutorial for a while, but I just don't know how to. So I cant help you there - maybe someone else can.

  8. #27
    hasvn is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Java:Tutorial - Tic-Tac-Toe

    John, can you help me, when i use this code

    Code:
    package general;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TicTacToeV2 implements ActionListener {
    	/*Instance Variables*/
    	private int[][] winCombinations = new int[][] {
    			{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
    			{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
    			{0, 4, 8}, {2, 4, 6}			 //diagonal wins
    		};
    	private JFrame window = new JFrame("Tic-Tac-Toe");
    	private JButton buttons[] = new JButton[9];
    	private int count, xWins, oWins = 0;
    	private String letter = "";
    	private boolean win = false;
    
    	public TicTacToeV2(){
    	/*Create Window*/
    	window.setSize(300,300);
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	window.setLayout(new GridLayout(3,3));
    	
    	/*Add Buttons To The Window*/
    	for(int i=0; i<=8; i++){
    		buttons[i] = new JButton();
    		window.add(buttons[i]);
    		buttons[i].addActionListener(this);
    	}
    	
    	/*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) {
    		count++;
    		
    		/*Calculate whose turn it is*/
    		if(count % 2 == 0)
    			letter = "O";
    		else
    			letter = "X";
    
    		/*Write the letter to the button and deactivate it*/
    		 JButton pressedButton = (JButton)a.getSource(); 
    		 pressedButton.setText(letter);
    		 pressedButton.setEnabled(false);
    		
    		/*Determine who won*/
    		for(int i=0; i<=7; i++){
    			if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) && 
    				buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) && 
    				buttons[winCombinations[i][0]].getText() != ""){
    				win = true;
    			}
    		}
    		
    		/*Show a dialog when game is over*/
    		if(win == true){
    			JOptionPane.showMessageDialog(null, letter + " wins the game!");
    			playAgainDialog();
    		} else if(count == 9 && win == false){
    			JOptionPane.showMessageDialog(null, "The game was tie!");
    			playAgainDialog();
    		}		
    	}
    	
    	public void playAgainDialog() {
    		if(letter.equals("X"))  xWins++;
    		else                    oWins++;
    		
    		int response = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    		
    		if(response == JOptionPane.YES_OPTION)   reset();
    		else                                     System.exit(0);
    		
    	}
    	
    	public void reset() {
    		for(int i=0; i<=8; i++){
    			buttons[i].setText("");
    			buttons[i].setEnabled(true);
    		}
    		win = false;
    		count = 0;
    	}
    	
    	public static void main(String[] args){
    		TicTacToeV2 starter = new TicTacToeV2();
    	}
    }

    and i want to expand moer pole for example [4][4], so i dont know how to edit this code, can you help me ? Thanks


    Code:
    		for(int i=0; i<=7; i++){
    			if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) && 
    				buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) && 
    				buttons[winCombinations[i][0]].getText() != ""){
    				win = true;
    			}

  9. #28
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Java:Tutorial - Tic-Tac-Toe

    For the AI
    1. You would need to edit the winCombinations to account for a 4x4 grid
    Code:
    private int[][] winCombinations = new int[][] {
    			{0, 1, 2, 3}, {4, 5, 6, 7}...
    2. Check for a win condition:
    Code:
    for(int i=0; i<=7; i++){
    	if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) && 
    		buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) && 
     		buttons[winCombinations[i][2]].getText().equals(buttons[winCombinations[i][3]].getText()) && 
    		buttons[winCombinations[i][0]].getText() != ""){
    		win = true;
    }
    You will obviously need to edit the GUI as well.

  10. #29
    hasvn is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Java:Tutorial - Tic-Tac-Toe

    Thanks man , can you help me last stuff ? I want to create MenuBar, when i paste this code to your code, but it opens two windows... can you help me ?

    Thanks you very much



    Code:
            setSize(200,200);
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            JMenu fileMenu = new JMenu("File");
            menuBar.add(fileMenu);
            JMenuItem newAction = new JMenuItem("New");
            JMenuItem openAction = new JMenuItem("Open");
            JMenuItem exitAction = new JMenuItem("Exit");
            fileMenu.add(newAction);
            fileMenu.add(openAction);
            fileMenu.addSeparator();
            fileMenu.add(exitAction);
            newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    Main novy = new Main();
                }
            });
                    openAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    System.out.println("You havddddddde clicked on the new action");
                }
            });
        
        public static void main(String[] args) {
            MujMenu me = new MujMenu();
            me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            me.setVisible(true);
        }

  11. #30
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Java:Tutorial - Tic-Tac-Toe

    I'm not sure. I haven't programmed in Java in two years - try asking in the Java section/forum.

+ Reply to Thread
Page 3 of 5 FirstFirst 12345 LastLast

Thread Information

Users Browsing this Thread

There are currently 10 users browsing this thread. (0 members and 10 guests)

Similar Threads

  1. Tutorial: Java Inner Classes
    By Jordan in forum Java Tutorials
    Replies: 1
    Last Post: 02-13-2009, 10:54 PM
  2. Java:Tutorial - A better looking GUI
    By John in forum Java Tutorials
    Replies: 6
    Last Post: 01-12-2009, 03:31 AM
  3. Tutorial: Java Serialization
    By Jordan in forum Java Tutorials
    Replies: 0
    Last Post: 05-07-2008, 02:34 PM
  4. Tutorial: Java Collections
    By Jordan in forum Java Tutorials
    Replies: 0
    Last Post: 05-05-2008, 04:38 PM
  5. Java UML Tutorial
    By gszauer in forum Java Help
    Replies: 4
    Last Post: 01-30-2008, 08:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts