Coded in Java 3: Tic-Tac-Toe [40 marks] Implement the game of Tic-Tac-Toe. Your
ID: 3598869 • Letter: C
Question
Coded in Java
3: Tic-Tac-Toe [40 marks] Implement the game of Tic-Tac-Toe. Your game must consist of at least three (3) classes: Board, Player, and TicTacToeApp. You can add more classes if you wish. The TicTacToeApp program will run the game, using a Board object and two Player objects. The Player class will implement a player in the game. A player could be a human user (and so user input will be needed) or a computer . The Board class will be the board in the game. It can either be a 3x3 grid or a 5x5 grid. The constructor of the class will create an appropriate grid User input for specifying which grid element in the board will be a comma separated list of two numbers, where the first number is row (starting at 0) and the second number is the column (starting at 0). The amount of whitespace around the numbers (and comma) can be arbitrary. .A single command line argument (for the TicTacToeApp program) will specify if the game is a 3x3 or 5x5 grid. Acceptable command line arguments will be "3", "5", "three" and "five". For any other command line argument (or lack) your program will terminate with a usage message (of how to run the program properly). The game board must be displayed before any player makes a move. The game should be easy to play Note: Style for this problem includes class design. Encapsulation is required. Mark breakdown: 15 marks for Style, 25 marks for CorrectnessExplanation / Answer
The Answer for your question is given below clearly
The different classes are given so you can create a project and add all the following classes and run the code of tictactoe.
TicTacToe.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 javaapplication1;
/**
*
* @author miracle
*/
public class TicTacToe {
public static void main(String[] args) {
Game game = new Game();
}
}
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 javaapplication1;
/**
*
* @author miracle
*/
public class Board {
private int[][] Board= new int[3][3];
public Board(){
clearBoard();
}
public void clearBoard(){
for(int line=0 ; line<3 ; line++)
for(int column=0 ; column<3 ; column++)
Board[line][column]=0;
}
public void showBoard(){
System.out.println();
for(int line=0 ; line<3 ; line++){
for(int column=0 ; column<3 ; column++){
if(Board[line][column]==-1){
System.out.print(" X ");
}
if(Board[line][column]==1){
System.out.print(" O ");
}
if(Board[line][column]==0){
System.out.print(" ");
}
if(column==0 || column==1)
System.out.print("|");
}
System.out.println();
}
}
public int getPosition(int[] attempt){
return Board[attempt[0]][attempt[1]];
}
public void setPosition(int[] attempt, int player){
if(player == 1)
Board[attempt[0]][attempt[1]] = -1;
else
Board[attempt[0]][attempt[1]] = 1;
}
public int checkLines(){
for(int line=0 ; line<3 ; line++){
if( (Board[line][0] + Board[line][1] + Board[line][2]) == -3)
return -1;
if( (Board[line][0] + Board[line][1] + Board[line][2]) == 3)
return 1;
}
return 0;
}
public int checkColumns(){
for(int column=0 ; column<3 ; column++){
if( (Board[0][column] + Board[1][column] + Board[2][column]) == -3)
return -1;
if( (Board[0][column] + Board[1][column] + Board[2][column]) == 3)
return 1;
}
return 0;
}
public int checkDiagonals(){
if( (Board[0][0] + Board[1][1] + Board[2][2]) == -3)
return -1;
if( (Board[0][0] + Board[1][1] + Board[2][2]) == 3)
return 1;
if( (Board[0][2] + Board[1][1] + Board[2][0]) == -3)
return -1;
if( (Board[0][2] + Board[1][1] + Board[2][0]) == 3)
return 1;
return 0;
}
public boolean fullBoard(){
for(int line=0 ; line<3 ; line++)
for(int column=0 ; column<3 ; column++)
if( Board[line][column]==0 )
return false;
return true;
}
}
Game.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 javaapplication1;
/**
*
* @author miracle
*/
import java.util.Scanner;
public class Game {
private Board board;
private int turn=1, who=1;
private Player player1;
private Player player2;
public Scanner input = new Scanner(System.in);
public Game(){
board = new Board();
startPlayers();
while( Play() );
}
public void startPlayers(){
System.out.println("Who will be player1 ?");
if(choosePlayer() == 1)
this.player1 = new Human(1);
else
this.player1 = new Computer(1);
System.out.println("----------------------");
System.out.println("Who will be Player 2 ?");
if(choosePlayer() == 1)
this.player2 = new Human(2);
else
this.player2 = new Computer(2);
}
public int choosePlayer(){
int option=0;
do{
System.out.println("1. Human");
System.out.println("2. Computer ");
System.out.print("Option: ");
option = input.nextInt();
if(option != 1 && option != 2)
System.out.println("Invalid Option! Try again");
}while(option != 1 && option != 2);
return option;
}
public boolean Play(){
board.showBoard();
if(won() == 0 ){
System.out.println("----------------------");
System.out.println(" Turn "+turn);
System.out.println("It's turn of Player " + who() );
if(who()==1)
player1.play(board);
else
player2.play(board);
if(board.fullBoard()){
System.out.println("Full Board. Draw!");
return false;
}
who++;
turn++;
return true;
} else{
if(won() == -1 )
System.out.println("Player 1 won!");
else
System.out.println("Player 2 won!");
return false;
}
}
public int who(){
if(who%2 == 1)
return 1;
else
return 2;
}
public int won(){
if(board.checkLines() == 1)
return 1;
if(board.checkColumns() == 1)
return 1;
if(board.checkDiagonals() == 1)
return 1;
if(board.checkLines() == -1)
return -1;
if(board.checkColumns() == -1)
return -1;
if(board.checkDiagonals() == -1)
return -1;
return 0;
}
}
Player.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 javaapplication1;
/**
*
* @author miracle
*/
public abstract class Player {
protected int[] attempt = new int[2];
protected int player;
public Player(int player){
this.player = player;
}
public abstract void play(Board board);
public abstract void Try(Board board);
public boolean checkTry(int[] attempt, Board board){
if(board.getPosition(attempt) == 0)
return true;
else
return false;
}
}
Human.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 javaapplication1;
/**
*
* @author miracle
*/
import java.util.Scanner;
public class Human extends Player{
public Scanner input = new Scanner(System.in);
public Human(int player){
super(player);
this.player = player;
System.out.println("Player 'Human' created!");
}
@Override
public void play(Board board){
Try(board);
board.setPosition(attempt, player);
}
@Override
public void Try(Board board){
do{
do{
System.out.print("Line: ");
attempt[0] = input.nextInt();
if( attempt[0] > 3 ||attempt[0] < 1)
System.out.println("Invalid line. It's 1, 2 or 3");
}while( attempt[0] > 3 ||attempt[0] < 1);
do{
System.out.print("Column: ");
attempt[1] = input.nextInt();
if(attempt[1] > 3 ||attempt[1] < 1)
System.out.println("Invalid column. É 1, 2 or 3");
}while(attempt[1] > 3 ||attempt[1] < 1);
attempt[0]--;
attempt[1]--;
if(!checkTry(attempt, board))
System.out.println("Placed already marked. Try other.");
}while( !checkTry(attempt, board) );
}
}
Computer.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 javaapplication1;
/**
*
* @author miracle
*/
public class Computer extends Player{
public Computer(int player){
super(player);
System.out.println("Player 'Computer' created");
}
@Override
public void play(Board board){
}
@Override
public void Try(Board board){
}
}
Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up