Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree1Likes

Thread: Problem understanding basic Simple Enumerators

  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Problem understanding basic Simple Enumerators

    Hi,
    I'm writing a Tic Tac Toe application - I've made an enum for the types of game the user can play. In the constructor - i've written
    Java Code:
    playMode = Single
    to set single as the default play mode. However there is a problem with the syntax.

    The code is as follows:
    Java Code:
    public class TicTacToe {
    	private enum GameType {Single,Double,Server}
    	private enum Move {X,O,Empty};
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private Move[] cellArray;
    	private JLabel  statusLabel;
    	
    	public void TicTacToe()
    	{
    		playMode = Single;
    		createUI();
    		newGame();
    	}
    I know the syntax is write in terms of C++ but what would be the Java equivalent. Thanks for your help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Go to the tutorial for doc on how to use enums: The Really Big Index
    Find enum on the opening page and read all about them.

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks Again, Norm. I'd read the documentation before - but I realized where I'd gone wrong on the second reading.

    Java Code:
    public class TicTacToe {
    	private enum GameType {Single,Double,Server}
    	private enum Move {X,O,Empty};
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private Move[] cellArray;
    	private JLabel  statusLabel;
    	
    	public void TicTacToe()
    	{
    		playMode = GameType.Single;
    		createUI();
    		newGame();
    	}

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You might want to also follow naming conventions, enums should be all capitals(SINGLE, DOUBLE, SERVER, etc.)

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Ok, this is a rough outline of the program. When i select "Two player game" from the main menu, and click on a button - the label should say "douuble " followed by the number of the cell i've clicked on. Likewise, if i select "Internet game" and click a button, it should say "server cellNumber". Problem is that it alwyas says "Single buttonNumber". Why's that?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    
    
    public class TicTacToe {
    	private enum GameType {SINGLE,DOUBLE,SERVER}
    	private enum State {X,O,EMPTY};
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private State[] cellArray;
    	private JLabel  statusLabel;
    	
    	private int move;
    	
    	public TicTacToe()
    	{
    		createUI();
    		newGame();
    		
    	}
    	
    	public void createUI()
    	{
    		final JFrame frame = new JFrame();
    		frame.setTitle("Tic Tac Toe");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		
    		panel.setLayout(null);
    		
    		JMenuBar menubar = new JMenuBar();
    		
    		JMenu gameMenu = new JMenu("Start");
    		
    		JMenuItem newGame = new JMenuItem("New Game");
    		newGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(newGame);
    		
    		JMenuItem onePlayerGame = new JMenuItem("One Player");
    		onePlayerGame.addActionListener(
    				new ActionListener()
    				{
    
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SINGLE;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(onePlayerGame);
    		
    		JMenuItem twoPlayersGame = new JMenuItem("Two Players");
    		twoPlayersGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.DOUBLE;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(twoPlayersGame);
    		
    		JMenuItem internetGame = new JMenuItem("Internet");
    		twoPlayersGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SERVER;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(internetGame);
    		
    		JMenuItem exit = new JMenuItem("Exit");
    		exit.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						frame.dispose();
    					}
    				}
    		);
    		gameMenu.add(exit);
    		
    		menubar.add(gameMenu);
    		
    		//-------top row ---------------//
    		
    		cell = new JButton[9];
    		cellArray = new State[9];
    		
    		cell[0] = new JButton();
    		cell[0].setBounds(20, 20, 75, 75);
    		cell[0].setActionCommand("0");
    		panel.add(this.cell[0]);
    		
    		cell[1] = new JButton();
    		cell[1].setBounds(100, 20, 75, 75);
    		cell[1].setActionCommand("1");
    		panel.add(this.cell[1]);
    		
    		cell[2] = new JButton();
    		cell[2].setBounds(180, 20, 75, 75);
    		cell[2].setActionCommand("2");
    		panel.add(this.cell[2]);
    		
    		//-------------mid row-----------//
    		
    		cell[3] = new JButton();
    		cell[3].setBounds(20, 100, 75, 75);
    		cell[3].setActionCommand("3");
    		panel.add(this.cell[3]);
    		
    		cell[4] = new JButton();
    		cell[4].setBounds(100, 100, 75, 75);
    		cell[4].setActionCommand("4");
    		panel.add(this.cell[4]);
    		
    		cell[5] = new JButton();
    		cell[5].setBounds(180, 100, 75, 75);
    		cell[5].setActionCommand("5");
    		panel.add(this.cell[5]);
    		
    		//-----------bottom row------------//
    		
    		cell[6] = new JButton();
    		cell[6].setBounds(20, 180, 75, 75);
    		cell[6].setActionCommand("6");
    		panel.add(this.cell[6]);
    		
    		cell[7] = new JButton();
    		cell[7].setBounds(100, 180, 75, 75);
    		cell[7].setActionCommand("7");
    		panel.add(this.cell[7]);
    		
    		cell[8] = new JButton();
    		cell[8].setBounds(180, 180, 75, 75);
    		cell[8].setActionCommand("8");
    		panel.add(this.cell[8]);
    		
    		statusLabel = new JLabel("");
    		statusLabel.setText("Welcome to TicTacToe");
    		statusLabel.setVisible(true);
    		
    		//---------the rest-------------//
    		
    		frame.setJMenuBar(menubar);
    		frame.getContentPane().add(panel);
    		frame.getContentPane().add(BorderLayout.SOUTH, statusLabel);
    		frame.setMinimumSize(new Dimension(290,330));
    		frame.setResizable(false);
    		frame.setVisible(true);
    	}
    
    	public void newGame()
    	{
    		this.move =0;
    		//make all cells blank
    		emptyOutCells();
    		updateUI();
    		
    		//set all cells enabled;
    		for(int index=0;index<cell.length;index++)
    		{
    			cell[index].setEnabled(true);
    		}
    		
    		//set color of all cells
    		for(int index=0;index<cell.length;index++)
    		{
    			if(index%2!=0)
    			{
    				cell[index].setBackground(Color.GRAY);
    			}else
    			{
    				cell[index].setBackground(Color.WHITE);
    			}
    		}
    		
    		//add Action listeners for each cell depending on type of game
    		switch (playMode)
    		{
    			case SINGLE:
    			
    				this.statusLabel.setText("New Single Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    			
    			case DOUBLE:
    			
    				this.statusLabel.setText("New Double Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new DoublePlayerListener());
    				}
    				break;
    			
    			
    			case SERVER:
    			
    				this.statusLabel.setText("New Server Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new ServerPlayerListener());
    				}
    				break;
    			
    			
    			default:
    			
    				this.statusLabel.setText("Default Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    		}
    	}
    	
    	private void emptyOutCells()
    	{
    		for(int index =0; index<cellArray.length;index++)
    		{
    			cellArray[index]=State.EMPTY;
    			System.out.printf("cellArray[%d]=State.EMPTY\n", index);
    		}
    	}
    	
    	private void updateUI()
    	{
    		String state;
    		
    		for(int index =0; index < cell.length;index++)
    		{
    			if(cellArray[index]==State.X)state = "X";
    			else if(cellArray[index]==State.O)state = "O";
    			else state = "";
    			
    			cell[index].setText(state);
    			System.out.printf("cell[%d].setText(%s).\n", index,state);
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		new TicTacToe();
    	}
    	
    	private class SinglePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Single %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    	
    	private class DoublePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Double %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    	
    	private class ServerPlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Server %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    }

  6. #6
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Ok, this is a rough outline of the program. When i select "Two player game" from the main menu, and click on a button - the label should say "douuble " followed by the number of the cell i've clicked on. Likewise, if i select "Internet game" and click a button, it should say "server cellNumber". Problem is that it alwyas says "Single buttonNumber". Why's that?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    
    
    public class TicTacToe {
    	private enum GameType {SINGLE,DOUBLE,SERVER}
    	private enum State {X,O,EMPTY};
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private State[] cellArray;
    	private JLabel  statusLabel;
    	
    	private int move;
    	
    	public TicTacToe()
    	{
    		createUI();
    		newGame();
    		
    	}
    	
    	public void createUI()
    	{
    		final JFrame frame = new JFrame();
    		frame.setTitle("Tic Tac Toe");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		
    		panel.setLayout(null);
    		
    		JMenuBar menubar = new JMenuBar();
    		
    		JMenu gameMenu = new JMenu("Start");
    		
    		JMenuItem newGame = new JMenuItem("New Game");
    		newGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(newGame);
    		
    		JMenuItem onePlayerGame = new JMenuItem("One Player");
    		onePlayerGame.addActionListener(
    				new ActionListener()
    				{
    
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SINGLE;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(onePlayerGame);
    		
    		JMenuItem twoPlayersGame = new JMenuItem("Two Players");
    		twoPlayersGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.DOUBLE;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(twoPlayersGame);
    		
    		JMenuItem internetGame = new JMenuItem("Internet");
    		twoPlayersGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SERVER;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(internetGame);
    		
    		JMenuItem exit = new JMenuItem("Exit");
    		exit.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						frame.dispose();
    					}
    				}
    		);
    		gameMenu.add(exit);
    		
    		menubar.add(gameMenu);
    		
    		//-------top row ---------------//
    		
    		cell = new JButton[9];
    		cellArray = new State[9];
    		
    		cell[0] = new JButton();
    		cell[0].setBounds(20, 20, 75, 75);
    		cell[0].setActionCommand("0");
    		panel.add(this.cell[0]);
    		
    		cell[1] = new JButton();
    		cell[1].setBounds(100, 20, 75, 75);
    		cell[1].setActionCommand("1");
    		panel.add(this.cell[1]);
    		
    		cell[2] = new JButton();
    		cell[2].setBounds(180, 20, 75, 75);
    		cell[2].setActionCommand("2");
    		panel.add(this.cell[2]);
    		
    		//-------------mid row-----------//
    		
    		cell[3] = new JButton();
    		cell[3].setBounds(20, 100, 75, 75);
    		cell[3].setActionCommand("3");
    		panel.add(this.cell[3]);
    		
    		cell[4] = new JButton();
    		cell[4].setBounds(100, 100, 75, 75);
    		cell[4].setActionCommand("4");
    		panel.add(this.cell[4]);
    		
    		cell[5] = new JButton();
    		cell[5].setBounds(180, 100, 75, 75);
    		cell[5].setActionCommand("5");
    		panel.add(this.cell[5]);
    		
    		//-----------bottom row------------//
    		
    		cell[6] = new JButton();
    		cell[6].setBounds(20, 180, 75, 75);
    		cell[6].setActionCommand("6");
    		panel.add(this.cell[6]);
    		
    		cell[7] = new JButton();
    		cell[7].setBounds(100, 180, 75, 75);
    		cell[7].setActionCommand("7");
    		panel.add(this.cell[7]);
    		
    		cell[8] = new JButton();
    		cell[8].setBounds(180, 180, 75, 75);
    		cell[8].setActionCommand("8");
    		panel.add(this.cell[8]);
    		
    		statusLabel = new JLabel("");
    		statusLabel.setText("Welcome to TicTacToe");
    		statusLabel.setVisible(true);
    		
    		//---------the rest-------------//
    		
    		frame.setJMenuBar(menubar);
    		frame.getContentPane().add(panel);
    		frame.getContentPane().add(BorderLayout.SOUTH, statusLabel);
    		frame.setMinimumSize(new Dimension(290,330));
    		frame.setResizable(false);
    		frame.setVisible(true);
    	}
    
    	public void newGame()
    	{
    		this.move =0;
    		//make all cells blank
    		emptyOutCells();
    		updateUI();
    		
    		//set all cells enabled;
    		for(int index=0;index<cell.length;index++)
    		{
    			cell[index].setEnabled(true);
    		}
    		
    		//set color of all cells
    		for(int index=0;index<cell.length;index++)
    		{
    			if(index%2!=0)
    			{
    				cell[index].setBackground(Color.GRAY);
    			}else
    			{
    				cell[index].setBackground(Color.WHITE);
    			}
    		}
    		
    		//add Action listeners for each cell depending on type of game
    		switch (playMode)
    		{
    			case SINGLE:
    			
    				this.statusLabel.setText("New Single Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    			
    			case DOUBLE:
    			
    				this.statusLabel.setText("New Double Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new DoublePlayerListener());
    				}
    				break;
    			
    			
    			case SERVER:
    			
    				this.statusLabel.setText("New Server Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new ServerPlayerListener());
    				}
    				break;
    			
    			
    			default:
    			
    				this.statusLabel.setText("Default Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    		}
    	}
    	
    	private void emptyOutCells()
    	{
    		for(int index =0; index<cellArray.length;index++)
    		{
    			cellArray[index]=State.EMPTY;
    			System.out.printf("cellArray[%d]=State.EMPTY\n", index);
    		}
    	}
    	
    	private void updateUI()
    	{
    		String state;
    		
    		for(int index =0; index < cell.length;index++)
    		{
    			if(cellArray[index]==State.X)state = "X";
    			else if(cellArray[index]==State.O)state = "O";
    			else state = "";
    			
    			cell[index].setText(state);
    			System.out.printf("cell[%d].setText(%s).\n", index,state);
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		new TicTacToe();
    	}
    	
    	private class SinglePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Single %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    	
    	private class DoublePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Double %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    	
    	private class ServerPlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Server %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    I get an execution error because playMode is null in the switch statement about line 216

  8. #8
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    I didn't get that and i'm using the same code on eclipse.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    I added this in newGame:
    System.out.println("newGame pM=" + playMode);

    newGame pM=null
    Exception in thread "main" java.lang.NullPointerException
    at TicTacToe.newGame(TicTacToe.java:214)
    at TicTacToe.<init>(TicTacToe.java:37)
    at TicTacToe.main(TicTacToe.java:285)
    Where is playMode set before the call to newGame?
    Last edited by Norm; 06-14-2011 at 08:52 PM. Reason: Wrong reason

  10. #10
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Oh wait wait wait wait I forgot. I was going to move playMode = GameType.Single from newGame() to the constructor and that line is missing from the constructor here.
    The constructor looks like this:

    Java Code:
    public TicTacToe()
    	{
    		playMode = GameType.SINGLE;
    		createUI();
    		newGame();
    		
    	}
    Sorry about that.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    I think I've found it. I'll tell you what I did and you can try it to see if you can see what the problem is.
    Add a println to all the the places that playMode's value is changed. Execute the code and look at the output.

    Be sure to id all print statements so you can track them back to the source:

    System.out.println("2createUI pM=" + playMode);

  12. #12
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    yay - for once i can say that i'm already on it.

  13. #13
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    There is some really strange behaviour.

    Here's my output

    Function: TicTacToe, playMode: SINGLE
    New Game. Move: 0
    Function: newGame(), playMode: SINGLE

    //I press Two Player Game from menu and for some reason, playMode is set to Server.
    //This doesn't make sense. I clearly set the actionListener for Two player game to playMode = GameType.Double!
    //Where does the server come from!

    Function: CreateUI, playMode: SERVER
    New Game. Move: 0
    Function: newGame, playMode: SERVER


    Function: CreateUI, playMode: DOUBLE
    New Game. Move: 0
    Function: newGame, playMode: DOUBLE
    I'm tracing the code at the moment to see where Server comes in.

  14. #14
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Found the error:

    Java Code:
    JMenuItem internetGame = new JMenuItem("Internet");
    		[B]internetGame[/B].addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SERVER;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(internetGame);

    Thanks Again!

  15. #15
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Found the error:

    Java Code:
    JMenuItem internetGame = new JMenuItem("Internet");
    		[B]internetGame[/B].addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SERVER;
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(internetGame);

    Thanks Again!

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Not often that you have two listeners to one component. The debug output shows them both being called. That would ring a bell.
    eLancaster likes this.

  17. #17
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    OK, I'm totally flummoxed by the abnormal behaviour of the code.
    What happens is this

    -if i launch the game, it starts a single player game by default - which works perfect
    -if i launch the game, and select "single player game" from the menu - after i make the first move, it makes three moves on its own.
    -if I launch the game, and select "double player game" from the menu - it follows the code of singlePlayerListener.
    -"Internet game" has not been implemented yet but when it is selected, it runs a perfect single player game.
    The code is below. I would really appreciate any tips that would have prevented this programming from being so gosh-darned buggy!!!!

    Thanks.

    TicTacToe.java
    Java Code:
    //sorry to make you read so much.
    public class TicTacToe {
    	private enum GameType {SINGLE,DOUBLE,SERVER}
    	private enum State {X,O,EMPTY};
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private State[] cellArray;
    	private JLabel  statusLabel;
    	
    	private int move;
    	
    	public TicTacToe()
    	{
    		playMode = GameType.SINGLE;
    		createUI();
    		newGame();	
    	}
    	
    	public void createUI()
    	{
    		final JFrame frame = new JFrame();
    		frame.setTitle("Tic Tac Toe");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel panel = new JPanel();
    		
    		panel.setLayout(null);
    		
    		JMenuBar menubar = new JMenuBar();
    		
    		JMenu gameMenu = new JMenu("Start");
    		
    		JMenuItem newGame = new JMenuItem("New Game");
    		newGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(newGame);
    		
    		JMenuItem onePlayerGame = new JMenuItem("One Player");
    		onePlayerGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SINGLE;
    						System.out.printf("Log: Playmode set to %s\n", playMode);
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(onePlayerGame);
    		
    		JMenuItem twoPlayersGame = new JMenuItem("Two Players");
    		twoPlayersGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.DOUBLE;
    						System.out.printf("Log: Playmode set to %s\n", playMode);
    						newGame();
    					}
    				}
    		);
    		
    		gameMenu.add(twoPlayersGame);
    		
    		JMenuItem internetGame = new JMenuItem("Internet");
    		internetGame.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						playMode = GameType.SERVER;
    						System.out.printf("Log: Playmode set to %s\n", playMode);
    						newGame();
    					}
    				}
    		);
    		gameMenu.add(internetGame);
    		
    		JMenuItem exit = new JMenuItem("Exit");
    		exit.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						frame.dispose();
    					}
    				}
    		);
    		gameMenu.add(exit);
    		
    		menubar.add(gameMenu);
    		
    		//I've left out UI set-up relevant code to save space, but they're mentioned in the previous code-quote in this thread.
    	}
    
    	public void newGame()
    	{
    		this.move=1;
    		System.out.printf("New Game. Move: %d. Game Type: ",this.move);
    		
    		//make all cells blank
    		emptyOutCells();
    		updateUI();
    		
    		//set all cells enabled;
    		for(int index=0;index<cell.length;index++)
    		{
    			cell[index].setEnabled(true);
    		}
    		
    		//set color of all cells
    		for(int index=0;index<cell.length;index++)
    		{
    			if(index%2!=0)
    			{
    				cell[index].setBackground(Color.GRAY);
    			}else
    			{
    				cell[index].setBackground(Color.WHITE);
    			}
    		}
    		
    		//add Action listeners for each cell depending on type of game
    		switch (playMode)
    		{
    			case SINGLE:
    				System.out.printf("%s\n", playMode);
    				this.statusLabel.setText("New Single Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    			
    			case DOUBLE:
    				System.out.printf("%s\n", playMode);
    				this.statusLabel.setText("New Double Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new DoublePlayerListener());
    				}
    				break;
    			
    			
    			case SERVER:
    				System.out.printf("%s\n", playMode);
    				this.statusLabel.setText("New Server Player Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new ServerPlayerListener());
    				}
    				break;
    			
    			
    			default:
    				System.out.printf("%s (by Default)\n", playMode);
    				this.statusLabel.setText("Default Game");
    				for(int index=0;index<cell.length;index++)
    				{
    					cell[index].addActionListener(new SinglePlayerListener());
    				}
    				break;
    			
    		}
    	}
    	
    	private void emptyOutCells()
    	{
    		for(int index =0; index<cellArray.length;index++)
    		{
    			cellArray[index]=State.EMPTY;
    		}
    	}
    	
    	private void updateUI()
    	{
    		for(int index =0; index < cell.length;index++)
    		{
    			if(cellArray[index]==State.X)		cell[index].setText("X");
    			else if(cellArray[index]==State.O)	cell[index].setText("O");
    			else 								cell[index].setText("");
    		}
    	}
    	
    	private void disableUI()
    	{
    		for(int index=0;index < cell.length; index++)
    		{
    			cell[index].setEnabled(false);
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		new TicTacToe();
    	}
    	
    	private boolean isXTurn()
    	{
    		if(move%2 != 0)
    		{
    			this.statusLabel.setText(String.format("X's Turn.\tMove: %d\n", this.move));
    			return true;
    		}else
    		{
    			this.statusLabel.setText(String.format("O's Turn.\tMove: %d\n", this.move));
    			return false;
    		}
    	}
    	
    	private boolean checkForWinner()
    	{
    		int count=0;
    		for(int index=0; index<cellArray.length;index++)
    		{
    			if(cellArray[index]==State.EMPTY)count++;
    		}
    		if(count==0)
    		{
    			System.out.println("Log: No Empty Cells. GAME OVER");
    			return true;
    		}
    		
    		if(this.move>9)
    		{
    			System.out.printf("Move %d: GAME OVER\n",move);
    			return true;
    		}else return false;
    			
    	}
    	
    	private void gameOver()
    	{
    		disableUI();
    		this.statusLabel.setText("GAME OVER!");
    		move=0;
    	}
    	
    	private void counterAttack()
    	{
    		for(int index=0;index<cellArray.length;index++)
    		{
    			if(cellArray[index]==State.EMPTY) {
    				cellArray[index]=State.O;
    				cell[index].setEnabled(false);
    				System.out.printf("Move %d: Counter Attack: Cell %s is marked with O\n", move, index);
    				break;}
    			else continue;
    		}
    	}
    	
    	private class SinglePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(isXTurn()==true)
    			{
    				if(checkForWinner()==true)gameOver();
    				else
    				{
    					System.out.printf("Move %d: Cell %s is marked with X\n", move,event.getActionCommand());
    					cellArray[Integer.parseInt(event.getActionCommand())] = State.X;
    					cell[Integer.parseInt(event.getActionCommand())].setEnabled(false);
    					updateUI();
    					move++;
    					
    					if(checkForWinner()==true)gameOver();
    					else
    					{
    						counterAttack();
    						updateUI();
    						move++;
    					}
    				}
    			}
    		}
    	}
    	
    	private class DoublePlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if(isXTurn()==true)
    			{
    				if(checkForWinner()==true)gameOver();
    				else{
    					System.out.printf("Move %d: Cell %s is marked with X\n", move,event.getActionCommand());
    					cellArray[Integer.parseInt(event.getActionCommand())] = State.X;
    					cell[Integer.parseInt(event.getActionCommand())].setEnabled(false);
    					updateUI();
    					move++;
    				}
    			}else
    			{
    				if(checkForWinner()==true)gameOver();
    				else{
    					System.out.printf("Move %d: Cell %s is marked with O\n", move,event.getActionCommand());
    					cellArray[Integer.parseInt(event.getActionCommand())] = State.O;
    					cell[Integer.parseInt(event.getActionCommand())].setEnabled(false);
    					updateUI();
    					move++;
    				}
    			}
    		}
    	}
    	
    	private class ServerPlayerListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			statusLabel.setText(String.format("Server %s", event.getActionCommand()));
    			//code goes here
    		}
    	}
    }

  18. #18
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    OK, I've narrowed down the problem. What I did was I commented out each actionListener so that only one of them was working at a time.

    The DoublePlayerListener and ServerPlayerListener worked fine.

    When I ran with SinglePlayerListener only, each event was handled twice - which i believe would mean that the cells already had a SinglePlayerListener and another was added next to it and they register the same event twice.
    That would imply that the reason the other listeners don't work when they're all running is because say the game is set to "Two player game". When an event takes place - the DoublePlayerListener and SinglePlayerListener both handle the same event causing a mix up.

    My guess is that I shouldn't add the singlePlayerListener to all the cells by default and ought to make the user select a game type at the beginning.
    Last edited by eLancaster; 06-15-2011 at 10:06 PM.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    You can remove listeners also. So if you add one and need to change to another, you can remove the first one and add in the second one.

    I get this exception when I execute your code:

    New Game. Move: 1. Game Type: Exception in thread "main" java.lang.NullPointerException
    at TicTacToe2.emptyOutCells(TicTacToe2.java:198)
    at TicTacToe2.newGame(TicTacToe2.java:130)
    at TicTacToe2.<init>(TicTacToe2.java:35)
    at TicTacToe2.main(TicTacToe2.java:224)
    Last edited by Norm; 06-15-2011 at 10:53 PM.

  20. #20
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    OK, I managed to get that problem sorted out.
    I want to add an About Menu to the menubar. I thought of many simple ways of doing this (except for pop-up messages, which i hate). In the end I decided that i'd put two panels: an aboutPanel and a gamePanel. When the user clicks on the about menu, gamePanel.setVisible(false) & aboutPanel.setVisible(true) and when the user clicks on gameMenu, gamePanel.setVisible(true) and aboutPanel.setVisible(false).

    When I click on About, nothing happens - the menu button just takes on a clicked appearance. What am i doing wrong?
    If there's a better way of doing this, please do suggets - thanks!

    code:
    Java Code:
    public class TicTacToe implements ActionListener {
    	private enum GameType {SINGLE,DOUBLE,ONLINE}
    	private enum State {X,O,EMPTY};
    	
    	JPanel gamePanel;
    	private JPanel aboutPanel;
    	
    	private GameType playMode;
    	private JButton[] cell;
    	private State[] cellArray;
    	private JLabel  statusLabel;
    	private int[] winningCells;
    	private Timer timer;
    	
    	private int move;
    	
    	public TicTacToe()
    	{
    		System.out.println("APPLICATION LAUNCHED.");
    		playMode = GameType.SINGLE;
    		createUI();
    		newGame();
    	}
    	
    	public void createUI()
    	{
    		final JFrame window = new JFrame();
    		window.setTitle("Tic Tac Toe");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		aboutPanel = new JPanel();
    		aboutPanel.setLayout(new BorderLayout());
    		String text = "<html><p>Tic Tac Toe v 1.3</p>" +
    				"<p>Developed by: EL</p>" +
    				"<p>Website: el.com</p>";
    		
    		JLabel aboutLabel = new JLabel(text);
    		aboutPanel.add(BorderLayout.CENTER, aboutLabel);
    		aboutPanel.setVisible(false);
    		
    		gamePanel = new JPanel();
    		gamePanel.setLayout(null);
    		
    		JMenuBar menubar = new JMenuBar();
    		
    		JMenu aboutMenu = new JMenu("About");
    		aboutMenu.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						gamePanel.setVisible(false);
    						aboutPanel.setVisible(true);
    					}
    				}
    			);
    		
    		JMenu gameMenu = new JMenu("Start");
    		gameMenu.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						aboutPanel.setVisible(false);
    						gamePanel.setVisible(true);
    					}
    				}
    			);
    		
    		//other code
    
    		menubar.add(gameMenu);
    		menubar.add(aboutMenu);
    		
    		//-------top row ---------------//
    		
    		cell = new JButton[9];
    		cellArray = new State[9];
    		
    		for(int index = 0; index<cell.length; index++)
    		{
    			cell[index] = new JButton();
    			cell[index].setFont(new Font("arial",Font.BOLD,14));
    			cell[index].setActionCommand(Integer.toString(index));
    		}
    		
    		cell[0].setBounds(20, 20, 75, 75);
    		cell[1].setBounds(100, 20, 75, 75);
    		cell[2].setBounds(180, 20, 75, 75);
    		cell[2].setActionCommand("2");
    		cell[3].setBounds(20, 100, 75, 75);
    		cell[4].setBounds(100, 100, 75, 75);
    		cell[5].setBounds(180, 100, 75, 75);
    		cell[6].setBounds(20, 180, 75, 75);
    		cell[7].setBounds(100, 180, 75, 75);
    		cell[8].setBounds(180, 180, 75, 75);
    		
    		for(int index =0; index<cell.length;index++)
    		{
    			gamePanel.add(cell[index]);
    		}
    		
    		statusLabel = new JLabel("",JLabel.CENTER);
    		statusLabel.setText("Welcome to TicTacToe");
    		statusLabel.setVisible(true);
    		
    		//--------add action Listners-----//
    		for(int index=0;index<cell.length;index++)
    		{
    			cell[index].addActionListener(this);
    		}
    		//---------the rest-------------//
    		
    		
    		window.setJMenuBar(menubar);
    		[B]window.getContentPane().add(BorderLayout.NORTH, aboutPanel);[/B]
    		[B]window.getContentPane().add(gamePanel);[/B]
    		window.getContentPane().add(BorderLayout.SOUTH, statusLabel);
    		window.setMinimumSize(new Dimension(290,330));
    		window.setResizable(false);
    		window.setVisible(true);
    	}

Page 1 of 2 12 LastLast

Similar Threads

  1. Very basic java problem
    By mrparker2 in forum New To Java
    Replies: 6
    Last Post: 05-26-2011, 08:50 PM
  2. Replies: 1
    Last Post: 05-10-2011, 08:10 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  4. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 06:58 PM
  5. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 07:45 PM

Posting Permissions

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