Connect Four is a game played between two players. Each player chooses a color -
ID: 3540075 • Letter: C
Question
Connect Four is a game played between two players. Each player chooses a color - say red or black. The players then take turns in dropping plastic counters of their chosen color into a vertical grid that has 7 columns and 6 rows. The object of the game is to be the first person to have 4 contiguous counters of their chosen color in the same row, or same column, or along the same diagonal.
Your input will be in a file called connect.txt. The first line in that file will be two integer numbers m and n. The number m represents the number of rows and the number n represents the number of columns. This will be followed by m lines of data. Each line of data will have n characters - R, B, and X. The character R represents a red counter, B represents a black counter, and X represents an empty space in the grid.
Your program will open the input file and read the first line of data. It will then create a 2-D array having the number of rows and columns as specified. It will then populate the array with data from the input file.
You will develop an algorithm to determine if there are 4 contiguous R or B characters in the array. There will be only 3 possible outputs:
Red wins
Black wins
Tie
Assume that there are no errors in the data file. However, in the event that there are 4 contiguous counters for both colors, then print Tie.
the connect.txt contains:
6 7
B R R X X B B
R R B X B R B
B B B R B R R
R B R B R R B
B R B R R B B
R R B B R R B
I need help with this problem. I'm absolutely stuck and it's due tomorrow!
Explanation / Answer
please rate - thanks
any problems-I will fix
the board is output just to show it. that can be removed
import java.util.*;
import java.io.*;
public class FileIn
{public static void main(String[] args)throws FileNotFoundException
{int n,m,i,j;
boolean red, black;
Scanner in=new Scanner(new File("connect.txt"));
n=in.nextInt();
m=in.nextInt();
char[][]b=new char[n][m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[i][j]=in.next().charAt(0);
for(i=0;i<n;i++)
{for(j=0;j<m;j++)
System.out.print(b[i][j]+" ");
System.out.println();
}
red=checkRows(b,n,m,'R')||checkCols(b,n,m,'R')||checkDiag1(b,n,m,'R')||checkDiag2(b,n,m,'R');
black=checkRows(b,n,m,'B')||checkCols(b,n,m,'B')||checkDiag1(b,n,m,'B')||checkDiag2(b,n,m,'B');
if(red&&black)
System.out.println("Tie");
else if(red)
System.out.println("Red Wins");
else if(black)
System.out.println("Black Wins");
}
public static boolean checkRows(char b[][],int n,int m,char c)
{int i,j,count;
for(i=0;i<n;i++)
{count=0;
for(j=0;j<m;j++)
if(b[i][j]==c)
count++;
else
count=0;
if(count>=4)
return true;
}
return false;
}
public static boolean checkCols(char b[][],int n,int m,char c)
{int i,j,count;
for(j=0;j<m;j++)
{count=0;
for(i=0;i<n;i++)
if(b[i][j]==c)
count++;
else
count=0;
if(count>=4)
return true;
}
return false;
}
public static boolean checkDiag1(char b[][],int n,int m,char c)
{int row,col,j,count;
for(j=0;j<n;j++)
{count=0;
row=j;
col=0;
while(col<m&&row<n)
{//System.out.println(row+" "+col+" "+count);
if(b[row][col]==c)
count++;
else
count=0;
col++;
row++;
}
if(count>=4)
return true;
}
for(j=1;j<m;j++)
{count=0;
row=0;
col=j;
while(col<m&&row<n)
{//System.out.println(col+" "+row+" "+count);
if(b[row][col]==c)
count++;
else
count=0;
col++;
row++;
}
if(count>=4)
return true;
}
return false;
}
public static boolean checkDiag2(char b[][],int n,int m,char c)
{int row,col,j,count;
for(j=n-1;j>=0;j--)
{count=0;
row=j;
col=m-1;
while(col>=0&&row>=0)
{//System.out.println(row+" "+col+" "+count);
if(b[row][col]==c)
count++;
else
count=0;
col--;
row--;
}
if(count>=4)
return true;
}
for(j=m-1;j>=1;j--)
{count=0;
row=n-1;
col=j;
while(col>=0&&row>=0)
{//System.out.println(col+" "+row+" "+count);
if(b[row][col]==c)
count++;
else
count=0;
col--;
row--;
}
if(count>=4)
return true;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.