Using Java program this: /* This program is preparing to play a Tic-tac-toe game. For now, it should only place X-s on the board in the place specified by the user on each turn. Here, you should implement the method printBoard() that should print a "board" passed to it. Notice that the board is SHIFTED to the right a bit. You should do it too! Don't change your main. Eventually your output should look something like this: 1 2 3 1 . . . 2 . . . 3 . . . Where do you want to place X? Enter row and column (separated by blank space): 2 2 1 2 3 1 . . . 2 . X . 3 . . . Where do you want to place X? Enter row and column (separated by blank space): 3 3 1 2 3 1 . . . 2 . X . 3 . . X Where do you want to place X? Enter row and column (separated by blank space): ... ... and so on. */ You can use this and complete the program: import java.util.Scanner; public class PrepTicTacToe1{ public static void printBoard(String[][] board){ // Print each element of the board as shown above } public static void main(String[] args){ Scanner scan = new Scanner(System.in); String board [][]={ {" ","1","2","3"}, {"1",".",".","."}, {"2",".",".","."}, {"3",".",".","."} }; int row= 0; int col= 0; do{ printBoard(board); System.out.println("Where do you want to place X?"); System.out.print("Enter row and column (separated by blank space): "); row = scan.nextInt(); col = scan.nextInt(); if (row>3 || row<1 || col>3 || col<1){ System.out.println("...........................................Wrong input"); continue; } board[row][col]="X"; }while(true); } }
Answer:
Java Source Code:
import java.util.Scanner;
class Main {
public static void printBoard(String [][] board)
{
System.out.print("\n");
for(int x = 0;x < 4; x++)
{
for(int y = 0; y < 4; y++)
{
System.out.print(board[x][y]+" ");
}
System.out.print("\n");
}
}
public static void main(String args[] ) {
Scanner scan= new Scanner(System.in);
String board [][]={{" ","1","2","3"},
{"1",".",".","."},
{"2",".",".","."},
{"3",".",".","."}};
int row=0;
int col=0;
do{
printBoard(board);
System.out.println("Where do you want to place X?");
System.out.print("Enter row and column (separated by blank space): ");
row=scan.nextInt();
col=scan.nextInt();
if(row>3||row<1||col>3||col<1)
{
System.out.println("...............................Wrong Input");
continue;
}
board[row][col]="X";
}while(true);
}
}
Step by step
Solved in 2 steps with 1 images