Block/Square | Phone | connect four modified

I got somewhat medium but bit harder to come up solutions for my telephone round for square.

There were three parts

  1. Create 2 d array of the connect four grid. Example 3x6 grid.
- - - - - - 
- - - - - - 
- - - - - - 
  1. Display the grid after playing . play(column, color). Always start placing the color at the bottom row up

After play(0,'R'), looks like below

- - - - - - 
- - - - - - 
R - - - - - 
  1. Additional parameter given called toWin: which is the number of consecutive same color in east, west, north, south direction, not diagonal. Basically, find path from a certain location in grid using left,right, up & down movement of same color. This is where I got stuck. Although i was given hint of BFS, i could not complete and result was rejected.
    Example winner after play(2,'R') and toWin=5 as total Rs is 6 from position (0,2)

Y R R - - - 
Y Y R - - - 
R R R - - - 

I did come up with solution afterwards and saw how BFS could solve this. But too late and lession learned.



import com.sun.tools.javac.util.Pair;
import java.util.*;
import java.util.stream.IntStream;

public class ConnectFour {
    char[][] grid;
    Map<Integer, Integer> freeMap;
    int toWin;
    boolean isWon;
    public ConnectFour(int height, int width,int toWin) {
        this.grid = new char[height][width];
        this.toWin = toWin;
        for (char[] rows : grid)
            Arrays.fill(rows, '-');
        freeMap = new HashMap<>();
        IntStream.range(0, width).forEach(x -> freeMap.put(x, height - 1));
        display();
    }

    public void display() {
        for (char[] row : grid) {
            for (char spot : row)
                System.out.print(spot+" ");
            System.out.println();
        }
        System.out.println();
    }

    public void play(int col, char color) {
        if (freeMap.get(col) < 0 || isWon) // cannot play
            return;
        grid[freeMap.get(col)][col] = color;
        display();
        whoWon(freeMap.get(col), col, color);
        freeMap.put(col, freeMap.get(col) - 1);
    }

    public boolean validMove(int x, int y, boolean [][] visited, char color){
        return  !(x < 0 || y < 0 || x >= this.grid.length || y >= this.grid[0].length||visited[x][y]||grid[x][y]=='-'||grid[x][y]!=color);
    }

    public void whoWon(int x, int y, char color) {
        if (isWon)
            return;
        Queue<Pair<Integer,Integer>> q = new LinkedList();
        q.add(Pair.of(x,y));
        boolean [][] visited = new boolean[this.grid.length][this.grid[0].length];
        int count=0;
        while (!q.isEmpty()){
//            System.out.println("q size "+q.size());
            Pair<Integer,Integer> spot = q.poll();
            if(grid[spot.fst][spot.snd]==color){
                count++;
            }
            visited[spot.fst][spot.snd]=true;
            Pair<Integer,Integer> agadi = Pair.of(spot.fst+1, spot.snd);
            Pair<Integer,Integer> pachadi= Pair.of(spot.fst-1, spot.snd);
            Pair<Integer,Integer> up= Pair.of(spot.fst, spot.snd+1);
            Pair<Integer,Integer> down= Pair.of(spot.fst, spot.snd-1);
            if(validMove(agadi.fst,agadi.snd,visited,color)) {
                q.add(agadi);
            }
            if(validMove(pachadi.fst,pachadi.snd,visited,color)){
                q.add(pachadi);
            }
            if(validMove(up.fst,up.snd,visited,color)){
                q.add(up);
            }
            if(validMove(down.fst,down.snd,visited,color)){
                q.add(down);
            }
            if(count>=toWin){
                break;
            }
        }
        isWon = count>=toWin;
        if(count>=toWin)
            System.out.println(color+" won the game at x "+x+", "+y+" got matchs "+count+" needed: "+toWin);
        else
            System.out.println(color+" did not win the game "+x+", "+y+" got matchs "+count+" needed: "+toWin);
    }

    public static void main(String[] args) {
        ConnectFour cf = new ConnectFour(3,6,5);
        cf.play(0,'R');
        cf.play(0,'Y');
        cf.play(0,'Y');
        cf.play(1,'R');
        cf.play(1,'Y');
        cf.play(1,'R');

        cf.play(2,'R');
        cf.play(2,'R');
        cf.play(2,'R');
    }
}

Comments (8)