Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

the_insider

macrumors regular
Original poster
Apr 20, 2005
177
0
USA / UK
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
We could help you fix the errors but it's dishonest to have someone else do your work for you.
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
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
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
the_insider said:
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.

F.C

What are you offering in return?
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
I basically told him how to do it. If he still cannot figure it out. Then he better pay you.
 

4409723

Suspended
Jun 22, 2001
2,221
0
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
 

janey

macrumors 603
Dec 20, 2002
5,316
0
sunny los angeles
the_insider said:
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...
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.
 

ReanimationLP

macrumors 68030
Jan 8, 2005
2,782
33
On the moon.
savar said:
I would accept payment in the form of money, power, or fame.

Cheers

PS J/k I only take money

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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
ReanimationLP said:
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.

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.
 

weg

macrumors 6502a
Mar 29, 2004
888
0
nj
the_insider said:
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.

Yeah.. I was wondering if somebody could write me a few papers and a thesis, just to help me to get my PhD... :eek:
 

the_insider

macrumors regular
Original poster
Apr 20, 2005
177
0
USA / UK
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
 

Attachments

  • import java.doc
    37.5 KB · Views: 611

jtalerico

macrumors 6502
Nov 23, 2005
358
0
the_insider said:
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

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)
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
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[][]
 

4409723

Suspended
Jun 22, 2001
2,221
0
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.
 

the_insider

macrumors regular
Original poster
Apr 20, 2005
177
0
USA / UK
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?
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
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];
 

jtalerico

macrumors 6502
Nov 23, 2005
358
0
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.