Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You can find a copy of java implementation of the minimax version of the tic-tic

ID: 669396 • Letter: Y

Question

You can find a copy of java implementation of the minimax version of the tic-tic-toe
zipped with this document.
The objective of this assignment is for you to identify a scoring technique that is better
than the one presented in class and implemented by the attached code.   You will then
have to modify my code to include your scoring technique. Please note that you may not
alter the mini-max algorithm, just the scoring of the leaf node.
Please provide me a typed description of your solution and an explanation of why you
think your scoring technique is better than the one presented in class. hand me a hard copy of your code with
your modifications highlighted on the hard copy.

Package tic-tac-toe;

public class TicTacToeMain {

public TicTacToeMain() {

}

public static void main(String[] args) throws Exception{

    TicTacToeMain ticTacToeMain1 = new TicTacToeMain();

    ticTacToeMain1.userInstruct();

    TicTacToe tictactoe;

    do{

      tictactoe = new TicTacToe();

      while (!(tictactoe.checkForWin() || tictactoe.getTurn() == 10)){

        if((tictactoe.getComputerPlaysOddTurns() && tictactoe.getTurn()%2 == 1) || //computer plays odd turns and this is an odd turn

          (!tictactoe.getComputerPlaysOddTurns() && tictactoe.getTurn()%2 == 0)) //computer plays even turns and this is an even turn

          tictactoe.playAHandForComputer();

        else

          tictactoe.playAHandForUser();

          tictactoe.display();

      }

      tictactoe.display();

      if(tictactoe.getTurn() == 10)

        System.out.println("Tie Game!");

      else{

        if((tictactoe.getComputerPlaysOddTurns() && (tictactoe.getTurn()-1)%2 == 1) || //computer plays odd turns and this is an odd turn

             (!tictactoe.getComputerPlaysOddTurns() && (tictactoe.getTurn()-1)%2 == 0)) //computer plays even turns and this is an even turn

            System.out.println("I WIN! Best luck next time.");

        else

            System.out.println("YOU WIN :-(");

      }

    }while (tictactoe.userWantsToPlayAnotherGame());

  return;

}

void userInstruct(){

          System.out.println("Welcome to the game of tic-tac-toe");

          System.out.println("PLEASE NOTE that X will always play first.");

          System.out.println("GOOD LUCK!");

          return;

}

}

Explanation / Answer

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
class Point
{
int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "[" + x + ", " + y + "]";
}
}
class PointAndScore
{
int score;
Point point;
PointAndScore(int score, Point point)
{
this.score = score;
this.point = point;
}
}
class Board
{
List<Point> availablePoints;
Scanner scan = new Scanner(System.in);
int[][] board = new int[3][3];
public Board()
{
}
public boolean isGameOver()
{
return (hasXWon() || hasOWon() || getAvailableStates().isEmpty());
}
public boolean hasXWon()
{
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 1) || (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 1))
{
//System.out.println("X Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i)
{
if (((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 1)|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 1)))
{
// System.out.println("X Row or Column win");
return true;
}
}
return false;
}
public boolean hasOWon()
{
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 2) || (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 2))
{
// System.out.println("O Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i)
{
if ((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 2)|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 2))
{
// System.out.println("O Row or Column win");
return true;
}
}
return false;
}
public List<Point> getAvailableStates()
{
availablePoints = new ArrayList<>();
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (board[i][j] == 0)
{
availablePoints.add(new Point(i, j));
}
}
}
return availablePoints;
}
public void placeAMove(Point point, int player)
{
board[point.x][point.y] = player;  
}
void takeHumanInput()
{
System.out.println("Your move: ");
int x = scan.nextInt();
int y = scan.nextInt();
Point point = new Point(x, y);
placeAMove(point, 2);
}
public void displayBoard()
{
System.out.println();
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
Point computersMove;
public int minimax(int depth, int turn)
{
if (hasXWon()) return +1;
if (hasOWon()) return -1;
List<Point> pointsAvailable = getAvailableStates();
if (pointsAvailable.isEmpty()) return 0;
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int i = 0; i < pointsAvailable.size(); ++i)
{
Point point = pointsAvailable.get(i);  
if (turn == 1)
{
placeAMove(point, 1);
int currentScore = minimax(depth + 1, 2);
max = Math.max(currentScore, max);
if(depth == 0)System.out.println("Score for position "+(i+1)+" = "+currentScore);
if(currentScore >= 0){ if(depth == 0) computersMove = point;
}
if(currentScore == 1){board[point.x][point.y] = 0; break;
}
if(i == pointsAvailable.size()-1 && max < 0){if(depth == 0)computersMove = point;
}
}
else if (turn == 2)
{
placeAMove(point, 2);
int currentScore = minimax(depth + 1, 1);
min = Math.min(currentScore, min);
if(min == -1){board[point.x][point.y] = 0; break;
}
}
board[point.x][point.y] = 0;
}
return turn == 1?max:min;
}
}
public class TicTacToe
{
public static void main(String[] args)
{
Board b = new Board();
Random rand = new Random();
b.displayBoard();
System.out.println("Select turn: 1. Computer 2. User: ");
int choice = b.scan.nextInt();
if(choice == 1)
{
Point p = new Point(rand.nextInt(3), rand.nextInt(3));
b.placeAMove(p, 1);
b.displayBoard();
}
while (!b.isGameOver())
{
System.out.println("Your move: ");
Point userMove = new Point(b.scan.nextInt(), b.scan.nextInt());
b.placeAMove(userMove, 2);
b.displayBoard();
if (b.isGameOver()) break;
b.minimax(0, 1);
b.placeAMove(b.computersMove, 1);
b.displayBoard();
}
if (b.hasXWon())
System.out.println("Unfortunately, you lost!");
else if (b.hasOWon())
System.out.println("You win!");
else
System.out.println("It's a draw!");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote