P.S For future refrence, he thanks you for doing his assignment
Nice code Sidewinder *pulls out a dusty javadoc refrence and begins to read - yes, no swing just AWT...say something about age?*
sorry about that...
Sorry I know this thread is over a year old, but however it has been a big help for me while writing my own java program. I have read through all of the posts, and tried to implement your ideas. However, my instructor requires that our program use two files to get the job done, not just one. Any ideas or pointers in the right direction as to how I could accomplish this? Any help would be appreciated. I included the two files I have tried to do already. Whenever I run it, I recieve no build messages but get some weird exception, (Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException) and am not sure how to fix this. Thanks.
A NullPointerException means you have tried to instantiate an object that hasn't been given a value.
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...
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.
John, can you help me, when i use this code
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
Code: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; }
For the AI
1. You would need to edit the winCombinations to account for a 4x4 grid
2. Check for a win condition:Code:private int[][] winCombinations = new int[][] { {0, 1, 2, 3}, {4, 5, 6, 7}...
You will obviously need to edit the GUI as well.Code: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; }
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
Code: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); }
I'm not sure. I haven't programmed in Java in two years - try asking in the Java section/forum.
There are currently 10 users browsing this thread. (0 members and 10 guests)
Bookmarks