What will I Learn?You will learn how to use variables,loops and arraysYou will learn just how to do a tic tac toe gameRequirementsKnowledge of coding ~ above Java and also EclipseEclipse and Java need to be setted increase on the computer.DifficultyIntermediateTutorial ContentsHello,in this tutorial we are going to make a player vs computer system tic-tac-toe video game by utilizing java top top eclipse.First i identified my required one heat library then called my program.After i identified integer kind variables in a,b,c which is mine rows and also a1,c3 etc. Is my columns,then i characterized a brand-new static called "Scanner" to scan wherein user and computer is choosed to make their move.

You are watching: Tic tac toe java code against computer

import java.util.Scanner;public course TicTacToeGameByDrewie{static int A1, A2, A3, B1, B2, B3, C1, C2, C3;static Scanner sc = brand-new Scanner(System.in);public static void main(String<> args){In the following step i defined my strings come welcome the user,and display user moves.In the below i used Boolean,this function is provided to define True/false situation.GameisWon variable is false at the line because game has actually not began yet.

public static void main(String<> args)String notice = "Welcome to Tic-Tac-Toe Game. Please enter your first move: ";String humanMove = "";String computerMove = "";boolean gameIsWon = false; now we room going to use for and also if loop together.We do ifor (int ns = 1; ns

*

Now game is beggining.First of all we develop a course to acquire users relocate if user do a dorn input for example "a4" this is a wrong input,we will certainly warn user.Then us create one more class,if the entry is valid we specify all the rows and columns v using "equalsignorecase" this method is usually compares the strings that you have actually defined.We same them all to zero for this reason if the user or computer system make a motion it will change.

public static String getMove(String prompt) string play; System.out.print(prompt); do pat = sc.nextLine(); if (!isValidPlay(play)) System.out.println("That is not a precious play."); when (!isValidPlay(play)); return play; public revolution boolean isValidPlay(String play) if (play.equalsIgnoreCase("A1") & A1 == 0) return true; if (play.equalsIgnoreCase("A2") & A2 == 0) return true; if (play.equalsIgnoreCase("A3") & A3 == 0) return true; if (play.equalsIgnoreCase("B1") & B1 == 0) return true; if (play.equalsIgnoreCase("B2") & B2 == 0) return true; if (play.equalsIgnoreCase("B3") & B3 == 0) return true; if (play.equalsIgnoreCase("C1") & C1 == 0) return true; if (play.equalsIgnoreCase("C2") & C2 == 0) return true; if (play.equalsIgnoreCase("C3") & C3 == 0) return true; return false;

*

Now until here user and also computer has actually made their move now we have to present it to user and update it.To do this we use "equalsignorecase" again.Basically we define users movement to the routine with analyzing all the possibilities.

public revolution void updateBoard(String play, int player) if (play.equalsIgnoreCase("A1")) A1 = player; if (play.equalsIgnoreCase("A2")) A2 = player; if (play.equalsIgnoreCase("A3")) A3 = player; if (play.equalsIgnoreCase("B1")) B1 = player; if (play.equalsIgnoreCase("B2")) B2 = player; if (play.equalsIgnoreCase("B3")) B3 = player; if (play.equalsIgnoreCase("C1")) C1 = player; if (play.equalsIgnoreCase("C2")) C2 = player; if (play.equalsIgnoreCase("C3")) C3 = player; In this step since of us haven"t do an applet,we have to attract a tic-tac-toe video game for the visuality.With utilizing system.print.out together you recognize we inquiry user to make first move over after user select where to relocate for instance "a1" this present will attract a X come a1.

Then we must define to the routine who is playing.To carry out that we create another class and as you understand we defined "square".This is to uncover out whos rotate is it.If that is customers turn program will draw X if no it will draw "O".

public revolution void displayBoard() " + getXO(B2) + " public revolution String getXO(int square) if (square == 1) return "X"; if (square == 2) return "O"; return " ";

*

In the final step we perform same point we did for the user.We define computers move come the routine with developing a class and using if loop to advice the possibilities and also define every line and also column and also equal come zero.

See more: Which Leader Turned The Tuskegee Institute Into A Model School Answers Com?

Then we create another class to specify the possibilites that winning.You have the right to win with making 3 heat cross,vertical or horizontal therefore we define all the possibilities and also if user or computer system made it through the 3 lines appropriately we use "return true" i beg your pardon will show the message whoever won.It have the right to be a attract so if any of situations didn"t take place it will certainly return false "draw" message.

public revolution String getComputerMove() if (A1 == 0) return "A1"; if (A2 == 0) return "A2"; if (A3 == 0) return "A3"; if (B1 == 0) return "B1"; if (B2 == 0) return "B2"; if (B3 == 0) return "B3"; if (C1 == 0) return "C1"; if (C2 == 0) return "C2"; if (C3 == 0) return "C3"; return ""; public revolution boolean isGameWon() if (isRowWon(A1, A2, A3)) return true; if (isRowWon(B1, B2, B3)) return true; if (isRowWon(C1, C2, C3)) return true; if (isRowWon(A1, B1, C1)) return true; if (isRowWon(A2, B2, C2)) return true; if (isRowWon(A3, B3, C3)) return true; if (isRowWon(A1, B2, C3)) return true; if (isRowWon(A3, B2, C1)) return true; return false; public static boolean isRowWon(int a, int b, int c) return ((a == b) & (a == c) & (a != 0));

*

Whole code v the screenshots and also output

income java.util.Scanner;public course TicTacToeGameByDrewie{ revolution int A1, A2, A3, B1, B2, B3, C1, C2, C3; revolution Scanner sc = new Scanner(System.in); public revolution void main(String<> args) { cable prompt = "Welcome to Tic-Tac-Toe Game. Please enter your an initial move: "; string humanMove = ""; wire computerMove = ""; boolean gameIsWon = false; for (int i = 1; ns

*