help with basic tic tac toe program
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 main
if 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
public static void main(String[] args) {
printBoard();
while (!isWinner()) {
readMove(); // get move from stdin and mark on board
printBoard(); // redraw board
}
printWinner(); // say who won
}
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 your isWinner()
function. Once you've got some working pieces you can assemble them into larger and larger chunks until you've got a functioning program.
You say you don't know what to do in main:
public static void main(String[] args)
{
// what the hell do i need here?!?!
// TicTacToe() <--???
// print() <-????
}
Simply calling the methods won't work for two reasons:
main
is a static method which runs without an instance of an object. The methods you want to call are instance methods, i.e. you need an object to call them on.You have defined an inner class. The methods are part of that inner class.
You can solve both of these issues, of course, although as bemace said, the 2. makes your program more complicated than necessary. You could just drop the "class TicTacToe" definition. If you want to keep it, you can create an instance of it like follows:
TicTacToe ttt = new TicTacToe();
Edit: Note that as NamshubWriter commented, this won't work unless you declare your inner class as static
:
static class TicTacToe
Then you can call methods on it:
ttt.print();
The TicTacToe()
you tried to call is actually a constructor. It is automatically called when you do a new TicTacToe()
to create a new object.
If you put the above two lines in your main method, you should be a step further.
One thing that is confusing things is the inner/nested class. I'm assuming that the assignment requires you to have a class named Six1
. I would make TicTacToe
a top-level class. So you would have two source files in the same directory:
begin Six1.java
public class Six1 {
public static void main(String[] args)
TicTacToe ttt = new TicTacToe();
ttt.play();
}
}
end Six1.java
begin TicTacToe.java
public class TicTacToe {
private char[][] board;
private char player; // 'X' or 'O'
/**
* Constructs a new Tic Tac Toe object
*/
public TicTacToe() {
// initialize board
}
/**
* Plays a game
*/
public void play() {
// play the game
print();
}
/**
* Prints the board
*/
public void print() {
}
}
end TicTacToe.java
Break this problem down a bit and try not to tackle it as a whole.
For now focus on a method to test for a win. What do you want the function to return in each instances (Draw, unfinished game, player 1 won or player 2 won)?
How will you test for this win?
Once you have that method the rest should fit into place a lot more easily.
This is what I have come up with. Hope this helps if anybody is still interested!
public class TicTacToe {
static int SIZE = 3;
static int MAX_MOVES = SIZE * SIZE;
static char[][] matrix = new char[SIZE][SIZE];
static int[][] moveMatrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 },
{ 7, 8, 9 } };
public static void printState() {
System.out.println("Current State:");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
if ((matrix[i][j] == 'x') || (matrix[i][j] == 'o'))
System.out.print(" " + matrix[i][j]);
else
System.out.print(" -");
System.out.println();
}
}
public static int enterMove() {
System.out.println("Enter your move as follows:");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
if ((matrix[i][j] == 'x') || (matrix[i][j] == 'o'))
System.out.print(" -");
else
System.out.print(" " + moveMatrix[i][j]);
System.out.println();
}
System.out.println("Enter your move :");
int move = 0;
move = Integer.parseInt(System.console().readLine());
return move;
}
public static boolean updateMatrix(int move, char ch) {
int i = 0;
int j = 0;
boolean status;
switch (move) {
case 1:
break;
case 2:
i = 0;
j = 1;
break;
case 3:
i = 0;
j = 2;
break;
case 4:
i = 1;
j = 0;
break;
case 5:
i = 1;
j = 1;
break;
case 6:
i = 1;
j = 2;
break;
case 7:
i = 2;
j = 0;
break;
case 8:
i = 2;
j = 1;
break;
case 9:
i = 2;
j = 2;
break;
default:
System.out.println("Invalid entry");
status = false;
break;
}
if (moveMatrix[i][j] == -1) {
System.out
.println("Entry already marked,please re-enter your move.");
status = false;
} else {
matrix[i][j] = ch;
moveMatrix[i][j] = -1;
status = true;
}
return status;
}
public static boolean isGameOver() {
boolean status = false;
if ((matrix[0][0] == matrix[1][1]) && (matrix[1][1] == matrix[2][2])
&& ((matrix[1][1] == 'x') || (matrix[1][1]) == 'o'))
status = true;
else if ((matrix[2][0] == matrix[1][1])
&& (matrix[1][1] == matrix[0][2])
&& ((matrix[1][1] == 'x') || (matrix[1][1]) == 'o'))
status = true;
for (int i = 0; i < SIZE; i++)
if ((matrix[i][0] == matrix[i][1])
&& (matrix[i][1] == matrix[i][2])
&& ((matrix[i][1] == 'x') || (matrix[i][1]) == 'o'))
status = true;
else if ((matrix[0][i] == matrix[1][i])
&& (matrix[1][i] == matrix[2][i])
&& ((matrix[1][i] == 'x') || (matrix[1][i]) == 'o'))
status = true;
return status;
}
public static void main(String[] args) {
char ch;
int k = 0;
int player;
do {
player = k % 2 + 1;
k++;
if (player == 1)
ch = 'x';
else
ch = 'o';
printState();
System.out.println("Player" + player + " [" + ch + "]:");
if (!updateMatrix(enterMove(), ch))
k--;
} while ((!isGameOver()) && (k < MAX_MOVES));
printState();
if (isGameOver())
System.out.println("Game Over. Player" + player + " [" + ch
+ "] wins!");
else
System.out.println("Game Tied. No winner!");
}
}
精彩评论