array
I am developing this program for the game Noughts and crosses
this is what I have done so far:
publicclass OXOGame
{
privatechar [][] board;
privatestaticfinalint ROWS=3;
privatestaticfinalint COLUMNS=3;
staticchar marker1='x';
staticchar marker2='o';
int currentPlayer;
public OXOGame()
{
board=newchar[ROWS][COLUMNS];
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLUMNS;j++)
board[i][j]=' ';
}
/**There are two players, Player 1 and Player 2.
This method returns 1 if it is Player 1抯 turn next,
and it returns 2 if it is Player 2抯 turn next.
At the start of the game, player 1 is always to first one to go.
*/
int getCurrentPlayer()
{
if(currentPlayer==1)
{
currentPlayer=marker1;
}
else
{
currentPlayer=marker2;
currentPlayer=2;
}
return currentPlayer;
}
/**(This method attempts to put a marker in the square at column i, row j.
Both i and j can take values of 0, 1 and 2, to access the 3x3=9 squares.
Player 1抯 markers are the character 憍? Player 2抯 markers are the character 憃?
If i & j are valid and that square is free, the current player抯 marker is placed, the other player becomes the current player, and the method returns true.
Otherwise, there is no change to the board, or player, and it returns false. )
*/
boolean go(int i,int j)
{
if(board[i][j].equals(' '))
board[i][j]=currentPlayer;
}
/**(returns the marker at column i, row j. The top left square is (0,0).
The two markers are: 憍?(Player 1) and 憃?(Player 2).
If there is no marker at this square, the method returns a space character (?.
*/
char get(int i,int j)
{
}
public String toString()
{
String r=" ";
for(int i=0;i<ROWS;i++)
{
r=r+"|";
for(int j=0;j<COLUMNS;j++)
r=r+board[i][j];
r=r+"|\n";
}
return r;
}
}
Can Anyone help me I don t know if I am right and how to finish the programm
Thanks>
[4331 byte] By [
gigapra] at [2007-11-27 1:13:03]

public class TicTacToe
{
public TicTacToe()
{
board = new String[ROWS][COLUMNS];
// Fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = " ";
}
public void set(int i, int j, String player)
{
if (board[i][j].equals(" "))
board[i][j] = player;
}
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}
private String[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
}
TicTacToeTester.java
import java.util.Scanner;
public class TicTacToeTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String player = "x";
TicTacToe game = new TicTacToe();
boolean done = false;
while (!done)
{
System.out.print(game.toString());
System.out.print(
"Row for " + player + " (-1 to exit): ");
int row = in.nextInt();
if (row < 0) done = true;
else
{
System.out.print("Column for " + player + ": ");
int column = in.nextInt();
game.set(row, column, player);
if (player.equals("x"))
player = "o";
else
player = "x";
}
}
}
}
output
Row for x (-1 to exit): 2
Column for x: 1
||
||
| x |
Row for o (-1 to exit): 1
Column for o: 0
||
|o |
| x |
you can try as follows
public static void go(int i, int j, Player player) {
while (player.isAcitve) {
if (!(board[i][j] == ' ')) {
System.err.println("Already Marked");
player.isAcitve = true;
} else {
board[i][j] = player.marker;
player.isAcitve = false;
}
}
}
Where Player is a class. Defined as
class Player{
boolean isAcitve;
char marker;
Player(boolean isActive, char marker){
this.isAcitve = isActive;
this.marker = marker;
}
}
Create two players to start with public static Player player1 = new Player(true, '*');
public static Player player2 = new Player(false, '0');
And You have to take care of
In what seqence you call go() for each Player
I dont know what is exact requirement. I think you can proceed with this code.