Java ULTIMATE TIC-TAC-TOE programming (not 2D array)

Instructions and source codes are attached. most of method, class, interfaces are already exists, most of works are combining and manipulating existing codes

Get Help With a similar task to - Java ULTIMATE TIC-TAC-TOE programming (not 2D array)

Login to view and/or buy answers.. or post an answer
Additional Instructions:

TTTGame.java TTTGame.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; class TTTGame {          private APlayer[] players= new APlayer[2];     private Iboard board;          private String[] marks = {"X", "O"};     private String name = "TicTacToe";          private int gameRowSize = 3;     private int gameColSize = 3;     private int gameScoreToWin = 3;          private int currentPlayerIndex = -1;          public TTTGame() {         setPlayers();         setBoard();     }     private void setPlayers() {         for(int i = 0; i < players.length; i++) {                 ComputerPlayer p = new ComputerPlayer("player" + (i+1), marks[i]);             players[i] = p;         }      }          public void setPlayers(APlayer player1, APlayer player2) {         players[0] = player1;         players[1] = player2;              }     private void setBoard() {         this.board = new Board(gameRowSize, gameColSize, "TTTGame");     }          public void setBoard(Iboard board) {         System.out.println("The Other board will be used!");         this.board = board;     }    /*     1 = start the game message     2 = select the current palyer index     3 = player sould select row and col to place the mark     4 = board should check if the box is available and if so place the mark otherwise repeat the process     5 = then print the board     6 = repeat the steps for the other player until one player win or board is full            */          public void start() {         System.out.println("\ngame started... ");         do {             switchPlayer();             while(!board.makeMove(players[this.currentPlayerIndex].getMark(),                      players[this.currentPlayerIndex].selectRowValue(gameRowSize),                     players[this.currentPlayerIndex].selectColValue(gameColSize))){                            } board.print();         }while(!gameOver());                  // display each message whether the game is tied or not         if(board.isWinner()) {               System.out.println("\n" + players[this.currentPlayerIndex].getName() + " won!");         } else {             System.out.println("\nGame tied!");         }                  System.out.println("Game Over");     }          // TODO     private boolean gameOver() {                   if(board.isFull()){      // if the board is full return true while isWinner() is false              return true;         }                    if(board.isWinner()){   // if there is a winner, return true even though board is not fully filled.              return true;          }                   return false;           // start loop keep looping ustil this method return false.     }          // TODO         // switch player method     private void switchPlayer() {            if(currentPlayerIndex == 1) {   // if player is 1, current player becomes 0, and opposite for player 0.             currentPlayerIndex = 0;                     } else{             currentPlayerIndex = 1;         }                 } } APlayer.java APlayer.java package TTTGame; public abstract class APlayer {        private String name;        private String mark;                public APlayer(String name, String mark) {            setName(name);            setMark(mark);        }                public String getName() {            return name;        }               public void setName(String name) {            this.name = name;        }               public String getMark() {            return mark;        }               public void setMark(String mark) {            this.mark = mark;        }            public abstract int selectRowValue(int range);               public abstract int selectColValue(int range);         } Board.java Board.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class Board implements Iboard {          private Box[] boxes;     private String name;     private int boardRowSize;     private int boardColSize;          Board(){         this(3, 3, "3 * 3 Board");     }          Board(int rowSize, int colSize, String name){         this.setName(name);         this.setSize(rowSize, colSize);     }          public void setSize(int row, int col){                  if(row < 3 || col < 3) {             System.out.println("mon board size is 3 * 3");         } else {         this.boardColSize = col;         this.boardRowSize = row;         }         init();     }          void init() {         boxes = new Box[boardColSize * boardRowSize];         for(int i = 0; i < boxes.length; i++) {             Box b = new Box(i/boardColSize, i%boardColSize);             boxes[i] = b;         }         print();     }          public void setName(String name) {         this.name = name;         }               public void print(){         System.out.println("\nprinting the " + this.name + "_" +                 this.boardRowSize + "*" + this.boardColSize + " board info....");         for(int i = 0; i < boxes.length; i++) {             if(i != 0 && i%boardColSize == 0) System.out.println();                          boxes[i].print();         }              }     //TODO     public boolean makeMove(String mark, int row, int col) {                  int index = 0; //index var with initial value 0                  // calculating the place of array by comparing each row and col         if(row == 0) {             index = col;         } else if(row == 1) {             index = col + 3;         } else {             index = col + 6;         }                  // if marked, return true and move to next player, and if not, return false and keep loop with current player         if(boxes[index].setPlaceHolder(mark)) {             return true;         }         return false;                }          public boolean isFull() {         for(Box b : boxes)             if(b.isAvailable()) {                 return false;             }         return true;     }          public boolean isWinner(){                  // win condition         // .equal method was used since the code is comparing the contents not address         if(!boxes[0].getPlaceHolder().equals("-")){             if(boxes[0].getPlaceHolder().equals(boxes[1].getPlaceHolder())  && boxes[0].getPlaceHolder().equals(boxes[2].getPlaceHolder())){             return true;             }             if(boxes[0].getPlaceHolder().equals(boxes[3].getPlaceHolder())  && boxes[0].getPlaceHolder().equals( boxes[6].getPlaceHolder())){             return true;             }             if(boxes[0].getPlaceHolder().equals(boxes[4].getPlaceHolder())  && boxes[0].getPlaceHolder().equals( boxes[8].getPlaceHolder())){             return true;             }         }               if(boxes[1].getPlaceHolder().equals(boxes[4].getPlaceHolder())  && boxes[1].getPlaceHolder().equals(boxes[7].getPlaceHolder()) && !(boxes[1].getPlaceHolder().equals("-"))){             return true;         }         if(boxes[2].getPlaceHolder().equals(boxes[5].getPlaceHolder())  && boxes[2].getPlaceHolder().equals(boxes[8].getPlaceHolder()) && !(boxes[2].getPlaceHolder().equals("-"))){             return true;         }        if(boxes[2].getPlaceHolder().equals(boxes[4].getPlaceHolder())  && boxes[2].getPlaceHolder().equals(boxes[6].getPlaceHolder()) && !(boxes[2].getPlaceHolder().equals("-"))){             return true;         }         if(boxes[3].getPlaceHolder().equals(boxes[4].getPlaceHolder())  && boxes[3].getPlaceHolder().equals(boxes[5].getPlaceHolder()) && !(boxes[3].getPlaceHolder().equals("-"))){             return true;         }         if(boxes[6].getPlaceHolder().equals(boxes[7].getPlaceHolder())  && boxes[6].getPlaceHolder().equals(boxes[8].getPlaceHolder()) && !(boxes[6].getPlaceHolder().equals("-"))){             return true;         }                  return false;     }               @Override     public String getMark(int row, int col) {         return boxes[row * this.boardRowSize + col].getPlaceHolder();     }          @Override     public String getName() {         return this.name;     }     @Override     public void reset() {              }     @Override     public int getColSize() {         return this.boardColSize;        }     @Override     public int getRowSize() {         return this.boardRowSize;     }           } Box.java Box.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class Box {     private int row;     private int col;     static final String DASH = "-";     private String placeHolder = Box.DASH;                  Box(int row, int col){         this.row = row;         this.col = col;      }          String getPlaceHolder() {         return placeHolder;     }          boolean setPlaceHolder(String mark) {         if(isAvailable()) {             this.placeHolder = mark;             return true;         }         return false;     }          boolean isAvailable() {         return (this.placeHolder.equals(Box.DASH));     }          void print() {         System.out.print(" " + placeHolder);             }      } ComputerPlayer.java ComputerPlayer.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class ComputerPlayer extends APlayer{     public ComputerPlayer(String name, String mark){         super(name, mark);       }         private int randomNumber(int range) {         return (int) (Math.random() * range);     }     @Override     public int selectRowValue(int range) {         return randomNumber(range);     }     @Override     public int selectColValue(int range) {         return randomNumber(range);     }      } DriverMain.java DriverMain.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class DriverMain {       public static void main(String[] args) {             TTTGame game = new TTTGame();             //game.setBoard(new OtherBoardWrapper());             //game.setPlayers(new ComputerPlayer("Player1", Mark.X.getMark()), new HumanPlayer("Player2", Mark.O.getMark()));             game.start();       } } HumanPlayer.java HumanPlayer.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; import java.util.Scanner; public class HumanPlayer extends APlayer{     Scanner input = new Scanner(System.in);          public HumanPlayer(String name, String mark) {         super(name,mark);     }          @Override     public int selectRowValue(int range) {         System.out.println("\nplease enter a valid row number (0 to " + range + ")");         int row;         do {             row = input.nextInt();         } while(row < 0 && row > range);                  return row;     }     @Override     public int selectColValue(int range) {         System.out.println("\nplease enter a valid col number (0 to " + range + ")");         int col;         do {             col = input.nextInt();         } while(col < 0 && col > range);                  return col;     }      } Iboard.java Iboard.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public interface Iboard {     void print();     void reset();     String getMark(int row, int col);     boolean makeMove(String player, int row, int col);     void setSize(int row, int col);     int getColSize();     int getRowSize();     String getName();     boolean isFull();     public boolean isWinner();      } Mark.java Mark.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public enum Mark {        X("X"),     O("O"),     DASH("-");          private String mark;     Mark(String mark) {         this.mark = mark;     }     public String getMark() {         return mark;     }     } OtherBoard.java OtherBoard.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class OtherBoard{     private int boardRowSize;     private int boardColSize;     private char[][] board;     private String name;     OtherBoard(){         this(3, 3, "TTT 2D array of char");     }     OtherBoard(int row, int col, String name){         this.setName(name);         this.setSize(row, col);     }     private void init() {         this.board = new char[boardRowSize][boardColSize];         for(int i = 0 ; i < board.length; i++){             for(int j = 0 ; j < board[i].length; j++){                 board[i][j] = Mark.DASH.getMark().charAt(0);             }         }         printMyBoard()  ;     }     public void printMyBoard() {         System.out.println("printing the " + this.name + " - " +                 this.boardRowSize + "*" + this.boardColSize + " board info....");         for(int i = 0 ; i < board.length; i++){             for(int j = 0 ; j < board[i].length; j++){                 System.out.print(board[i][j] + " ");             }             System.out.println();         }     }     private int availability() {         int count = 0;         for(int i = 0 ; i < board.length; i++){             for(int j = 0 ; j < board[i].length; j++){                 if(board[i][j] == Mark.DASH.getMark().charAt(0)) count++;             }         }         return count;     }     public String getMark(int row, int col) {         return board[row][col] + "";     }     public boolean setMark(int row, int col, String player) {         if(board[row][col] == Mark.DASH.getMark().charAt(0)){             board[row][col] = player.charAt(0);             return true;         }else return false;     }     //  public boolean makeMove(String s, Move m) {     //      if(board[m.getRow()][m.getCol()] == Mark.DASH.getMark().charAt(0)){     //          board[m.getRow()][m.getCol()] = s.charAt(0);     //          return true;     //      }else return false;     //  }     public void setSize(int row, int col) {         this.boardRowSize = row;         this.boardColSize = col;         this.init();     }     private void setName(String name) {         this.name = name;     }     public String getName() {         return this.name;     }     public void reset() {         // TODO Auto-generated method stub     }     public int getColSize() {         return this.boardColSize;     }     public int getRowSize() {         return this.boardRowSize;     }     /*      * it will give us an array of all the empty spots      * by multiplying row and col      */     public int[] emptyCells(){         int[] emptyCells = new int[availability()];         int index = 0;         if(availability()>0)             for(int i = 0 ; i < board.length; i++){                 for(int j = 0 ; j < board[i].length; j++){                     if(board[i][j] == Mark.DASH.getMark().charAt(0))                          emptyCells[index++] = i * this.boardRowSize + j;                 }             }         return emptyCells;     }     //  public Move[] getAllPossibleMoves() {     //      Move[] validMoves = new Move[availability()];     //      int count = 0;     //      for(int i = 0 ; i < board.length; i++){     //          for(int j = 0 ; j < board[i].length; j++){     //              if(board[i][j] == Mark.DASH.getMark().charAt(0)) {     //                  Move m = new Move(i,j);     //                  validMoves[count++] = m;     //              }     //          }     //      }     //      return validMoves;     //  } } OtherBoardWrapper.java OtherBoardWrapper.java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package TTTGame; public class OtherBoardWrapper implements Iboard{          OtherBoard otherBoard = new OtherBoard();          @Override     public void print() {         otherBoard.printMyBoard();     }          @Override     public void reset(){             }          @Override     public String getMark(int row, int col){        return otherBoard.getMark(row, col); // forwarding     }          @Override     public boolean makeMove(String player, int row, int col){         return otherBoard.setMark(row, col, player);     }          @Override     public void setSize(int row, int col){         otherBoard.setSize(row,col);     }          @Override     public int getColSize(){         return otherBoard.getColSize();     }          @Override     public int getRowSize(){         return otherBoard.getRowSize();     }          @Override     public String getName(){         return otherBoard.getName();     }          @Override     public boolean isFull(){         return (otherBoard.emptyCells().length == 0);     }     @Override     public boolean isWinner() {         return true;     }      }

ULTIMATE TIC-TAC-TOE (COVID-19) Due: Thursday, November 19, 2020 by the end of the day (No Late Submission Will Be Accepted) There is a very simple solution to keeping Tic-tac-toe fresh and interesting, though, and it has been thought up by a group of mathematicians. It’s being called Ultimate Tic-tac-toe and simply embeds a Tic-tac-toe board in each of the nine squares of the original game. 0 O 1 2 0 O 1 2 0 O 1 2 3 4 5 3 4 X 5 3 4 5 6 7 8 6 7 8 6 7 8 O Board #0 Board #1 Board #2 0 X 1 2 0 O 1 2 0 X 1 2 3 4 5 3 4 O 5 3 4 5 6 7 8 6 7 8 6 7 8 O Board #3 Board #4 Board #5 0 O 1 2 0 X 1 2 0 O 1 2 3 4 5 3 4 O 5 3 4 5 6 7 8 6 7 8 6 7 8 X Board #6 Board #7 Board #8 Now instead of just winning the main board, you also need to win each of the smaller boards first until you have a line of three. NEW RULE: You can only place a mark on the board determined by the position of your opponent’s last placed mark. So, if they put an X or O in the top right corner of a square, your next move must occur in the top right board. By adding that rule the game is no longer about winning a single board, it’s about tactically managing up to 9 games at once and plotting ahead. You still win by marking three squares in a row, but that now involves winning three games. THE EFFECTS OF THE NEW RULE: 1- Your opponent can force you to choose an open square in an already won/tie board. 1- Player X Move: - Board# : 0 - Square#: 1 2- Player O Move: Only valid Board is Board #1 - Square#: 5 3- Player X Move: Only valid Board is Board #5 - Square#: 6 4- Player O Move: Only valid Board is Board #6 - Square#: 4 For example, after a while Board # 0 has a winner, Player O. It is the Player X turn and the only valid Board# for Player X is Board#0 Player X Move: Only valid Board is Board #0 - Square#: 6 0 O 1 X 2 0 1 2 0 1 2 3 4 O 5 3 4 5 O 3 4 5 6 X 7 8 O 6 7 8 6 7 8 Board #0 Board #1 Board #2 0 1 2 0 1 2 0 1 2 3 4 5 3 4 5 3 4 5 6 7 8 6 7 8 6 X 7 8 Board #3 Board #4 Board #5 0 1 2 0 1 2 0 1 2 3 4 O 5 3 4 5 3 4 5 6 7 8 6 7 8 6 7 8 Board #6 Board #7 Board #8 2- If the board is already full, you get a free pass to place your mark in any board For example, after a while Board # 0 is completely full. It is the Player X turn and the only valid Board# for Player X is Board#0. Since Board#0 is full, then the Player X gets to pick any other Board. Player X Move: - Board# : 8 - Square#: 8 0 O 1 X 2 X 0 1 2 0 1 2 3 X 4 O 5 X 3 4 5 O 3 4 5 6 O 7 X 8 O 6 7 8 6 7 8 Board #0 Board #1 Board #2 0 1 2 0 1 2 0 1 2 3 4 5 3 4 5 3 4 5 6 7 8 6 7 8 6 X 7 8 Board #3 Board #4 Board #5 0 1 2 0 1 2 0 1 2 3 4 O 5 3 4 5 3 4 5 6 7 8 6 7 8 6 7 8 X Board #6 Board #7 Board #8 The great thing about this rule change and board layout is it remains easy to understand. BASIC RULES: 1. Each turn, you mark one of the small squares. 2. When you get three in a row on a small board, you’ve won that board. 3. To win the game, you need to win three small boards in a row. 4. You don’t get to pick which of the nine boards to play on. That’s determined by your opponent’s previous move. Whichever square he picks, that’s the board you must play in next. (And whichever square you pick will determine which board he plays on next.) A FEW CLARIFYING RULES ARE NECESSARY: 1. What if my opponent sends me to a board that’s already been won/tie but not full? In that case, you get to play on that board, but your move doesn’t affect the outcome of that board. 2. What if my opponent sends me to a board that’s already been full? In that case, congratulations – you get to go anywhere you like, on any of the other boards. WHAT/HOW/WHERE/WHEN TO SUBMIT: • Draw a UML class diagram (manual diagram also acceptable). o Identify classes and the relation between them (for example inheritance or aggregation) o Submit your diagram as a PDF file. o Convert your class diagram to software classes. o Your class diagram should reflect your code • Review the Grading criteria and the project rubric in the eLearning o The project is an individual project. o Copying, in whole or in part, from other sources will be considered an act of scholastic dishonesty. • The Project Due on Nov 19, 2020 (by the end of the day) o No Late Submission Will Be Accepted. • The project must be turned in via eLearning ONLY. Please: o Zip all the source codes (.java) in a zip file. All the Java source code necessary to compile and execute your code. o Include a comment section at the beginning of your code with your name, section number, and describe Your Analysis, Design and Test. o A readme file, documenting what you have and/or have not done, describing specific details that you feel we need to know when grading your work. o A video of your running code (to show that, it is working) o No E-Mail Submissions Are Accepted. • This is a command Line game. No GUI. DIFFERENT VERSION OF THE GAME 1. Player versus AI (Mandatory) 2. Program provides a list of possible legal moves to each player. (Optional – extra 5 points) 3. Player versus smart AI* (Optional – extra 5 points) *smart AI will always select the best move to win the game. SAMPLE RUNS: • Payer X is the winner x-winner.txt • Player O is the winner o-winner.txt • no-winner.txt REFERENCES: § Https://mathwithbaddrawings.com/ultimate-tic-tac-toe-original-post/ § https://www.geek.com/games/tic-tac-toe-made-much-more-interesting-with-a-simple- tweak-1559289/ § https://mathwithbaddrawings.com/2013/11/18/tic-tac-toe-puzzles-and-the-difference- between-a-puzzle-and-a-game/

Related Questions

Similar orders to Java ULTIMATE TIC-TAC-TOE programming (not 2D array)
6
Views
0
Answers
Creating a DNS server (written in C)
WANT >=50% due to being swamped (only standard option minimal, don't care about cache or non-blocking). I require periodic updates of code with a description as there is a Git commit tracking. Also require a makefile according to the specifications and a g...
20
Views
0
Answers
Create inheritance project for a restaurant
Projects must include: -at least three different levels of inheritance - at least nine classes total -the highest superclass must have at least two methods -every subclass must contain a unique method that was not present in its super...
16
Views
0
Answers
Python Code for Suggesting Pets
Must have ___init___ ; ___str___ ; for loops ; while loops ; and must define a function which returns something, I wrote down what idea I had in that form so its best to follow it, this is an entry-level computer science project so it should be pretty easy...
22
Views
0
Answers
Quick HW Computer Science on LL(1) and object creation
Due at 12:00pm today. The question details are in the files attached. One question is on the impact of a parser being LL(1) or not. Another is on imagining a mistake in object creation. The answers are probably max 2 lines long....