Question
This is the code that I got for a two player Tic Tac Toe. JAVA Programming import java.util.Scanner; public class TicTacToe { public static void
This is the code that I got for a two player Tic Tac Toe.
JAVA Programming
import java.util.Scanner;
public class TicTacToe { public static void main(String[] args) { Scanner input = new Scanner(System.in);
String board[][] = {{"1","2","3"}, {"4","5","6"}, {"7","8","9"}};
String player="X"; while(checkWin(board)!=true){ System.out.println("This is the current layout..."); printBoard(board); System.out.println("It's the turn of "+player); System.out.print("Choose a position (1-9): "); int choice = input.nextInt(); while(positionFree(board, choice)!=true){ System.out.print("The position choosed is already taken. Choose other position: "); choice = input.nextInt(); } board = insertMove(board,player,choice); if(player=="X") player = "O"; else player = "X"; } if(player=="X") player = "O"; else player = "X"; printBoard(board); System.out.println("Player "+player+" wins..."); } public static void printBoard(String[][] board) { for(int i=0;i
public static boolean positionFree(String[][] board, int choice) { int x = (choice-1)/3; int y = (choice-1)%3;
if(board[x][y]!="X" && board[x][y]!="O") return true; else return false; } public static String[][] insertMove(String[][] board, String player, int choice) { int x = (choice-1)/3; int y = (choice-1)%3;
board[x][y] = player; return board; } public static boolean checkWin(String[][] board) { for(int i=0;i Part 2: Tic-Tac-Toe Computer Player For the second part we will build a basic computer player using a random number generator. How to modify the program from part1 Ask the user how many players will be playing. If there is only 1, a computer player should be added. The human will always play as "X" and go first. Modify your program so that after the human makes a selection, the computer will make a selection of the remaining places. The computer should not be able to overwrite an already used position and should make a random choice. The computer does not need any special logic beyond randomly picking an available position 1. 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started