r/learnjava • • Jan 24 '21

How do I approach a tic-tac-toe program with Java?

Would I need to use functions?

7 Upvotes

14 comments sorted by

37

u/vladadj Jan 24 '21

In general, asking questions like this won't yield any worthwhile answer. You need to be specific. What have you done so far, what approaches you tried, what does not work etc.

The best I can offer is this:

public class TicTacToe{ public static void main(String[] args) { } }

This will get you started. Now think of how you can break your program down into smaller chunks and try implementing them. When you get stuck in specific step, post your code and we can try to help you.

5

u/zolanih Jan 24 '21

This is a really good underrated answer.

7

u/pyordie Jan 24 '21

Yes, you will need to use functions.

You could use 3 or 4 classes - Game, Player, Board and maybe Space.

You should be able to take it from there.

6

u/Fridginator Jan 24 '21

I am faaaaaaaaaaaaaar from a java expert, but dont you generally want to use functions for most programs?

I guess you could make a class board and a class sign as a start and functions to write the board, write new signs, check if the spot on the board is available and check if the latest move won or not.

But yet again i am faaaaaaaaaaar from an expert

3

u/[deleted] Jan 24 '21

I wrote a simple tictactoe program a while back. I set up a "gameloop" using a while loop and broke the game down into different functions (drawGameBoard, playerY, playerX, checking if board is full, checking if a player has won). If I can help some more let me know

4

u/DigitalTorture Jan 24 '21

Do you mean methods? I believe functions are for C++.

7

u/PabloDavico Jan 24 '21

I see you got downvoted a little. You are not completely wrong though, just that methods and functions share a common definition but with a subtle difference. Conceptually, methods are just functions encapsulated by classes, because they define a functionality and you will usually find this in OOP languages. However, they are not "pure functions" since they can have side effects, like mutation of variables that were implicitly passed into the method. Pure functions you will find in functional programming languages like haskell. It can also be done in C++ with the right discipline, it's just that it doesn't enforce you to use one or the other.

2

u/DigitalTorture Jan 24 '21

Ok, cool. You learn new things every day. I always wanted to learn a functional programming language. I guess i messed around with basic when i was a kid, but that was a long time ago and I don't think it is a functional language.

1

u/voiceofonecrying Jan 24 '21

The question is broad enough that you can have multiple correct answers.

You could make a console app that would make a visualization of the board like: [ ] | [ ] | [ ] —————- [ ] | [O] | [ ] —————- [ ] | [ ] | [ ]

Then write a main function that prints the current game state, prompt for the current player to enter a number corresponding to the space they want to fill, then alternate players until a checked win condition is met.

You could also make a JavaFX program that actually makes a board for you, then let’s you click a space for input, have a new game button and a counter for how many X wins and O wins etc.

But yeah, you would probably want to write some methods to abstract away some things like void placeX(int space) void placeO(int space)

1

u/HecknChonker Jan 24 '21

Start by breaking the problem down into smaller pieces, and solve each of them one at a time. If one of those questions is still too large you can continue to break it into smaller and smaller pieces.

How will you display the board to the players? How will the players choose where to play? How do you determine when the game ends, and which player won?

Using functions will help you organize the code into manageable chunks.

1

u/travishummel Jan 24 '21

You will need functions like placePiece(boolean isX, int row, int column) that will put an x or an o in the correct place if applicable.

You will need a function that determines if someone won. Maybe it returns a Boolean object, null representing that the game continues, true that x won, or false that o won... idk

A function to print out the board.

If you had those in a clean way, you would pretty much be done. Then you could expand this to connect 4

1

u/tidewater41009 Jan 24 '21

How about some object oriented design analysis or modelling first. Put the programming language aside. Decompose the problem into a set of objects. Objects have characteristics and implement operations (for value or side effect). Define the interaction of the objects. Define the relationship between objects.

Once a OO model is form, realize the model through the programming language of choice. In this case Java.