Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java:Tutorial - Tic-Tac-Toe

tic-tac-toe

  • Please log in to reply
60 replies to this topic

#25 chrontiger

chrontiger

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 February 2008 - 05:31 PM

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...
  • 0

#26 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 27 February 2008 - 04:41 PM

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.
  • 0

#27 hasvn

hasvn

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 11 March 2010 - 07:19 AM

John, can you help me, when i use this 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


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;
			}

  • 0

#28 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 11 March 2010 - 03:46 PM

For the AI
1. You would need to edit the winCombinations to account for a 4x4 grid
private int[][] winCombinations = new int[][] {
			{0, 1, 2, 3}, {4, 5, 6, 7}...

2. Check for a win condition:
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.
  • 0

#29 hasvn

hasvn

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 12 March 2010 - 06:19 PM

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



        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);
    }

  • 0

#30 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 12 March 2010 - 09:53 PM

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

#31 ToMegaTherion

ToMegaTherion

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 09 April 2010 - 08:09 PM

I did a Tic Tac Toe in a tutorial eariler this year, it is quite a good program to practice various skills and I found it a useful way of learning how to use arrays and simple windows. thanks for the tut guys.
  • 0

#32 Mohammed

Mohammed

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 May 2010 - 05:04 AM

hey, I'm a beginner in java and i want to know what to do with the code you posted to make the game work ( probably a dumb question :/ ). Thanks in advance.
  • 0

#33 Whitescar

Whitescar

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 28 May 2010 - 04:05 AM

Copy and Paste it into a compiler. If you haven't figured that out. Then you really haven't started coding yet.
  • 0

#34 GMVResources

GMVResources

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 71 posts

Posted 13 June 2010 - 06:06 PM

The only thing with your program is that I think you should explain your package: mytictactoe; because it will put up an error on everyone else's IDE's
  • 0

#35 GMVResources

GMVResources

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 71 posts

Posted 13 June 2010 - 06:07 PM

Mohammed I recommend for you to download the latest Java and also a compiler and interpreter which is what i use: NetBeans though I heard Eclipse is better.
  • 0

#36 nikoscrfrt

nikoscrfrt

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 June 2010 - 04:41 AM

Hello guys.
I am quite new in java programming and I would appreciate some help in a problem I have.
I am currently in an artificial intelligence lesson and I have o project of making a tic tac toe game in xj3d. However I have tried a lot lately I could not manage great progress.
So I would need some help if that's not a big problem for you.
Thanks in advance.
  • 0





Also tagged with one or more of these keywords: tic-tac-toe