As instructed by jim, I am reposting my question with work done so far...

In this lab, you will demonstrate your understanding of two-dimensional arrays by implementing a TicTacToe game. The service class created in this lab will be utilized later in the semester as we move into subclasses and inheritance.

Service Class

TicTacToe

Develop a TicTacToe class that manages a TicTacToe game. The class should define a two-dimensional array of characters named board. Two constructors are defined. The default constructor should set the player names to "Player 1" and "Player 2", set markers for the two players to 'X' and 'O', note that player1 goes first, and set the game board to its initial state. The overloaded constructor should accept player names and their selected character markers. The first player listed goes first. The game should also track the number of valid plays to determine when a tie has occurred (only 9 squares can be marked.)

The initial game state marks each square (element of the array) with an indicator that the player can use to identify the square. For example, the squares below have the characters '1' through '9' stored in the array:

1 2 3
4 5 6
7 8 9

Although the array stores characters, the player will be able to enter an integer value to select a square. For example, a value of 1 will allow a player to select the upper left square. When the player using an 'X' marker has select square 1, the game board will store:

X 2 3
4 5 6
7 8 9

In addition to accessor and mutator methods, the class should define methods to switch from player 1's control to player 2 (and vice versa), place a marker on the board, determine whether there is a winner or tied game, and construct strings containing the rules of the game, user prompts, and the display of the game board. For methods that return a boolean, the value indicates whether the action was successful or not.

There should be no I/O in this class; Strings are composed and returned to the caller (client application). It is the caller's responsibility to display the returned String.

Class UML diagram (constructors, accessor, and mutator methods not listed):

TicTacToe

- board : char[][]
- player1 : String
- player2 : String
- currentPlayer : int
- marker1 : char
- marker2 : char
- plays : int
+ switchPlayers( )
+ placeMarker(int) : boolean
+ tie( ) : boolean
+ winner( ) : boolean
+ getRules( ) : String
+ getPrompt( ) : String
+ drawBoard() : String



Client Application:

Demonstrate the TicTacToe class in an application that allows users to play the game of TicTacToe. Players will play by alternating the use of a single keyboard.

The application should display a welcome message and query for any set up information needed. Display of the game board is helpful as the game progresses. A sample run of the client application is below:

Sample run of the application:

Welcome! Tic Tac Toe is a two player game.
Enter player one's name: Trish
Enter player two's name: Pat

Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!

Game Board
1|2|3
4|5|6
7|8|9

It is Trish's turn. Pick a square: 1

Game Board
X|2|3
4|5|6
7|8|9

It is Pat's turn. Pick a square: 2

Game Board
X|O|3
4|5|6
7|8|9

It is Trish's turn. Pick a square: 1
Square can not be selected. Retry

Game Board
X|O|3
4|5|6
7|8|9

It is Trish's turn. Pick a square: 4

... game continues until finally

Game Board
X|O|X
X|O|O
7|X|O

It is Trish's turn. Pick a square:
7

Game Over - Trish WINS!!!

Game Board
X|O|X
X|O|O
X|X|O





NOTES:

1.You will be developing TWO classes: the TicTacToe class and the Client class. The Client class contains the main method and should be named Lab2.
2.There should be no I/O performed in the TicTacToe class. All interaction with the end user should be done in the Client class. However, you will use methods of the TicTacToe class to generate the text. For example, when the client creates an instance of the TicTacToe class named myGame, the game prompt can be displayed from the client application to the end-user using:
System.out.print(myGame.getPrompt());
3.All prompts and display formats are up to you; the exact text in the above sample run is not required.
4.You may use any method for indicating a square. Just be sure the players understand how to pick a square.
5.Do not allow a player to select a square already marked. The player does not lose a turn when an illegal square is selected.
6.You may define additional methods in the TicTacToe class as needed, but you really shouldn't need any.
7.Warning: You must use the TicTacToe data members and method signatures listed in the UML diagram. The TicTacToe class will be reused in a later lab requiring the data members and method signatures listed. If you change these items, you will have to rewrite this class later. Not fun...
8.Hint: On the above sample run, make notes as to the order of the TicTacToe method calls. This should help you write the client.
9.Optional: When a game completes, provide an option to play again.
10.Optional: Allow each player to select a marker, either their own or from a list of available markers.
11.The TicTacToe class should be documented using Javadocs. Javadocs are not needed for the client class, although your name should be included within comments at the top of the program.
12.Adhere to the Style Guide for both classes.
Evaluation:

