How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask LogicPro Your Own Question

LogicPro
LogicPro, Computer Software Engineer
Category: Programming
Satisfied Customers: 9350
Experience:  Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
Type Your Programming Question Here...
LogicPro is online now
A new question is answered every 9 seconds

The TicTacToe class contains a 3x3 two-dimensional array of

Customer Question

The TicTacToe class contains a 3x3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. It should have a method playerMove that accepts two integers as parameters, the first is the player (1 or 2) and the second is the actual move (1-9). If it is a valid move it changes the value of that square to 1 or 2 (depending on whose turn it is and returns the Boolean value true to indicate a valid move has been completed). If it is not a valid move the board does not change and it returns a Boolean value of false to indicate an invalid move. TicTacToe should also have the method displayBoard that will display the current state of the board. The last method should be determineWinner which returns a 1 if player one won, a 2 if player 2 won, a 0 if it’s a ‘cat’s game’, and -1 if there is no winner yet and the game is not over.

implement the TicTacToe class and create a test class for it. Your test class should demonstrate the capabilities (fully test the functionality) of the TicTacToe class
Submitted: 3 years ago.
Category: Programming
Expert:  LogicPro replied 3 years ago.
Hi,
Welcome to JustAnswer.
If you have any file(s). Zip, upload the file to http://wikisend.com and post download link here.
Customer: replied 3 years ago.
having trouble trying to send you what i got on file
Expert:  LogicPro replied 3 years ago.
I can write it from scratch. let me know if it is okay?
Customer: replied 3 years ago.
K! here is some of it

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author percy
*/
public class TicTacToe {
//attributes
int[][] board = new int [3][3];
private int player;

//constructor
public TicTacToe() {


for(int row=0; row<3; row++) {
for(int col=0; col<3; col++) {
board[row][col] = 0;
}
}
}


/**
* @return the player
*/
public int getPlayer() {

return player;
}

/**
* @param player the player to set
*/
public void setPlayer(int player) {
this.player = player;
}




public boolean playerMove(int row, int col ){
boolean move;
//check to see if the move has been already taken
if (board[row][col] == 0)
{
board[row][col] = player;

return true;
}
else
{
// The specified cell is already occupied.
move = false;
}
//check to see if the move is a number 1 through 9
//if the move is valid then
//do the move
//isValid = true;
//else don't do the move
//isValid = false;
return move;
}

public void switchPlayer(){
if (player ==1){
player = 2;
}
else {
player =1;
}
}












public boolean isGameOver(){

boolean gameOver = true;

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




public void displayBoard () {

//use nested for loops to print out your 3x3 array
System.out.println("Tic Tac Toe Game\n" + "-----------");
for(int i = 0; i < 3; i++){

for (int j = 0; j < 3; j++){

System.out.print(board[j] + " | ");
}
System.out.println("\n-----------");

}

}




public int determineWinner() {
int winner =0;
//check each row for a winner
if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != 0 ){

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

winner = board[1][0];
}
// check row 3 for winner
if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != 0 ){

winner = board[2][0];
}
//check each column for a winner
// check column 1 for winner
if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0] != 0 ){

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

winner = board[0][1];
}
// check column 3 for winner
if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != 0 ){

winner = board[0][2];
}

//check each diagnoal for a winner
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0 ){

winner = board[0][0];
}
// check second diagonal for winner
if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != 0 ){

winner = board[0][2];

}
return winner;
}

// Display board after each input
public void showBoard(){

System.out.println("Tic Tac Toe Game\n" + "-----------");
for(int i = 0; i < 3; i++){

for (int j = 0; j < 3; j++){

System.out.print(board[j] + " | ");
}
System.out.println("\n-----------");

}

}

}
Expert:  LogicPro replied 3 years ago.
Download this zip file
Unzip to get your project.
Ask me if you need more information.
Customer: replied 3 years ago.
You covered everything except how to do test method for "determineWinner". I have been trying but cannot figure how to test it. can you help with the last part? I will then gladly accept your answer consider a bonus.
Expert:  LogicPro replied 3 years ago.
I will check and let you know asap.
Customer: replied 3 years ago.
K
Expert:  LogicPro replied 3 years ago.
Download this zip file.
Unzip to get netbeans project.

Replace existing project with this one.
Ask me if you need more information.
Customer: replied 3 years ago.
Im sorry it did not help. determineWinner is like the the hardest part and I have spent a day already trying to figure it out. Hoping you can help because I need this to help build on the the "tic tac toe game with the tictactoe object."

"The last method should be determineWinner which returns a 1 if player one won, a 2 if player 2 won, a 0 if it’s a ‘cat’s game’, and -1 if there is no winner yet and the game is not over."

