PDA

Click to See Complete Forum and Search --> : Help Java Tic tac toe


richsmith
Dec 10th, 2004, 06:19 AM
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:


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

CornedBee
Dec 10th, 2004, 06:55 AM
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.)

richsmith
Dec 10th, 2004, 07:15 AM
thanks, i kinda have all that written down as what i need to do, its the actual coding that i am having trouble with.

CornedBee
Dec 10th, 2004, 08:18 AM
But the actual coding is what we won't do for you. Any particular things you don't know how to do?

richsmith
Dec 10th, 2004, 08:35 AM
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

CornedBee
Dec 10th, 2004, 08:39 AM
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.

System_Error
Dec 10th, 2004, 05:05 PM
I can give you an example of a tic tac game if you like. Let me know and I will post the code.

vbNeo
Dec 27th, 2004, 07:03 AM
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...

System_Error
Dec 27th, 2004, 08:06 AM
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.

vbNeo
Dec 27th, 2004, 08:41 AM
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 ?

System_Error
Dec 27th, 2004, 10:51 AM
I don't think they had regex when I made mine, but they might have. Here was the algo's that I used:

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

System_Error
Dec 27th, 2004, 10:57 AM
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..

vbNeo
Dec 27th, 2004, 12:01 PM
RegEx is to big a subject to show with a simple example... Look it up on Google

System_Error
Dec 27th, 2004, 02:44 PM
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.

vbNeo
Dec 27th, 2004, 04:20 PM
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...

System_Error
Dec 27th, 2004, 05:04 PM
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..

vbNeo
Dec 27th, 2004, 08:19 PM
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...

System_Error
Dec 27th, 2004, 09:31 PM
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..

vbNeo
Dec 28th, 2004, 05:02 AM
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.

System_Error
Dec 28th, 2004, 09:24 AM
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?

richsmith
Dec 28th, 2004, 12:35 PM
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.

System_Error
Dec 28th, 2004, 12:42 PM
I'm curious, how did you do your searching or finding out wether or not three in a row was present?

richsmith
Dec 28th, 2004, 12:48 PM
i just used some if commands:


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

System_Error
Dec 28th, 2004, 01:18 PM
looks pretty good