|
-
Dec 10th, 2004, 07:19 AM
#1
Thread Starter
Hyperactive Member
Help Java Tic tac toe
Hi, i am trying to make a java tic tac toe game and am completely stuck. Totaly stuck even, i have the idea of how it should be done but i dont know how the code should be. i have this code which is very very basic and doesnt do much:
Code:
public class tictactoe
{
public static void main(String args[])
{
System.out.println(" A B C ");
System.out.println(" | | ");
System.out.println("1 | | ");
System.out.println(" _____|_____|_____");
System.out.println(" | | ");
System.out.println("2 | | ");
System.out.println(" _____|_____|_____");
System.out.println(" | | ");
System.out.println("3 | | ");
System.out.println(" | | ");
}
}
please can you help
thanks very much
-
Dec 10th, 2004, 07:55 AM
#2
Re: Help Java Tic tac toe
You need a main loop that runs until either all squares are full or one player has won. So you need to be able to determine this condition. This in turn means that you need a way to internally store which fields are set and which are not.
Then you need to prompt the user for input in order to know where to place the next piece. Then you need to prompt the other user, or you need some AI to determine where the computer is supposed to place his piece. (Mind you, a proper AI cannot be defeated in this game, just as a proper player cannot be defeated.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2004, 08:15 AM
#3
Thread Starter
Hyperactive Member
Re: Help Java Tic tac toe
thanks, i kinda have all that written down as what i need to do, its the actual coding that i am having trouble with.
-
Dec 10th, 2004, 09:18 AM
#4
Re: Help Java Tic tac toe
But the actual coding is what we won't do for you. Any particular things you don't know how to do?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2004, 09:35 AM
#5
Thread Starter
Hyperactive Member
Re: Help Java Tic tac toe
yes
i have a memory location called loc
now the user inputs a value for loc
say loc = a2
now what i need to do is to make the value of loc at the moment is a2 to = X or 0
so loc = a2
a2 = X
if you understand
i do not know how to do this or indeed if it can be done
thanks
-
Dec 10th, 2004, 09:39 AM
#6
Re: Help Java Tic tac toe
How do you store your data, anyway? The best method usually is a 2d-array.
Then you read in something from the user, say, a2, separate it into the two components and make indices out of them.
Then you use the indices to get the correct location in the array.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 10th, 2004, 06:05 PM
#7
Frenzied Member
Re: Help Java Tic tac toe
I can give you an example of a tic tac game if you like. Let me know and I will post the code.
-
Dec 27th, 2004, 08:03 AM
#8
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by System_Error
I can give you an example of a tic tac game if you like. Let me know and I will post the code.
Don't, that way he won't learn anything.
What he should do, however, is:
Make a 2D array storing each field of the game bord. Everytime a brick is placed, set the value of the field the player set the brick to a value depending on if it's player1, or player2(or if a brick was removed). You could use 0, for nothing, 1 for player1, and 2 for player2 - I'd advise using constants for theese values, making them easier to use/remember. When you got this down, you should make a loop. (Almost)Every computer game has a main loop, ensuring that it runs over and over. Inside your loop, you need to dermine what the player wants to do next - if you're using a console, you need to have an inputreader, to get commands from the user, and interpret this; if the user types "Exit" the game should break out of the loop, if the user types "set a,1" a brick should be placed at that location.
I hope this got you on the way... It's fairly simple when you think about it...
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 27th, 2004, 09:06 AM
#9
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by vbNeo
Don't, that way he won't learn anything.
What he should do, however, is:
Make a 2D array storing each field of the game bord. Everytime a brick is placed, set the value of the field the player set the brick to a value depending on if it's player1, or player2(or if a brick was removed). You could use 0, for nothing, 1 for player1, and 2 for player2 - I'd advise using constants for theese values, making them easier to use/remember. When you got this down, you should make a loop. (Almost)Every computer game has a main loop, ensuring that it runs over and over. Inside your loop, you need to dermine what the player wants to do next - if you're using a console, you need to have an inputreader, to get commands from the user, and interpret this; if the user types "Exit" the game should break out of the loop, if the user types "set a,1" a brick should be placed at that location.
I hope this got you on the way... It's fairly simple when you think about it...
This is exactly what I did, except I made a GUI. Personally I don't see how he could use the console, but if he can that's great. Anyways, it is easy once you get started. The only thing you would have trouble with is the search algorithms. I know I had to look them up, and if you don't, your very good. But that's the only hard part, and the only part that really requires any thinking.
-
Dec 27th, 2004, 09:41 AM
#10
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by System_Error
This is exactly what I did, except I made a GUI. Personally I don't see how he could use the console, but if he can that's great. Anyways, it is easy once you get started. The only thing you would have trouble with is the search algorithms. I know I had to look them up, and if you don't, your very good. But that's the only hard part, and the only part that really requires any thinking.
Search algorithms ? You want to use RegEx for a Tic Tac Toe game ?
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 27th, 2004, 11:51 AM
#11
Frenzied Member
Re: Help Java Tic tac toe
I don't think they had regex when I made mine, but they might have. Here was the algo's that I used:
Code:
private boolean hasWon(int x, int y) {
boolean won;
won = true;
for (int col=0; col<3; col++) {
if (!btn[x][col].getText().equals(player)) {
won = false;
}
}
if (!won) {
won = true;
for (int row=0; row<3; row++) {
if (!btn[row][y].getText().equals(player)) {
won = false;
}
}
}
if (!won) {
won = true;
for (int num=0; num<3; num++) {
if (!btn[num][num].getText().equals(player)) {
won = false;
}
}
}
if (!won) {
won = true;
for (int num=0; num<3; num++) {
if (!btn[2-num][num].getText().equals(player)) {
won = false;
}
}
}
return won;
}
Remember, there is not just one way to solve a problem. I am sure there a ton of different ways to make a tic tac toe game without using regex
-
Dec 27th, 2004, 11:57 AM
#12
Frenzied Member
Re: Help Java Tic tac toe
vbNeo, do you mind showing me an example of using RegEx? I'm not to familar with it and would like to see what your talking about..if you don't mind of course..
-
Dec 27th, 2004, 01:01 PM
#13
Frenzied Member
Re: Help Java Tic tac toe
RegEx is to big a subject to show with a simple example... Look it up on Google
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 27th, 2004, 03:44 PM
#14
Frenzied Member
Re: Help Java Tic tac toe
I didn't mean the whole program...All I was asking for is like a small code snippet of how you would search to see if there was three-in-a-row.
-
Dec 27th, 2004, 05:20 PM
#15
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by System_Error
I didn't mean the whole program...All I was asking for is like a small code snippet of how you would search to see if there was three-in-a-row.
I thought you ment searching a string... I really wouldn't call what you posted an Algorithm...
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 27th, 2004, 06:04 PM
#16
Frenzied Member
Re: Help Java Tic tac toe
Well, what do you consider an algorithm? If that's not, then nothing is.
Do you even know the definition of an algoritm?
A way to solve a problem ---- exactly what the code I posted does..
-
Dec 27th, 2004, 09:19 PM
#17
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by System_Error
Well, what do you consider an algorithm? If that's not, then nothing is.
Do you even know the definition of an algoritm?
A way to solve a problem ---- exactly what the code I posted does..
That's a rather broad definition, I consider an algorithm to be a shortest way to solve a big problem, like an equation, anyways, I think you would agree, that anything in an application could be called an algorithm if your definition as true, and therefore there's no point in using the word, since it'll always be a part of the application...
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 27th, 2004, 10:31 PM
#18
Frenzied Member
Re: Help Java Tic tac toe
Well, why don't you show me an example of what you consider an algorithm, that does the same thing as mine? I would like to see exactly what you consider an alogorithm... Now if your can't, I don't want to hear anymore debating..
-
Dec 28th, 2004, 06:02 AM
#19
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by System_Error
Well, why don't you show me an example of what you consider an algorithm, that does the same thing as mine? I would like to see exactly what you consider an alogorithm... Now if your can't, I don't want to hear anymore debating..
Wether or not I'm capable of constructing what I consider to be an algorithm, that does what your code does(assuming you wrote it), has nothing to do with wether or not I wish to accept your definition. You are not the one to decide if the debating is to go on, you can ignore it, and stop being in the discussion, but you are in no way capable of stopping me from saying my opinion.
I'd consider an algorithm an equation, that you would be capable of implementing with one or two code blocks.
You, however, never answered wether or not you agreed that your definition was to broad, and that everything in an application would be an algorithm, not to mention the application itself.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Dec 28th, 2004, 10:24 AM
#20
Frenzied Member
Re: Help Java Tic tac toe
 Originally Posted by vbNeo
You, however, never answered wether or not you agreed that your definition was to broad, and that everything in an application would be an algorithm, not to mention the application itself.
Sorry if I came of being rude in that last comment..
Well, what you said makes sense, but all the definitions of an algorithm is pretty broad. Now, you said your version of an algorithm is something that is the shortest way to solve a big problem. You still could consider the whole program to be an algorithm, if this was the case, but I think we both know what each other means. Also the the code that I posted is not just one algorithm. It's several, and all are pretty short. One find's if there was three in a row diaganoly, then one finds wether or not their was one horizontal and so on..
Note: I don't doubt that you can come up with an algorithm like my using reg expression, but I am just curious as to how you would do this.
PS: Do you not like how I did the searching?
-
Dec 28th, 2004, 01:35 PM
#21
Thread Starter
Hyperactive Member
Re: Help Java Tic tac toe
Hi all,
I have finished my tic tac toe game now, changed the code quite a lot and also learnt lots, i did use an array. thanks for your help.
-
Dec 28th, 2004, 01:42 PM
#22
Frenzied Member
Re: Help Java Tic tac toe
I'm curious, how did you do your searching or finding out wether or not three in a row was present?
-
Dec 28th, 2004, 01:48 PM
#23
Thread Starter
Hyperactive Member
Re: Help Java Tic tac toe
i just used some if commands:
Code:
if((Grid[0]=='x' && Grid[1]=='x' && Grid[2]=='x') ||
(Grid[3]=='x' && Grid[4]=='x' && Grid[5]=='x') ||
(Grid[6]=='x' && Grid[7]=='x' && Grid[8]=='x') ||
(Grid[0]=='x' && Grid[3]=='x' && Grid[6]=='x') ||
(Grid[1]=='x' && Grid[4]=='x' && Grid[7]=='x') ||
(Grid[2]=='x' && Grid[5]=='x' && Grid[8]=='x') ||
(Grid[0]=='x' && Grid[4]=='x' && Grid[8]=='x') ||
(Grid[2]=='x' && Grid[4]=='x' && Grid[6]=='x'))
do another for 0s aswell
-
Dec 28th, 2004, 02:18 PM
#24
Frenzied Member
Re: Help Java Tic tac toe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|