Classes must compile without syntax errors or will not be evaluated. This lab is worth 25 points. Points will be distributed as follows:

•TicTacToe class (18 points)
◦Use of a two-dimensional array
◦Definition of constructors, accessors, and mutators to support class fields
◦Definition of methods that support the objectives of the class
◦Methods produce correct results
◦Methods contain no end-user I/O
◦Code aheres to the style guide
•Client class (6 points)
◦Supports communication with the end-user
◦Main method utilizes the TicTacToe service class to meet the objectives of the application
◦Code aheres to the style guide
•On time (late penalty of 7 points assessed for projects submitted late)

My Client Application so far :
Under megaupload, extension ?d=3HZW436R

My Game Application so far:
Under megaupload, extension ?d=1BMXLN3F

Nice to see you back, Tim.

I'll be away from the forum until tonight, but somebody else may help in the meantime.

To help them help you, you could post your thinking, given your current code. Where are you stuck? What doesn't seem to be working that you think should be working? What don't you understand? Like that. It'll save them answering the wrong questions.

I'll be on after 10 am. So feel free to follow Jim's recommendations if and when you'll be ready.

Just home and had a quick look.

My two immediate thoughts:

1. You need to write getCurrentPlayer() in TicTacToe so that you can get the whole thing to start running, from where you can get a handle on what it is doing.

2. I still don't think the instructions I/O should be in TicTacToe class. Note 2 in your instructions seems to say to me that the instructions may/should be returned from TicTacToe to Lab2 as a string, and then Lab2 should be doing the actual System.out.println. But that's not critical at the moment.

Ok so I think I got a handle on the getCurrentPLayer() method....

char mark;
if (currentPlayer == 1)
mark = marker1;
else
mark = marker2;

I think you want a String rather than a char.

Here's what I did:

1. I made a little batch file to recompile and rerun, containing
javac TicTacToe.java
jar cvf TicTacToe.jar TicTacToe.class
javac -cp .\TicTacToe.jar Lab2.java
java -classpath . Lab2

I don't know whether you're using an IDE, but Java is long gone from my machine, so I'm just using the JDK from a command prompt. If you're using an IDE, you may be doing this differently.

2. I added this method to TicTacToe:
public String getCurrentPlayer()
{
String playername;
if (currentPlayer == 1)
playername = player1;
else
playername = player2;
return playername;
}

and I replaced the last few lines in Lab2 - the else after Tie Game - with

System.out.println("And the Winner is... " + myGame.getCurrentPlayer());

to reflect the change.

Now when I run it, I get:

C:\Program Files\Java\jdk1.6.0_16\bin>maketic

C:\Program Files\Java\jdk1.6.0_16\bin>javac TicTacToe.java

C:\Program Files\Java\jdk1.6.0_16\bin>jar cvf TicTacToe.jar TicTacToe.class
added manifest
adding: TicTacToe.class(in = 2644) (out= 1399)(deflated 47%)

C:\Program Files\Java\jdk1.6.0_16\bin>javac -cp .\TicTacToe.jar Lab2.java

C:\Program Files\Java\jdk1.6.0_16\bin>java -classpath . Lab2
Welcome! Tic Tac Toe is a two player game.
Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!
Player Player 2
Make your move.
Row please (0-2):1
Col please (0-2):1
Welcome! Tic Tac Toe is a two player game.
Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!
Player Player 2
Make your move.
Row please (0-2):1
Col please (0-2):1
Invalid Move... Try Again.
Row please (0-2):0
Col please (0-2):0
Welcome! Tic Tac Toe is a two player game.
Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!
Player Player 2
Make your move.
Row please (0-2):

