i’ve never been this frustrated in my life. cant even do the basics here.. just need to make a simple tic tac toe program. i feel so alone in this world right now.. i get the basic idea, but can’t put it together logically.
Class instance variables:
- private char[][] board; private char
- player; // ‘X’ or ‘O’
Methods:
- public TicTacToe()
- public void print()
- public boolean play(String s)
- public boolean won()
- public boolean stalemate()
Here’s what i’ve got for code:
import java.util.Scanner;
public class Six1
{
public static void main(String[] args)
{
TicTacToe ttt = new TicTacToe();
ttt.TicTacToe();
ttt.print();
}
static class TicTacToe
{
private char player; // 'X' or 'O'
private char[][] board;
// make board
public TicTacToe()
{
// construct board
board = new char[3][3];
// initialize elements
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ' ;
}
}
}
// print board
public void print()
{
for ( int i = 0; i < 3; i++)
{
System.out.println(" ");
for( int j = 0; j < 3; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println("\n------");
}
}
}
}
You don’t have a lot done yet, but what you have seems mostly reasonable. You may be over-complicating things by using an inner class. And you’ll need to put something in
mainif you want something to happen.You can’t write the whole program all at once. You can either start at the top and work down to the details, or work on the details and then assemble them into a working whole.
Working top-down
If you’re not sure where to start, this can be a good way to get things moving. Write the main body of the code using whatever functions you wish existed. Maybe you’d start with
It’s ok that these functions don’t exist yet. Once you’ve got the main level, start implementing these made-up functions, using more made-up functions if necessary. Repeat until you get down to simple functions that you do know how to implement.
If you want to compile your code without implementing every method, you can use
throw new UnsupportedOperationException("not implemented");as the body of any methods that need to return values.Working bottom-up
If you know you’ll need certain pieces but aren’t sure how they’ll fit together, start with this method.
You know you’ll need some way to ask the user what move they want to make. So create a function that does that and test it on it’s own. You know you’ll need a way to check if there’s a winner. Hardcode some values into
board[]and test yourisWinner()function. Once you’ve got some working pieces you can assemble them into larger and larger chunks until you’ve got a functioning program.