Saturday
September 20, 2014

Homework Help: Programming

Posted by Tim on Tuesday, October 13, 2009 at 1:11am.

Hello, I seem to be having some difficulties with an assignement of mine, and pretty much need a fill in the blank for a program I have written for it. Heres the assignment:



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)

Here's what I have so far:


public class TicTacToe
{

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

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

board = new char[3][3];

}

public TicTacToe(String play1, String play2, char mark1, char mark2)
{
play1 = player1;
play2 = player2;
mark1 = marker1;
mark2 = marker2;
}


public boolean tie(int row, int col)
{

boolean move;

if (board[row][col] == 0)
{
board[row][col] = 1;
return true;

}
else
{

move = false;
}
return move;
}

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 int winner()
{
int winner = 0;

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

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

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

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

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

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

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

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

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.");
}
}

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

English - I have an essay about difficulties in life which is based on a book I ...
C++ programming computer - I'm having some trouble trying to program these ...
Math - I really need help with this question please Break-even equation. Fill in...
Computer Programming - Hello, Any help on the following problem would be greatly...
help - Introduction to quadratic equations? If you solve the equation by ...
Algebra 2 - Introduction to quadratic equations? If you solve the equation by ...
Visual Basic (Computer) - In the following examples, identify the condition a ...
Spanish please help - I have no idea how to do these. Will somebody please help...
Spanish: Please check answers - Please check my answers. The nswers are at the ...
PROGRAMMING - design a program for the following. Registration workers at a ...

Search
Members