Not a finished program, but there are good signs:

1. It's running!!! This is the hardest part. :-)

2. It knows that I made an invalid move.

Personally, I'd concentrate next on fixing the bug that throws the instructions on every turn, and get into showing the board instead.

OK, doing what I said last is really easy. Just move the two displays abve the loop, and actually draw the board instead of just returning it, like:

myGame.getPrompt();
myGame.getRules();

while(myGame.winner() == 0 && !myGame.placeMarker())
{
System.out.println(myGame.drawBoard());

Nw I get:

Welcome! Tic Tac Toe is a two player game.
Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!

------
------
------
Player Player 2
Make your move.
Row please (0-2):1
Col please (0-2):1

------ ☺
------
------
Player Player 2
Make your move.
Row please (0-2):

You're putting integer 1 or 0 into board in tie, which I don't quite understand. I would think that marker1 or marker2 would be what you want here. I also don't quite get why you're putting this into the tie method.

Personally, I'd prefer logic like:

in Lab2:
input move
call makemove(x, y, player) - check for error
then in TicTacToe
makemove(x, y, player)
{
check valid - if not, return error code
board[x][y] = marker[player] (you can use an "if" for player rather than an array, if you prefer)
}

or maybe use placeMarker for what I'm calling makemove

I'll be away until tonight. How long do you have for this project?

I have until tomorrow evening to finish, but that really shouldn't be a problem. I have to wait until tonight to work on it, since I'm not on my home computer atm.

Given the short deadline, and that I'll be away for a while, I hacked nastily into your code, to produce something that runs all the way through, I think.

I want to emphasise just how ugly this is, and that it needs a lot of work to be a worthy project! But it runs, and if you spend a little time with it, you should be able to make something of it.

I compile and run it using the little batch file I cited earlier, using just a downloaded JDK. You'll probably want to change that as well.

If you have any more questions, ask, and if I get to it in time, I'll help out. Just don't ask me _why_ I did anything some specific way: the answer will be because I had half-an-hour, and was working from the existing code. :-)

Welcome! Tic Tac Toe is a two player game.
Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, they win! If all squares
are marked and no three squares are the same, a tied game is declared.
Have Fun!
---
---
---

Player Player 1
Make your move.
Row please (0-2):1
Col please (0-2):1
---
-X-
---

Player Player 2
Make your move.
Row please (0-2):2
Col please (0-2):2
---
-X-
--O

Player Player 1
Make your move.
Row please (0-2):0
Col please (0-2):0
X--
-X-
--O

Player Player 2
Make your move.
Row please (0-2):0
Col please (0-2):1
XO-
-X-
--O

Player Player 1
Make your move.
Row please (0-2):2
Col please (0-2):1
XO-
-X-
-XO

Player Player 2
Make your move.
Row please (0-2):0
Col please (0-2):2
XOO
-X-
-XO

Player Player 1
Make your move.
Row please (0-2):2
Col please (0-2):0
XOO
-X-
XXO

Player Player 2
Make your move.
Row please (0-2):1
Col please (0-2):0
XOO
OX-
XXO

Player Player 1
Make your move.
Row please (0-2):1
Col please (0-2):1
XOO
OX-
XXO

Player Player 1
Make your move.
Row please (0-2):1
Col please (0-2):2
Tie Game

import java.util.Scanner;

public class Lab2
{

public static void main(String[] args)
{
TicTacToe myGame = new TicTacToe();
Scanner input = new Scanner(System.in);
int row;
int col;

myGame.getPrompt();
myGame.getRules();

while(myGame.winner() == "" && !myGame.tie())
{
System.out.println(myGame.drawBoard());

System.out.println("Player " + myGame.getCurrentPlayer());
System.out.println("Make your move.");
System.out.print("Row please (0-2):");
row = input.nextInt();

while(row < 0 || row > 2)
{
System.out.println("Invalid Row.");
System.out.print("Try again (0-2):");
row = input.nextInt();
}

System.out.print("Col please (0-2):");
col = input.nextInt();

while(col < 0 || col > 2)
{
System.out.println("Invalid Col.");
System.out.print("Try again (0-2):");
col = input.nextInt();
}

if (!myGame.makemove(row, col)) continue;

}

if (myGame.winner() == "")
{
System.out.println("Tie Game");
}
else
{
System.out.println("And the Winner is... " + myGame.winner());
}
}

}

