Tic Tac Toe java app

Discussion in 'Mac Programming' started by the_insider, May 5, 2006.

  1. macrumors regular

    the_insider

    Joined:
    Apr 20, 2005
    Location:
    USA / UK
    #1
    Im taking a high school course in computer science, and one of my many projects is to program a simple tic tac toe program. It involves two players, who can play against each other, no A.I. or anything like that. I dont have alot of time left, and my current program just keeps spitting errors at me, so i was wondering if someone could write me a simple tic tac toe program in java just to help me get through my senior year. Lots of IB's to study for and lots of exams to pass over the next 2 weeks. Any help would be greatly appreciated.
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    We could help you fix the errors but it's dishonest to have someone else do your work for you.
     
  3. macrumors 6502

    Joined:
    Nov 23, 2005
    #3
    Does this have to have a GUI? Or can it just be console?

    Here are a few hints.
    Use 2D array.. int[][] board = new int[3][3];
    Then have a set method that takes in public void set(pos1, pos2, int)
    Check to see if the incoming positions are valid
    make sure the the int is not > 2. (you are storing 1 or 2 for X or O)
    Now do a toString method that will print everything and if it is a 1 it will be X if it is a two it will be a O..

    that would just be your Game class, you will also have a player class, but that should be a very easy class to write.

    -joe
     
  4. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #4
    What are you offering in return?
     
  5. macrumors 6502

    Joined:
    Nov 23, 2005
    #5
    A high five?? What are you expecting from this kid? :rolleyes:
     
  6. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #6
    I would accept payment in the form of money, power, or fame.

    Cheers

    PS J/k I only take money
     
  7. macrumors 6502

    Joined:
    Nov 23, 2005
    #7
    I basically told him how to do it. If he still cannot figure it out. Then he better pay you.
     
  8. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #8
    I did a similar thing a few weeks ago, I'm in my first year of University. It didn't take long to do, it's really very simple.
     
  9. Wes
    macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #9
    If no GUI is required it's a pretty easy project. As a previous poster said use a 2d array of characters.

    Then have an isWinner(char) method which goes across all rows and columns checking for a winner.

    http://www.doc.ic.ac.uk/~sue/121/all-6up.pdf

    Look on page 60 of the notes to get a lot of help about how to implement it.

    Basically you need to do this sort of thing:

    Init code
    Loop while there is not a winnner {
    print the board
    read in move
    while (entered move is not valid) {
    read in another move
    }
    plot move on board
    check for winner
    }
    print the board
    print congratulatory message to correct player


    edit: added printing the board haha
     
  10. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #10
    academic dishonesty has worse-off consequences compared to turning in an assignment full of errors. Colleges really do not like accepting students with a past history of academic dishonesty, and teachers/professors don't write letters of recommendation for you if they know you cheat.

    The hints given by other people in this thread are very good advice, I had a similar assignment (2 player tictactoe over a network) last semester and what Wes and jtalerico are pretty much similar to what I did.

    You could of course post the errors and whatever you have so far in this post, and maybe we could help you fix it.
     
  11. macrumors 68030

    ReanimationLP

    Joined:
    Jan 8, 2005
    Location:
    On the moon.
    #11
    But you'd think Master Shake would want all 3 at the same time. :D

    Seriously though, dont cheat. It could come back to haunt you down the road.

    I dont recommend taking little snippets from others and gluing them together. You really never know when this kind of stuff can come back to bite you in your butt.
     
  12. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #12
    Yeah, its only fair to add that there ARE programs which compare code and can measure various similarity metrics. It was developed primarily at Berkeley and is IN USE at most medium and large schools, and people do get caught plagarizing.

    So if you turn in code you copied from somewhere else thinking, how would they ever know, you could get busted really easily. And changing names of variables and methods won't get you off the hook either.
     
  13. weg
    macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #13
    Yeah.. I was wondering if somebody could write me a few papers and a thesis, just to help me to get my PhD... :eek:
     
  14. macrumors 6502

    Joined:
    Nov 23, 2005
    #14
    Sure when do you need it by?
     
  15. thread starter macrumors regular

    the_insider

    Joined:
    Apr 20, 2005
    Location:
    USA / UK
    #15
    alright. sorry i havnt checked this tread yet. ive been writing the program on JCreator, and i couldnt upload a .java file, so i copied it into word.. It would be nice to get some people to double check my program and correct any errors you can find.

    thanks alot for all the help
     

    Attached Files:

  16. macrumors 6502

    Joined:
    Nov 23, 2005
    #16
    Errors out on me... Right off bat. First number i click it errors, then i get a system.out.println...

    000001000

    If i click 9 I get...
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
    	at TicTacToe$MoveHandler.actionPerformed(TicTacToe.java:84)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
    	at java.awt.Component.processMouseEvent(Component.java:5488)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    	at java.awt.Component.processEvent(Component.java:5253)
    	at java.awt.Container.processEvent(Container.java:1966)
    	at java.awt.Component.dispatchEventImpl(Component.java:3955)
    	at java.awt.Container.dispatchEventImpl(Container.java:2024)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    	at java.awt.Container.dispatchEventImpl(Container.java:2010)
    	at java.awt.Window.dispatchEventImpl(Window.java:1774)
    	at java.awt.Component.dispatchEvent(Component.java:3803)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    
     
  17. macrumors 6502

    Joined:
    Nov 23, 2005
    #17
    Also forgot to mention that comments would be nice.
     
  18. Wes
    macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #18
    I see a comment (line 27)...

    //######

    Makes perfect sense to me...
     
  19. macrumors 6502

    Joined:
    Nov 23, 2005
    #19
    Sorry for posting like 10 times in the matter of minutes.. But look at your code and ask yourself. Does an array start from 0-8 or is it 1-9?

    Also it seems that you did not take any of the advice we gave you. 2D array should look like int[][]
     
  20. macrumors 6502

    Joined:
    Nov 23, 2005
    #20
    Oh, i missed that... NOWWWW i understand... :rolleyes:

    :) :) :)
     
  21. Wes
    macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #21
    For OP:

    I could give the code a look on Saturday but I have final exams tomorrow and the day after so I can't look at in the interim.
     
  22. thread starter macrumors regular

    the_insider

    Joined:
    Apr 20, 2005
    Location:
    USA / UK
    #22
    ok the //##### is a note to myself, where i have to work. What im trying to do there is store the moves, so that the game will know when one of the players have won.

    and the //****

    is where ive got another prob.. when i do get the program running, if i click on one box, it will change to an X, or an O, and if i click the same box, il get a pop up to tell the player that you have to click on another box. but after that instead of an X again, it will be an O or vice versa.
    does that make any sense?
     
  23. macrumors 6502

    Joined:
    Nov 23, 2005
    #23
    insider
    Save yourself the headaces, rewrite the code with a 2D array. It will make things a lot easier on you...

    static int[][] moves = new int[3][3];
     
  24. thread starter macrumors regular

    the_insider

    Joined:
    Apr 20, 2005
    Location:
    USA / UK
    #24
    here i fixed the an error, it should run fine now.
     

    Attached Files:

  25. macrumors 6502

    Joined:
    Nov 23, 2005
    #25
    To find a winner you want to go through the array and check if instance [0] is x [1] is x [2] is x. assuming your array is somehow setup like this...

    0 1 2
    3 4 5
    6 7 8

    reason wny 2D arrays are so much better is because you can break it up...

    [0][0] [0][1] [0][2]
    [1][0] [1][1] [1][2]
    [2][0] [2][1] [2][2]

    Your array is really setup like this...

    [0][1][2][3][4][5][6][7][8]

    That is why doing a 2D array would be better for like error checking because you could just do 3 for looks to check it out...
     

Share This Page