Jump to content

How to use SelectionListener in our Tic-Tac-Toe game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hi!

We have an assigment of making a game of tic-tac-toe in SWT and have problems using the listener.
We are using an matrix of 3x3 and for-loop for the buttons.

Our code look like this:

 import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;

public class Tictactoe {
	public static void main(String[] args) {
		
		
		int str = 9;
		int size = str/3;
		String toggle = "";
		int [][] matris = new int[str][str];
		Button [][] knappmatris = new Button [size][size];

		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Tic-Tac-Toe!");
		shell.setSize(500, 500);
		shell.setLayout(new GridLayout(3, true));

		// GridData gridData = new GridData(GridData.FILL_BOTH);

		for (int i=0; i<3; i++)
			for (int k=0; k<3; k++)
			{
				GridData gridData = new GridData();
				gridData.horizontalAlignment = GridData.FILL;
				gridData.verticalAlignment = GridData.FILL;
				gridData.grabExcessHorizontalSpace = true;
				gridData.grabExcessVerticalSpace = true;
				knappmatris[i][k] = new Button(shell, SWT.TOGGLE);
				knappmatris[i][k].setLayoutData(gridData);
				knappmatris[i][k].setText(toggle);

				knappmatris[i][k].addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent selectionEvent)
					{
						SOMETHING = ((Button)(selectionEvent.widget)).getText();
					}
				});

			}


		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
}

Somehow, but were finding it difficult to know what to replace SOMETHING with. Also, we need to put an X or O every other time you click on one of the buttons which are related. We tried to use toggle, knappmatris[i][k] but it just says that we cannot refer to a non-final variable inside a inner class defined in another method. but we dont want it do be final since the 1st it will show X, next time O and so on (with if()...)

/Manne & Jonas

Edited by Darkone85, 21 April 2010 - 07:32 AM.


#2
Corwin

Corwin

    Newbie

  • Members
  • PipPip
  • 12 posts
I am not sure why you need to getText on the button that was clicked.

Here's what I think needs to be done. Have a static variable which tells you whose turn is it and use that to determine whether to write an X or an O in the clicked button. You also need to disable the button after it's clicked once so that what's written on it cannot be changed. Here's my code:


knappmatris[i][k].addSelectionListener(new SelectionAdapter() {

	public void widgetSelected(SelectionEvent selectionEvent)

	{

		Button b = ((Button)(selectionEvent.widget));

		if (myTurn)

			b.setText("X");

		else

			b.setText("O");

		myTurn = !myTurn;

		b.setEnabled(false);

	}

});


The rest of the code is the same with the exception of adding a declaration for myTurn in the class:


static boolean myTurn = true;