public class TicTacToe
{

String player1;
String player2;
char board[][];
int currentPlayer;
char marker1;
char marker2;
char emptysquare;
int plays;

public TicTacToe()
{
player1 = "Player 1";
player2 = "Player 2";
marker1 = 'X';
marker2 = 'O';
emptysquare = '-';

board = new char[3][3];
currentPlayer = 1;
for (int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
board[i][j] = emptysquare;
}
}
}

public TicTacToe(String play1, String play2, char mark1, char mark2)
{
play1 = player1;
play2 = player2;
mark1 = marker1;
mark2 = marker2;
currentPlayer = 1;
emptysquare = '-';

for (int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
board[i][j] = emptysquare;
}
}

}

public boolean makemove(int row, int col)
{

boolean move;

if (board[row][col] == marker1 || board[row][col] == marker2 )
{
move = false;
}
else
{
if (currentPlayer == 1)
{
board[row][col] = marker1;
currentPlayer = 2;
}
else
{
board[row][col] = marker2;
currentPlayer = 1;
}
move = true;

}
return move;
}

public boolean tie()
{
boolean complete = true;

for (int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
if (board[i][j] != marker1 && board[i][j] != marker2)
{
complete = false;
}
}
}
return complete;
}

public boolean placeMarker()
{
boolean complete = true;

for (int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
if (board[i][j] == 0)
{
complete = false;
}
}
}
return complete;
}

public String winner()
{
char winner = emptysquare;

if (board[0][0] == board[0][1] && board[0][0] == board[0][2] &&
board[0][0] != emptysquare)
{
winner = board[0][0];
}

if (board[1][0] == board[1][1] && board[1][0] == board[1][2] &&
board[1][0] != emptysquare)
{
winner = board[1][0];
}

if (board[2][0] == board[2][1] && board[2][0] == board[2][2] &&
board[2][0] != emptysquare)
{
winner = board[2][0];
}

if (board[0][0] == board[1][0] && board[0][0] == board[2][0] &&
board[0][0] != emptysquare)
{
winner = board[0][0];
}

if (board[0][1] == board[1][1] && board[0][1] == board[2][1] &&
board[0][1] != emptysquare)
{
winner = board[0][1];
}

if (board[0][2] == board[1][2] && board[0][2] == board[2][2] &&
board[0][2] != emptysquare)
{
winner = board[0][2];
}

if (board[0][0] == board[1][1] && board[0][0] == board[2][2] &&
board[0][0] != emptysquare)
{
winner = board[0][0];
}

if (board[2][0] == board[1][1] && board[2][0] == board[0][2] &&
board[2][0] != emptysquare)
{
winner = board[2][0];
}
if (winner == marker1)
return player1;
if (winner == marker2)
return player2;
return "";
}

public String getCurrentPlayer()
{
String playername;
if (currentPlayer == 1)
playername = player1;
else
playername = player2;
return playername;
}

public String drawBoard()
{
String s = "";
for ( int i = 0; i < 3; i++)
{
for( int j = 0; j < 3; j++)
{
s = s + board[i][j];
}
s = s + "\n";
}
return s;
}

public void getRules()
{
System.out.println("Players take turns marking a square. Only squares");
System.out.println("not already marked can be picked. Once a player has");
System.out.println("marked three squares in a row, they win! If all squares");
System.out.println("are marked and no three squares are the same, a tied game is declared.");
System.out.println("Have Fun!");
}

public void getPrompt()
{
System.out.println("Welcome! Tic Tac Toe is a two player game.");
}
}

I think I can take it from here.

But first I would like to say thank you for your time and dedication. Without you , I would of been up the creek without a paddle. lol