java tic tac toe help

i'm trying to make a gui tic tac toe with JButton for a homework

Page 1 of 1

2 Replies - 2701 Views - Last Post: 22 May 2009 - 02:46 AM Rate Topic: -----

#1 darthmutum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-May 09

java tic tac toe help

Posted 21 May 2009 - 06:56 PM

import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

// instantiates a new TButton, the TButton constructor initializes the "value" property to 0 (empty)
// new TButton("X");

// an expression checking to seeing if the board position is empty
// if (boardButtons[row][column].getPlayerValue() != 0)

// statement for setting the board position to "1" or "2" (depending on the current player)
// boardButtons[row][column].setPlayerValue(currentPlayer);
// I need to get The gameStatus method working and for the getValue method to work so i can finish tonight

public class TicTacToeFrame extends JFrame
{
	private JButton buttons[][];
	private JButton newGameButton; 
	private JLabel playerLabel;
	private FlowLayout layout;
	private JPanel buttonPanel;
	private String players[] = {"-","X","O"};
	private int boxOccupier;
	private int playerTurn = 1;
	//private JLabel spaceMoved;

	public TicTacToeFrame()
	{
		super("Tic Tac Toe");

		buttons = new JButton[3][3];
		newGameButton = new JButton("Do you want to play agian");
		playerLabel = new JLabel("Player X's turn");
		//spaceMoved = new JLabel("");
		layout = new FlowLayout();
		layout.setAlignment(FlowLayout.CENTER);
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(3,1));
		setLayout(layout);
		new TButton("X");
		new TButton("O");
		new TButton("-");

		//do
		//{

			for(int row = 0;row < buttons.length;row++)
			{
				for(int column = 0; column < buttons[row].length;column++)
				{
					//currentPlayer.add(new TButton(currentPlayer));
					buttons[row][column] = new JButton("-");
					buttonPanel.add(buttons[row][column]);
					//buttons[row][column].setValue(0);
					buttons[row][column].addActionListener(new ButtonFieldHandler(row,column));
				}
			}

		//}while((gameStatus != "Win")||(gameStatus != "Tie"))

		add(buttonPanel);
		add(newGameButton);
		add(playerLabel);
		//add(spaceMoved);
	}

	private class ButtonFieldHandler implements ActionListener
	{
		private int rowSelected;
		private int columnSelected;

		public ButtonFieldHandler(int r,int c)
		{
			rowSelected = r;
			columnSelected = c;
		}

		public void actionPerformed(ActionEvent event)
		{
			buttons[rowSelected][columnSelected].setText(players[playerTurn]);
			nextPlayer(playerTurn);

//			playerTurn++;
//			if (playerTurn >= 2)
	//			playerTurn = 1;
	//		spaceMoved.setText("row " +(rowSelected + 1)+" column "+(columnSelected + 1));
		}

		/*public int TurnFinder(int turn)
		{
			playerTurn++;
			if (playerTurn >= 2)
				playerTurn = 1;

			return turn;
		}*/

	}

	public int nextPlayer(int currentPlayer)
	{
		if (currentPlayer == 1 )
			currentPlayer =2;
		else
			currentPlayer =1;

		return currentPlayer;
	}

	public String gameStatus(JButton buttonselected)
	{
		String gameStatus = "in progress";

		//if (buttonselected.getValue == )

		return gameStatus;
	}

	private class TButton extends JButton
    {
        private int value;

        public TButton(String str)
        {
            this.setText(str);
            value = 0;
        }

        public int getValue()
        {
            return value;
        }

        public void setValue(int val)
        {
            // 0 - Empty
            // 1 - Player 1's piece
            // 2 - Player 2's piece

            if ((val >=0) && (val <= 2))
            {
                value = val;
                this.setText(players[val]);
            }
        }
    }
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 21 May 2009 - 06:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java tic tac toe help

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: java tic tac toe help

Posted 21 May 2009 - 06:59 PM

And you need help with what exactly? Please state a problem question that tells us what you need help with in this program.

Thanks. :)
Was This Post Helpful? 0
  • +
  • -

#3 darthmutum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-May 09

Re: java tic tac toe help

Posted 22 May 2009 - 02:46 AM

View PostMartyr2, on 21 May, 2009 - 05:59 PM, said:

And you need help with what exactly? Please state a problem question that tells us what you need help with in this program.

Thanks. :)

well what i need is getting the Buttons to use the getValue and setvalue correctly making the JButtons text change I have it show player x when i press it but it doesn't show player O ever
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1