Expert:  LogicPro replied 3 years ago.
Could you elaborate these two scenarios:
0 if it’s a ‘cat’s game’, and -1 if there is no winner yet and the game is not over."
Customer: replied 3 years ago.
I just need it to show one winner.

"

TicTacToeTest

You also need to implement a test class, TicTacToeTest, for your TicTacToe class. Make a few calls to playerMove() to make sure it works. Make at least one call to determineWinner() to make sure it works. Make at least one call to displayboard() to make sure it works."

 

 

every thing works except the one thats underlined. it does not have to call them all just one.

the "0" for cats game mean it is a tye game and -1 means that the game is still going and not finished yet.
Expert:  LogicPro replied 3 years ago.
The method determineWinner() is working as expected.
Please put the statement
System.out.println("game.determineWinner()="+game.determineWinner());
after while loop in play() method of TicTacToeGame.java to see the output of determineWinner() method

You can remove it later on
Customer: replied 3 years ago.
my friend i have a tic tac toe class and a tic tac toe test class. arey you saying this statement, "System.out.println("game.determineWinner()="+game.determineWinner());" is supposed to go in the test class.

Im using the first link you gave me which is: Im confused as to where you are saying to put it.

import java.util.Arrays;

public class TicTacToe {
/* [1] [2] [3]
[4] [5] [6]
[7] [8] [9] */

private static int[] board = new int[9];

// Default Constructor - initialize the board with zeroes
TicTacToe() {
Arrays.fill(board, 0);
}

// player: 1 or 2
// move: board position (1-9) to put a move
// return true on success
public boolean playerMove(int player, int move) {
// check for valid input
if (player != 1 && player != 2 || move <= 0 || move > 9) {
return false;
}

// check if the cell is busy or assigned
if (board[move - 1] != 0) {
return false;
}

// perform move
board[move - 1] = player;
return true;
}

/* * Display the board */
public void displayBoard() {
for (int i = 0; i < board.length; ++i) {
System.out.print('|');
switch (board) {
case 1:
System.out.print('X');
break;
case 2:
System.out.print('O');
break;
default:
System.out.print(' ');
break;
}

if ((i+1) % 3 == 0) {
System.out.print('|');
System.out.println();
}
}
}

}
Expert:  LogicPro replied 3 years ago.
Download this zip file.
Unzip to get netbeans project.

Then put the statement
System.out.println("game.determineWinner()="+game.determineWinner());
after while loop in play() method of TicTacToeGame.java to see the output of determineWinner() method
Customer: replied 3 years ago.
I feel really dumb here but when i try to run this it says there is no main method in either one of them
Expert:  LogicPro replied 3 years ago.
Main.java contains the main() method. Run Main.java
Customer: replied 3 years ago.
So that means I would have to change the name of the class to run Main.java
Expert:  LogicPro replied 3 years ago.
No,
The zip file contains 3 files:
1. Main.java
2. TicTacToe.java
3. TicTacToeGame.java

So, run the Main.java
Customer: replied 3 years ago.
Oh OK! I did not know I had to load the Main.java. It works now that is the coolest thing I ever seen! Thank you so much. I will send an accept answer now and throw in a bonus for you. Thanks for being so patient with me. I feel like my instructors are just thrown us to the wolves. Where did you learn to write codes?
Expert:  LogicPro replied 3 years ago.
Thank you very much and it is just a matter of practice.
LogicPro, Computer Software Engineer
Category: Programming
Satisfied Customers: 9350
Experience: Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
LogicPro and 5 other Programming Specialists are ready to help you
Customer: replied 3 years ago.
"For LogicPro only" The constructor should initialize the empty board to all zeros. Hey LogicPro I hope you remember me from the tic tac toe question. I have to turn this in assignment in tonight. I just realize this last night that the board has to be initialized to all zeros. I tried to fix it but I could not. you think you could help me out with that.
Expert:  LogicPro replied 3 years ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.

Customer: replied 3 years ago.
ok it says in the constructor// Default Constructor - initialize the board with zeroes
TicTacToe() {
Arrays.fill(board, 0);
but when I run it I get a tic tac toe box. so are you saying that the tic tac toe box is the same as showing 0's.


View Full Image
View Full Image
Expert:  LogicPro replied 3 years ago.
Yes. You are right as it is not logical to display board filled with 0s
I have also provide you the answer of Nim game and you have seen it.
Customer: replied 3 years ago.
yes I know i am looking at it right now! I have a question on that one. It says it should have Nim class and a NimGame application. doesnt that mean it shouild have a nim class and a nim game application
Expert:  LogicPro replied 3 years ago.
I will reply on your other question thread to track this communication.

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)