TicTacToe

Status
Nicht offen für weitere Antworten.

Han

Bekanntes Mitglied
Hallo....ich will grad ein TicTacToe Spiel programmieren....bin aber grad am verzweifeln wie ich das mittels Swing realisieren soll....hier mal der Swing Code:


Code:
/**
* Main application class.
*/
public class TicTacToe {
	
	/** Data model for managing the marks at the board. */
	private Board board;

	/** Main application window. */
	private JFrame frame;

	/** Graphical output and modification of marks at the board. */
	private JPanel boardPanel; //?????????????????????

	/** For grouping of input components. */
	private JPanel inputPanel;
	
	/** Menu Bar for the menu */
	private JMenuBar menuBar;
	
	/** Menu (PvP,CvP,CvC,Quit) which is set into the MenuBar */
	private JMenu menu;
	
	/** Menu-Item Player vs. Player*/
	private JMenuItem itemPvP;
	
	/** Menu-Item Player vs. Computer*/
	private JMenuItem itemPvC;
	
	/** Menu-Item Computer vs. Computer*/
	private JMenuItem itemCvC;
	
	/** Menu-Item Quit */
	private JMenuItem itemQuit;
	
	/** Textual output of marks. */
//	?????	
	
	/**
	 * Action listener for the MenuItem Player vs Player. 
	 */
	private ActionListener itemPvPListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	};
	
	/**
	 * Action listener for the MenuItem Player vs. Computer. 
	 */
	private ActionListener itemPvCListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	};
	
	/**
	 * Action listener for the MenuItem Computer vs. Computer. 
	 */
	private ActionListener itemCvCListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	};
	
	/**
	 * Action listener for the MenuItem Quit. Quits the application.
	 */
	private ActionListener itemQuitListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	};
	
	/**
	 * Build and show main application window.
	 */
	public void start() {
		// Create the data model.
		board = new Board();

		// Create the swing components.
		frame = new JFrame("TicTacToe");
		boardPanel = new JPanel();
		inputPanel = new JPanel();
		
		menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		menu = new JMenu("Game");
		menuBar.add(menu);
		
		
		itemPvP = new JMenuItem("Person vs. Person");
		itemPvP.addActionListener(itemPvPListener);
		menu.add(itemPvP);
	
		itemPvC = new JMenuItem("Person vs. Computer");
		itemPvC.addActionListener(itemPvPListener);
		menu.add(itemPvC);
		
		itemCvC = new JMenuItem("Computer vs. Computer");
		itemCvC.addActionListener(itemCvCListener);
		menu.add(itemCvC);
		
		itemQuit = new JMenuItem("Quit");
		itemQuit.addActionListener(itemQuitListener);
		menu.add(itemQuit);
		
		
		// Build layout.
		frame.getContentPane().setLayout(new GridLayout(1, 2));
		frame.getContentPane().add(boardPanel);
		frame.getContentPane().add(inputPanel);

		inputPanel.setLayout(new BorderLayout());
		inputPanel.add(new JScrollPane());

		// Set EXIT_ON_CLOSE flag so that the application is terminated when the
		// main window is closed.
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		// Show window.
		frame.setSize(500, 300);
		frame.setVisible(true);
	}

	/**
	 * Main entry point of application.
	 * 
	 * @param args
	 *            Command line parameters.
	 */
	public static void main(String[] args) {
		TicTacToe app = new TicTacToe();
		app.start();
	}
}


Erstmal....wie realisiere ich das ich wenn ich z.b beim Menü auf Palyer vs. Player drücke er mir ein Fesnter aufmacht wo ich z.b den Namen von Player1 eingebe und dann wenn ich in dem Fenster ENTER drücke er mir ein 2tes Fenster aufmacht wo ich dann den Namen von Player2 eingebe?
So...und jetzt will ich ja im boardPanel eine 3x3 Matrix haben....wie mache ich das? Und zudem soll es mir bei einem Mausklick in der 3x3 Matrix ja ein x oder ein 0 machen...wie mache ich das? Ich weiß blöde Fragen aber ich hab echt keine Ahnung bzw. wo ich nachschauen könnt denn in unserem Skript steht das leider nicht und in der Dokumentation bin ich hoffnungsloss verloren...von der Fülle der Informationen mir genau das Passende zu finden.

mfg,
Hannes
 

Timmah

Bekanntes Mitglied
Schon wieder TicTacToe...


http://www.java-forum.org/de/viewtopic.php?t=31591

Für diese einfachen Eingabe-Dialoge schau dir mal die Klasse JOptionPane an. Dort gibt es u.A. eine Methode showInputDialog(), die dir eine einfache Eingabemaske liefert. Dann kannst du auch den gedrückten Knopf über den int-Wert,den die Methode als return-Wert zurückgibt, ermitteln, und dann entsprechend den zweiten Dialog öffnen.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben