Question

You are going to write a method (to be called validateBoard) that is going to validate whether or not a Tic-Tac-Toe board is

Problem of the Day The board is represented by a 3 x 3 array that holds strings that represent the game piece at each locatio

Problem of the Day Write the body of the program called PoD.java to the left Details The main method of the program has been

The validateBoard method should take as input a 2-dimensional array that holds strings (representing the 3 x 3 game board whe

Output If the board is determined to be valid, the method will return a boolean value of true. Otherwise, it will return fals

X. . X) [x, o, o} true } { [X. o. .) t1 [x. true (x. 0. o) false

import java.util.*; public class PoD //PLEASE START YOUR WORK HERE 1/PLEASE END YOUR WORK HERE public static void main(String

String[][]ticTacToeBoard = new String[3] [3]; for (int i-0; i<3; i++) { for (int j-0; j<3; j++) { //Read in board ticTacToeBo

System.out.print(ticTacToeBoard [i] [j] + ); } + 11 else System.out.println (ticTacToeBoard [i] [j]); I if (validate Board

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

import java.util.*;
public class PoD {
   public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       int gamepeice;
       String [][] ticTacBoard = new String[3][3];
       for(int i=0;i<3;i++) {
           for(int j=0;j<3;j++) {
              
               ticTacBoard[i][j] = in.next();
          
           if(i<2) {
               System.out.print(ticTacBoard[i][j]+" ");
           }
           else {
               System.out.print(ticTacBoard[i][j]+" ");
           }
       }
       }
       if(validateBoard(ticTacBoard)) {
           System.out.println("valid board");
       }
       else System.out.println("Invalid board");
      
       System.out.println("END OF OUTPUT");
       in.close();
       }

   private static boolean validateBoard(String[][] ticTacBoard) {
       int countx=0;
       int counto = 0;
       for(int i=0;i<3;i++) {
           for(int j=0;j<3;j++) {
               if(ticTacBoard[i][j]=="X"||ticTacBoard[i][j]=="x")countx++;
               if(ticTacBoard[i][j]=="O"||ticTacBoard[i][j]=="o")counto++;
           }
       }
       return (countx==counto ||(countx == counto+1));
   }
  
}

Add a comment
Know the answer?
Add Answer to:
You are going to write a method (to be called validateBoard) that is going to validate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • Hi, I am a student in college who is trying to make a tic tac toe...

    Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Lab 01 Tic Tac Toe Objective: Write a class called TicTacToeBoard which is used by a...

    Lab 01 Tic Tac Toe Objective: Write a class called TicTacToeBoard which is used by a driver to play a game of Tic Tac Toe. Download the driver which contains the game loop and the user input. DO NOT MODIFY THE DRIVER! The class TicTacToeBoard needs to have the following: Instance Variables A 2D 3x3 matrix of Strings called spaces. This will be used to select the where the player put their marker Class Variables (these need to be public...

  • Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending)....

    Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT