Table of Contents

Problem Statement

For problem statement, refer Here.

Class Diagram

Will be creating 3 Classes for representing 3 important aspects of the game.

  1. GameCoordinator: This will we resposible for conducting the whole game. can be called as the brain of the game. will take care of input, initilising objects, output, etc.
  2. Player: This will represent a particular player in the game. so that necessary player infos(name, number, etc.) can be stored and accessed.
  3. Grid: This will be responsible for storing and conducting the whole game. Every change in the state of game will be detected by this.

Class diagram below explains this:

classDiagram GameCoordinator *-- Grid : Aggregation GameCoordinator *-- Player : Composition class GameCoordinator { -Integer numberOfPlayers -List~Player~ listOfPlayers -Grid gameGrid -gameInitializer() +gameHandler() } class Player { -Integer numberOfPlayer -String nameOfPlayer +getName() +getNumber() } class Grid { -Integer numberOfRows -Integer numberOfColumns -List~List~Integer~~ grid -Integer emptyPlaces -printGrid() -checkValidCell(int row,int col) +setCell(int row,int col,int number) +isEmptyAvailable() +checkForWinner(int number) }

Relationships

  • GameCoordinator has a has a, one-to-many relationship with Player, as there can be multiple players in a game. game coordinator will have all the players in it.
  • GameCoordinator has a has a, one-to-one relationship with Grid, as there can be only one grid in a game. game coordinator will have the grid in it.

Code Files

Main.java

import TicTacToe.GameCoordinator;

public class Main {
    public static void main(String[] args) {
        GameCoordinator gameCoordinator = new GameCoordinator();
        gameCoordinator.gameHandler();
    }
}

TicTacToe/GameCoordinator.java

package TicTacToe;

import java.util.ArrayList;
import java.util.Scanner;

public class GameCoordinator {
    private Integer numberOfPlayers;
    private ArrayList<Player> listOfPlayers;
    private Grid gameGrid;
    private Scanner scanner = new Scanner(System.in);

    public GameCoordinator(){
        gameInitializer();
    }

    private void gameInitializer(){
        System.out.println("Please provide number of players:");
        numberOfPlayers = scanner.nextInt();
        scanner.nextLine();
        listOfPlayers = new ArrayList<>(numberOfPlayers);

        for(int i = 1; i <= numberOfPlayers; i++) {
            System.out.println("Please provide the name of Player " + i + ":");
            String name = scanner.nextLine();
            listOfPlayers.add(new Player(i, name));
        }

        System.out.println("Please provide number of rows: ");
        int row = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Please provide number of columns: ");
        int col = scanner.nextInt();
        scanner.nextLine();
        gameGrid = new Grid(row, col);
    }

    public void gameHandler() {
        while (true) {
            for (int i = 0; i < numberOfPlayers; i++) {
                if(!gameGrid.isEmptyAvailable()){
                    System.out.println("The game is Tied");
                    return;
                }
                Player currentPlayer = listOfPlayers.get(i);
                while (true) {
                    System.out.println("Player " + currentPlayer.getName() + 
                    ", it's your turn.");
                    System.out.println("Please provide row: ");
                    int row = scanner.nextInt();
                    scanner.nextLine();
                    System.out.println("Please provide column: ");
                    int column = scanner.nextInt();
                    scanner.nextLine();

                    if (gameGrid.setCell(row, column, i + 1)) {
                        break;
                    } else {
                        System.out.println("Invalid move. Please try again.");
                    }
                }

                if (gameGrid.checkForWinner(i + 1)) {
                    System.out.println("Congratulations " + currentPlayer.getName() + 
                    ", you WON!");
                    return;
                }
            }
        }
    }
}

TicTacToe/Player.java

package TicTacToe;

public class Player {
    private final Integer numberOfPlayer;
    private final String nameOfPlayer;

    public Player(int numberOfPlayer,String nameOfPlayer){
        this.nameOfPlayer = nameOfPlayer;
        this.numberOfPlayer = numberOfPlayer;
    }

    public String getName(){
        return this.nameOfPlayer;
    }

    public Integer getNumber(){
        return this.numberOfPlayer;
    }
}

TicTacToe/Grid.java

package TicTacToe;

import java.util.ArrayList;

public class Grid {
    private final Integer numberOfRows;
    private final Integer numberOfColumns;
    private final ArrayList<ArrayList<Integer>> grid;
    private Integer emptyPlaces;

    public Grid(int numberOfRows,int numberOfColumns){
        this.numberOfColumns = numberOfColumns;
        this.numberOfRows = numberOfRows;
        emptyPlaces = numberOfColumns*numberOfRows;
        this.grid = new ArrayList<>(numberOfRows);
        for (int i = 0; i < numberOfRows; i++) {
            ArrayList<Integer> row = new ArrayList<>(numberOfColumns);
            for (int j = 0; j < numberOfColumns; j++) {
                row.add(0);
            }
            grid.add(row);
        }
    }

    private void printGrid(){
        System.out.println("\n");
        for(ArrayList<Integer> i: grid)
        {
            for(Integer j: i)
            {
                if(j.equals(0))
                    System.out.print("_ ");
                else
                    System.out.print(j + " ");
            }
            System.out.println("\n");
        }
    }

    private boolean checkValidCell(int row,int col){
        return row>=0
                && row<numberOfRows
                && col>=0
                && col<numberOfColumns
                && grid.get(row).get(col).equals(0);
    }

    public boolean setCell(int row,int col,int number){
        if(!checkValidCell(row,col)){
            return false;
        }
        grid.get(row).set(col, number);
        emptyPlaces--;
        printGrid();
        return true;
    }

    public boolean isEmptyAvailable(){
        return !emptyPlaces.equals(0);
    }

    public boolean checkForWinner(int number){
        boolean ans=false;
        for(int i=0;i<numberOfRows;i++)
        {
            boolean loopans=true;
            for(int j=0;j<numberOfColumns;j++)
            {
                if(!grid.get(i).get(j).equals(number)){
                    loopans=false;
                    break;
                }
            }
            ans|=loopans;
        }
        for(int j=0;j<numberOfColumns;j++)
        {
            boolean loopans=true;
            for(int i=0;i<numberOfRows;i++)
            {
                if(!grid.get(i).get(j).equals(number)){
                    loopans=false;
                    break;
                }
            }
            ans|=loopans;
        }
        if(numberOfColumns.equals(numberOfRows))
        {
            int row=numberOfRows-1;
            int col=numberOfColumns-1;
            boolean loopans=true;
            while(row>=0)
            {
                if(!grid.get(row).get(col).equals(number))
                {
                    loopans=false;
                    break;
                }
                row--;
                col--;
            }
            ans|=loopans;
            row=0;
            col=0;
            loopans=true;
            while(row<numberOfRows)
            {
                if(!grid.get(row).get(col).equals(number))
                {
                    loopans=false;
                    break;
                }
                row++;
                col++;
            }
            ans|=loopans;
        }
        return ans;
    }
}

Summary

While there are many approaches to this problem, the one presented here is the simplest to devise and easiest to apply. One thing to note is that while this code fulfills the basic requirements provided in here, it is also extensible enough to extend the game to any number of players with any size of the grid.